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(DEBUG
, "Packaging took " + duration
+ " ms");
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/";
321 List
<String
> bundles
= multiArg(options
, "--bundles", true);
322 if (bundles
.isEmpty())
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
);
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
);
337 throw new IllegalArgumentException(VAR_BRANCH
+ " variable must be set.");
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
)) {
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");
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");
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
371 targetJarP
= targetA2
.resolve(a2Output
.relativize(jarP
));
374 if (uninstall
) { // uninstall
375 if (Files
.exists(targetJarP
)) {
376 Files
.delete(targetJarP
);
377 logger
.log(DEBUG
, "Removed " + targetJarP
);
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
);
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
);
393 logger
.log(INFO
, uninstall ? count
+ " bundles removed" : count
+ " bundles installed or updated");
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)
401 throw new IllegalArgumentException(arg
+ " argument must be set");
404 if (values
.size() != 1)
405 throw new IllegalArgumentException("One and only one " + arg
+ " arguments must be specified");
406 return values
.get(0);
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
<>();
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
))
423 if (!Files
.exists(targetParent
)) {
424 deleteEmptyParents(baseDir
, targetParent
.getParent());
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
);
433 return;// stop after deleting A2 base
434 deleteEmptyParents(baseDir
, targetParent
.getParent());
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
))) {
443 "Bundle " + bundle
+ " not found in " + execDirectory
+ ", assuming this is this directory.");
444 bundleSourceBase
= execDirectory
;
446 bundleSourceBase
= execDirectory
.resolve(bundle
);
448 Path srcP
= bundleSourceBase
.resolve("src");
450 Path compiled
= buildBase
.resolve(bundle
);
451 String bundleSymbolicName
= bundleSourceBase
.getFileName().toString();
454 Properties properties
= new Properties();
455 Path argeoBnd
= argeoBuildBase
.resolve("argeo.bnd");
456 try (InputStream in
= Files
.newInputStream(argeoBnd
)) {
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
)) {
468 Path bndBnd
= bundleSourceBase
.resolve("bnd.bnd");
469 if (Files
.exists(bndBnd
))
470 try (InputStream in
= Files
.newInputStream(bndBnd
)) {
475 if (!properties
.containsKey("Bundle-SymbolicName"))
476 properties
.put("Bundle-SymbolicName", bundleSymbolicName
);
478 // Calculate MANIFEST
479 Path binP
= compiled
.resolve("bin");
480 if (!Files
.exists(binP
))
481 Files
.createDirectories(binP
);
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
);
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");
498 Path manifestP
= compiled
.resolve("META-INF/MANIFEST.MF");
499 Files
.createDirectories(manifestP
.getParent());
500 try (OutputStream out
= Files
.newOutputStream(manifestP
)) {
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
);
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());
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
>() {
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
;
531 Files
.walkFileTree(bundleSourceBase
, new SimpleFileVisitor
<Path
>() {
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
;
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
;
544 return FileVisitResult
.CONTINUE
;
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
;
557 JarEntry entry
= new JarEntry(relativeP
.toString());
558 jarOut
.putNextEntry(entry
);
559 Files
.copy(file
, jarOut
);
560 return FileVisitResult
.CONTINUE
;
564 if (Files
.exists(srcP
)) {
565 // Add all resources from src/
566 Files
.walkFileTree(srcP
, new SimpleFileVisitor
<Path
>() {
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
);
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
;
589 // TODO add effective BND, Eclipse project file, etc., in order to be able to
591 if (!sourceBundles
) {
592 copySourcesToJar(srcP
, jarOut
, "OSGI-OPT/src/");
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
);
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
);
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());
621 boolean isJsBundle
= bundleSymbolicName
.endsWith(".js");
623 srcManifest
.getMainAttributes().putValue("Eclipse-SourceBundle",
624 bundleSymbolicName
+ ";version=\"" + manifest
.getMainAttributes().getValue("Bundle-Version"));
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
);
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
>() {
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
;
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
<>();
658 DirectoryStream
<Path
> sdkSrcLegal
= Files
.newDirectoryStream(sdkSrcBase
, (p
) -> {
659 String fileName
= p
.getFileName().toString();
660 return switch (fileName
) {
664 case "COPYING.LESSER":
670 for (Path p
: sdkSrcLegal
)
671 toInclude
.put(p
.getFileName().toString(), p
);
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
679 if (Files
.exists(inBundle
))
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
>() {
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
;
702 * Recursively find the base source directory (which contains the
703 * <code>{@value #SDK_MK}</code> file).
705 Path
findSdkMk(Path directory
) {
706 Path sdkMkP
= directory
.resolve(SDK_MK
);
707 if (Files
.exists(sdkMkP
)) {
708 return sdkMkP
.toAbsolutePath();
710 if (directory
.getParent() == null)
712 return findSdkMk(directory
.getParent());
716 * Reads Makefile variable assignments of the form =, :=, or ?=, ignoring white
717 * spaces. To be used with very simple included Makefiles only.
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())
726 String key
= st
.nextToken();
727 if (!st
.hasMoreTokens())
729 String value
= st
.nextToken();
730 if (st
.hasMoreTokens()) // probably not a simple variable assignment
732 context
.put(key
, value
);
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");
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
));
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
<>());
756 options
.get(currentOption
).add(args
[i
]);
761 Make argeoMake
= new Make();
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);
769 default -> throw new IllegalArgumentException("Unkown action: " + action
);
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
);
783 /** A jar file in A2 format */
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
);
806 * An ECJ {@link CompilationProgress} printing a progress bar while compiling.
808 static class MakeCompilationProgress
extends CompilationProgress
{
809 private int totalWork
;
810 private long currentChunk
= 0;
811 private long chunksCount
= 80;
814 public void worked(int workIncrement
, int remainingWork
) {
815 if (!logger
.isLoggable(Level
.INFO
)) // progress bar only at INFO level
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("#");
823 for (long i
= currentChunk
; i
< chunksCount
; i
++) {
824 System
.out
.print("-");
826 System
.out
.print("\r");
828 if (remainingWork
== 0)
829 System
.out
.print("\n");
833 public void setTaskName(String name
) {
837 public boolean isCanceled() {
846 public void begin(int remainingWork
) {
847 this.totalWork
= remainingWork
;