summaryrefslogtreecommitdiff
path: root/Makefile
blob: 9b54ce74e72cc0b457a9679cd3e8e78933a78f87 (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
top := $(shell pwd)

all: package

# 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)

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

download: PHONY $(foreach package,$(packages),build/download/$(shell utils/spec2file '$($(package))'))

build/download/git/%: .tokens/network
	if [ -d '$@' ]; then \
		cd '$@' && git fetch --all -p; \
	else \
		mkdir -p '$(@D)' && git clone --mirror '$(shell utils/file2url 'git/$*')' '$@'; \
	fi
	touch '$@'
build/download/svn/%: .tokens/network
	if ! [ -d '$(shell utils/file2base 'svn/$*')' ]; then \
		rm -rf '$@' && \
		mkdir -p build/download/svn && \
		svn checkout --depth=empty \
			               '$(shell utils/file2url  'svn/$*')' \
			build/download/'$(shell utils/file2base 'svn/$*')' && \
		cd build/download/'$(shell utils/file2base 'svn/$*')'; \
	else \
		cd build/download/'$(shell utils/file2base 'svn/$*')' && \
		svn update; \
	fi && \
	svn update --parents '$(shell utils/file2extra 'svn/$*')'
	touch '$@'
build/download/tar/%: .tokens/network
	mkdir -p '$(@D)'
	wget -c -O '$@' '$(shell utils/file2url 'tar/$*')'
	touch '$@'

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

extract: PHONY $(foreach package,$(packages),build/extract/$(shell utils/spec2file '$($(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`, it's `extra` component is present in `build/download`.

build/extract/git/%: # magic foreach loop
	gitref='$(shell utils/file2extra 'git/$*'|cut -d/ -f1)' && \
	gitdir=build/extract/'$(shell utils/file2base 'git/$*')'/$${gitref} && \
	rm -rf "$$gitdir" && \
	{ \
		mkdir -p "$$(dirname "$$gitdir")" && \
		git clone build/download/'$(shell utils/file2base 'git/$*')' "$$gitdir" && \
		( cd "$$gitdir" && git checkout "$$gitref" ); \
	} || rm -rf "$$gitdir"
	touch '$@'

build/extract/svn/%: build/download/svn/%
	rm -rf '$@'
	mkdir -p '$(@D)'
	cp -a '$<' '$@'

build/extract/tar/%: # magic foreach loop
	basedir='build/extract/$(shell utils/file2base 'tar/$*')' && \
	rm -rf "$$basedir" && \
	{ \
		mkdir -p "$$basedir" && \
		( cd "$$basedir" && tar -m --strip-components 1 -xf '$(top)/$<' ); \
	} || rm -rf "$$basedir"

$(foreach package,$(packages),$(if $(filter-out svn|%,$($(package))),$(eval \
    build/extract/$(shell utils/spec2file '$($(package))'): \
        build/download/$(shell utils/file2base "$$(utils/spec2file '$($(package))')") \
)))

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

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

$(addprefix build/workdir/,$(packages)): \
build/workdir/%:
	rm -rf '$@'
	mkdir -p '$(@D)'
	cp -a '$<' '$@'
	cd '$@' && \
	for patch in $(sort $(wildcard $(top)/rules/$*/*.patch)); do \
		patch -f -Np1 -i $$patch || { rm -rf '$@'; exit 1; }; \
	done && \
	if [ -f '$(top)/rules/$*/delete.list' ]; then \
		rm -rf -- $$(< '$(top)/rules/$*/delete.list'); \
	fi

# 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/$(shell utils/spec2file '$($(package))') \
        $(shell find rules/$(package) 2>/dev/null) \
))

# 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)) | tr ' ' :)

$(addprefix build/packages/,$(package_specific)): \
build/packages/%: RECURSIVE build/workdir/% rules/%/Makefile
	CLASSPATH='$(call deps2classpath,$^)' extra_makefiles='$(abspath $(wildcard rules/$*/*.mk))' \
		$(MAKE) -C build/workdir/$* -f '$(top)/rules/$*/Makefile' install DESTDIR='$(top)/$@'
	mkdir -p build/packages/all && lndir -silent '$(top)/$@' build/packages/all

$(addprefix build/packages/,$(package_generic)): \
build/packages/%: RECURSIVE build/workdir/% rules/generic/Makefile
	CLASSPATH='$(call deps2classpath,$^)' extra_makefiles='$(abspath $(wildcard rules/$*/*.mk))' \
		$(MAKE) -C build/workdir/$* -f '$(top)/rules/generic/Makefile' install DESTDIR='$(top)/$@'
	mkdir -p build/packages/all && lndir -silent '$(top)/$@' build/packages/all

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

clean: PHONY
	rm -rf build/compile build/packages
distclean: PHONY
	rm -rf .tokens build
.tokens/%:
	mkdir -p '$(@D)' && touch '$@'

.PHONY: RECURSIVE PHONY