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