summaryrefslogtreecommitdiff
path: root/Makefile
blob: 91c5dc4bc563f2b15d92354c0779e4c5e79ac189 (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
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 ####################################################################

# spec  = type|url|extra
# file ~= type/url/extra # but with 'url' mangled
# base ~= type/url       # but with 'url' mangled
spec2base  = $(shell utils/file2base "$$(utils/spec2file '$1')")
name2base  = $(call spec2base,$($1))
name2file  = $(shell utils/spec2file '$($1)')
file2url   = $(shell utils/file2url   '$1')
file2base  = $(shell utils/file2base  '$1')
file2extra = $(shell utils/file2extra '$1')

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)
tarbombs := $(foreach spec,$(_tarbombs),build/extract/$(call spec2base,$(spec)))

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

download: PHONY $(foreach package,$(packages),build/download/$(call name2file,$(package)))

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

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

extract: PHONY $(foreach package,$(packages),build/extract/$(call name2file,$(package)))

# 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 file2extra,git/$*)))' && \
	gitdir=build/extract/'$(call file2base,git/$*)'/$${gitref} && \
	$(RM) -r "$$gitdir" && \
	{ \
		$(MKDIRS) "$$(dirname "$$gitdir")" && \
		$(GIT) clone build/download/'$(call file2base,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 file2base,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 $(foreach s,git tar,$(filter $s|%,$($(package)))),                                               \
    $(eval                                                                                              \
      build/extract/$(call name2file,$(package)):                                                       \
        build/download/$(call name2base,$(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 name2file,$(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: