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