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