-JVM := $(JAVA_HOME)/bin/java
-JAVADOC := $(JAVA_HOME)/bin/javadoc
-ECJ_JAR := $(A2_BASE)/org.argeo.tp.sdk/org.eclipse.jdt.core.compiler.batch.3.28.jar
-BND_TOOL := /usr/bin/bnd
-
-BUILD_BASE = $(SDK_BUILD_BASE)/$(A2_CATEGORY)
-
-WORKSPACE_BNDS := $(shell cd $(SDK_SRC_BASE) && find cnf -name '*.bnd') sdk/argeo-build/argeo.bnd
-BUILD_WORKSPACE_BNDS := $(WORKSPACE_BNDS:%=$(BUILD_BASE)/%)
-
-A2_JARS = $(foreach category, $(DEP_CATEGORIES), $(shell find $(A2_BASE)/$(category) -name '*.jar'))
-A2_CLASSPATH = $(subst $(space),$(pathsep),$(strip $(A2_JARS)))
-
-A2_BUNDLES = $(foreach bundle, $(BUNDLES),$(A2_OUTPUT)/$(A2_CATEGORY)/$(shell basename $(bundle)).$(MAJOR).$(MINOR).jar)
-
-JAVA_SRCS = $(foreach bundle, $(BUNDLES), $(shell find $(bundle) -name '*.java'))
-BNDS = $(foreach bundle, $(BUNDLES), $(BUILD_BASE)/$(shell basename $(bundle))/bnd.bnd)
-ECJ_SRCS = $(foreach bundle, $(BUNDLES), $(bundle)/src[-d $(BUILD_BASE)/$(shell basename $(bundle))/bin])
-
-JAVADOC_SRCS = $(foreach bundle, $(JAVADOC_BUNDLES),$(bundle)/src)
-
-osgi: $(BUILD_WORKSPACE_BNDS) $(A2_BUNDLES)
-
-javadoc: $(BUILD_BASE)/java-compiled
- $(JAVADOC) -d $(BUILD_BASE)/api --source-path $(subst $(space),$(pathsep),$(strip $(JAVADOC_SRCS))) -subpackages $(JAVADOC_PACKAGES)
-
-
-# SDK level
-$(BUILD_BASE)/cnf/%.bnd: cnf/%.bnd
- mkdir -p $(dir $@)
- cp $< $@
-
-$(BUILD_BASE)/sdk/argeo-build/%.bnd: sdk/argeo-build/%.bnd
- mkdir -p $(dir $@)
- cp $< $@
-
-$(A2_OUTPUT)/$(A2_CATEGORY)/%.$(MAJOR).$(MINOR).jar : $(BUILD_BASE)/%.jar
- mkdir -p $(dir $@)
- cp $< $@
-
-$(BUILD_BASE)/%.jar: $(BUILD_BASE)/jars-built
- mv $(basename $@)/generated/*.jar $(basename $@).jar
-
-# Build level
-$(BUILD_BASE)/jars-built: $(BNDS)
- cd $(BUILD_BASE) && $(BND_TOOL) build
- touch $@
-
-$(BUILD_BASE)/%/bnd.bnd : %/bnd.bnd $(BUILD_BASE)/java-compiled
- mkdir -p $(dir $@)bin
- rsync -r --exclude "*.java" $(dir $<)src/ $(dir $@)bin
- rsync -r --exclude-from $(SDK_SRC_BASE)/sdk/argeo-build/excludes.txt $(dir $<) $(dir $@)bin
- if [ -d "$(dir $<)OSGI-INF" ]; then rsync -r $(dir $<)OSGI-INF/ $(dir $@)/OSGI-INF; fi
- cp $< $@
- echo "\n-sourcepath:$(SDK_SRC_BASE)/$(dir $<)src\n" >> $@
-
-$(BUILD_BASE)/java-compiled : $(JAVA_SRCS)
- $(JVM) -jar $(ECJ_JAR) @$(SDK_SRC_BASE)/sdk/argeo-build/ecj.args -cp $(A2_CLASSPATH) $(ECJ_SRCS)
- touch $@
+# The following variables are found in the sdk.mk file which is generated by the configure script:
+# SDK_SRC_BASE the base of the source code, typically the root of the cloned git repository
+# SDK_BUILD_BASE the base of the output
+# JAVA_HOME the base of the JDK used to build
+A2_OUTPUT = $(SDK_BUILD_BASE)/a2
+JVM ?= $(JAVA_HOME)/bin/java
+JAVADOC ?= $(JAVA_HOME)/bin/javadoc
+
+# The following variables should be declared in the including Makefile:
+# BUNDLES the space-separated list of bundles to build
+# A2_CATEGORY the (single) a2 category the bundles will belong to
+
+# The following environment variables can change the behaviour of the build
+# SOURCE_BUNDLES sources will be packaged separately in Eclipse-compatible source bundles
+# NO_MANIFEST_COPY generated MANIFESTs won't be copied to the source tree
+
+# The following variables have default values which can be overriden
+# DEP_CATEGORIES the a2 categories the compilation depends on
+# JAVADOC_PACKAGES the space-separated list of packages for which javadoc will be generated
+# A2_BASE the space-separated directories where already built a2 categories can be found
+DEP_CATEGORIES ?=
+JAVADOC_PACKAGES ?=
+A2_BASE ?=/usr/share/a2 /usr/local/share/a2 $(A2_OUTPUT)
+
+# We always use the latest version of the ECJ compiler
+ECJ_JAR ?= $(lastword $(foreach base, $(A2_BASE), $(sort $(wildcard $(base)/org.argeo.tp.build/org.eclipse.jdt.core.compiler.batch.$(ECJ_MAJOR).*.jar))))
+# Third-party libraries
+LOGGER_JAR ?= $(lastword $(foreach base, $(A2_BASE), $(wildcard $(base)/log/syslogger/org.argeo.tp/org.argeo.tp.syslogger.$(SYSLOGGER_BRANCH).jar)))
+BNDLIB_JAR ?= $(lastword $(foreach base, $(A2_BASE), $(wildcard $(base)/org.argeo.tp.build/biz.aQute.bndlib.$(BNDLIB_BRANCH).jar)))
+
+# Internal variables
+ARGEO_MAKE = $(JVM) -cp $(LOGGER_JAR):$(ECJ_JAR):$(BNDLIB_JAR) $(ARGEO_BUILD_BASE)src/org/argeo/build/Make.java
+JAVADOC_SRCS = $(foreach bundle, $(BUNDLES), $(bundle)/src)
+ifneq ($(NO_MANIFEST_COPY),true)
+MANIFESTS = $(foreach bundle, $(BUNDLES), $(bundle)/META-INF/MANIFEST.MF)
+endif
+BUILD_BASE = $(SDK_BUILD_BASE)/$(shell basename $(SDK_SRC_BASE))
+TARGET_BUNDLES = $(abspath $(foreach bundle, $(BUNDLES),$(A2_OUTPUT)/$(shell dirname $(bundle))/$(A2_CATEGORY)/$(shell basename $(bundle)).$(major).$(minor).jar))
+TODOS = $(foreach bundle, $(BUNDLES),$(BUILD_BASE)/$(bundle)/to-build)
+
+# Needed in order to be able to expand $$ variables
+.SECONDEXPANSION:
+.PHONY: osgi manifests javadoc
+
+osgi: $(BUILD_BASE)/built $(MANIFESTS)
+
+# Actual build (compilation + bundle packaging)
+$(BUILD_BASE)/built : BUNDLES_TO_BUILD = $(strip $(subst $(abspath $(BUILD_BASE))/,, $(subst to-build,, $?)))
+$(BUILD_BASE)/built : $(TODOS)
+ @echo "| A2 category : $(A2_CATEGORY)"
+ @echo "| Bundles : $(BUNDLES_TO_BUILD)"
+ @echo "| Dependencies : $(DEP_CATEGORIES)"
+ @echo "| Compiler : $(notdir $(ECJ_JAR))"
+ @$(ARGEO_MAKE) \
+ all --a2-bases $(A2_BASE) --dep-categories $(DEP_CATEGORIES) \
+ --category $(A2_CATEGORY) --bundles $(BUNDLES_TO_BUILD)
+ @touch $(BUILD_BASE)/built
+
+$(A2_OUTPUT)/%.$(major).$(minor).jar : $(BUILD_BASE)/$$(subst $(A2_CATEGORY)/,,$$*)/to-build
+ $(ARGEO_MAKE) \
+ all --a2-bases $(A2_BASE) --dep-categories $(DEP_CATEGORIES) \
+ --category $(A2_CATEGORY) --bundles $(subst $(A2_CATEGORY)/,,$*)
+
+$(BUILD_BASE)/%/to-build : $$(shell find % -type f -not -path 'bin/*' -not -path '*/MANIFEST.MF' | sed 's/ /\\ /g')
+ @rm -rf $(dir $@)
+ @mkdir -p $(dir $@)
+ @touch $@