author | alundblad |
Tue, 22 Mar 2016 13:14:12 +0100 | |
changeset 37643 | 626e07816dce |
parent 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
25605
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
1 |
/* |
35356
e919fd8db52c
8056989: Sjavac --server option should be optional
alundblad
parents:
34991
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
25605
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
4 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
10 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
15 |
* accompanied this code). |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
16 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
20 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
23 |
* questions. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
24 |
*/ |
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
34560
diff
changeset
|
25 |
|
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
26 |
package com.sun.tools.sjavac.comp; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
27 |
|
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
26992
diff
changeset
|
28 |
import java.io.IOException; |
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
29 |
import java.io.PrintWriter; |
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
30 |
import java.io.StringWriter; |
32335 | 31 |
import java.nio.file.Files; |
32 |
import java.nio.file.Path; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.HashMap; |
|
36 |
import java.util.HashSet; |
|
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
37 |
import java.util.List; |
32335 | 38 |
import java.util.Map; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
39 |
import java.util.Set; |
32799 | 40 |
import java.util.stream.Stream; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
41 |
|
32799 | 42 |
import com.sun.tools.javac.file.JavacFileManager; |
43 |
import com.sun.tools.javac.main.Main; |
|
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
44 |
import com.sun.tools.javac.main.Main.Result; |
32799 | 45 |
import com.sun.tools.javac.util.Context; |
32335 | 46 |
import com.sun.tools.sjavac.JavacState; |
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
27388
diff
changeset
|
47 |
import com.sun.tools.sjavac.Log; |
32335 | 48 |
import com.sun.tools.sjavac.Module; |
49 |
import com.sun.tools.sjavac.ProblemException; |
|
50 |
import com.sun.tools.sjavac.Source; |
|
51 |
import com.sun.tools.sjavac.Transformer; |
|
26991
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
26107
diff
changeset
|
52 |
import com.sun.tools.sjavac.Util; |
32799 | 53 |
import com.sun.tools.sjavac.options.Option; |
32335 | 54 |
import com.sun.tools.sjavac.options.Options; |
55 |
import com.sun.tools.sjavac.options.SourceLocation; |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
56 |
import com.sun.tools.sjavac.server.Sjavac; |
36526 | 57 |
import java.io.UncheckedIOException; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
58 |
|
32799 | 59 |
import javax.tools.JavaFileManager; |
60 |
||
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
61 |
/** |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
62 |
* The sjavac implementation that interacts with javac and performs the actual |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
63 |
* compilation. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
64 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
65 |
* <p><b>This is NOT part of any supported API. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
66 |
* If you write code that depends on this, you do so at your own risk. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
67 |
* This code and its internal interfaces are subject to change or |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
68 |
* deletion without notice.</b> |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
69 |
*/ |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
70 |
public class SjavacImpl implements Sjavac { |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
71 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
72 |
@Override |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
73 |
public Result compile(String[] args) { |
32335 | 74 |
Options options; |
75 |
try { |
|
76 |
options = Options.parseArgs(args); |
|
77 |
} catch (IllegalArgumentException e) { |
|
78 |
Log.error(e.getMessage()); |
|
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
79 |
return Result.CMDERR; |
32335 | 80 |
} |
81 |
||
82 |
if (!validateOptions(options)) |
|
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
83 |
return Result.CMDERR; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
84 |
|
36268
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
85 |
if (srcDstOverlap(options.getSources(), options.getDestDir())) { |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
86 |
return Result.CMDERR; |
36268
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
87 |
} |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
88 |
|
32335 | 89 |
if (!createIfMissing(options.getDestDir())) |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
90 |
return Result.ERROR; |
32335 | 91 |
|
32799 | 92 |
Path stateDir = options.getStateDir(); |
93 |
if (stateDir != null && !createIfMissing(options.getStateDir())) |
|
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
94 |
return Result.ERROR; |
32335 | 95 |
|
96 |
Path gensrc = options.getGenSrcDir(); |
|
97 |
if (gensrc != null && !createIfMissing(gensrc)) |
|
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
98 |
return Result.ERROR; |
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
27388
diff
changeset
|
99 |
|
32335 | 100 |
Path hdrdir = options.getHeaderDir(); |
101 |
if (hdrdir != null && !createIfMissing(hdrdir)) |
|
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
102 |
return Result.ERROR; |
32335 | 103 |
|
32799 | 104 |
if (stateDir == null) { |
105 |
// Prepare context. Direct logging to our byte array stream. |
|
106 |
Context context = new Context(); |
|
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
107 |
StringWriter strWriter = new StringWriter(); |
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
108 |
PrintWriter printWriter = new PrintWriter(strWriter); |
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
109 |
com.sun.tools.javac.util.Log.preRegister(context, printWriter); |
32799 | 110 |
JavacFileManager.preRegister(context); |
32335 | 111 |
|
32799 | 112 |
// Prepare arguments |
113 |
String[] passThroughArgs = Stream.of(args) |
|
114 |
.filter(arg -> !arg.startsWith(Option.SERVER.arg)) |
|
115 |
.toArray(String[]::new); |
|
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
116 |
// Compile |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
117 |
Result result = new Main("javac", printWriter).compile(passThroughArgs, context); |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
118 |
|
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
119 |
// Process compiler output (which is always errors) |
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
120 |
printWriter.flush(); |
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
121 |
Util.getLines(strWriter.toString()).forEach(Log::error); |
32335 | 122 |
|
32799 | 123 |
// Clean up |
124 |
JavaFileManager fileManager = context.get(JavaFileManager.class); |
|
125 |
if (fileManager instanceof JavacFileManager) { |
|
34560
b6a567b677f7
8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents:
33553
diff
changeset
|
126 |
try { |
b6a567b677f7
8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents:
33553
diff
changeset
|
127 |
((JavacFileManager) fileManager).close(); |
36526 | 128 |
} catch (IOException es) { |
129 |
throw new UncheckedIOException(es); |
|
34560
b6a567b677f7
8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents:
33553
diff
changeset
|
130 |
} |
32799 | 131 |
} |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
132 |
return result; |
32799 | 133 |
|
134 |
} else { |
|
135 |
// Load the prev build state database. |
|
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
136 |
JavacState javac_state = JavacState.load(options); |
32799 | 137 |
|
138 |
// Setup the suffix rules from the command line. |
|
139 |
Map<String, Transformer> suffixRules = new HashMap<>(); |
|
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
27388
diff
changeset
|
140 |
|
32799 | 141 |
// Handling of .java-compilation |
142 |
suffixRules.putAll(javac_state.getJavaSuffixRule()); |
|
143 |
||
144 |
// Handling of -copy and -tr |
|
145 |
suffixRules.putAll(options.getTranslationRules()); |
|
32335 | 146 |
|
32799 | 147 |
// All found modules are put here. |
148 |
Map<String,Module> modules = new HashMap<>(); |
|
149 |
// We start out in the legacy empty no-name module. |
|
150 |
// As soon as we stumble on a module-info.java file we change to that module. |
|
151 |
Module current_module = new Module("", ""); |
|
152 |
modules.put("", current_module); |
|
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
153 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
154 |
try { |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
155 |
// Find all sources, use the suffix rules to know which files are sources. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
156 |
Map<String,Source> sources = new HashMap<>(); |
26991
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
26107
diff
changeset
|
157 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
158 |
// Find the files, this will automatically populate the found modules |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
159 |
// with found packages where the sources are found! |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
160 |
findSourceFiles(options.getSources(), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
161 |
suffixRules.keySet(), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
162 |
sources, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
163 |
modules, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
164 |
current_module, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
165 |
options.isDefaultPackagePermitted(), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
166 |
false); |
32799 | 167 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
168 |
if (sources.isEmpty()) { |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
169 |
Log.error("Found nothing to compile!"); |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
170 |
return Result.ERROR; |
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
171 |
} |
32335 | 172 |
|
173 |
||
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
174 |
// Create a map of all source files that are available for linking. Both -src and |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
175 |
// -sourcepath point to such files. It is possible to specify multiple |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
176 |
// -sourcepath options to enable different filtering rules. If the |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
177 |
// filters are the same for multiple sourcepaths, they may be concatenated |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
178 |
// using :(;). Before sending the list of sourcepaths to javac, they are |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
179 |
// all concatenated. The list created here is used by the SmartFileWrapper to |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
180 |
// make sure only the correct sources are actually available. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
181 |
// We might find more modules here as well. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
182 |
Map<String,Source> sources_to_link_to = new HashMap<>(); |
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
27388
diff
changeset
|
183 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
184 |
List<SourceLocation> sourceResolutionLocations = new ArrayList<>(); |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
185 |
sourceResolutionLocations.addAll(options.getSources()); |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
186 |
sourceResolutionLocations.addAll(options.getSourceSearchPaths()); |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
187 |
findSourceFiles(sourceResolutionLocations, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
188 |
Collections.singleton(".java"), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
189 |
sources_to_link_to, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
190 |
modules, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
191 |
current_module, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
192 |
options.isDefaultPackagePermitted(), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
193 |
true); |
32335 | 194 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
195 |
// Add the set of sources to the build database. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
196 |
javac_state.now().flattenPackagesSourcesAndArtifacts(modules); |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
197 |
javac_state.now().checkInternalState("checking sources", false, sources); |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
198 |
javac_state.now().checkInternalState("checking linked sources", true, sources_to_link_to); |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
199 |
javac_state.setVisibleSources(sources_to_link_to); |
32335 | 200 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
201 |
int round = 0; |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
202 |
printRound(round); |
32335 | 203 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
204 |
// If there is any change in the source files, taint packages |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
205 |
// and mark the database in need of saving. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
206 |
javac_state.checkSourceStatus(false); |
32335 | 207 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
208 |
// Find all existing artifacts. Their timestamp will match the last modified timestamps stored |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
209 |
// in javac_state, simply because loading of the JavacState will clean out all artifacts |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
210 |
// that do not match the javac_state database. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
211 |
javac_state.findAllArtifacts(); |
32335 | 212 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
213 |
// Remove unidentified artifacts from the bin, gensrc and header dirs. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
214 |
// (Unless we allow them to be there.) |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
215 |
// I.e. artifacts that are not known according to the build database (javac_state). |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
216 |
// For examples, files that have been manually copied into these dirs. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
217 |
// Artifacts with bad timestamps (ie the on disk timestamp does not match the timestamp |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
218 |
// in javac_state) have already been removed when the javac_state was loaded. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
219 |
if (!options.areUnidentifiedArtifactsPermitted()) { |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
220 |
javac_state.removeUnidentifiedArtifacts(); |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
221 |
} |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
222 |
// Go through all sources and taint all packages that miss artifacts. |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
223 |
javac_state.taintPackagesThatMissArtifacts(); |
32335 | 224 |
|
33553
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
225 |
// Check recorded classpath public apis. Taint packages that depend on |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
226 |
// classpath classes whose public apis have changed. |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
227 |
javac_state.taintPackagesDependingOnChangedClasspathPackages(); |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
228 |
|
33553
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
229 |
// Now clean out all known artifacts belonging to tainted packages. |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
230 |
javac_state.deleteClassArtifactsInTaintedPackages(); |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
231 |
// Copy files, for example property files, images files, xml files etc etc. |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
232 |
javac_state.performCopying(Util.pathToFile(options.getDestDir()), suffixRules); |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
233 |
// Translate files, for example compile properties or compile idls. |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
234 |
javac_state.performTranslation(Util.pathToFile(gensrc), suffixRules); |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
235 |
// Add any potentially generated java sources to the tobe compiled list. |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
236 |
// (Generated sources must always have a package.) |
9a742b6b95d3
8137075: Sjavac tests are leaking file managers
alundblad
parents:
32799
diff
changeset
|
237 |
Map<String,Source> generated_sources = new HashMap<>(); |
32335 | 238 |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
239 |
Source.scanRoot(Util.pathToFile(options.getGenSrcDir()), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
240 |
Util.set(".java"), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
241 |
Collections.emptyList(), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
242 |
Collections.emptyList(), |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
243 |
generated_sources, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
244 |
modules, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
245 |
current_module, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
246 |
false, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
247 |
true, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
248 |
false); |
32799 | 249 |
javac_state.now().flattenPackagesSourcesAndArtifacts(modules); |
250 |
// Recheck the the source files and their timestamps again. |
|
251 |
javac_state.checkSourceStatus(true); |
|
32335 | 252 |
|
32799 | 253 |
// Now do a safety check that the list of source files is identical |
254 |
// to the list Make believes we are compiling. If we do not get this |
|
255 |
// right, then incremental builds will fail with subtility. |
|
256 |
// If any difference is detected, then we will fail hard here. |
|
257 |
// This is an important safety net. |
|
258 |
javac_state.compareWithMakefileList(Util.pathToFile(options.getSourceReferenceList())); |
|
32335 | 259 |
|
32799 | 260 |
// Do the compilations, repeatedly until no tainted packages exist. |
261 |
boolean again; |
|
262 |
// Collect the name of all compiled packages. |
|
263 |
Set<String> recently_compiled = new HashSet<>(); |
|
264 |
boolean[] rc = new boolean[1]; |
|
32335 | 265 |
|
32799 | 266 |
CompilationService compilationService = new CompilationService(); |
267 |
do { |
|
268 |
if (round > 0) |
|
269 |
printRound(round); |
|
270 |
// Clean out artifacts in tainted packages. |
|
271 |
javac_state.deleteClassArtifactsInTaintedPackages(); |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
272 |
again = javac_state.performJavaCompilations(compilationService, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
273 |
options, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
274 |
recently_compiled, |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
275 |
rc); |
32799 | 276 |
if (!rc[0]) { |
277 |
Log.debug("Compilation failed."); |
|
278 |
break; |
|
279 |
} |
|
280 |
if (!again) { |
|
281 |
Log.debug("Nothing left to do."); |
|
282 |
} |
|
283 |
round++; |
|
284 |
} while (again); |
|
285 |
Log.debug("No need to do another round."); |
|
286 |
||
287 |
// Only update the state if the compile went well. |
|
288 |
if (rc[0]) { |
|
289 |
javac_state.save(); |
|
290 |
// Reflatten only the artifacts. |
|
291 |
javac_state.now().flattenArtifacts(modules); |
|
292 |
// Remove artifacts that were generated during the last compile, but not this one. |
|
293 |
javac_state.removeSuperfluousArtifacts(recently_compiled); |
|
32335 | 294 |
} |
295 |
||
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
296 |
return rc[0] ? Result.OK : Result.ERROR; |
32799 | 297 |
} catch (ProblemException e) { |
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
298 |
// For instance make file list mismatch. |
32799 | 299 |
Log.error(e.getMessage()); |
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
300 |
Log.debug(e); |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
301 |
return Result.ERROR; |
32799 | 302 |
} catch (Exception e) { |
36161
a025c0619f25
8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents:
35356
diff
changeset
|
303 |
Log.error(e); |
37643
626e07816dce
8048146: sjavac uses unexpected exit code of -1
alundblad
parents:
36526
diff
changeset
|
304 |
return Result.ERROR; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
305 |
} |
26992
92e69fa21956
8059349: Public API scanning should be implemented in the form of a TaskListener
alundblad
parents:
26991
diff
changeset
|
306 |
} |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
307 |
} |
26088 | 308 |
|
309 |
@Override |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
310 |
public void shutdown() { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
311 |
// Nothing to clean up |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
312 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
313 |
|
32335 | 314 |
private static boolean validateOptions(Options options) { |
315 |
||
316 |
String err = null; |
|
317 |
||
318 |
if (options.getDestDir() == null) { |
|
319 |
err = "Please specify output directory."; |
|
320 |
} else if (options.isJavaFilesAmongJavacArgs()) { |
|
321 |
err = "Sjavac does not handle explicit compilation of single .java files."; |
|
322 |
} else if (!options.getImplicitPolicy().equals("none")) { |
|
323 |
err = "The only allowed setting for sjavac is -implicit:none"; |
|
32799 | 324 |
} else if (options.getSources().isEmpty() && options.getStateDir() != null) { |
325 |
err = "You have to specify -src when using --state-dir."; |
|
32335 | 326 |
} else if (options.getTranslationRules().size() > 1 |
327 |
&& options.getGenSrcDir() == null) { |
|
328 |
err = "You have translators but no gensrc dir (-s) specified!"; |
|
329 |
} |
|
330 |
||
331 |
if (err != null) |
|
332 |
Log.error(err); |
|
333 |
||
334 |
return err == null; |
|
335 |
||
26088 | 336 |
} |
337 |
||
36268
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
338 |
private static boolean srcDstOverlap(List<SourceLocation> locs, Path dest) { |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
339 |
for (SourceLocation loc : locs) { |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
340 |
if (isOverlapping(loc.getPath(), dest)) { |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
341 |
Log.error("Source location " + loc.getPath() + " overlaps with destination " + dest); |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
342 |
return true; |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
343 |
} |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
344 |
} |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
345 |
return false; |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
346 |
} |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
347 |
|
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
348 |
private static boolean isOverlapping(Path p1, Path p2) { |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
349 |
p1 = p1.toAbsolutePath().normalize(); |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
350 |
p2 = p2.toAbsolutePath().normalize(); |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
351 |
return p1.startsWith(p2) || p2.startsWith(p1); |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
352 |
} |
382811896236
8061320: Sjavac should prevent using source dir as dest dir
alundblad
parents:
36161
diff
changeset
|
353 |
|
32335 | 354 |
private static boolean createIfMissing(Path dir) { |
355 |
||
356 |
if (Files.isDirectory(dir)) |
|
357 |
return true; |
|
358 |
||
359 |
if (Files.exists(dir)) { |
|
360 |
Log.error(dir + " is not a directory."); |
|
361 |
return false; |
|
362 |
} |
|
363 |
||
364 |
try { |
|
365 |
Files.createDirectories(dir); |
|
366 |
} catch (IOException e) { |
|
367 |
Log.error("Could not create directory: " + e.getMessage()); |
|
368 |
return false; |
|
369 |
} |
|
370 |
||
371 |
return true; |
|
372 |
} |
|
373 |
||
374 |
/** Find source files in the given source locations. */ |
|
375 |
public static void findSourceFiles(List<SourceLocation> sourceLocations, |
|
376 |
Set<String> sourceTypes, |
|
377 |
Map<String,Source> foundFiles, |
|
378 |
Map<String, Module> foundModules, |
|
379 |
Module currentModule, |
|
380 |
boolean permitSourcesInDefaultPackage, |
|
34991
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
381 |
boolean inLinksrc) |
ff8be37d1164
8144226: Sjavac's handling of include/exclude patterns is buggy, redundant and inconsistent
alundblad
parents:
34752
diff
changeset
|
382 |
throws IOException { |
32335 | 383 |
|
384 |
for (SourceLocation source : sourceLocations) { |
|
385 |
source.findSourceFiles(sourceTypes, |
|
386 |
foundFiles, |
|
387 |
foundModules, |
|
388 |
currentModule, |
|
389 |
permitSourcesInDefaultPackage, |
|
390 |
inLinksrc); |
|
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
27388
diff
changeset
|
391 |
} |
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
27388
diff
changeset
|
392 |
} |
32335 | 393 |
|
394 |
private static void printRound(int round) { |
|
395 |
Log.debug("****************************************"); |
|
396 |
Log.debug("* Round " + round + " *"); |
|
397 |
Log.debug("****************************************"); |
|
398 |
} |
|
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
399 |
} |