author | wenzelm |
Sun, 11 Jul 2021 13:46:14 +0200 | |
changeset 73961 | f090787bb4c4 |
parent 73960 | 027f837d18ee |
child 73964 | c9771e1b3223 |
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 |
|
73957
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
117 |
public Path path(String file) |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
118 |
throws IOException, InterruptedException |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
119 |
{ |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
120 |
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
|
121 |
} |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
122 |
public boolean exists(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 Files.exists(path(file)); |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
126 |
} |
73914 | 127 |
|
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
128 |
// historic |
73957
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
129 |
public Path shasum_path() |
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 path(lib_name() + ".shasum"); |
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
133 |
} |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
134 |
|
73921 | 135 |
public String item_name(String s) |
73918 | 136 |
{ |
137 |
int i = s.indexOf(':'); |
|
138 |
return i > 0 ? s.substring(0, i) : s; |
|
139 |
} |
|
140 |
||
73921 | 141 |
public String item_target(String s) |
73918 | 142 |
{ |
143 |
int i = s.indexOf(':'); |
|
144 |
return i > 0 ? s.substring(i + 1) : s; |
|
145 |
} |
|
146 |
||
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
147 |
public String shasum(String name, List<Path> paths) |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
148 |
throws IOException, NoSuchAlgorithmException |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
149 |
{ |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
150 |
boolean exists = false; |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
151 |
MessageDigest sha = MessageDigest.getInstance("SHA"); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
152 |
for (Path file : paths) { |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
153 |
if (Files.exists(file)) { |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
154 |
exists = true; |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
155 |
sha.update(Files.readAllBytes(file)); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
156 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
157 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
158 |
if (exists) { |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
159 |
String digest = String.format(Locale.ROOT, "%040x", new BigInteger(1, sha.digest())); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
160 |
return digest + " " + name + "\n"; |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
161 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
162 |
else { return ""; } |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
163 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
164 |
|
73914 | 165 |
public String shasum(String file) |
73957
110a027a5473
expand file paths, e.g. to allow $ISABELLE_HOME, $ISABELLE_HOME_USER;
wenzelm
parents:
73955
diff
changeset
|
166 |
throws IOException, NoSuchAlgorithmException, InterruptedException |
73914 | 167 |
{ |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
168 |
return shasum(file, List.of(path(file))); |
73914 | 169 |
} |
170 |
} |
|
171 |
||
73922 | 172 |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
173 |
/** compile sources **/ |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
174 |
|
73954 | 175 |
private static void add_options(List<String> options_list, String options) |
176 |
{ |
|
177 |
if (options != null) { |
|
178 |
for (String s : options.split("\\s+")) { |
|
179 |
if (!s.isEmpty()) { options_list.add(s); } |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
public static void compile_scala_sources( |
|
185 |
Path target_dir, String more_options, List<Path> deps, List<Path> sources) |
|
186 |
throws IOException, InterruptedException |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
187 |
{ |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
188 |
ArrayList<String> args = new ArrayList<String>(); |
73954 | 189 |
add_options(args, Environment.getenv("ISABELLE_SCALAC_OPTIONS")); |
190 |
add_options(args, more_options); |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
191 |
args.add("-d"); |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
192 |
args.add(target_dir.toString()); |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
193 |
args.add("-bootclasspath"); |
73960 | 194 |
args.add(Environment.join_platform_paths(deps)); |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
195 |
args.add("--"); |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
196 |
|
73955 | 197 |
boolean scala_sources = false; |
198 |
for (Path p : sources) { |
|
199 |
args.add(p.toString()); |
|
200 |
if (p.toString().endsWith(".scala")) { scala_sources = true; } |
|
201 |
} |
|
202 |
if (scala_sources) { |
|
203 |
MainClass main = new MainClass(); |
|
204 |
boolean ok = main.process(args.toArray(String[]::new)); |
|
205 |
if (!ok) throw new RuntimeException("Failed to compile Scala sources"); |
|
206 |
} |
|
73954 | 207 |
} |
208 |
||
209 |
public static void compile_java_sources( |
|
210 |
Path target_dir, String more_options, List<Path> deps, List<Path> sources) |
|
211 |
throws IOException, InterruptedException |
|
212 |
{ |
|
213 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
214 |
StandardJavaFileManager file_manager = |
|
215 |
compiler.getStandardFileManager(null, Locale.ROOT, StandardCharsets.UTF_8); |
|
216 |
||
217 |
List<String> options = new LinkedList<String>(); |
|
218 |
add_options(options, Environment.getenv("ISABELLE_JAVAC_OPTIONS")); |
|
219 |
add_options(options, more_options); |
|
220 |
options.add("-d"); |
|
221 |
options.add(target_dir.toString()); |
|
222 |
options.add("-classpath"); |
|
73960 | 223 |
options.add(Environment.join_platform_paths(deps)); |
73954 | 224 |
|
225 |
List<JavaFileObject> java_sources = new LinkedList<JavaFileObject>(); |
|
226 |
for (Path p : sources) { |
|
227 |
if (p.toString().endsWith(".java")) { |
|
228 |
for (JavaFileObject o : file_manager.getJavaFileObjectsFromPaths(List.of(p))) { |
|
229 |
java_sources.add(o); |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
if (!java_sources.isEmpty()) { |
|
234 |
boolean ok = compiler.getTask(null, file_manager, null, options, null, java_sources).call(); |
|
235 |
if (!ok) throw new RuntimeException("Failed to compile Java sources"); |
|
236 |
} |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
237 |
} |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
238 |
|
73922 | 239 |
|
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
240 |
/** shasum for jar content **/ |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
241 |
|
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
242 |
private static String SHASUM = "META-INF/shasum"; |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
243 |
|
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
244 |
public static String get_shasum(Path jar_path) |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
245 |
throws IOException |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
246 |
{ |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
247 |
if (Files.exists(jar_path)) { |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
248 |
try (JarFile jar_file = new JarFile(jar_path.toFile())) |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
249 |
{ |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
250 |
JarEntry entry = jar_file.getJarEntry(SHASUM); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
251 |
if (entry != null) { |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
252 |
byte[] bytes = jar_file.getInputStream(entry).readAllBytes(); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
253 |
return new String(bytes, StandardCharsets.UTF_8); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
254 |
} |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
255 |
else { return ""; } |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
256 |
} |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
257 |
} |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
258 |
else { return ""; } |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
259 |
} |
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 |
public static void create_shasum(Path dir, String shasum) |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
262 |
throws IOException |
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 |
Path path = dir.resolve(SHASUM); |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
265 |
Files.createDirectories(path.getParent()); |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
266 |
Files.writeString(path, shasum); |
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 |
|
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
269 |
|
73922 | 270 |
/** create jar **/ |
271 |
||
272 |
public static void create_jar(Path dir, String main, Path jar) |
|
273 |
throws IOException |
|
274 |
{ |
|
73949 | 275 |
Files.createDirectories(dir.resolve(jar).getParent()); |
73922 | 276 |
Files.deleteIfExists(jar); |
277 |
||
278 |
Manifest manifest = new Manifest(); |
|
279 |
Attributes attributes = manifest.getMainAttributes(); |
|
280 |
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
|
281 |
attributes.put(new Attributes.Name("Created-By"), |
|
282 |
System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"); |
|
283 |
if (!main.isEmpty()) { attributes.put(Attributes.Name.MAIN_CLASS, main); } |
|
284 |
||
285 |
try (JarOutputStream out = |
|
286 |
new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jar)), manifest)) |
|
287 |
{ |
|
288 |
for (Path path : Files.walk(dir).sorted().toArray(Path[]::new)) { |
|
289 |
boolean is_dir = Files.isDirectory(path); |
|
290 |
boolean is_file = Files.isRegularFile(path); |
|
291 |
if (is_dir || is_file) { |
|
292 |
String name = Environment.slashes(dir.relativize(path).toString()); |
|
73950 | 293 |
if (!name.isEmpty()) { |
294 |
JarEntry entry = new JarEntry(is_dir ? name + "/" : name); |
|
295 |
entry.setTime(path.toFile().lastModified()); |
|
296 |
out.putNextEntry(entry); |
|
297 |
if (is_file) { out.write(Files.readAllBytes(path)); } |
|
298 |
out.closeEntry(); |
|
299 |
} |
|
73922 | 300 |
} |
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
||
305 |
||
73961 | 306 |
/** classpath **/ |
307 |
||
308 |
public static List<Path> classpath() |
|
309 |
throws IOException, InterruptedException |
|
310 |
{ |
|
311 |
List<Path> result = new LinkedList<Path>(); |
|
312 |
for (Context context : component_contexts()) { |
|
313 |
result.add(context.path(context.jar_name())); |
|
314 |
} |
|
315 |
return List.copyOf(result); |
|
316 |
} |
|
317 |
||
318 |
public static List<String> services() |
|
319 |
throws IOException, InterruptedException |
|
320 |
{ |
|
321 |
List<String> result = new LinkedList<String>(); |
|
322 |
for (Context context : component_contexts()) { |
|
323 |
for (String s : context.services()) { |
|
324 |
result.add(s); |
|
325 |
} |
|
326 |
} |
|
327 |
return List.copyOf(result); |
|
328 |
} |
|
329 |
||
330 |
||
73954 | 331 |
/** build **/ |
73922 | 332 |
|
73954 | 333 |
public static void build(Context context, boolean fresh) |
73914 | 334 |
throws IOException, InterruptedException, NoSuchAlgorithmException |
335 |
{ |
|
73915 | 336 |
String jar_name = context.jar_name(); |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
337 |
Path jar_path = context.path(jar_name); |
73914 | 338 |
|
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
339 |
List<String> requirements = context.requirements(); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
340 |
List<String> resources = context.resources(); |
73931 | 341 |
List<String> sources = context.sources(); |
73914 | 342 |
|
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
343 |
Files.deleteIfExists(context.shasum_path()); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
344 |
|
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
345 |
if (sources.isEmpty() && resources.isEmpty()) { |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
346 |
Files.deleteIfExists(jar_path); |
73914 | 347 |
} |
348 |
else { |
|
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
349 |
String shasum_old = get_shasum(jar_path); |
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
350 |
String shasum; |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
351 |
List<Path> compiler_deps = new LinkedList<Path>(); |
73914 | 352 |
{ |
353 |
StringBuilder _shasum = new StringBuilder(); |
|
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
354 |
for (String s : requirements) { |
73952 | 355 |
if (s.startsWith("env:")) { |
73948
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
356 |
List<Path> paths = new LinkedList<Path>(); |
73952 | 357 |
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
|
358 |
if (!p.isEmpty()) { |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
359 |
Path path = Path.of(Environment.platform_path(p)); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
360 |
compiler_deps.add(path); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
361 |
paths.add(path); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
362 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
363 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
364 |
_shasum.append(context.shasum(s, paths)); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
365 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
366 |
else { |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
367 |
compiler_deps.add(context.path(s)); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
368 |
_shasum.append(context.shasum(s)); |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
369 |
} |
731ab64bae97
more compiler_deps via "requirements", notably jar list from settings;
wenzelm
parents:
73946
diff
changeset
|
370 |
} |
73918 | 371 |
for (String s : resources) { |
73921 | 372 |
_shasum.append(context.shasum(context.item_name(s))); |
73918 | 373 |
} |
73920 | 374 |
for (String s : sources) { _shasum.append(context.shasum(s)); } |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
375 |
shasum = _shasum.toString(); |
73914 | 376 |
} |
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
377 |
if (fresh || !shasum_old.equals(shasum)) { |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
378 |
System.out.println( |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
379 |
"### Building " + context.description() + " (" + jar_path + ") ..."); |
73914 | 380 |
|
381 |
String isabelle_class_path = Environment.getenv("ISABELLE_CLASSPATH"); |
|
382 |
||
383 |
Path build_dir = Files.createTempDirectory("isabelle"); |
|
384 |
try { |
|
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
385 |
/* compile sources */ |
73914 | 386 |
|
387 |
for (String s : isabelle_class_path.split(":", -1)) { |
|
73931 | 388 |
if (!s.isEmpty()) { |
389 |
compiler_deps.add(Path.of(Environment.platform_path(s))); |
|
390 |
} |
|
73914 | 391 |
} |
392 |
||
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
393 |
List<Path> compiler_sources = new LinkedList<Path>(); |
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
394 |
for (String s : sources) { compiler_sources.add(context.path(s)); } |
73914 | 395 |
|
73954 | 396 |
compile_scala_sources( |
397 |
build_dir, context.scalac_options(), compiler_deps, compiler_sources); |
|
398 |
||
399 |
compiler_deps.add(build_dir); |
|
400 |
compile_java_sources( |
|
401 |
build_dir, context.javac_options(), compiler_deps, compiler_sources); |
|
73914 | 402 |
|
403 |
||
73918 | 404 |
/* copy resources */ |
405 |
||
406 |
for (String s : context.resources()) { |
|
73921 | 407 |
String name = context.item_name(s); |
408 |
String target = context.item_target(s); |
|
73918 | 409 |
Path file_name = Path.of(name).normalize().getFileName(); |
410 |
Path target_path = Path.of(target).normalize(); |
|
411 |
||
412 |
Path target_dir; |
|
413 |
Path target_file; |
|
414 |
{ |
|
415 |
if (target.endsWith("/") || target.endsWith("/.")) { |
|
416 |
target_dir = build_dir.resolve(target_path); |
|
417 |
target_file = target_dir.resolve(file_name); |
|
418 |
} |
|
419 |
else { |
|
420 |
target_file = build_dir.resolve(target_path); |
|
421 |
target_dir = target_file.getParent(); |
|
422 |
} |
|
423 |
} |
|
424 |
Files.createDirectories(target_dir); |
|
425 |
Files.copy(context.path(name), target_file, |
|
426 |
StandardCopyOption.COPY_ATTRIBUTES); |
|
427 |
} |
|
428 |
||
429 |
||
73922 | 430 |
/* packaging */ |
73914 | 431 |
|
73946
4d4c806cb7c8
clarified shasum: sources / resources within jar;
wenzelm
parents:
73944
diff
changeset
|
432 |
create_shasum(build_dir, shasum); |
73930
17c09d1b3588
invoke Scala compiler from Java, without external process;
wenzelm
parents:
73922
diff
changeset
|
433 |
create_jar(build_dir, context.main(), jar_path); |
73914 | 434 |
} |
435 |
finally { |
|
436 |
try (Stream<Path> walk = Files.walk(build_dir)) { |
|
437 |
walk.sorted(Comparator.reverseOrder()) |
|
438 |
.map(Path::toFile) |
|
439 |
.forEach(File::delete); |
|
440 |
} |
|
441 |
} |
|
442 |
} |
|
443 |
} |
|
444 |
} |
|
73961 | 445 |
|
446 |
public static void build_components(boolean fresh) |
|
447 |
throws IOException, InterruptedException, NoSuchAlgorithmException |
|
448 |
{ |
|
449 |
for (Context context : component_contexts()) { |
|
450 |
build(context, fresh); |
|
451 |
} |
|
452 |
} |
|
73914 | 453 |
} |