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
.stream
.Collectors
;
42 import java
.util
.zip
.Deflater
;
44 import org
.eclipse
.jdt
.core
.compiler
.CompilationProgress
;
46 import aQute
.bnd
.osgi
.Analyzer
;
47 import aQute
.bnd
.osgi
.Jar
;
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/>
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>
59 private final static Logger logger
= System
.getLogger(Make
.class.getName());
62 * Environment variable on whether sources should be packaged separately or
63 * integrated in the bundles.
65 private final static String ENV_SOURCE_BUNDLES
= "SOURCE_BUNDLES";
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.
73 private final static String ENV_NO_SDK_LEGAL
= "NO_SDK_LEGAL";
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.
80 private final static String ENV_ARGEO_BUILD_CONFIG
= "ARGEO_BUILD_CONFIG";
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";
86 * Make file variable (in {@link #SDK_MK}) with a path to the build output base.
88 private final static String VAR_SDK_BUILD_BASE
= "SDK_BUILD_BASE";
90 * Make file variable (in {@link #BRANCH_MK}) with the branch.
92 private final static String VAR_BRANCH
= "BRANCH";
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";
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
;
104 * The base of the builder, typically a submodule pointing to the INCLUDEpublic
105 * argeo-build directory.
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. */
114 /** The base of the a2 sources when packaged separately. */
115 final Path a2srcOutput
;
117 /** Whether sources should be packaged separately. */
118 final boolean sourceBundles
;
119 /** Whether common legal files should be included. */
120 final boolean noSdkLegal
;
122 /** Constructor initialises the base directories. */
123 public Make() throws IOException
{
124 sourceBundles
= Boolean
.parseBoolean(System
.getenv(ENV_SOURCE_BUNDLES
));
126 logger
.log(Level
.INFO
, "Sources will be packaged separately");
127 noSdkLegal
= Boolean
.parseBoolean(System
.getenv(ENV_NO_SDK_LEGAL
));
129 logger
.log(Level
.INFO
, "SDK legal files will NOT be included");
131 execDirectory
= Paths
.get(System
.getProperty("user.dir"));
132 Path sdkMkP
= findSdkMk(execDirectory
);
133 Objects
.requireNonNull(sdkMkP
, "No " + SDK_MK
+ " found under " + execDirectory
);
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();
140 Path argeoBuildBaseT
= sdkSrcBase
.resolve("sdk/argeo-build");
141 if (!Files
.exists(argeoBuildBaseT
)) {
142 String fromEnv
= System
.getenv(ENV_ARGEO_BUILD_CONFIG
);
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?");
151 argeoBuildBase
= argeoBuildBaseT
;
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");
164 /** Compile and create the bundles in one go. */
165 void all(Map
<String
, List
<String
>> options
) throws IOException
{
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())
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());
184 List
<String
> compilerArgs
= new ArrayList
<>();
186 Path ecjArgs
= argeoBuildBase
.resolve("ecj.args");
187 compilerArgs
.add("@" + ecjArgs
);
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
<>();
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
))
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
213 a2Jars
.put(a2Jar
.name
, a2Jar
);
219 StringJoiner classPath
= new StringJoiner(File
.pathSeparator
);
220 for (Iterator
<A2Jar
> it
= a2Jars
.values().iterator(); it
.hasNext();)
221 classPath
.add(it
.next().path
.toString());
223 compilerArgs
.add("-cp");
224 compilerArgs
.add(classPath
.toString());
225 // compilerArgs.add("--module-path");
226 // compilerArgs.add(modulePath.toString());
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
;
240 throw new IllegalArgumentException("Bundle " + bundle
+ " not found in " + execDirectory
);
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");
247 sb
.append(bundleSrc
);
249 compilerArgs
.add(sb
.toString());
250 sb
= new StringBuilder();
251 sb
.append(buildBase
.resolve(bundle
).resolve("bin"));
253 compilerArgs
.add(sb
.toString());
254 atLeastOneBundleToCompile
= true;
257 if (!atLeastOneBundleToCompile
)
260 if (logger
.isLoggable(INFO
))
261 compilerArgs
.add("-time");
263 if (logger
.isLoggable(DEBUG
)) {
264 logger
.log(DEBUG
, "Compiler arguments:");
265 for (String arg
: compilerArgs
)
266 logger
.log(DEBUG
, arg
);
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");
276 /** Package the bundles. */
277 void bundle(Map
<String
, List
<String
>> options
) throws IOException
{
279 List
<String
> bundles
= options
.get("--bundles");
280 Objects
.requireNonNull(bundles
, "--bundles argument must be set");
281 if (bundles
.isEmpty())
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);
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
);
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(() -> {
305 createBundle(branch
, bundle
, category
);
306 } catch (IOException e
) {
307 throw new RuntimeException("Packaging of " + bundle
+ " failed", e
);
311 CompletableFuture
.allOf(toDos
.toArray(new CompletableFuture
[toDos
.size()])).join();
312 long duration
= System
.currentTimeMillis() - begin
;
313 logger
.log(DEBUG
, "Packaging took " + duration
+ " ms");
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/";
322 List
<String
> bundles
= multiArg(options
, "--bundles", true);
323 if (bundles
.isEmpty())
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
);
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
);
338 throw new IllegalArgumentException(VAR_BRANCH
+ " variable must be set.");
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
)) {
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");
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");
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
372 targetJarP
= targetA2
.resolve(a2Output
.relativize(jarP
));
375 if (uninstall
) { // uninstall
376 if (Files
.exists(targetJarP
)) {
377 Files
.delete(targetJarP
);
378 logger
.log(DEBUG
, "Removed " + targetJarP
);
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
);
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
);
394 logger
.log(INFO
, uninstall ? count
+ " bundles removed" : count
+ " bundles installed or updated");
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)
402 throw new IllegalArgumentException(arg
+ " argument must be set");
405 if (values
.size() != 1)
406 throw new IllegalArgumentException("One and only one " + arg
+ " arguments must be specified");
407 return values
.get(0);
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
<>();
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
))
424 if (!Files
.exists(targetParent
)) {
425 deleteEmptyParents(baseDir
, targetParent
.getParent());
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
);
434 return;// stop after deleting A2 base
435 deleteEmptyParents(baseDir
, targetParent
.getParent());
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
))) {
444 "Bundle " + bundle
+ " not found in " + execDirectory
+ ", assuming this is this directory.");
445 bundleSourceBase
= execDirectory
;
447 bundleSourceBase
= execDirectory
.resolve(bundle
);
449 Path srcP
= bundleSourceBase
.resolve("src");
451 Path compiled
= buildBase
.resolve(bundle
);
452 String bundleSymbolicName
= bundleSourceBase
.getFileName().toString();
455 Properties properties
= new Properties();
456 Path argeoBnd
= argeoBuildBase
.resolve("argeo.bnd");
457 try (InputStream in
= Files
.newInputStream(argeoBnd
)) {
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
)) {
469 Path bndBnd
= bundleSourceBase
.resolve("bnd.bnd");
470 if (Files
.exists(bndBnd
))
471 try (InputStream in
= Files
.newInputStream(bndBnd
)) {
476 if (!properties
.containsKey("Bundle-SymbolicName"))
477 properties
.put("Bundle-SymbolicName", bundleSymbolicName
);
479 // Calculate MANIFEST
480 Path binP
= compiled
.resolve("bin");
481 if (!Files
.exists(binP
))
482 Files
.createDirectories(binP
);
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
);
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");
499 Path manifestP
= compiled
.resolve("META-INF/MANIFEST.MF");
500 Files
.createDirectories(manifestP
.getParent());
501 try (OutputStream out
= Files
.newOutputStream(manifestP
)) {
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
);
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());
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
>() {
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
;
532 Files
.walkFileTree(bundleSourceBase
, new SimpleFileVisitor
<Path
>() {
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
;
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
;
545 return FileVisitResult
.CONTINUE
;
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
;
558 JarEntry entry
= new JarEntry(relativeP
.toString());
559 jarOut
.putNextEntry(entry
);
560 Files
.copy(file
, jarOut
);
561 return FileVisitResult
.CONTINUE
;
565 if (Files
.exists(srcP
)) {
566 // Add all resources from src/
567 Files
.walkFileTree(srcP
, new SimpleFileVisitor
<Path
>() {
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
);
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
;
590 // TODO add effective BND, Eclipse project file, etc., in order to be able to
592 if (!sourceBundles
) {
593 copySourcesToJar(srcP
, jarOut
, "OSGI-OPT/src/");
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
);
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
);
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());
622 boolean isJsBundle
= bundleSymbolicName
.endsWith(".js");
624 srcManifest
.getMainAttributes().putValue("Eclipse-SourceBundle",
625 bundleSymbolicName
+ ";version=\"" + manifest
.getMainAttributes().getValue("Bundle-Version"));
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
);
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
>() {
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
;
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
<>();
659 DirectoryStream
<Path
> sdkSrcLegal
= Files
.newDirectoryStream(sdkSrcBase
, (p
) -> {
660 String fileName
= p
.getFileName().toString();
661 return switch (fileName
) {
665 case "COPYING.LESSER":
671 for (Path p
: sdkSrcLegal
)
672 toInclude
.put(p
.getFileName().toString(), p
);
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
680 if (Files
.exists(inBundle
))
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
>() {
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
;
703 * Recursively find the base source directory (which contains the
704 * <code>{@value #SDK_MK}</code> file).
706 Path
findSdkMk(Path directory
) {
707 Path sdkMkP
= directory
.resolve(SDK_MK
);
708 if (Files
.exists(sdkMkP
)) {
709 return sdkMkP
.toAbsolutePath();
711 if (directory
.getParent() == null)
713 return findSdkMk(directory
.getParent());
717 * Reads Makefile variable assignments of the form =, :=, or ?=, ignoring white
718 * spaces. To be used with very simple included Makefiles only.
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())
727 String key
= st
.nextToken();
728 if (!st
.hasMoreTokens())
730 String value
= st
.nextToken();
731 if (st
.hasMoreTokens()) // probably not a simple variable assignment
733 context
.put(key
, value
);
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");
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
));
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
<>());
757 options
.get(currentOption
).add(args
[i
]);
762 Make argeoMake
= new Make();
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);
770 default -> throw new IllegalArgumentException("Unkown action: " + action
);
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
);
784 /** A jar file in A2 format */
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
);
807 * An ECJ {@link CompilationProgress} printing a progress bar while compiling.
809 static class MakeCompilationProgress
extends CompilationProgress
{
810 private int totalWork
;
811 private long currentChunk
= 0;
812 private long chunksCount
= 80;
815 public void worked(int workIncrement
, int remainingWork
) {
816 if (!logger
.isLoggable(Level
.INFO
)) // progress bar only at INFO level
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("#");
824 for (long i
= currentChunk
; i
< chunksCount
; i
++) {
825 System
.out
.print("-");
827 System
.out
.print("\r");
829 if (remainingWork
== 0)
830 System
.out
.print("\n");
834 public void setTaskName(String name
) {
838 public boolean isCanceled() {
847 public void begin(int remainingWork
) {
848 this.totalWork
= remainingWork
;