summaryrefslogtreecommitdiff
path: root/Makefile
blob: fa542ab3a6eb4402356b03d1d3da57c9486f9e30 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
top := $(shell pwd)

CAT    = cat
CP     = cp
ECHO   = echo
EXISTS = test -e
FAIL   = exit 1
FIND   = find
GIT    = git
MKDIRS = mkdir -p
PATCH  = patch
RM     = rm -f
SED    = sed
SVN    = svn
TAR    = bsdtar
TOUCH  = touch
WGET   = wget

all: package

# utilities ####################################################################

# murl = "mangled url"
# spec = type|url|extra
# file = type/murl/extra
# base = type/murl
name2 = $(call spec2,$1,$(foreach package,$2,$($(package))))
spec2 = $(shell utils/spec2 $1 $(foreach a,$2,'$a'))
file2 = $(shell utils/file2 $1 $(foreach a,$2,'$a'))

specs_for = $(strip $(foreach t,$1,$(filter $t|%,$(if $2,$2,$(specs)))))

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

# configuration ################################################################

packages := $(filter-out _%,$(shell $(SED) "s/[\# ].*//" conf/sources.mk))
include conf/sources.mk
include conf/dependencies.mk
export MAVEN_LOCAL_REPO := $(shell $(CAT) conf/maven.local.repo.txt)
export JAR_DIR := $(shell $(CAT) conf/jardir.txt)

specs := $(foreach package,$(packages),$($(package)))
tarbombs := $(addprefix build/extract/,$(call spec2,base,$(_tarbombs)))

# download #####################################################################

download: PHONY \
          $(addprefix build/download/,                            \
                      $(call spec2,base,$(call specs_for,git tar)) \
                      $(call spec2,file,$(call specs_for,svn))     )

build/download/git/%: .tokens/network
	if [ -d '$@' ]; then \
		cd '$@' && $(GIT) fetch --all -p; \
	else \
		$(MKDIRS) '$(@D)' && $(GIT) clone --mirror '$(call file2,url,git/$*)' '$@'; \
	fi
	$(TOUCH) '$@'
build/download/svn/%: .tokens/network
	if ! [ -d '$(call file2,base,svn/$*)' ]; then \
		$(RM) -r '$@' && \
		$(MKDIRS) build/download/svn && \
		$(SVN) checkout --depth=empty \
			               '$(call file2,url ,svn/$*)' \
			build/download/'$(call file2,base,svn/$*)' && \
		cd build/download/'$(call file2,base,svn/$*)'; \
	else \
		cd build/download/'$(call file2,base,svn/$*)' && \
		$(SVN) update; \
	fi && \
	$(SVN) update --parents '$(call file2,extra,svn/$*)'
	$(EXISTS) '$@'
	$(TOUCH) '$@'
build/download/tar/%: .tokens/network
	$(MKDIRS) '$(@D)'
	$(WGET) -c -O '$@' '$(call file2,url,tar/$*)'
	$(TOUCH) '$@'

# extract ######################################################################

extract: PHONY $(addprefix build/extract,$(call spec2,file,$(specs)))

# This is a little gross because to get the dependencies right for
# `git` and `tar`, we need to do a bit more complex processing of
# `%`/`$*`.  We could do that with `.SECONDEXPANSION:`, but that is a
# whole can of worms.  Instead, we use a foreach loop to loop over all
# possibilities.  `svn` doesn't have this issue, because, unlike `git`
# and `tar`, its `extra` component is present in `build/download`.

# git
build/extract/git/%: # magic foreach loop
	gitref='$(firstword $(subst /, ,$(call file2,extra,git/$*)))' && \
	gitdir=build/extract/'$(call file2,base,git/$*)'/$${gitref} && \
	$(RM) -r "$$gitdir" && \
	{ \
		$(MKDIRS) "$$(dirname "$$gitdir")" && \
		$(GIT) clone build/download/'$(call file2,base,git/$*)' "$$gitdir" && \
		( cd "$$gitdir" && $(GIT) checkout "$$gitref" ) && \
		$(EXISTS) '$@'; \
	} || { $(RM) -r "$$gitdir"; $(FAIL); }
	$(TOUCH) '$@'

# svn
build/extract/svn/%: build/download/svn/%
	$(RM) -r '$@'
	$(MKDIRS) '$(@D)'
	$(CP) -a '$<' '$@' || { $(RM) -r '$@'; $(FAIL); }
	$(TOUCH) '$@'

# tar
_tar_basedir=build/extract/$(call file2,base,tar/$*)
build/extract/tar/%: # magic foreach loop
	$(RM) -r '$(_tar_basedir)' && \
	{ \
		$(MKDIRS) '$(_tar_basedir)' && \
		( cd '$(_tar_basedir)' && $(TAR) -m $(if $(filter $(_tar_basedir),$(tarbombs)),,--strip-components 1) -xf '$(top)/$<' ); \
	} || { $(RM) -r '$(_tar_basedir)'; $(FAIL); }

# magic foreach loop
$(foreach package,$(packages),                                                                           \
  $(if $(call specs_for,git tar,$($(package))),                                                          \
    $(eval                                                                                               \
      build/extract/$(call name2,file,$(package)):                                                       \
        build/download/$(call name2,base,$(package))                                                     \
     )                                                                                                   \
   )                                                                                                     \
 )

# place (patch) ################################################################

place: PHONY $(addprefix build/workdir/,$(packages))

$(addprefix build/workdir/,$(packages)): \
build/workdir/%:
	$(RM) -r '$@'
	$(MKDIRS) '$(@D)'
	$(CP) -a '$<' '$@' || { $(RM) -r '$@'; $(FAIL); }
	cd '$@' && \
	for patch in $(sort $(wildcard $(top)/rules/$*/*.patch)); do \
		$(PATCH) -p1 < $$patch || { $(RM) -r '$@'; $(FAIL); }; \
	done && \
	if [ -f '$(top)/rules/$*/delete.list' ]; then \
		$(RM) -r -- $$($(SED) '/^#/d' '$(top)/rules/$*/delete.list'); \
	fi
	$(TOUCH) '$@'

# Loop over our source configuration and set up the dependencies
# beteen `build/compile` and `build/extract`.
$(foreach package,$(packages),$(eval \
    build/workdir/$(package): \
        build/extract/$(call name2,file,$(package)) \
        $(call dep_optdir,rules/$(package)) \
))

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

package: PHONY $(addprefix build/packages/,$(packages))

package_specific=$(filter     $(patsubst rules/%/Makefile,%,$(wildcard rules/*/Makefile)),$(packages))
package_generic =$(filter-out $(patsubst rules/%/Makefile,%,$(wildcard rules/*/Makefile)),$(packages))

dirs2jars = $(if $1,$(shell $(FIND) $1 -name '*.jar'))
deps2jars = $(filter %.jar,$1) $(call dirs2jars,$(filter build/packages/%,$1))
deps2classpath = $(shell $(ECHO) $(abspath $(call deps2jars,$1)) $(CLASSPATH) | $(SED) 'y/ /:/')

deps2bindirs = $(if $1,$(sort $(shell $(FIND) $1 -type f -executable -printf '%h\n' 2>/dev/null)))
sanitize_bindirs = $(filter-out $(subst :, ,$(PATH)),$(abspath $1))
deps2path = $(shell $(ECHO) $(call sanitize_bindirs,$(call deps2bindirs,$1)) $(PATH) | $(SED) 'y/ /:/')

recurse = PATH='$(call deps2path,$^)' CLASSPATH='$(call deps2classpath,$^)' extra_makefiles='$(abspath $(wildcard rules/$*/*.mk))' $(MAKE) -C build/workdir/$* -f '$1' install DESTDIR='$(top)/$@' && $(EXISTS) '$@'

$(addprefix build/packages/,$(package_specific)): \
build/packages/%: RECURSIVE build/workdir/% rules/%/Makefile
	for dep in $(filter-out RECURSIVE,$^); do $(EXISTS) $$dep || { $(RM) -r '$@'; $(FAIL); }; done
	$(call recurse,$(top)/rules/$*/Makefile) || { $(RM) -r '$@'; $(FAIL); }

$(addprefix build/packages/,$(package_generic)): \
build/packages/%: RECURSIVE build/workdir/% rules/generic/Makefile
	for dep in $(filter-out RECURSIVE,$^); do $(EXISTS) $$dep || { $(RM) -r '$@'; $(FAIL); }; done
	$(call recurse,$(top)/rules/generic/Makefile) || { $(RM) -r '$@'; $(FAIL); }

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

clean: PHONY
	$(RM) -r build/compile build/packages
distclean: PHONY
	$(RM) -r .tokens build
.tokens/%:
	$(MKDIRS) '$(@D)'
	$(TOUCH) '$@'

.PHONY: RECURSIVE PHONY
.DELETE_ON_ERROR:
.SECONDARY: