author | wenzelm |
Wed, 22 Sep 2021 21:20:36 +0200 | |
changeset 74355 | f77474665b2f |
parent 74061 | 203dfa8bc0fc |
child 74738 | cba1da393958 |
permissions | -rw-r--r-- |
74031 | 1 |
/* Title: Tools/Setup/src/Build.java |
73914 | 2 |
Author: Makarius |
3 |
||
73954 | 4 |
Build Isabelle/Scala/Java modules. |
73914 | 5 |
*/ |
6 |
||
7 |
package isabelle.setup; |
|
8 |
||
9 |
||
73922 | 10 |
import java.io.BufferedOutputStream; |
74018
9d6c9a55f450
more informative errors: capture low-level compiler output;
wenzelm
parents:
73987
diff
changeset
|
11 |
import java.io.ByteArrayOutputStream; |
9d6c9a55f450
more informative errors: capture low-level compiler output;
wenzelm
parents:
73987
diff
changeset
|
12 |
import java.io.CharArrayWriter; |
73914 | 13 |
import java.io.File; |
14 |
import java.io.IOException; |
|
74018
9d6c9a55f450
more informative errors: capture low-level compiler output;
wenzelm
parents:
73987
diff
changeset
|
15 |
import java.io.PrintStream; |
73914 | 16 |
import java.math.BigInteger; |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
17 |
import java.nio.charset.StandardCharsets; |
73914 | 18 |
import java.nio.file.Files; |
19 |
import java.nio.file.Path; |
|
73918 | 20 |
import java.nio.file.StandardCopyOption; |
73914 | 21 |
import java.security.MessageDigest; |
22 |
import java.security.NoSuchAlgorithmException; |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
23 |
import java.util.ArrayList; |
73914 | 24 |
import java.util.Comparator; |
25 |
import java.util.LinkedList; |
|
26 |
import java.util.List; |
|
73919 | 27 |
import java.util.Locale; |
73914 | 28 |
import java.util.Properties; |
73967 | 29 |
import java.util.TreeMap; |
73922 | 30 |
import java.util.jar.Attributes; |
31 |
import java.util.jar.JarEntry; |
|
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
32 |
import java.util.jar.JarFile; |
73922 | 33 |
import java.util.jar.JarOutputStream; |
34 |
import java.util.jar.Manifest; |
|
73914 | 35 |
import java.util.stream.Stream; |
36 |
||
73954 | 37 |
import javax.tools.JavaCompiler; |
38 |
import javax.tools.JavaFileObject; |
|
39 |
import javax.tools.StandardJavaFileManager; |
|
40 |
import javax.tools.ToolProvider; |
|
41 |
||
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
42 |
import scala.tools.nsc.MainClass; |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
43 |
|
73914 | 44 |
|
73944 | 45 |
public class Build |
73914 | 46 |
{ |
73951 | 47 |
/** context **/ |
48 |
||
49 |
public static String BUILD_PROPS = "build.props"; |
|
73961 | 50 |
public static String COMPONENT_BUILD_PROPS = "etc/build.props"; |
74059 | 51 |
|
52 |
public static String TITLE = "title"; |
|
74057 | 53 |
public static String MODULE = "module"; |
54 |
public static String NO_BUILD = "no_build"; |
|
73951 | 55 |
|
56 |
public static Context component_context(Path dir) |
|
57 |
throws IOException |
|
58 |
{ |
|
59 |
Properties props = new Properties(); |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
60 |
Path props_path = dir.resolve(COMPONENT_BUILD_PROPS); |
74055 | 61 |
props.load(Files.newBufferedReader(props_path)); |
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
62 |
return new Context(dir, props, props_path.toString()); |
73951 | 63 |
} |
73922 | 64 |
|
73961 | 65 |
public static List<Context> component_contexts() |
66 |
throws IOException, InterruptedException |
|
67 |
{ |
|
68 |
List<Context> result = new LinkedList<Context>(); |
|
69 |
for (String p : Environment.getenv("ISABELLE_COMPONENTS").split(":", -1)) { |
|
70 |
if (!p.isEmpty()) { |
|
71 |
Path dir = Path.of(Environment.platform_path(p)); |
|
72 |
if (Files.exists(dir.resolve(COMPONENT_BUILD_PROPS))) { |
|
73 |
result.add(component_context(dir)); |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
return List.copyOf(result); |
|
78 |
} |
|
79 |
||
73967 | 80 |
private static String sha_digest(MessageDigest sha, String name) |
81 |
{ |
|
82 |
String digest = String.format(Locale.ROOT, "%040x", new BigInteger(1, sha.digest())); |
|
83 |
return digest + " " + name + "\n"; |
|
84 |
} |
|
85 |
||
73914 | 86 |
public static class Context |
87 |
{ |
|
73951 | 88 |
private final Path _dir; |
89 |
private final Properties _props; |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
90 |
private final String _location; |
73914 | 91 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
92 |
public Context(Path dir, Properties props, String location) |
73914 | 93 |
{ |
73951 | 94 |
_dir = dir; |
95 |
_props = props; |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
96 |
_location = location; |
73914 | 97 |
} |
98 |
||
73951 | 99 |
@Override public String toString() { return _dir.toString(); } |
73914 | 100 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
101 |
public String error_message(String msg) |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
102 |
{ |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
103 |
if (_location == null || _location.isEmpty()) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
104 |
return msg; |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
105 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
106 |
else { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
107 |
return msg +" (in " + Library.quote(_location) + ")"; |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
108 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
109 |
} |
73915 | 110 |
|
74057 | 111 |
public boolean get_bool(String name) { |
112 |
String prop = _props.getProperty(name, "false"); |
|
113 |
switch(prop) { |
|
114 |
case "true": return true; |
|
115 |
case "false": return false; |
|
116 |
default: |
|
117 |
throw new RuntimeException( |
|
118 |
error_message("Bad boolean property " + Library.quote(name) + ": " + Library.quote(prop))); |
|
119 |
} |
|
120 |
} |
|
121 |
||
74059 | 122 |
public String title() { return _props.getProperty(TITLE, ""); } |
74057 | 123 |
|
124 |
public String module_name() { return _props.getProperty(MODULE, ""); } |
|
125 |
public String module_result() { return get_bool(NO_BUILD) ? "" : module_name(); } |
|
126 |
||
73954 | 127 |
public String scalac_options() { return _props.getProperty("scalac_options", ""); } |
128 |
public String javac_options() { return _props.getProperty("javac_options", ""); } |
|
73951 | 129 |
public String main() { return _props.getProperty("main", ""); } |
73914 | 130 |
|
73931 | 131 |
private List<String> get_list(String name) |
132 |
{ |
|
133 |
List<String> list = new LinkedList<String>(); |
|
73951 | 134 |
for (String s : _props.getProperty(name, "").split("\\s+")) { |
73931 | 135 |
if (!s.isEmpty()) { list.add(s); } |
136 |
} |
|
137 |
return List.copyOf(list); |
|
138 |
} |
|
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
139 |
public List<String> requirements() { return get_list("requirements"); } |
73931 | 140 |
public List<String> sources() { return get_list("sources"); } |
141 |
public List<String> resources() { return get_list("resources"); } |
|
142 |
public List<String> services() { return get_list("services"); } |
|
73914 | 143 |
|
73965 | 144 |
public boolean is_vacuous() |
145 |
{ |
|
146 |
return sources().isEmpty() && resources().isEmpty() && services().isEmpty(); |
|
147 |
} |
|
148 |
||
73957
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
149 |
public Path path(String file) |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
150 |
throws IOException, InterruptedException |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
151 |
{ |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
152 |
return _dir.resolve(Environment.expand_platform_path(file)); |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
153 |
} |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
154 |
public boolean exists(String file) |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
155 |
throws IOException, InterruptedException |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
156 |
{ |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
157 |
return Files.exists(path(file)); |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
158 |
} |
73914 | 159 |
|
74030
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
160 |
public List<Path> requirement_paths(String s) |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
161 |
throws IOException, InterruptedException |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
162 |
{ |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
163 |
if (s.startsWith("env:")) { |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
164 |
List<Path> paths = new LinkedList<Path>(); |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
165 |
for (String p : Environment.getenv(s.substring(4)).split(":", -1)) { |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
166 |
if (!p.isEmpty()) { |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
167 |
Path path = Path.of(Environment.platform_path(p)); |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
168 |
paths.add(path); |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
169 |
} |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
170 |
} |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
171 |
return List.copyOf(paths); |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
172 |
} |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
173 |
else { return List.of(path(s)); } |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
174 |
} |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
175 |
|
73921 | 176 |
public String item_name(String s) |
73918 | 177 |
{ |
178 |
int i = s.indexOf(':'); |
|
179 |
return i > 0 ? s.substring(0, i) : s; |
|
180 |
} |
|
181 |
||
73921 | 182 |
public String item_target(String s) |
73918 | 183 |
{ |
184 |
int i = s.indexOf(':'); |
|
185 |
return i > 0 ? s.substring(i + 1) : s; |
|
186 |
} |
|
187 |
||
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
188 |
public String shasum(String name, List<Path> paths) |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
189 |
throws IOException, NoSuchAlgorithmException |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
190 |
{ |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
191 |
MessageDigest sha = MessageDigest.getInstance("SHA"); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
192 |
for (Path file : paths) { |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
193 |
if (Files.exists(file)) { |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
194 |
sha.update(Files.readAllBytes(file)); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
195 |
} |
73966 | 196 |
else { |
197 |
throw new RuntimeException( |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
198 |
error_message("Missing input file " + Library.quote(file.toString()))); |
73966 | 199 |
} |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
200 |
} |
73967 | 201 |
return sha_digest(sha, name); |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
202 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
203 |
|
73914 | 204 |
public String shasum(String file) |
73957
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
205 |
throws IOException, NoSuchAlgorithmException, InterruptedException |
73914 | 206 |
{ |
73966 | 207 |
return shasum(file, List.of(path(file))); |
73914 | 208 |
} |
73967 | 209 |
|
210 |
public String shasum_props() |
|
211 |
throws NoSuchAlgorithmException |
|
212 |
{ |
|
213 |
TreeMap<String,Object> sorted = new TreeMap<String,Object>(); |
|
214 |
for (Object x : _props.entrySet()) { |
|
215 |
sorted.put(x.toString(), _props.get(x)); |
|
216 |
} |
|
217 |
MessageDigest sha = MessageDigest.getInstance("SHA"); |
|
218 |
sha.update(sorted.toString().getBytes(StandardCharsets.UTF_8)); |
|
219 |
return sha_digest(sha, "<props>"); |
|
220 |
} |
|
73914 | 221 |
} |
222 |
||
73922 | 223 |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
224 |
/** compile sources **/ |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
225 |
|
73954 | 226 |
private static void add_options(List<String> options_list, String options) |
227 |
{ |
|
228 |
if (options != null) { |
|
229 |
for (String s : options.split("\\s+")) { |
|
230 |
if (!s.isEmpty()) { options_list.add(s); } |
|
231 |
} |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
public static void compile_scala_sources( |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
236 |
PrintStream output, // ignored, but see scala.Console.withOut/withErr |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
237 |
Path target_dir, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
238 |
String more_options, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
239 |
List<Path> deps, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
240 |
List<Path> sources) throws IOException, InterruptedException |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
241 |
{ |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
242 |
ArrayList<String> args = new ArrayList<String>(); |
73954 | 243 |
add_options(args, Environment.getenv("ISABELLE_SCALAC_OPTIONS")); |
244 |
add_options(args, more_options); |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
245 |
args.add("-d"); |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
246 |
args.add(target_dir.toString()); |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
247 |
args.add("-bootclasspath"); |
73960 | 248 |
args.add(Environment.join_platform_paths(deps)); |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
249 |
args.add("--"); |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
250 |
|
73955 | 251 |
boolean scala_sources = false; |
252 |
for (Path p : sources) { |
|
253 |
args.add(p.toString()); |
|
254 |
if (p.toString().endsWith(".scala")) { scala_sources = true; } |
|
255 |
} |
|
256 |
if (scala_sources) { |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
257 |
boolean ok = new MainClass().process(args.toArray(String[]::new)); |
74355 | 258 |
if (!ok) { throw new RuntimeException("Failed to compile Scala sources"); } |
73955 | 259 |
} |
73954 | 260 |
} |
261 |
||
262 |
public static void compile_java_sources( |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
263 |
PrintStream output, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
264 |
Path target_dir, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
265 |
String more_options, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
266 |
List<Path> deps, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
267 |
List<Path> sources) throws IOException, InterruptedException |
73954 | 268 |
{ |
269 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
270 |
StandardJavaFileManager file_manager = |
|
271 |
compiler.getStandardFileManager(null, Locale.ROOT, StandardCharsets.UTF_8); |
|
272 |
||
273 |
List<String> options = new LinkedList<String>(); |
|
274 |
add_options(options, Environment.getenv("ISABELLE_JAVAC_OPTIONS")); |
|
275 |
add_options(options, more_options); |
|
276 |
options.add("-d"); |
|
277 |
options.add(target_dir.toString()); |
|
278 |
options.add("-classpath"); |
|
73960 | 279 |
options.add(Environment.join_platform_paths(deps)); |
73954 | 280 |
|
281 |
List<JavaFileObject> java_sources = new LinkedList<JavaFileObject>(); |
|
282 |
for (Path p : sources) { |
|
283 |
if (p.toString().endsWith(".java")) { |
|
284 |
for (JavaFileObject o : file_manager.getJavaFileObjectsFromPaths(List.of(p))) { |
|
285 |
java_sources.add(o); |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
74018
9d6c9a55f450
more informative errors: capture low-level compiler output;
wenzelm
parents:
73987
diff
changeset
|
289 |
|
73954 | 290 |
if (!java_sources.isEmpty()) { |
74018
9d6c9a55f450
more informative errors: capture low-level compiler output;
wenzelm
parents:
73987
diff
changeset
|
291 |
CharArrayWriter out = new CharArrayWriter(); |
9d6c9a55f450
more informative errors: capture low-level compiler output;
wenzelm
parents:
73987
diff
changeset
|
292 |
boolean ok = compiler.getTask(out, file_manager, null, options, null, java_sources).call(); |
74025 | 293 |
out.flush(); |
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
294 |
|
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
295 |
String msg = Library.trim_line(out.toString()); |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
296 |
if (ok) { if (!msg.isEmpty()) { output.print(msg + "\n"); } } |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
297 |
else { |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
298 |
throw new RuntimeException( |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
299 |
(msg.isEmpty() ? "" : msg + "\n") + "Failed to compile Java sources"); |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
300 |
} |
73954 | 301 |
} |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
302 |
} |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
303 |
|
73922 | 304 |
|
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
305 |
/** shasum for jar content **/ |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
306 |
|
73965 | 307 |
private static String SHASUM = "META-INF/isabelle/shasum"; |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
308 |
|
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
309 |
public static String get_shasum(Path jar_path) |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
310 |
{ |
74058 | 311 |
try { |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
312 |
try (JarFile jar_file = new JarFile(jar_path.toFile())) |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
313 |
{ |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
314 |
JarEntry entry = jar_file.getJarEntry(SHASUM); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
315 |
if (entry != null) { |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
316 |
byte[] bytes = jar_file.getInputStream(entry).readAllBytes(); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
317 |
return new String(bytes, StandardCharsets.UTF_8); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
318 |
} |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
319 |
else { return ""; } |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
320 |
} |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
321 |
} |
74058 | 322 |
catch (IOException exn) { return ""; } |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
323 |
} |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
324 |
|
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
325 |
public static void create_shasum(Path dir, String shasum) |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
326 |
throws IOException |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
327 |
{ |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
328 |
Path path = dir.resolve(SHASUM); |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
329 |
Files.createDirectories(path.getParent()); |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
330 |
Files.writeString(path, shasum); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
331 |
} |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
332 |
|
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
333 |
|
73965 | 334 |
/** services **/ |
335 |
||
336 |
private static String SERVICES = "META-INF/isabelle/services"; |
|
337 |
||
338 |
public static List<String> get_services(Path jar_path) |
|
339 |
throws IOException |
|
340 |
{ |
|
341 |
if (Files.exists(jar_path)) { |
|
342 |
try (JarFile jar_file = new JarFile(jar_path.toFile())) |
|
343 |
{ |
|
344 |
JarEntry entry = jar_file.getJarEntry(SERVICES); |
|
345 |
if (entry != null) { |
|
346 |
byte[] bytes = jar_file.getInputStream(entry).readAllBytes(); |
|
347 |
return Library.split_lines(new String(bytes, StandardCharsets.UTF_8)); |
|
348 |
} |
|
349 |
else { return List.of(); } |
|
350 |
} |
|
351 |
} |
|
352 |
else { return List.of(); } |
|
353 |
} |
|
354 |
||
355 |
public static void create_services(Path dir, List<String> services) |
|
356 |
throws IOException |
|
357 |
{ |
|
358 |
if (!services.isEmpty()) { |
|
359 |
Path path = dir.resolve(SERVICES); |
|
360 |
Files.createDirectories(path.getParent()); |
|
361 |
Files.writeString(path, Library.cat_lines(services)); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
||
73922 | 366 |
/** create jar **/ |
367 |
||
368 |
public static void create_jar(Path dir, String main, Path jar) |
|
369 |
throws IOException |
|
370 |
{ |
|
73949 | 371 |
Files.createDirectories(dir.resolve(jar).getParent()); |
73922 | 372 |
Files.deleteIfExists(jar); |
373 |
||
374 |
Manifest manifest = new Manifest(); |
|
375 |
Attributes attributes = manifest.getMainAttributes(); |
|
376 |
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
|
377 |
attributes.put(new Attributes.Name("Created-By"), |
|
378 |
System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"); |
|
379 |
if (!main.isEmpty()) { attributes.put(Attributes.Name.MAIN_CLASS, main); } |
|
380 |
||
381 |
try (JarOutputStream out = |
|
382 |
new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jar)), manifest)) |
|
383 |
{ |
|
384 |
for (Path path : Files.walk(dir).sorted().toArray(Path[]::new)) { |
|
385 |
boolean is_dir = Files.isDirectory(path); |
|
386 |
boolean is_file = Files.isRegularFile(path); |
|
387 |
if (is_dir || is_file) { |
|
388 |
String name = Environment.slashes(dir.relativize(path).toString()); |
|
73950 | 389 |
if (!name.isEmpty()) { |
390 |
JarEntry entry = new JarEntry(is_dir ? name + "/" : name); |
|
391 |
entry.setTime(path.toFile().lastModified()); |
|
392 |
out.putNextEntry(entry); |
|
393 |
if (is_file) { out.write(Files.readAllBytes(path)); } |
|
394 |
out.closeEntry(); |
|
395 |
} |
|
73922 | 396 |
} |
397 |
} |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
||
73961 | 402 |
/** classpath **/ |
403 |
||
404 |
public static List<Path> classpath() |
|
405 |
throws IOException, InterruptedException |
|
406 |
{ |
|
407 |
List<Path> result = new LinkedList<Path>(); |
|
408 |
for (Context context : component_contexts()) { |
|
74057 | 409 |
String module = context.module_result(); |
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
410 |
if (!module.isEmpty()) { result.add(context.path(module)); } |
73961 | 411 |
} |
412 |
return List.copyOf(result); |
|
413 |
} |
|
414 |
||
415 |
public static List<String> services() |
|
416 |
throws IOException, InterruptedException |
|
417 |
{ |
|
418 |
List<String> result = new LinkedList<String>(); |
|
419 |
for (Context context : component_contexts()) { |
|
420 |
for (String s : context.services()) { |
|
421 |
result.add(s); |
|
422 |
} |
|
423 |
} |
|
424 |
return List.copyOf(result); |
|
425 |
} |
|
426 |
||
427 |
||
73954 | 428 |
/** build **/ |
73922 | 429 |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
430 |
public static void build(PrintStream output, Context context, boolean fresh) |
73914 | 431 |
throws IOException, InterruptedException, NoSuchAlgorithmException |
432 |
{ |
|
74057 | 433 |
String module = context.module_result(); |
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
434 |
if (!module.isEmpty()) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
435 |
String title = context.title(); |
73914 | 436 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
437 |
Path jar_path = context.path(module); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
438 |
String jar_name = jar_path.toString(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
439 |
if (!jar_name.endsWith(".jar")) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
440 |
throw new RuntimeException( |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
441 |
context.error_message("Bad jar module " + Library.quote(jar_name))); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
442 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
443 |
|
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
444 |
if (context.is_vacuous()) { Files.deleteIfExists(jar_path); } |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
445 |
else { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
446 |
List<String> requirements = context.requirements(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
447 |
List<String> resources = context.resources(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
448 |
List<String> sources = context.sources(); |
73914 | 449 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
450 |
String shasum_old = get_shasum(jar_path); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
451 |
String shasum; |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
452 |
List<Path> compiler_deps = new LinkedList<Path>(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
453 |
{ |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
454 |
StringBuilder _shasum = new StringBuilder(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
455 |
_shasum.append(context.shasum_props()); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
456 |
for (String s : requirements) { |
74030
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
457 |
List<Path> paths = context.requirement_paths(s); |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
458 |
compiler_deps.addAll(paths); |
39e05601faeb
more accurate scala_project, based on build.props of components;
wenzelm
parents:
74029
diff
changeset
|
459 |
_shasum.append(context.shasum(s, paths)); |
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
460 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
461 |
for (String s : resources) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
462 |
_shasum.append(context.shasum(context.item_name(s))); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
463 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
464 |
for (String s : sources) { _shasum.append(context.shasum(s)); } |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
465 |
shasum = _shasum.toString(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
466 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
467 |
if (fresh || !shasum_old.equals(shasum)) { |
74059 | 468 |
if (!title.isEmpty()) { |
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
469 |
output.print("### Building " + title + " (" + jar_path + ") ...\n"); |
74059 | 470 |
} |
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
471 |
|
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
472 |
String isabelle_classpath = Environment.getenv("ISABELLE_CLASSPATH"); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
473 |
|
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
474 |
Path build_dir = Files.createTempDirectory("isabelle"); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
475 |
try { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
476 |
/* compile sources */ |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
477 |
|
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
478 |
for (String s : isabelle_classpath.split(":", -1)) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
479 |
if (!s.isEmpty()) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
480 |
compiler_deps.add(Path.of(Environment.platform_path(s))); |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
481 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
482 |
} |
73914 | 483 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
484 |
List<Path> compiler_sources = new LinkedList<Path>(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
485 |
for (String s : sources) { compiler_sources.add(context.path(s)); } |
73914 | 486 |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
487 |
compile_scala_sources(output, build_dir, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
488 |
context.scalac_options(), compiler_deps, compiler_sources); |
73914 | 489 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
490 |
compiler_deps.add(build_dir); |
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
491 |
compile_java_sources(output, build_dir, |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
492 |
context.javac_options(), compiler_deps, compiler_sources); |
73914 | 493 |
|
494 |
||
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
495 |
/* copy resources */ |
73918 | 496 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
497 |
for (String s : context.resources()) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
498 |
String name = context.item_name(s); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
499 |
String target = context.item_target(s); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
500 |
Path file_name = Path.of(name).normalize().getFileName(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
501 |
Path target_path = Path.of(target).normalize(); |
73918 | 502 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
503 |
Path target_dir; |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
504 |
Path target_file; |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
505 |
{ |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
506 |
if (target.endsWith("/") || target.endsWith("/.")) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
507 |
target_dir = build_dir.resolve(target_path); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
508 |
target_file = target_dir.resolve(file_name); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
509 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
510 |
else { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
511 |
target_file = build_dir.resolve(target_path); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
512 |
target_dir = target_file.getParent(); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
513 |
} |
73918 | 514 |
} |
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
515 |
Files.createDirectories(target_dir); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
516 |
Files.copy(context.path(name), target_file, |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
517 |
StandardCopyOption.COPY_ATTRIBUTES); |
73918 | 518 |
} |
519 |
||
520 |
||
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
521 |
/* packaging */ |
73914 | 522 |
|
74029
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
523 |
create_shasum(build_dir, shasum); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
524 |
create_services(build_dir, context.services()); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
525 |
create_jar(build_dir, context.main(), jar_path); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
526 |
} |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
527 |
finally { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
528 |
try (Stream<Path> walk = Files.walk(build_dir)) { |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
529 |
walk.sorted(Comparator.reverseOrder()) |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
530 |
.map(Path::toFile) |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
531 |
.forEach(File::delete); |
0701ff55780d
clarified build_props: empty module means no build;
wenzelm
parents:
74028
diff
changeset
|
532 |
} |
73914 | 533 |
} |
534 |
} |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
73961 | 538 |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
539 |
public static void build_components(PrintStream output, boolean fresh) |
73961 | 540 |
throws IOException, InterruptedException, NoSuchAlgorithmException |
541 |
{ |
|
542 |
for (Context context : component_contexts()) { |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74059
diff
changeset
|
543 |
build(output, context, fresh); |
73961 | 544 |
} |
545 |
} |
|
73914 | 546 |
} |