]> git.argeo.org Git - cc0/argeo-build.git/blob - Make.java
9ebbfd462f10c8f0052c065b0c19e871ba1cbdb8
[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.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(INFO, "Packaging took " + duration + " ms");
313 }
314
315 /** Install the bundles. */
316 void install(Map<String, List<String>> options, boolean uninstall) throws IOException {
317 // check arguments
318 List<String> bundles = options.get("--bundles");
319 Objects.requireNonNull(bundles, "--bundles argument must be set");
320 if (bundles.isEmpty())
321 return;
322
323 List<String> categories = options.get("--category");
324 Objects.requireNonNull(categories, "--category argument must be set");
325 if (categories.size() != 1)
326 throw new IllegalArgumentException("One and only one --category must be specified");
327 String category = categories.get(0);
328
329 List<String> targetDirs = options.get("--target");
330 Objects.requireNonNull(targetDirs, "--target argument must be set");
331 if (targetDirs.size() != 1)
332 throw new IllegalArgumentException("One and only one --target must be specified");
333 Path targetA2 = Paths.get(targetDirs.get(0));
334 logger.log(INFO, (uninstall ? "Uninstalling from " : "Installing to ") + targetA2);
335
336 final String branch;
337 Path branchMk = sdkSrcBase.resolve(BRANCH_MK);
338 if (Files.exists(branchMk)) {
339 Map<String, String> branchVariables = readMakefileVariables(branchMk);
340 branch = branchVariables.get(VAR_BRANCH);
341 } else {
342 throw new IllegalArgumentException(VAR_BRANCH + " variable must be set.");
343 }
344
345 Properties properties = new Properties();
346 Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
347 if (Files.exists(branchBnd))
348 try (InputStream in = Files.newInputStream(branchBnd)) {
349 properties.load(in);
350 }
351 String major = properties.getProperty("major");
352 Objects.requireNonNull(major, "'major' must be set");
353 String minor = properties.getProperty("minor");
354 Objects.requireNonNull(minor, "'minor' must be set");
355
356 int count = 0;
357 for (String bundle : bundles) {
358 Path bundlePath = Paths.get(bundle);
359 Path bundleParent = bundlePath.getParent();
360 Path a2JarDirectory = bundleParent != null ? a2Output.resolve(bundleParent).resolve(category)
361 : a2Output.resolve(category);
362 Path jarP = a2JarDirectory.resolve(bundlePath.getFileName() + "." + major + "." + minor + ".jar");
363
364 Path targetJarP = targetA2.resolve(a2Output.relativize(jarP));
365 if (uninstall) { // uninstall
366 if (Files.exists(targetJarP)) {
367 Files.delete(targetJarP);
368 logger.log(DEBUG, "Removed " + targetJarP);
369 count++;
370 }
371 Path targetParent = targetJarP.getParent();
372 deleteEmptyParents(targetA2, targetParent);
373 } else { // install
374 Files.createDirectories(targetJarP.getParent());
375 boolean update = Files.exists(targetJarP);
376 Files.copy(jarP, targetJarP, StandardCopyOption.REPLACE_EXISTING);
377 logger.log(DEBUG, (update ? "Updated " : "Installed ") + targetJarP);
378 count++;
379 }
380 }
381 logger.log(INFO, uninstall ? count + " bundles removed" : count + " bundles installed or updated");
382 }
383
384 /** Delete empty parent directory up to the A2 target (included). */
385 void deleteEmptyParents(Path targetA2, Path targetParent) throws IOException {
386 if (!Files.isDirectory(targetParent))
387 throw new IllegalArgumentException(targetParent + " must be a directory");
388 boolean isA2target = Files.isSameFile(targetA2, targetParent);
389 if (!Files.list(targetParent).iterator().hasNext()) {
390 Files.delete(targetParent);
391 if (isA2target)
392 return;// stop after deleting A2 base
393 deleteEmptyParents(targetA2, targetParent.getParent());
394 }
395 }
396
397 /** Package a single bundle. */
398 void createBundle(String branch, String bundle, String category) throws IOException {
399 final Path bundleSourceBase;
400 if (!Files.exists(execDirectory.resolve(bundle))) {
401 logger.log(WARNING,
402 "Bundle " + bundle + " not found in " + execDirectory + ", assuming this is this directory.");
403 bundleSourceBase = execDirectory;
404 } else {
405 bundleSourceBase = execDirectory.resolve(bundle);
406 }
407 Path srcP = bundleSourceBase.resolve("src");
408
409 Path compiled = buildBase.resolve(bundle);
410 String bundleSymbolicName = bundleSourceBase.getFileName().toString();
411
412 // Metadata
413 Properties properties = new Properties();
414 Path argeoBnd = argeoBuildBase.resolve("argeo.bnd");
415 try (InputStream in = Files.newInputStream(argeoBnd)) {
416 properties.load(in);
417 }
418
419 if (branch != null) {
420 Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
421 if (Files.exists(branchBnd))
422 try (InputStream in = Files.newInputStream(branchBnd)) {
423 properties.load(in);
424 }
425 }
426
427 Path bndBnd = bundleSourceBase.resolve("bnd.bnd");
428 if (Files.exists(bndBnd))
429 try (InputStream in = Files.newInputStream(bndBnd)) {
430 properties.load(in);
431 }
432
433 // Normalise
434 if (!properties.containsKey("Bundle-SymbolicName"))
435 properties.put("Bundle-SymbolicName", bundleSymbolicName);
436
437 // Calculate MANIFEST
438 Path binP = compiled.resolve("bin");
439 if (!Files.exists(binP))
440 Files.createDirectories(binP);
441 Manifest manifest;
442 try (Analyzer bndAnalyzer = new Analyzer()) {
443 bndAnalyzer.setProperties(properties);
444 Jar jar = new Jar(bundleSymbolicName, binP.toFile());
445 bndAnalyzer.setJar(jar);
446 manifest = bndAnalyzer.calcManifest();
447 } catch (Exception e) {
448 throw new RuntimeException("Bnd analysis of " + compiled + " failed", e);
449 }
450
451 String major = properties.getProperty("major");
452 Objects.requireNonNull(major, "'major' must be set");
453 String minor = properties.getProperty("minor");
454 Objects.requireNonNull(minor, "'minor' must be set");
455
456 // Write manifest
457 Path manifestP = compiled.resolve("META-INF/MANIFEST.MF");
458 Files.createDirectories(manifestP.getParent());
459 try (OutputStream out = Files.newOutputStream(manifestP)) {
460 manifest.write(out);
461 }
462
463 // Load excludes
464 List<PathMatcher> excludes = new ArrayList<>();
465 Path excludesP = argeoBuildBase.resolve("excludes.txt");
466 for (String line : Files.readAllLines(excludesP)) {
467 PathMatcher pathMatcher = excludesP.getFileSystem().getPathMatcher("glob:" + line);
468 excludes.add(pathMatcher);
469 }
470
471 Path bundleParent = Paths.get(bundle).getParent();
472 Path a2JarDirectory = bundleParent != null ? a2Output.resolve(bundleParent).resolve(category)
473 : a2Output.resolve(category);
474 Path jarP = a2JarDirectory.resolve(compiled.getFileName() + "." + major + "." + minor + ".jar");
475 Files.createDirectories(jarP.getParent());
476
477 try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarP), manifest)) {
478 jarOut.setLevel(Deflater.DEFAULT_COMPRESSION);
479 // add all classes first
480 Files.walkFileTree(binP, new SimpleFileVisitor<Path>() {
481 @Override
482 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
483 jarOut.putNextEntry(new JarEntry(binP.relativize(file).toString()));
484 Files.copy(file, jarOut);
485 return FileVisitResult.CONTINUE;
486 }
487 });
488
489 // add resources
490 Files.walkFileTree(bundleSourceBase, new SimpleFileVisitor<Path>() {
491 @Override
492 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
493 // skip output directory if it happens to be within the sources
494 if (Files.isSameFile(sdkBuildBase, dir))
495 return FileVisitResult.SKIP_SUBTREE;
496
497 // skip excluded patterns
498 Path relativeP = bundleSourceBase.relativize(dir);
499 for (PathMatcher exclude : excludes)
500 if (exclude.matches(relativeP))
501 return FileVisitResult.SKIP_SUBTREE;
502
503 return FileVisitResult.CONTINUE;
504 }
505
506 @Override
507 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
508 Path relativeP = bundleSourceBase.relativize(file);
509 for (PathMatcher exclude : excludes)
510 if (exclude.matches(relativeP))
511 return FileVisitResult.CONTINUE;
512 // skip JavaScript source maps
513 if (sourceBundles && file.getFileName().toString().endsWith(".map"))
514 return FileVisitResult.CONTINUE;
515
516 JarEntry entry = new JarEntry(relativeP.toString());
517 jarOut.putNextEntry(entry);
518 Files.copy(file, jarOut);
519 return FileVisitResult.CONTINUE;
520 }
521 });
522
523 if (Files.exists(srcP)) {
524 // Add all resources from src/
525 Files.walkFileTree(srcP, new SimpleFileVisitor<Path>() {
526 @Override
527 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
528 // skip directories ending with .js
529 // TODO find something more robust?
530 if (dir.getFileName().toString().endsWith(".js"))
531 return FileVisitResult.SKIP_SUBTREE;
532 return super.preVisitDirectory(dir, attrs);
533 }
534
535 @Override
536 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
537 if (file.getFileName().toString().endsWith(".java")
538 || file.getFileName().toString().endsWith(".class"))
539 return FileVisitResult.CONTINUE;
540 jarOut.putNextEntry(new JarEntry(srcP.relativize(file).toString()));
541 if (!Files.isDirectory(file))
542 Files.copy(file, jarOut);
543 return FileVisitResult.CONTINUE;
544 }
545 });
546
547 // add sources
548 // TODO add effective BND, Eclipse project file, etc., in order to be able to
549 // repackage
550 if (!sourceBundles) {
551 copySourcesToJar(srcP, jarOut, "OSGI-OPT/src/");
552 }
553 }
554
555 // add legal notices and licenses
556 for (Path p : listLegalFilesToInclude(bundleSourceBase).values()) {
557 jarOut.putNextEntry(new JarEntry(p.getFileName().toString()));
558 Files.copy(p, jarOut);
559 }
560 }
561
562 if (sourceBundles) {// create separate sources jar
563 Path a2srcJarDirectory = bundleParent != null ? a2srcOutput.resolve(bundleParent).resolve(category)
564 : a2srcOutput.resolve(category);
565 Files.createDirectories(a2srcJarDirectory);
566 Path srcJarP = a2srcJarDirectory.resolve(compiled.getFileName() + "." + major + "." + minor + ".src.jar");
567 createSourceBundle(bundleSymbolicName, manifest, bundleSourceBase, srcP, srcJarP);
568 }
569 }
570
571 /** Create a separate bundle containing the sources. */
572 void createSourceBundle(String bundleSymbolicName, Manifest manifest, Path bundleSourceBase, Path srcP,
573 Path srcJarP) throws IOException {
574 Manifest srcManifest = new Manifest();
575 srcManifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
576 srcManifest.getMainAttributes().putValue("Bundle-SymbolicName", bundleSymbolicName + ".src");
577 srcManifest.getMainAttributes().putValue("Bundle-Version",
578 manifest.getMainAttributes().getValue("Bundle-Version").toString());
579
580 boolean isJsBundle = bundleSymbolicName.endsWith(".js");
581 if (!isJsBundle) {
582 srcManifest.getMainAttributes().putValue("Eclipse-SourceBundle",
583 bundleSymbolicName + ";version=\"" + manifest.getMainAttributes().getValue("Bundle-Version"));
584
585 try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
586 copySourcesToJar(srcP, srcJarOut, "");
587 // add legal notices and licenses
588 for (Path p : listLegalFilesToInclude(bundleSourceBase).values()) {
589 srcJarOut.putNextEntry(new JarEntry(p.getFileName().toString()));
590 Files.copy(p, srcJarOut);
591 }
592 }
593 } else {// JavaScript source maps
594 srcManifest.getMainAttributes().putValue("Fragment-Host", bundleSymbolicName + ";bundle-version=\""
595 + manifest.getMainAttributes().getValue("Bundle-Version"));
596 try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
597 Files.walkFileTree(bundleSourceBase, new SimpleFileVisitor<Path>() {
598 @Override
599 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
600 Path relativeP = bundleSourceBase.relativize(file);
601 if (!file.getFileName().toString().endsWith(".map"))
602 return FileVisitResult.CONTINUE;
603 JarEntry entry = new JarEntry(relativeP.toString());
604 srcJarOut.putNextEntry(entry);
605 Files.copy(file, srcJarOut);
606 return FileVisitResult.CONTINUE;
607 }
608 });
609 }
610 }
611 }
612
613 /** List the relevant legal files to include, from the SDK source base. */
614 Map<String, Path> listLegalFilesToInclude(Path bundleBase) throws IOException {
615 Map<String, Path> toInclude = new HashMap<>();
616 if (!noSdkLegal) {
617 DirectoryStream<Path> sdkSrcLegal = Files.newDirectoryStream(sdkSrcBase, (p) -> {
618 String fileName = p.getFileName().toString();
619 return switch (fileName) {
620 case "NOTICE":
621 case "LICENSE":
622 case "COPYING":
623 case "COPYING.LESSER":
624 yield true;
625 default:
626 yield false;
627 };
628 });
629 for (Path p : sdkSrcLegal)
630 toInclude.put(p.getFileName().toString(), p);
631 }
632 for (Iterator<Map.Entry<String, Path>> entries = toInclude.entrySet().iterator(); entries.hasNext();) {
633 Map.Entry<String, Path> entry = entries.next();
634 Path inBundle = bundleBase.resolve(entry.getValue().getFileName());
635 // remove file if it is also defined at bundle level
636 // since it has already been copied
637 // and has priority
638 if (Files.exists(inBundle))
639 entries.remove();
640 }
641 return toInclude;
642 }
643
644 /*
645 * UTILITIES
646 */
647 /** Add sources to a jar file */
648 void copySourcesToJar(Path srcP, JarOutputStream srcJarOut, String prefix) throws IOException {
649 Files.walkFileTree(srcP, new SimpleFileVisitor<Path>() {
650 @Override
651 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
652 srcJarOut.putNextEntry(new JarEntry(prefix + srcP.relativize(file).toString()));
653 if (!Files.isDirectory(file))
654 Files.copy(file, srcJarOut);
655 return FileVisitResult.CONTINUE;
656 }
657 });
658 }
659
660 /**
661 * Recursively find the base source directory (which contains the
662 * <code>{@value #SDK_MK}</code> file).
663 */
664 Path findSdkMk(Path directory) {
665 Path sdkMkP = directory.resolve(SDK_MK);
666 if (Files.exists(sdkMkP)) {
667 return sdkMkP.toAbsolutePath();
668 }
669 if (directory.getParent() == null)
670 return null;
671 return findSdkMk(directory.getParent());
672 }
673
674 /**
675 * Reads Makefile variable assignments of the form =, :=, or ?=, ignoring white
676 * spaces. To be used with very simple included Makefiles only.
677 */
678 Map<String, String> readMakefileVariables(Path path) throws IOException {
679 Map<String, String> context = new HashMap<>();
680 List<String> sdkMkLines = Files.readAllLines(path);
681 lines: for (String line : sdkMkLines) {
682 StringTokenizer st = new StringTokenizer(line, " :=?");
683 if (!st.hasMoreTokens())
684 continue lines;
685 String key = st.nextToken();
686 if (!st.hasMoreTokens())
687 continue lines;
688 String value = st.nextToken();
689 if (st.hasMoreTokens()) // probably not a simple variable assignment
690 continue lines;
691 context.put(key, value);
692 }
693 return context;
694 }
695
696 /** Main entry point, interpreting actions and arguments. */
697 public static void main(String... args) {
698 if (args.length == 0)
699 throw new IllegalArgumentException("At least an action must be provided");
700 int actionIndex = 0;
701 String action = args[actionIndex];
702 if (args.length > actionIndex + 1 && !args[actionIndex + 1].startsWith("-"))
703 throw new IllegalArgumentException(
704 "Action " + action + " must be followed by an option: " + Arrays.asList(args));
705
706 Map<String, List<String>> options = new HashMap<>();
707 String currentOption = null;
708 for (int i = actionIndex + 1; i < args.length; i++) {
709 if (args[i].startsWith("-")) {
710 currentOption = args[i];
711 if (!options.containsKey(currentOption))
712 options.put(currentOption, new ArrayList<>());
713
714 } else {
715 options.get(currentOption).add(args[i]);
716 }
717 }
718
719 try {
720 Make argeoMake = new Make();
721 switch (action) {
722 case "compile" -> argeoMake.compile(options);
723 case "bundle" -> argeoMake.bundle(options);
724 case "all" -> argeoMake.all(options);
725 case "install" -> argeoMake.install(options, false);
726 case "uninstall" -> argeoMake.install(options, true);
727
728 default -> throw new IllegalArgumentException("Unkown action: " + action);
729 }
730
731 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
732 logger.log(INFO, "Make.java action '" + action + "' succesfully completed after " + (jvmUptime / 1000) + "."
733 + (jvmUptime % 1000) + " s");
734 } catch (Exception e) {
735 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
736 logger.log(ERROR, "Make.java action '" + action + "' failed after " + (jvmUptime / 1000) + "."
737 + (jvmUptime % 1000) + " s", e);
738 System.exit(1);
739 }
740 }
741
742 /** A jar file in A2 format */
743 static class A2Jar {
744 final Path path;
745 final String name;
746 final int major;
747 final int minor;
748
749 A2Jar(Path path) {
750 try {
751 this.path = path;
752 String fileName = path.getFileName().toString();
753 fileName = fileName.substring(0, fileName.lastIndexOf('.'));
754 minor = Integer.parseInt(fileName.substring(fileName.lastIndexOf('.') + 1));
755 fileName = fileName.substring(0, fileName.lastIndexOf('.'));
756 major = Integer.parseInt(fileName.substring(fileName.lastIndexOf('.') + 1));
757 name = fileName.substring(0, fileName.lastIndexOf('.'));
758 } catch (Exception e) {
759 throw new IllegalArgumentException("Badly formatted A2 jar " + path, e);
760 }
761 }
762 }
763
764 /**
765 * An ECJ {@link CompilationProgress} printing a progress bar while compiling.
766 */
767 static class MakeCompilationProgress extends CompilationProgress {
768 private int totalWork;
769 private long currentChunk = 0;
770 private long chunksCount = 80;
771
772 @Override
773 public void worked(int workIncrement, int remainingWork) {
774 if (!logger.isLoggable(Level.INFO)) // progress bar only at INFO level
775 return;
776 long chunk = ((totalWork - remainingWork) * chunksCount) / totalWork;
777 if (chunk != currentChunk) {
778 currentChunk = chunk;
779 for (long i = 0; i < currentChunk; i++) {
780 System.out.print("#");
781 }
782 for (long i = currentChunk; i < chunksCount; i++) {
783 System.out.print("-");
784 }
785 System.out.print("\r");
786 }
787 if (remainingWork == 0)
788 System.out.print("\n");
789 }
790
791 @Override
792 public void setTaskName(String name) {
793 }
794
795 @Override
796 public boolean isCanceled() {
797 return false;
798 }
799
800 @Override
801 public void done() {
802 }
803
804 @Override
805 public void begin(int remainingWork) {
806 this.totalWork = remainingWork;
807 }
808 }
809 }