1 package org
.argeo
.build
;
3 import static java
.lang
.System
.Logger
.Level
.ERROR
;
4 import static java
.lang
.System
.Logger
.Level
.INFO
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.OutputStream
;
10 import java
.io
.PrintWriter
;
11 import java
.lang
.System
.Logger
;
12 import java
.lang
.System
.Logger
.Level
;
13 import java
.lang
.management
.ManagementFactory
;
14 import java
.nio
.file
.FileVisitResult
;
15 import java
.nio
.file
.Files
;
16 import java
.nio
.file
.Path
;
17 import java
.nio
.file
.PathMatcher
;
18 import java
.nio
.file
.Paths
;
19 import java
.nio
.file
.SimpleFileVisitor
;
20 import java
.nio
.file
.attribute
.BasicFileAttributes
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.HashMap
;
24 import java
.util
.List
;
26 import java
.util
.Objects
;
27 import java
.util
.Properties
;
28 import java
.util
.StringJoiner
;
29 import java
.util
.StringTokenizer
;
30 import java
.util
.concurrent
.CompletableFuture
;
31 import java
.util
.jar
.JarEntry
;
32 import java
.util
.jar
.JarOutputStream
;
33 import java
.util
.jar
.Manifest
;
34 import java
.util
.zip
.Deflater
;
36 import org
.eclipse
.jdt
.core
.compiler
.CompilationProgress
;
38 import aQute
.bnd
.osgi
.Analyzer
;
39 import aQute
.bnd
.osgi
.Jar
;
42 * Minimalistic OSGi compiler and packager, meant to be used as a single file
43 * without being itself compiled first. It depends on the Eclipse batch compiler
44 * (aka. ECJ) and the BND Libs library for OSGi metadata generation (which
45 * itselfs depends on slf4j).<br/>
47 * For example, a typical system call would be:<br/>
48 * <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>
51 private final static Logger logger
= System
.getLogger(Make
.class.getName());
53 /** Name of the local-specific Makefile (sdk.mk). */
54 final static String SDK_MK
= "sdk.mk";
55 /** Name of the branch definition Makefile (branch.mk). */
56 final static String BRANCH_MK
= "branch.mk";
58 /** The execution directory (${user.dir}). */
59 final Path execDirectory
;
60 /** Base of the source code, typically the cloned git repository. */
61 final Path sdkSrcBase
;
63 * The base of the builder, typically a submodule pointing to the public
64 * argeo-build directory.
66 final Path argeoBuildBase
;
67 /** The base of the build for all layers. */
68 final Path sdkBuildBase
;
69 /** The base of the build for this layer. */
71 /** The base of the a2 output for all layers. */
74 /** Constructor initialises the base directories. */
75 public Make() throws IOException
{
76 execDirectory
= Paths
.get(System
.getProperty("user.dir"));
77 Path sdkMkP
= findSdkMk(execDirectory
);
78 Objects
.requireNonNull(sdkMkP
, "No " + SDK_MK
+ " found under " + execDirectory
);
80 Map
<String
, String
> context
= readeMakefileVariables(sdkMkP
);
81 sdkSrcBase
= Paths
.get(context
.computeIfAbsent("SDK_SRC_BASE", (key
) -> {
82 throw new IllegalStateException(key
+ " not found");
84 argeoBuildBase
= sdkSrcBase
.resolve("sdk/argeo-build");
86 sdkBuildBase
= Paths
.get(context
.computeIfAbsent("SDK_BUILD_BASE", (key
) -> {
87 throw new IllegalStateException(key
+ " not found");
89 buildBase
= sdkBuildBase
.resolve(sdkSrcBase
.getFileName());
90 a2Output
= sdkBuildBase
.resolve("a2");
96 /** Compile and create the bundles in one go. */
97 void all(Map
<String
, List
<String
>> options
) throws IOException
{
102 /** Compile all the bundles which have been passed via the --bundle argument. */
103 @SuppressWarnings("restriction")
104 void compile(Map
<String
, List
<String
>> options
) throws IOException
{
105 List
<String
> bundles
= options
.get("--bundles");
106 Objects
.requireNonNull(bundles
, "--bundles argument must be set");
107 if (bundles
.isEmpty())
110 List
<String
> a2Categories
= options
.getOrDefault("--dep-categories", new ArrayList
<>());
111 List
<String
> a2Bases
= options
.getOrDefault("--a2-bases", new ArrayList
<>());
112 if (a2Bases
.isEmpty()) {
113 a2Bases
.add(a2Output
.toString());
116 List
<String
> compilerArgs
= new ArrayList
<>();
118 Path ecjArgs
= argeoBuildBase
.resolve("ecj.args");
119 compilerArgs
.add("@" + ecjArgs
);
122 if (!a2Categories
.isEmpty()) {
123 StringJoiner classPath
= new StringJoiner(File
.pathSeparator
);
124 StringJoiner modulePath
= new StringJoiner(File
.pathSeparator
);
125 for (String a2Base
: a2Bases
) {
126 for (String a2Category
: a2Categories
) {
127 Path a2Dir
= Paths
.get(a2Base
).resolve(a2Category
);
128 if (!Files
.exists(a2Dir
))
129 Files
.createDirectories(a2Dir
);
130 modulePath
.add(a2Dir
.toString());
131 for (Path jarP
: Files
.newDirectoryStream(a2Dir
,
132 (p
) -> p
.getFileName().toString().endsWith(".jar"))) {
133 classPath
.add(jarP
.toString());
137 compilerArgs
.add("-cp");
138 compilerArgs
.add(classPath
.toString());
139 // compilerArgs.add("--module-path");
140 // compilerArgs.add(modulePath.toString());
144 for (String bundle
: bundles
) {
145 StringBuilder sb
= new StringBuilder();
146 Path bundlePath
= execDirectory
.resolve(bundle
);
147 if (!Files
.exists(bundlePath
))
148 throw new IllegalArgumentException("Bundle " + bundle
+ " not found in " + execDirectory
);
149 sb
.append(bundlePath
.resolve("src"));
151 compilerArgs
.add(sb
.toString());
152 sb
= new StringBuilder();
153 sb
.append(buildBase
.resolve(bundle
).resolve("bin"));
155 compilerArgs
.add(sb
.toString());
158 if (logger
.isLoggable(INFO
))
159 compilerArgs
.add("-time");
161 // for (String arg : compilerArgs)
162 // System.out.println(arg);
164 boolean success
= org
.eclipse
.jdt
.core
.compiler
.batch
.BatchCompiler
.compile(
165 compilerArgs
.toArray(new String
[compilerArgs
.size()]), new PrintWriter(System
.out
),
166 new PrintWriter(System
.err
), new MakeCompilationProgress());
167 if (!success
) // kill the process if compilation failed
168 throw new IllegalStateException("Compilation failed");
171 /** Package the bundles. */
172 void bundle(Map
<String
, List
<String
>> options
) throws IOException
{
174 List
<String
> bundles
= options
.get("--bundles");
175 Objects
.requireNonNull(bundles
, "--bundles argument must be set");
176 if (bundles
.isEmpty())
179 List
<String
> categories
= options
.get("--category");
180 Objects
.requireNonNull(bundles
, "--category argument must be set");
181 if (categories
.size() != 1)
182 throw new IllegalArgumentException("One and only one --category must be specified");
183 String category
= categories
.get(0);
185 Path branchMk
= sdkSrcBase
.resolve(BRANCH_MK
);
186 if (!Files
.exists(branchMk
))
187 throw new IllegalStateException("No " + branchMk
+ " file available");
188 Map
<String
, String
> branchVariables
= readeMakefileVariables(branchMk
);
190 String branch
= branchVariables
.get("BRANCH");
192 long begin
= System
.currentTimeMillis();
193 // create jars in parallel
194 List
<CompletableFuture
<Void
>> toDos
= new ArrayList
<>();
195 for (String bundle
: bundles
) {
196 toDos
.add(CompletableFuture
.runAsync(() -> {
198 createBundle(branch
, bundle
, category
);
199 } catch (IOException e
) {
200 throw new RuntimeException("Packaging of " + bundle
+ " failed", e
);
204 CompletableFuture
.allOf(toDos
.toArray(new CompletableFuture
[toDos
.size()])).join();
205 long duration
= System
.currentTimeMillis() - begin
;
206 logger
.log(INFO
, "Packaging took " + duration
+ " ms");
212 /** Package a single bundle. */
213 void createBundle(String branch
, String bundle
, String category
) throws IOException
{
214 Path source
= execDirectory
.resolve(bundle
);
215 Path compiled
= buildBase
.resolve(bundle
);
216 String bundleSymbolicName
= source
.getFileName().toString();
219 Properties properties
= new Properties();
220 Path argeoBnd
= argeoBuildBase
.resolve("argeo.bnd");
221 try (InputStream in
= Files
.newInputStream(argeoBnd
)) {
225 Path branchBnd
= sdkSrcBase
.resolve("sdk/branches/" + branch
+ ".bnd");
226 try (InputStream in
= Files
.newInputStream(branchBnd
)) {
230 Path bndBnd
= source
.resolve("bnd.bnd");
231 try (InputStream in
= Files
.newInputStream(bndBnd
)) {
236 properties
.put("Bundle-SymbolicName", bundleSymbolicName
);
238 // Calculate MANIFEST
239 Path binP
= compiled
.resolve("bin");
240 if (!Files
.exists(binP
))
241 Files
.createDirectories(binP
);
243 try (Analyzer bndAnalyzer
= new Analyzer()) {
244 bndAnalyzer
.setProperties(properties
);
245 Jar jar
= new Jar(bundleSymbolicName
, binP
.toFile());
246 bndAnalyzer
.setJar(jar
);
247 manifest
= bndAnalyzer
.calcManifest();
248 } catch (Exception e
) {
249 throw new RuntimeException("Bnd analysis of " + compiled
+ " failed", e
);
252 String major
= properties
.getProperty("major");
253 Objects
.requireNonNull(major
, "'major' must be set");
254 String minor
= properties
.getProperty("minor");
255 Objects
.requireNonNull(minor
, "'minor' must be set");
258 Path manifestP
= compiled
.resolve("META-INF/MANIFEST.MF");
259 Files
.createDirectories(manifestP
.getParent());
260 try (OutputStream out
= Files
.newOutputStream(manifestP
)) {
265 List
<PathMatcher
> excludes
= new ArrayList
<>();
266 Path excludesP
= argeoBuildBase
.resolve("excludes.txt");
267 for (String line
: Files
.readAllLines(excludesP
)) {
268 PathMatcher pathMatcher
= excludesP
.getFileSystem().getPathMatcher("glob:" + line
);
269 excludes
.add(pathMatcher
);
272 Path bundleParent
= Paths
.get(bundle
).getParent();
273 Path a2JarDirectory
= bundleParent
!= null ? a2Output
.resolve(bundleParent
).resolve(category
)
274 : a2Output
.resolve(category
);
275 Path jarP
= a2JarDirectory
.resolve(compiled
.getFileName() + "." + major
+ "." + minor
+ ".jar");
276 Files
.createDirectories(jarP
.getParent());
278 try (JarOutputStream jarOut
= new JarOutputStream(Files
.newOutputStream(jarP
), manifest
)) {
279 jarOut
.setLevel(Deflater
.DEFAULT_COMPRESSION
);
280 // add all classes first
281 Files
.walkFileTree(binP
, new SimpleFileVisitor
<Path
>() {
283 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
284 jarOut
.putNextEntry(new JarEntry(binP
.relativize(file
).toString()));
285 Files
.copy(file
, jarOut
);
286 return FileVisitResult
.CONTINUE
;
291 Files
.walkFileTree(source
, new SimpleFileVisitor
<Path
>() {
293 public FileVisitResult
preVisitDirectory(Path dir
, BasicFileAttributes attrs
) throws IOException
{
294 Path relativeP
= source
.relativize(dir
);
295 for (PathMatcher exclude
: excludes
)
296 if (exclude
.matches(relativeP
))
297 return FileVisitResult
.SKIP_SUBTREE
;
299 return FileVisitResult
.CONTINUE
;
303 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
304 Path relativeP
= source
.relativize(file
);
305 for (PathMatcher exclude
: excludes
)
306 if (exclude
.matches(relativeP
))
307 return FileVisitResult
.CONTINUE
;
308 JarEntry entry
= new JarEntry(relativeP
.toString());
309 jarOut
.putNextEntry(entry
);
310 Files
.copy(file
, jarOut
);
311 return FileVisitResult
.CONTINUE
;
315 Path srcP
= source
.resolve("src");
316 // Add all resources from src/
317 Files
.walkFileTree(srcP
, new SimpleFileVisitor
<Path
>() {
319 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
320 if (file
.getFileName().toString().endsWith(".java")
321 || file
.getFileName().toString().endsWith(".class"))
322 return FileVisitResult
.CONTINUE
;
323 jarOut
.putNextEntry(new JarEntry(srcP
.relativize(file
).toString()));
324 if (!Files
.isDirectory(file
))
325 Files
.copy(file
, jarOut
);
326 return FileVisitResult
.CONTINUE
;
331 // TODO add effective BND, Eclipse project file, etc., in order to be able to
333 Files
.walkFileTree(srcP
, new SimpleFileVisitor
<Path
>() {
335 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
336 jarOut
.putNextEntry(new JarEntry("OSGI-OPT/src/" + srcP
.relativize(file
).toString()));
337 if (!Files
.isDirectory(file
))
338 Files
.copy(file
, jarOut
);
339 return FileVisitResult
.CONTINUE
;
347 * Recursively find the base source directory (which contains the
348 * <code>{@value #SDK_MK}</code> file).
350 Path
findSdkMk(Path directory
) {
351 Path sdkMkP
= directory
.resolve(SDK_MK
);
352 if (Files
.exists(sdkMkP
)) {
353 return sdkMkP
.toAbsolutePath();
355 if (directory
.getParent() == null)
357 return findSdkMk(directory
.getParent());
361 * Reads Makefile variable assignments of the form =, :=, or ?=, ignoring white
362 * spaces. To be used with very simple included Makefiles only.
364 Map
<String
, String
> readeMakefileVariables(Path path
) throws IOException
{
365 Map
<String
, String
> context
= new HashMap
<>();
366 List
<String
> sdkMkLines
= Files
.readAllLines(path
);
367 lines
: for (String line
: sdkMkLines
) {
368 StringTokenizer st
= new StringTokenizer(line
, " :=?");
369 if (!st
.hasMoreTokens())
371 String key
= st
.nextToken();
372 if (!st
.hasMoreTokens())
374 String value
= st
.nextToken();
375 if (st
.hasMoreTokens()) // probably not a simple variable assignment
377 context
.put(key
, value
);
382 /** Main entry point, interpreting actions and arguments. */
383 public static void main(String
... args
) {
384 if (args
.length
== 0)
385 throw new IllegalArgumentException("At least an action must be provided");
387 String action
= args
[actionIndex
];
388 if (args
.length
> actionIndex
+ 1 && !args
[actionIndex
+ 1].startsWith("-"))
389 throw new IllegalArgumentException(
390 "Action " + action
+ " must be followed by an option: " + Arrays
.asList(args
));
392 Map
<String
, List
<String
>> options
= new HashMap
<>();
393 String currentOption
= null;
394 for (int i
= actionIndex
+ 1; i
< args
.length
; i
++) {
395 if (args
[i
].startsWith("-")) {
396 currentOption
= args
[i
];
397 if (!options
.containsKey(currentOption
))
398 options
.put(currentOption
, new ArrayList
<>());
401 options
.get(currentOption
).add(args
[i
]);
406 Make argeoMake
= new Make();
408 case "compile" -> argeoMake
.compile(options
);
409 case "bundle" -> argeoMake
.bundle(options
);
410 case "all" -> argeoMake
.all(options
);
412 default -> throw new IllegalArgumentException("Unkown action: " + action
);
415 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
416 logger
.log(INFO
, "Make.java action '" + action
+ "' succesfully completed after " + (jvmUptime
/ 1000) + "."
417 + (jvmUptime
% 1000) + " s");
418 } catch (Exception e
) {
419 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
420 logger
.log(ERROR
, "Make.java action '" + action
+ "' failed after " + (jvmUptime
/ 1000) + "."
421 + (jvmUptime
% 1000) + " s", e
);
427 * An ECJ {@link CompilationProgress} printing a progress bar while compiling.
429 static class MakeCompilationProgress
extends CompilationProgress
{
430 private int totalWork
;
431 private long currentChunk
= 0;
432 private long chunksCount
= 80;
435 public void worked(int workIncrement
, int remainingWork
) {
436 if (!logger
.isLoggable(Level
.INFO
)) // progress bar only at INFO level
438 long chunk
= ((totalWork
- remainingWork
) * chunksCount
) / totalWork
;
439 if (chunk
!= currentChunk
) {
440 currentChunk
= chunk
;
441 for (long i
= 0; i
< currentChunk
; i
++) {
442 System
.out
.print("#");
444 for (long i
= currentChunk
; i
< chunksCount
; i
++) {
445 System
.out
.print("-");
447 System
.out
.print("\r");
449 if (remainingWork
== 0)
450 System
.out
.print("\n");
454 public void setTaskName(String name
) {
458 public boolean isCanceled() {
467 public void begin(int remainingWork
) {
468 this.totalWork
= remainingWork
;