summaryrefslogtreecommitdiff
path: root/rules/_generic/Makefile
blob: fa5e845b87d735d2396f11c4e77c9b84fec20a84 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/make -f

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

# Utilities
CP         = cp
ECHO       = echo
EXISTS     = test -e
FAIL       = exit 1
FIND       = find
INSTALL    = install
JAR        = jar
JAVA       = java
JAVAC      = javac
MKDIRS     = mkdir -p
PRINTF     = printf
RM         = rm -f
SED        = sed
TOUCH      = touch
XMLSTARLET = xml

JAVAC_FLAGS += $(if $(javac_source),-source $(javac_source))
JAVAC_FLAGS += $(if $(javac_target),-target $(javac_target))
JAVAC_FLAGS += $(if $(javac_encoding),-encoding $(javac_encoding))

# Detect information from POMs
pom_get = $(shell $(XMLSTARLET) sel -T -t $(foreach xpath,$1, -v '$(subst /,/_:,$(xpath))' -n) pom.xml)
plugin_conf = /project/build/plugins/plugin[./artifactId[text()="$1"]]/configuration/$2

artifactId      :=             $(call pom_get,/project/artifactId)
version         := $(firstword $(call pom_get,/project/version /project/parent/version) $(value version))
groupId         := $(firstword $(call pom_get,/project/groupId /project/parent/groupId))

sourceDirectory := $(firstword $(call pom_get,/project/build/sourceDirectory) $(value sourceDirectory) src/main/java     )
resources       := $(firstword $(call pom_get,/project/build/resources      ) $(value resources)       src/main/resources)

# The order of precidence with these isn't quite right, but it's rare that multiple are set.
javac_source    := $(firstword $(call pom_get,/project/properties/maven.compile.source /project/properties/maven.compiler.source $(call plugin_conf,maven-compiler-plugin,source)  ) $(value javac_source))
javac_target    := $(firstword $(call pom_get,/project/properties/maven.compile.target /project/properties/maven.compiler.target $(call plugin_conf,maven-compiler-plugin,target)  ) $(value javac_target))
javac_encoding  := $(firstword $(call pom_get,/project/properties/project.build.sourceEncoding                                   $(call plugin_conf,maven-compiler-plugin,encoding)) $(value javac_encoding))

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

# Post-processing

inherit_vars = version sourceDirectory resources javac_source javac_target javac_encoding
$(foreach var,$(inherit_vars),$(eval $(var) = $($(var))))

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

subdirs := $(subdirs)
targets := $(targets)

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

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

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

# generate-sources #############################################################

target/generated-sources:
	$(if $^,for dep in $^; do $(EXISTS) $$dep || { $(RM) -r $@; $(FAIL); }; done,$(MKDIRS) $@)
	$(TOUCH) $@

# generate-resources ###########################################################

target/generated-resources: \
    target/generated-resources/META-INF/maven/$(groupId)/$(artifactId)/pom.properties \
    target/generated-resources/META-INF/maven/$(groupId)/$(artifactId)/pom.xml
	for dep in $^; do $(EXISTS) $$dep || { $(RM) -r $@; $(FAIL); }; done
	$(TOUCH) $@

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

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

# compile ######################################################################

# 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)) \
    $(call dep_dir,target/generated-sources target/generated-resources)
	$(RM) -r $@
	$(MKDIRS) $@
	$(FIND) $(wildcard $(resources) target/generated-resources) -mindepth 1 -maxdepth 1 -exec $(CP) -r {} $@ \; || { $(RM) -r $@; $(FAIL); } # process-resources
	$(FIND) $(wildcard $(sourceDirectory) target/generated-sources) -name '*.java' -exec $(JAVAC) $(JAVAC_FLAGS) -d $@ {} + || { $(RM) -r $@; $(FAIL); } # compile
	$(TOUCH) $@

# package ######################################################################

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

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

# install ######################################################################

mvndir = $(subst .,/,$(groupId))/$(artifactId)/$(version)

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

$(DESTDIR)$(MAVEN_LOCAL_REPO)/$(mvndir)/%: 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) | $(SED) 'y/ /:/')

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

# boilerplate ##################################################################

.PHONY: PHONY FORCE
.DELETE_ON_ERROR:
.SECONDARY: