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