summaryrefslogtreecommitdiff
path: root/rules/generic/Makefile
blob: a7a03095a2497e0f33c16e20035cc790996de9dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/make -f

# Install paths
DESTDIR ?=
MAVEN_LOCAL_REPO ?= ~/.m2

# Utilities
CP = cp
ECHO = echo
FAIL = exit 1
FIND = find
INSTALL = install
JAR = jar
JAVAC = javac
MKDIRS = mkdir -p
PRINTF = printf
RM = rm -f
XMLSTARLET = xml

# Detect information from POMs
artifactId      :=             $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:artifactId -n                               pom.xml)
version         := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:version -n -c /_:project/_:parent/_:version pom.xml) $(version))
groupId         := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:groupId -n -c /_:project/_:parent/_:groupId pom.xml))

sourceDirectory := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:build/_:sourceDirectory -n                  pom.xml) $(sourceDirectory) src/main/java)
resources       :=             $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:build/_:resources -n                        pom.xml)
resources       := $(if $(resources),$(resources),src/main/resources)

subdirs          = $(patsubst %/pom.xml,%,$(wildcard */pom.xml))
targets          = pom $(if $(wildcard src/main/ $(sourceDirectory) $(resources)),jar)

$(if $(extra_makefiles),$(eval include $(extra_makefiles)))

################################################################################

dep_dir = $1 $(shell $(FIND) $1 2>/dev/null)
dep_optdir = $(shell $(FIND) $1 2>/dev/null)

# all

all: PHONY \
    $(addprefix target/$(artifactId)-$(version).,$(targets)) \
    $(addsuffix /all,$(subdirs))

target/$(artifactId)-$(version).pom: pom.xml
	$(INSTALL) -Dm644 $< $@

target/$(artifactId)-$(version).jar: %.jar: %
	$(JAR) -cf $@ -C $< .

target/$(artifactId)-$(version): %: \
    target/META-INF/maven/$(groupId)/$(artifactId)/pom.properties \
    target/META-INF/maven/$(groupId)/$(artifactId)/pom.xml \
    $(call dep_dir,target/classes)
	$(RM) -r $@
	$(MKDIRS) $@
	$(CP) -r target/META-INF $(wildcard target/classes/*) $@

target/META-INF/maven/$(groupId)/$(artifactId)/pom.xml: pom.xml
	$(INSTALL) -Dm644 $< $@

# That is almost the default date format for locale=C, but the DOM is
# 0-padded instead of space-padded.
target/META-INF/maven/$(groupId)/$(artifactId)/pom.properties: pom.xml
	$(MKDIRS) $(@D)
	$(PRINTF) '%s\n' \
		'#Generated by Make' \
		"#$$(LC_ALL=C date '+%a %b %d %H:%M:%S %Z %Y')" \
		'version=$(version)' \
		'groupId=$(groupId)' \
		'artifactId=$(artifactId)' \
		> $@

exec_nonempty_q = $(if $(strip $1),@$(ECHO) $2; $(subst {},$1,$2))
exec_nonempty_v = $(if $(strip $1),$(subst {},$1,$2))
# Maven puts `target/classes` in the classpath, but that's unnecessary
# here, as we don't do incremental/segmented compilation.
target/classes: $(call dep_optdir,$(sourceDirectory) $(resources))
	$(RM) -r $@
	$(MKDIRS) $@
	$(call exec_nonempty_q,$(wildcard $(addsuffix /*,$(resources))),$(CP) -r {} $@    || { $(RM) -r $@; $(FAIL); })
	$(call exec_nonempty_q,$(filter %.java,$^)                     ,$(JAVAC) -d $@ {} || { $(RM) -r $@; $(FAIL); })

# install

install: PHONY \
    $(addprefix $(DESTDIR)$(MAVEN_LOCAL_REPO)/$(subst .,/,$(groupId))/$(artifactId)/$(version)/$(artifactId)-$(version).,$(targets)) \
    $(addsuffix /install,$(subdirs))

$(DESTDIR)$(MAVEN_LOCAL_REPO)/$(subst .,/,$(groupId))/$(artifactId)/$(version)/%: target/%
	$(INSTALL) -Dm644 $< $@

# clean

clean: PHONY $(addsuffix /clean,$(subdirs))
	rm -rf target

# recurse

deps2jars = $(wildcard $(patsubst %/all,%/target/*.jar,$(filter $(addsuffix /all,$(subdirs)),$1)))
deps2classpath = $(shell echo $(abspath $(call deps2jars,$1)) $(CLASSPATH) | tr ' ' :)

define recurse-rule
$1/%: PHONY $(addsuffix /all,$($1_deps))
	CLASSPATH='$$(call deps2classpath,$$^)' sourceDirectory='$$(sourceDirectory)' version='$$(version)' $$(MAKE) -C '$1' -f '$$(abspath $$(firstword $$(MAKEFILE_LIST)))' '$$*'
endef
$(foreach subdir,$(subdirs),$(eval $(call recurse-rule,$(subdir))))

# boilerplate

.PHONY: PHONY FORCE
.DELETE_ON_ERROR: