]> git.argeo.org Git - cc0/argeo-build.git/blob - build/Make.java
Releasing
[cc0/argeo-build.git] / build / Make.java
1 package org.argeo.build;
2
3 import static java.lang.System.Logger.Level.DEBUG;
4 import static java.lang.System.Logger.Level.ERROR;
5 import static java.lang.System.Logger.Level.INFO;
6 import static java.lang.System.Logger.Level.WARNING;
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.io.PrintWriter;
13 import java.lang.System.Logger;
14 import java.lang.System.Logger.Level;
15 import java.lang.management.ManagementFactory;
16 import java.nio.file.DirectoryStream;
17 import java.nio.file.FileVisitResult;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.nio.file.PathMatcher;
21 import java.nio.file.Paths;
22 import java.nio.file.SimpleFileVisitor;
23 import java.nio.file.attribute.BasicFileAttributes;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.Properties;
32 import java.util.StringJoiner;
33 import java.util.StringTokenizer;
34 import java.util.TreeMap;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.jar.Attributes;
37 import java.util.jar.JarEntry;
38 import java.util.jar.JarOutputStream;
39 import java.util.jar.Manifest;
40 import java.util.zip.Deflater;
41
42 import org.eclipse.jdt.core.compiler.CompilationProgress;
43
44 import aQute.bnd.osgi.Analyzer;
45 import aQute.bnd.osgi.Jar;
46
47 /**
48 * Minimalistic OSGi compiler and packager, meant to be used as a single file
49 * without being itself compiled first. It depends on the Eclipse batch compiler
50 * (aka. ECJ) and the BND Libs library for OSGi metadata generation (which
51 * itselfs depends on slf4j).<br/>
52 * <br/>
53 * For example, a typical system call would be:<br/>
54 * <code>java -cp "/path/to/ECJ jar:/path/to/bndlib jar:/path/to/SLF4J jar" /path/to/cloned/argeo-build/src/org/argeo/build/Make.java action --option1 argument1 argument2 --option2 argument3 </code>
55 */
56 public class Make {
57 private final static Logger logger = System.getLogger(Make.class.getName());
58
59 /**
60 * Environment variable on whether sources should be packaged separately or
61 * integrated in the bundles.
62 */
63 private final static String ENV_SOURCE_BUNDLES = "SOURCE_BUNDLES";
64
65 /**
66 * Environment variable on whether legal files at the root of the sources should
67 * be included in the generated bundles. Should be set to true when building
68 * third-party software in order no to include the build harness license into
69 * the generated bundles.
70 */
71 private final static String ENV_NO_SDK_LEGAL = "NO_SDK_LEGAL";
72
73 /**
74 * Environment variable to override the default location for the Argeo Build
75 * configuration files. Typically used if Argeo Build has been compiled and
76 * packaged separately.
77 */
78 private final static String ENV_ARGEO_BUILD_CONFIG = "ARGEO_BUILD_CONFIG";
79
80 /** Make file variable (in {@link #SDK_MK}) with a path to the sources base. */
81 private final static String VAR_SDK_SRC_BASE = "SDK_SRC_BASE";
82
83 /**
84 * Make file variable (in {@link #SDK_MK}) with a path to the build output base.
85 */
86 private final static String VAR_SDK_BUILD_BASE = "SDK_BUILD_BASE";
87 /**
88 * Make file variable (in {@link #BRANCH_MK}) with the branch.
89 */
90 private final static String VAR_BRANCH = "BRANCH";
91
92 /** Name of the local-specific Makefile (sdk.mk). */
93 final static String SDK_MK = "sdk.mk";
94 /** Name of the branch definition Makefile (branch.mk). */
95 final static String BRANCH_MK = "branch.mk";
96
97 /** The execution directory (${user.dir}). */
98 final Path execDirectory;
99 /** Base of the source code, typically the cloned git repository. */
100 final Path sdkSrcBase;
101 /**
102 * The base of the builder, typically a submodule pointing to the INCLUDEpublic
103 * argeo-build directory.
104 */
105 final Path argeoBuildBase;
106 /** The base of the build for all layers. */
107 final Path sdkBuildBase;
108 /** The base of the build for this layer. */
109 final Path buildBase;
110 /** The base of the a2 output for all layers. */
111 final Path a2Output;
112 /** The base of the a2 sources when packaged separately. */
113 final Path a2srcOutput;
114
115 /** Whether sources should be packaged separately. */
116 final boolean sourceBundles;
117 /** Whether common legal files should be included. */
118 final boolean noSdkLegal;
119
120 /** Constructor initialises the base directories. */
121 public Make() throws IOException {
122 sourceBundles = Boolean.parseBoolean(System.getenv(ENV_SOURCE_BUNDLES));
123 if (sourceBundles)
124 logger.log(Level.INFO, "Sources will be packaged separately");
125 noSdkLegal = Boolean.parseBoolean(System.getenv(ENV_NO_SDK_LEGAL));
126 if (noSdkLegal)
127 logger.log(Level.INFO, "SDK legal files will NOT be included");
128
129 execDirectory = Paths.get(System.getProperty("user.dir"));
130 Path sdkMkP = findSdkMk(execDirectory);
131 Objects.requireNonNull(sdkMkP, "No " + SDK_MK + " found under " + execDirectory);
132
133 Map<String, String> context = readMakefileVariables(sdkMkP);
134 sdkSrcBase = Paths.get(context.computeIfAbsent(VAR_SDK_SRC_BASE, (key) -> {
135 throw new IllegalStateException(key + " not found");
136 })).toAbsolutePath();
137
138 Path argeoBuildBaseT = sdkSrcBase.resolve("sdk/argeo-build");
139 if (!Files.exists(argeoBuildBaseT)) {
140 String fromEnv = System.getenv(ENV_ARGEO_BUILD_CONFIG);
141 if (fromEnv != null)
142 argeoBuildBaseT = Paths.get(fromEnv);
143 if (fromEnv == null || !Files.exists(argeoBuildBaseT)) {
144 throw new IllegalStateException(
145 "Argeo Build not found. Did you initialise the git submodules or set the "
146 + ENV_ARGEO_BUILD_CONFIG + " environment variable?");
147 }
148 }
149 argeoBuildBase = argeoBuildBaseT;
150
151 sdkBuildBase = Paths.get(context.computeIfAbsent(VAR_SDK_BUILD_BASE, (key) -> {
152 throw new IllegalStateException(key + " not found");
153 })).toAbsolutePath();
154 buildBase = sdkBuildBase.resolve(sdkSrcBase.getFileName());
155 a2Output = sdkBuildBase.resolve("a2");
156 a2srcOutput = sdkBuildBase.resolve("a2.src");
157 }
158
159 /*
160 * ACTIONS
161 */
162 /** Compile and create the bundles in one go. */
163 void all(Map<String, List<String>> options) throws IOException {
164 compile(options);
165 bundle(options);
166 }
167
168 /** Compile all the bundles which have been passed via the --bundle argument. */
169 void compile(Map<String, List<String>> options) throws IOException {
170 List<String> bundles = options.get("--bundles");
171 Objects.requireNonNull(bundles, "--bundles argument must be set");
172 if (bundles.isEmpty())
173 return;
174
175 List<String> a2Categories = options.getOrDefault("--dep-categories", new ArrayList<>());
176 List<String> a2Bases = options.getOrDefault("--a2-bases", new ArrayList<>());
177 if (a2Bases.isEmpty() || !a2Bases.contains(a2Output.toString())) {
178 a2Bases.add(a2Output.toString());
179 }
180
181 List<String> compilerArgs = new ArrayList<>();
182
183 Path ecjArgs = argeoBuildBase.resolve("ecj.args");
184 compilerArgs.add("@" + ecjArgs);
185
186 // classpath
187 if (!a2Categories.isEmpty()) {
188 // We will keep only the highest major.minor
189 // and order by bundle name, for predictability
190 Map<String, A2Jar> a2Jars = new TreeMap<>();
191
192 // StringJoiner modulePath = new StringJoiner(File.pathSeparator);
193 for (String a2Base : a2Bases) {
194 categories: for (String a2Category : a2Categories) {
195 Path a2Dir = Paths.get(a2Base).resolve(a2Category);
196 if (!Files.exists(a2Dir))
197 continue categories;
198 // modulePath.add(a2Dir.toString());
199 for (Path jarP : Files.newDirectoryStream(a2Dir, (p) -> p.getFileName().toString().endsWith(".jar")
200 && !p.getFileName().toString().endsWith(".src.jar"))) {
201 A2Jar a2Jar = new A2Jar(jarP);
202 if (a2Jars.containsKey(a2Jar.name)) {
203 A2Jar current = a2Jars.get(a2Jar.name);
204 if (a2Jar.major > current.major)
205 a2Jars.put(a2Jar.name, a2Jar);
206 else if (a2Jar.major == current.major //
207 // if minor equals, we take the last one
208 && a2Jar.minor >= current.minor)
209 a2Jars.put(a2Jar.name, a2Jar);
210 } else {
211 a2Jars.put(a2Jar.name, a2Jar);
212 }
213 }
214 }
215 }
216
217 StringJoiner classPath = new StringJoiner(File.pathSeparator);
218 for (Iterator<A2Jar> it = a2Jars.values().iterator(); it.hasNext();)
219 classPath.add(it.next().path.toString());
220
221 compilerArgs.add("-cp");
222 compilerArgs.add(classPath.toString());
223 // compilerArgs.add("--module-path");
224 // compilerArgs.add(modulePath.toString());
225 }
226
227 // sources
228 for (String bundle : bundles) {
229 StringBuilder sb = new StringBuilder();
230 Path bundlePath = execDirectory.resolve(bundle);
231 if (!Files.exists(bundlePath)) {
232 if (bundles.size() == 1) {
233 logger.log(WARNING, "Bundle " + bundle + " not found in " + execDirectory
234 + ", assuming this is this directory, as only one bundle was requested.");
235 bundlePath = execDirectory;
236 } else
237 throw new IllegalArgumentException("Bundle " + bundle + " not found in " + execDirectory);
238 }
239 sb.append(bundlePath.resolve("src"));
240 sb.append("[-d");
241 compilerArgs.add(sb.toString());
242 sb = new StringBuilder();
243 sb.append(buildBase.resolve(bundle).resolve("bin"));
244 sb.append("]");
245 compilerArgs.add(sb.toString());
246 }
247
248 if (logger.isLoggable(INFO))
249 compilerArgs.add("-time");
250
251 if (logger.isLoggable(DEBUG)) {
252 logger.log(DEBUG, "Compiler arguments:");
253 for (String arg : compilerArgs)
254 logger.log(DEBUG, arg);
255 }
256
257 boolean success = org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile(
258 compilerArgs.toArray(new String[compilerArgs.size()]), new PrintWriter(System.out),
259 new PrintWriter(System.err), new MakeCompilationProgress());
260 if (!success) // kill the process if compilation failed
261 throw new IllegalStateException("Compilation failed");
262 }
263
264 /** Package the bundles. */
265 void bundle(Map<String, List<String>> options) throws IOException {
266 // check arguments
267 List<String> bundles = options.get("--bundles");
268 Objects.requireNonNull(bundles, "--bundles argument must be set");
269 if (bundles.isEmpty())
270 return;
271
272 List<String> categories = options.get("--category");
273 Objects.requireNonNull(categories, "--category argument must be set");
274 if (categories.size() != 1)
275 throw new IllegalArgumentException("One and only one --category must be specified");
276 String category = categories.get(0);
277
278 final String branch;
279 Path branchMk = sdkSrcBase.resolve(BRANCH_MK);
280 if (Files.exists(branchMk)) {
281 Map<String, String> branchVariables = readMakefileVariables(branchMk);
282 branch = branchVariables.get(VAR_BRANCH);
283 } else {
284 branch = null;
285 }
286
287 long begin = System.currentTimeMillis();
288 // create jars in parallel
289 List<CompletableFuture<Void>> toDos = new ArrayList<>();
290 for (String bundle : bundles) {
291 toDos.add(CompletableFuture.runAsync(() -> {
292 try {
293 createBundle(branch, bundle, category);
294 } catch (IOException e) {
295 throw new RuntimeException("Packaging of " + bundle + " failed", e);
296 }
297 }));
298 }
299 CompletableFuture.allOf(toDos.toArray(new CompletableFuture[toDos.size()])).join();
300 long duration = System.currentTimeMillis() - begin;
301 logger.log(INFO, "Packaging took " + duration + " ms");
302 }
303
304 /** Install the bundles. */
305 void install(Map<String, List<String>> options, boolean uninstall) throws IOException {
306 // check arguments
307 List<String> bundles = options.get("--bundles");
308 Objects.requireNonNull(bundles, "--bundles argument must be set");
309 if (bundles.isEmpty())
310 return;
311
312 List<String> categories = options.get("--category");
313 Objects.requireNonNull(categories, "--category argument must be set");
314 if (categories.size() != 1)
315 throw new IllegalArgumentException("One and only one --category must be specified");
316 String category = categories.get(0);
317
318 List<String> targetDirs = options.get("--target");
319 Objects.requireNonNull(targetDirs, "--target argument must be set");
320 if (targetDirs.size() != 1)
321 throw new IllegalArgumentException("One and only one --target must be specified");
322 Path targetA2 = Paths.get(targetDirs.get(0));
323 logger.log(INFO, (uninstall ? "Uninstalling from " : "Installing to ") + targetA2);
324
325 final String branch;
326 Path branchMk = sdkSrcBase.resolve(BRANCH_MK);
327 if (Files.exists(branchMk)) {
328 Map<String, String> branchVariables = readMakefileVariables(branchMk);
329 branch = branchVariables.get(VAR_BRANCH);
330 } else {
331 throw new IllegalArgumentException(VAR_BRANCH + " variable must be set.");
332 }
333
334 Properties properties = new Properties();
335 Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
336 if (Files.exists(branchBnd))
337 try (InputStream in = Files.newInputStream(branchBnd)) {
338 properties.load(in);
339 }
340 String major = properties.getProperty("major");
341 Objects.requireNonNull(major, "'major' must be set");
342 String minor = properties.getProperty("minor");
343 Objects.requireNonNull(minor, "'minor' must be set");
344
345 int count = 0;
346 for (String bundle : bundles) {
347 Path bundlePath = Paths.get(bundle);
348 Path bundleParent = bundlePath.getParent();
349 Path a2JarDirectory = bundleParent != null ? a2Output.resolve(bundleParent).resolve(category)
350 : a2Output.resolve(category);
351 Path jarP = a2JarDirectory.resolve(bundlePath.getFileName() + "." + major + "." + minor + ".jar");
352
353 Path targetJarP = targetA2.resolve(a2Output.relativize(jarP));
354 if (uninstall) { // uninstall
355 if (Files.exists(targetJarP)) {
356 Files.delete(targetJarP);
357 logger.log(DEBUG, "Removed " + targetJarP);
358 count++;
359 }
360 Path targetParent = targetJarP.getParent();
361 deleteEmptyParents(targetA2, targetParent);
362 } else { // install
363 Files.createDirectories(targetJarP.getParent());
364 boolean update = Files.exists(targetJarP);
365 Files.copy(jarP, targetJarP);
366 logger.log(DEBUG, (update ? "Updated " : "Installed ") + targetJarP);
367 count++;
368 }
369 }
370 logger.log(INFO, uninstall ? count + " bundles removed" : count + " bundles installed or updated");
371 }
372
373 /** Delete empty parent directory up to the A2 target (included). */
374 void deleteEmptyParents(Path targetA2, Path targetParent) throws IOException {
375 if (!Files.isDirectory(targetParent))
376 throw new IllegalArgumentException(targetParent + " must be a directory");
377 boolean isA2target = Files.isSameFile(targetA2, targetParent);
378 if (!Files.list(targetParent).iterator().hasNext()) {
379 Files.delete(targetParent);
380 if (isA2target)
381 return;// stop after deleting A2 base
382 deleteEmptyParents(targetA2, targetParent.getParent());
383 }
384 }
385
386 /** Package a single bundle. */
387 void createBundle(String branch, String bundle, String category) throws IOException {
388 final Path source;
389 if (!Files.exists(execDirectory.resolve(bundle))) {
390 logger.log(WARNING,
391 "Bundle " + bundle + " not found in " + execDirectory + ", assuming this is this directory.");
392 source = execDirectory;
393 } else {
394 source = execDirectory.resolve(bundle);
395 }
396 Path srcP = source.resolve("src");
397
398 Path compiled = buildBase.resolve(bundle);
399 String bundleSymbolicName = source.getFileName().toString();
400
401 // Metadata
402 Properties properties = new Properties();
403 Path argeoBnd = argeoBuildBase.resolve("argeo.bnd");
404 try (InputStream in = Files.newInputStream(argeoBnd)) {
405 properties.load(in);
406 }
407
408 if (branch != null) {
409 Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
410 if (Files.exists(branchBnd))
411 try (InputStream in = Files.newInputStream(branchBnd)) {
412 properties.load(in);
413 }
414 }
415
416 Path bndBnd = source.resolve("bnd.bnd");
417 if (Files.exists(bndBnd))
418 try (InputStream in = Files.newInputStream(bndBnd)) {
419 properties.load(in);
420 }
421
422 // Normalise
423 if (!properties.containsKey("Bundle-SymbolicName"))
424 properties.put("Bundle-SymbolicName", bundleSymbolicName);
425
426 // Calculate MANIFEST
427 Path binP = compiled.resolve("bin");
428 if (!Files.exists(binP))
429 Files.createDirectories(binP);
430 Manifest manifest;
431 try (Analyzer bndAnalyzer = new Analyzer()) {
432 bndAnalyzer.setProperties(properties);
433 Jar jar = new Jar(bundleSymbolicName, binP.toFile());
434 bndAnalyzer.setJar(jar);
435 manifest = bndAnalyzer.calcManifest();
436 } catch (Exception e) {
437 throw new RuntimeException("Bnd analysis of " + compiled + " failed", e);
438 }
439
440 String major = properties.getProperty("major");
441 Objects.requireNonNull(major, "'major' must be set");
442 String minor = properties.getProperty("minor");
443 Objects.requireNonNull(minor, "'minor' must be set");
444
445 // Write manifest
446 Path manifestP = compiled.resolve("META-INF/MANIFEST.MF");
447 Files.createDirectories(manifestP.getParent());
448 try (OutputStream out = Files.newOutputStream(manifestP)) {
449 manifest.write(out);
450 }
451
452 // Load excludes
453 List<PathMatcher> excludes = new ArrayList<>();
454 Path excludesP = argeoBuildBase.resolve("excludes.txt");
455 for (String line : Files.readAllLines(excludesP)) {
456 PathMatcher pathMatcher = excludesP.getFileSystem().getPathMatcher("glob:" + line);
457 excludes.add(pathMatcher);
458 }
459
460 Path bundleParent = Paths.get(bundle).getParent();
461 Path a2JarDirectory = bundleParent != null ? a2Output.resolve(bundleParent).resolve(category)
462 : a2Output.resolve(category);
463 Path jarP = a2JarDirectory.resolve(compiled.getFileName() + "." + major + "." + minor + ".jar");
464 Files.createDirectories(jarP.getParent());
465
466 try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarP), manifest)) {
467 jarOut.setLevel(Deflater.DEFAULT_COMPRESSION);
468 // add all classes first
469 Files.walkFileTree(binP, new SimpleFileVisitor<Path>() {
470 @Override
471 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
472 jarOut.putNextEntry(new JarEntry(binP.relativize(file).toString()));
473 Files.copy(file, jarOut);
474 return FileVisitResult.CONTINUE;
475 }
476 });
477
478 // add resources
479 Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
480 @Override
481 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
482 // skip output directory if it happens to be within the sources
483 if (Files.isSameFile(sdkBuildBase, dir))
484 return FileVisitResult.SKIP_SUBTREE;
485
486 // skip excluded patterns
487 Path relativeP = source.relativize(dir);
488 for (PathMatcher exclude : excludes)
489 if (exclude.matches(relativeP))
490 return FileVisitResult.SKIP_SUBTREE;
491
492 return FileVisitResult.CONTINUE;
493 }
494
495 @Override
496 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
497 Path relativeP = source.relativize(file);
498 for (PathMatcher exclude : excludes)
499 if (exclude.matches(relativeP))
500 return FileVisitResult.CONTINUE;
501 JarEntry entry = new JarEntry(relativeP.toString());
502 jarOut.putNextEntry(entry);
503 Files.copy(file, jarOut);
504 return FileVisitResult.CONTINUE;
505 }
506 });
507
508 // Add all resources from src/
509 Files.walkFileTree(srcP, new SimpleFileVisitor<Path>() {
510 @Override
511 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
512 if (file.getFileName().toString().endsWith(".java")
513 || file.getFileName().toString().endsWith(".class"))
514 return FileVisitResult.CONTINUE;
515 jarOut.putNextEntry(new JarEntry(srcP.relativize(file).toString()));
516 if (!Files.isDirectory(file))
517 Files.copy(file, jarOut);
518 return FileVisitResult.CONTINUE;
519 }
520 });
521
522 // add legal notices and licenses
523 for (Path p : listLegalFilesToInclude(source).values()) {
524 jarOut.putNextEntry(new JarEntry(p.getFileName().toString()));
525 Files.copy(p, jarOut);
526 }
527
528 // add sources
529 // TODO add effective BND, Eclipse project file, etc., in order to be able to
530 // repackage
531 if (!sourceBundles) {
532 copySourcesToJar(srcP, jarOut, "OSGI-OPT/src/");
533 }
534 }
535
536 if (sourceBundles) {// create separate sources jar
537 Path a2srcJarDirectory = bundleParent != null ? a2srcOutput.resolve(bundleParent).resolve(category)
538 : a2srcOutput.resolve(category);
539 Files.createDirectories(a2srcJarDirectory);
540 Path srcJarP = a2srcJarDirectory.resolve(compiled.getFileName() + "." + major + "." + minor + ".src.jar");
541 Manifest srcManifest = new Manifest();
542 srcManifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
543 srcManifest.getMainAttributes().putValue("Bundle-SymbolicName", bundleSymbolicName + ".src");
544 srcManifest.getMainAttributes().putValue("Bundle-Version",
545 manifest.getMainAttributes().getValue("Bundle-Version").toString());
546 srcManifest.getMainAttributes().putValue("Eclipse-SourceBundle",
547 bundleSymbolicName + ";version=\"" + manifest.getMainAttributes().getValue("Bundle-Version"));
548
549 try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
550 copySourcesToJar(srcP, srcJarOut, "");
551 // add legal notices and licenses
552 for (Path p : listLegalFilesToInclude(source).values()) {
553 srcJarOut.putNextEntry(new JarEntry(p.getFileName().toString()));
554 Files.copy(p, srcJarOut);
555 }
556 }
557 }
558 }
559
560 /** List the relevant legal files to include, from the SDK source base. */
561 Map<String, Path> listLegalFilesToInclude(Path bundleBase) throws IOException {
562 Map<String, Path> toInclude = new HashMap<>();
563 if (!noSdkLegal) {
564 DirectoryStream<Path> sdkSrcLegal = Files.newDirectoryStream(sdkSrcBase, (p) -> {
565 String fileName = p.getFileName().toString();
566 return switch (fileName) {
567 case "NOTICE":
568 case "LICENSE":
569 case "COPYING":
570 case "COPYING.LESSER":
571 yield true;
572 default:
573 yield false;
574 };
575 });
576 for (Path p : sdkSrcLegal)
577 toInclude.put(p.getFileName().toString(), p);
578 }
579 for (Iterator<Map.Entry<String, Path>> entries = toInclude.entrySet().iterator(); entries.hasNext();) {
580 Map.Entry<String, Path> entry = entries.next();
581 Path inBundle = bundleBase.resolve(entry.getValue().getFileName());
582 // remove file if it is also defined at bundle level
583 // since it has already been copied
584 // and has priority
585 if (Files.exists(inBundle))
586 entries.remove();
587 }
588 return toInclude;
589 }
590
591 /*
592 * UTILITIES
593 */
594 /** Add sources to a jar file */
595 void copySourcesToJar(Path srcP, JarOutputStream srcJarOut, String prefix) throws IOException {
596 Files.walkFileTree(srcP, new SimpleFileVisitor<Path>() {
597 @Override
598 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
599 srcJarOut.putNextEntry(new JarEntry(prefix + srcP.relativize(file).toString()));
600 if (!Files.isDirectory(file))
601 Files.copy(file, srcJarOut);
602 return FileVisitResult.CONTINUE;
603 }
604 });
605 }
606
607 /**
608 * Recursively find the base source directory (which contains the
609 * <code>{@value #SDK_MK}</code> file).
610 */
611 Path findSdkMk(Path directory) {
612 Path sdkMkP = directory.resolve(SDK_MK);
613 if (Files.exists(sdkMkP)) {
614 return sdkMkP.toAbsolutePath();
615 }
616 if (directory.getParent() == null)
617 return null;
618 return findSdkMk(directory.getParent());
619 }
620
621 /**
622 * Reads Makefile variable assignments of the form =, :=, or ?=, ignoring white
623 * spaces. To be used with very simple included Makefiles only.
624 */
625 Map<String, String> readMakefileVariables(Path path) throws IOException {
626 Map<String, String> context = new HashMap<>();
627 List<String> sdkMkLines = Files.readAllLines(path);
628 lines: for (String line : sdkMkLines) {
629 StringTokenizer st = new StringTokenizer(line, " :=?");
630 if (!st.hasMoreTokens())
631 continue lines;
632 String key = st.nextToken();
633 if (!st.hasMoreTokens())
634 continue lines;
635 String value = st.nextToken();
636 if (st.hasMoreTokens()) // probably not a simple variable assignment
637 continue lines;
638 context.put(key, value);
639 }
640 return context;
641 }
642
643 /** Main entry point, interpreting actions and arguments. */
644 public static void main(String... args) {
645 if (args.length == 0)
646 throw new IllegalArgumentException("At least an action must be provided");
647 int actionIndex = 0;
648 String action = args[actionIndex];
649 if (args.length > actionIndex + 1 && !args[actionIndex + 1].startsWith("-"))
650 throw new IllegalArgumentException(
651 "Action " + action + " must be followed by an option: " + Arrays.asList(args));
652
653 Map<String, List<String>> options = new HashMap<>();
654 String currentOption = null;
655 for (int i = actionIndex + 1; i < args.length; i++) {
656 if (args[i].startsWith("-")) {
657 currentOption = args[i];
658 if (!options.containsKey(currentOption))
659 options.put(currentOption, new ArrayList<>());
660
661 } else {
662 options.get(currentOption).add(args[i]);
663 }
664 }
665
666 try {
667 Make argeoMake = new Make();
668 switch (action) {
669 case "compile" -> argeoMake.compile(options);
670 case "bundle" -> argeoMake.bundle(options);
671 case "all" -> argeoMake.all(options);
672 case "install" -> argeoMake.install(options, false);
673 case "uninstall" -> argeoMake.install(options, true);
674
675 default -> throw new IllegalArgumentException("Unkown action: " + action);
676 }
677
678 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
679 logger.log(INFO, "Make.java action '" + action + "' succesfully completed after " + (jvmUptime / 1000) + "."
680 + (jvmUptime % 1000) + " s");
681 } catch (Exception e) {
682 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
683 logger.log(ERROR, "Make.java action '" + action + "' failed after " + (jvmUptime / 1000) + "."
684 + (jvmUptime % 1000) + " s", e);
685 System.exit(1);
686 }
687 }
688
689 /** A jar file in A2 format */
690 static class A2Jar {
691 final Path path;
692 final String name;
693 final int major;
694 final int minor;
695
696 A2Jar(Path path) {
697 try {
698 this.path = path;
699 String fileName = path.getFileName().toString();
700 fileName = fileName.substring(0, fileName.lastIndexOf('.'));
701 minor = Integer.parseInt(fileName.substring(fileName.lastIndexOf('.') + 1));
702 fileName = fileName.substring(0, fileName.lastIndexOf('.'));
703 major = Integer.parseInt(fileName.substring(fileName.lastIndexOf('.') + 1));
704 name = fileName.substring(0, fileName.lastIndexOf('.'));
705 } catch (Exception e) {
706 throw new IllegalArgumentException("Badly formatted A2 jar " + path, e);
707 }
708 }
709 }
710
711 /**
712 * An ECJ {@link CompilationProgress} printing a progress bar while compiling.
713 */
714 static class MakeCompilationProgress extends CompilationProgress {
715 private int totalWork;
716 private long currentChunk = 0;
717 private long chunksCount = 80;
718
719 @Override
720 public void worked(int workIncrement, int remainingWork) {
721 if (!logger.isLoggable(Level.INFO)) // progress bar only at INFO level
722 return;
723 long chunk = ((totalWork - remainingWork) * chunksCount) / totalWork;
724 if (chunk != currentChunk) {
725 currentChunk = chunk;
726 for (long i = 0; i < currentChunk; i++) {
727 System.out.print("#");
728 }
729 for (long i = currentChunk; i < chunksCount; i++) {
730 System.out.print("-");
731 }
732 System.out.print("\r");
733 }
734 if (remainingWork == 0)
735 System.out.print("\n");
736 }
737
738 @Override
739 public void setTaskName(String name) {
740 }
741
742 @Override
743 public boolean isCanceled() {
744 return false;
745 }
746
747 @Override
748 public void done() {
749 }
750
751 @Override
752 public void begin(int remainingWork) {
753 this.totalWork = remainingWork;
754 }
755 }
756 }