1 package org
.argeo
.build
;
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
;
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
;
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
;
43 import org
.eclipse
.jdt
.core
.compiler
.CompilationProgress
;
45 import aQute
.bnd
.osgi
.Analyzer
;
46 import aQute
.bnd
.osgi
.Jar
;
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/>
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>
58 private final static Logger logger
= System
.getLogger(Make
.class.getName());
61 * Environment variable on whether sources should be packaged separately or
62 * integrated in the bundles.
64 private final static String ENV_SOURCE_BUNDLES
= "SOURCE_BUNDLES";
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.
72 private final static String ENV_NO_SDK_LEGAL
= "NO_SDK_LEGAL";
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.
79 private final static String ENV_ARGEO_BUILD_CONFIG
= "ARGEO_BUILD_CONFIG";
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";
85 * Make file variable (in {@link #SDK_MK}) with a path to the build output base.
87 private final static String VAR_SDK_BUILD_BASE
= "SDK_BUILD_BASE";
89 * Make file variable (in {@link #BRANCH_MK}) with the branch.
91 private final static String VAR_BRANCH
= "BRANCH";
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";
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
;
103 * The base of the builder, typically a submodule pointing to the INCLUDEpublic
104 * argeo-build directory.
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. */
113 /** The base of the a2 sources when packaged separately. */
114 final Path a2srcOutput
;
116 /** Whether sources should be packaged separately. */
117 final boolean sourceBundles
;
118 /** Whether common legal files should be included. */
119 final boolean noSdkLegal
;
121 /** Constructor initialises the base directories. */
122 public Make() throws IOException
{
123 sourceBundles
= Boolean
.parseBoolean(System
.getenv(ENV_SOURCE_BUNDLES
));
125 logger
.log(Level
.INFO
, "Sources will be packaged separately");
126 noSdkLegal
= Boolean
.parseBoolean(System
.getenv(ENV_NO_SDK_LEGAL
));
128 logger
.log(Level
.INFO
, "SDK legal files will NOT be included");
130 execDirectory
= Paths
.get(System
.getProperty("user.dir"));
131 Path sdkMkP
= findSdkMk(execDirectory
);
132 Objects
.requireNonNull(sdkMkP
, "No " + SDK_MK
+ " found under " + execDirectory
);
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();
139 Path argeoBuildBaseT
= sdkSrcBase
.resolve("sdk/argeo-build");
140 if (!Files
.exists(argeoBuildBaseT
)) {
141 String fromEnv
= System
.getenv(ENV_ARGEO_BUILD_CONFIG
);
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?");
150 argeoBuildBase
= argeoBuildBaseT
;
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");
163 /** Compile and create the bundles in one go. */
164 void all(Map
<String
, List
<String
>> options
) throws IOException
{
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())
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());
182 List
<String
> compilerArgs
= new ArrayList
<>();
184 Path ecjArgs
= argeoBuildBase
.resolve("ecj.args");
185 compilerArgs
.add("@" + ecjArgs
);
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
<>();
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
))
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
);
212 a2Jars
.put(a2Jar
.name
, a2Jar
);
218 StringJoiner classPath
= new StringJoiner(File
.pathSeparator
);
219 for (Iterator
<A2Jar
> it
= a2Jars
.values().iterator(); it
.hasNext();)
220 classPath
.add(it
.next().path
.toString());
222 compilerArgs
.add("-cp");
223 compilerArgs
.add(classPath
.toString());
224 // compilerArgs.add("--module-path");
225 // compilerArgs.add(modulePath.toString());
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
;
239 throw new IllegalArgumentException("Bundle " + bundle
+ " not found in " + execDirectory
);
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");
246 sb
.append(bundleSrc
);
248 compilerArgs
.add(sb
.toString());
249 sb
= new StringBuilder();
250 sb
.append(buildBase
.resolve(bundle
).resolve("bin"));
252 compilerArgs
.add(sb
.toString());
253 atLeastOneBundleToCompile
= true;
256 if (!atLeastOneBundleToCompile
)
259 if (logger
.isLoggable(INFO
))
260 compilerArgs
.add("-time");
262 if (logger
.isLoggable(DEBUG
)) {
263 logger
.log(DEBUG
, "Compiler arguments:");
264 for (String arg
: compilerArgs
)
265 logger
.log(DEBUG
, arg
);
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");
275 /** Package the bundles. */
276 void bundle(Map
<String
, List
<String
>> options
) throws IOException
{
278 List
<String
> bundles
= options
.get("--bundles");
279 Objects
.requireNonNull(bundles
, "--bundles argument must be set");
280 if (bundles
.isEmpty())
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);
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
);
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(() -> {
304 createBundle(branch
, bundle
, category
);
305 } catch (IOException e
) {
306 throw new RuntimeException("Packaging of " + bundle
+ " failed", e
);
310 CompletableFuture
.allOf(toDos
.toArray(new CompletableFuture
[toDos
.size()])).join();
311 long duration
= System
.currentTimeMillis() - begin
;
312 logger
.log(INFO
, "Packaging took " + duration
+ " ms");
315 /** Install the bundles. */
316 void install(Map
<String
, List
<String
>> options
, boolean uninstall
) throws IOException
{
318 List
<String
> bundles
= options
.get("--bundles");
319 Objects
.requireNonNull(bundles
, "--bundles argument must be set");
320 if (bundles
.isEmpty())
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);
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
);
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
);
342 throw new IllegalArgumentException(VAR_BRANCH
+ " variable must be set.");
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
)) {
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");
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");
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
);
371 Path targetParent
= targetJarP
.getParent();
372 deleteEmptyParents(targetA2
, targetParent
);
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
);
381 logger
.log(INFO
, uninstall ? count
+ " bundles removed" : count
+ " bundles installed or updated");
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
);
392 return;// stop after deleting A2 base
393 deleteEmptyParents(targetA2
, targetParent
.getParent());
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
))) {
402 "Bundle " + bundle
+ " not found in " + execDirectory
+ ", assuming this is this directory.");
403 bundleSourceBase
= execDirectory
;
405 bundleSourceBase
= execDirectory
.resolve(bundle
);
407 Path srcP
= bundleSourceBase
.resolve("src");
409 Path compiled
= buildBase
.resolve(bundle
);
410 String bundleSymbolicName
= bundleSourceBase
.getFileName().toString();
413 Properties properties
= new Properties();
414 Path argeoBnd
= argeoBuildBase
.resolve("argeo.bnd");
415 try (InputStream in
= Files
.newInputStream(argeoBnd
)) {
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
)) {
427 Path bndBnd
= bundleSourceBase
.resolve("bnd.bnd");
428 if (Files
.exists(bndBnd
))
429 try (InputStream in
= Files
.newInputStream(bndBnd
)) {
434 if (!properties
.containsKey("Bundle-SymbolicName"))
435 properties
.put("Bundle-SymbolicName", bundleSymbolicName
);
437 // Calculate MANIFEST
438 Path binP
= compiled
.resolve("bin");
439 if (!Files
.exists(binP
))
440 Files
.createDirectories(binP
);
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
);
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");
457 Path manifestP
= compiled
.resolve("META-INF/MANIFEST.MF");
458 Files
.createDirectories(manifestP
.getParent());
459 try (OutputStream out
= Files
.newOutputStream(manifestP
)) {
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
);
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());
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
>() {
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
;
490 Files
.walkFileTree(bundleSourceBase
, new SimpleFileVisitor
<Path
>() {
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
;
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
;
503 return FileVisitResult
.CONTINUE
;
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
;
516 JarEntry entry
= new JarEntry(relativeP
.toString());
517 jarOut
.putNextEntry(entry
);
518 Files
.copy(file
, jarOut
);
519 return FileVisitResult
.CONTINUE
;
523 if (Files
.exists(srcP
)) {
524 // Add all resources from src/
525 Files
.walkFileTree(srcP
, new SimpleFileVisitor
<Path
>() {
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
);
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
;
548 // TODO add effective BND, Eclipse project file, etc., in order to be able to
550 if (!sourceBundles
) {
551 copySourcesToJar(srcP
, jarOut
, "OSGI-OPT/src/");
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
);
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
);
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());
580 boolean isJsBundle
= bundleSymbolicName
.endsWith(".js");
582 srcManifest
.getMainAttributes().putValue("Eclipse-SourceBundle",
583 bundleSymbolicName
+ ";version=\"" + manifest
.getMainAttributes().getValue("Bundle-Version"));
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
);
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
>() {
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
;
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
<>();
617 DirectoryStream
<Path
> sdkSrcLegal
= Files
.newDirectoryStream(sdkSrcBase
, (p
) -> {
618 String fileName
= p
.getFileName().toString();
619 return switch (fileName
) {
623 case "COPYING.LESSER":
629 for (Path p
: sdkSrcLegal
)
630 toInclude
.put(p
.getFileName().toString(), p
);
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
638 if (Files
.exists(inBundle
))
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
>() {
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
;
661 * Recursively find the base source directory (which contains the
662 * <code>{@value #SDK_MK}</code> file).
664 Path
findSdkMk(Path directory
) {
665 Path sdkMkP
= directory
.resolve(SDK_MK
);
666 if (Files
.exists(sdkMkP
)) {
667 return sdkMkP
.toAbsolutePath();
669 if (directory
.getParent() == null)
671 return findSdkMk(directory
.getParent());
675 * Reads Makefile variable assignments of the form =, :=, or ?=, ignoring white
676 * spaces. To be used with very simple included Makefiles only.
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())
685 String key
= st
.nextToken();
686 if (!st
.hasMoreTokens())
688 String value
= st
.nextToken();
689 if (st
.hasMoreTokens()) // probably not a simple variable assignment
691 context
.put(key
, value
);
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");
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
));
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
<>());
715 options
.get(currentOption
).add(args
[i
]);
720 Make argeoMake
= new Make();
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);
728 default -> throw new IllegalArgumentException("Unkown action: " + action
);
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
);
742 /** A jar file in A2 format */
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
);
765 * An ECJ {@link CompilationProgress} printing a progress bar while compiling.
767 static class MakeCompilationProgress
extends CompilationProgress
{
768 private int totalWork
;
769 private long currentChunk
= 0;
770 private long chunksCount
= 80;
773 public void worked(int workIncrement
, int remainingWork
) {
774 if (!logger
.isLoggable(Level
.INFO
)) // progress bar only at INFO level
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("#");
782 for (long i
= currentChunk
; i
< chunksCount
; i
++) {
783 System
.out
.print("-");
785 System
.out
.print("\r");
787 if (remainingWork
== 0)
788 System
.out
.print("\n");
792 public void setTaskName(String name
) {
796 public boolean isCanceled() {
805 public void begin(int remainingWork
) {
806 this.totalWork
= remainingWork
;