Commit | Line | Data |
---|---|---|
3a2f1a4b | 1 | ### -*-makefile-*- |
9c15124f MW |
2 | ### |
3 | ### Build script for the TrIPE Android app | |
4 | ### | |
5 | ### (c) 2018 Straylight/Edgeware | |
6 | ### | |
3a2f1a4b | 7 | |
9c15124f MW |
8 | ###----- Licensing notice --------------------------------------------------- |
9 | ### | |
10 | ### This file is part of the Trivial IP Encryption (TrIPE) Android app. | |
11 | ### | |
12 | ### TrIPE is free software: you can redistribute it and/or modify it under | |
13 | ### the terms of the GNU General Public License as published by the Free | |
14 | ### Software Foundation; either version 3 of the License, or (at your | |
15 | ### option) any later version. | |
16 | ### | |
17 | ### TrIPE is distributed in the hope that it will be useful, but WITHOUT | |
18 | ### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | ### FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | ### for more details. | |
21 | ### | |
22 | ### You should have received a copy of the GNU General Public License | |
23 | ### along with TrIPE. If not, see <https://www.gnu.org/licenses/>. | |
24 | ||
25 | PACKAGE = tripe-android | |
26 | VERSION := $(shell ./auto-version) | |
27 | ||
28 | .SECONDEXPANSION: #sorry | |
29 | ||
30 | ###-------------------------------------------------------------------------- | |
31 | ### Build parameters. | |
32 | ||
a5ec891a MW |
33 | abs_builddir := $(shell pwd) |
34 | ||
9c15124f | 35 | ## Where to put the object files. |
a5ec891a | 36 | OUTDIR = out |
9c15124f MW |
37 | |
38 | ## Native C compiler. | |
39 | CC = gcc | |
40 | CFLAGS = -O2 -g -Wall -pedantic -Werror | |
41 | ||
42 | ## Native linker. | |
3bb2303d | 43 | LD = gcc -Wl,-z,defs |
9c15124f MW |
44 | LDFLAGS.so = -shared |
45 | ||
46 | ## External `pkg-config' packages required. | |
47 | PKGS = mLib catacomb | |
48 | ||
49 | ## Java development kit. | |
50 | JDKDIR = /usr/lib/jvm/default-java | |
51 | JDK_PLAT = linux | |
52 | JNI_INCLUDES = $(JDKDIR)/include $(JDKDIR)/include/$(JDK_PLAT) | |
53 | ||
54 | ## Default arguments for the Java runtime. | |
55 | JAVADEFS = | |
56 | ||
57 | ## The Java runtime, for some reason, hardcodes its default for | |
58 | ## `java.io.tmpdir', inviting security problems. If the user has defined a | |
59 | ## `TMPDIR', then persuade Java to use it. | |
60 | explicit-tmpdir-p = $(if $(filter-out undefined,$(origin TMPDIR)),t,nil) | |
61 | ifeq ($(explicit-tmpdir-p), t) | |
62 | JAVADEFS += -Djava.io.tmpdir=$(TMPDIR) | |
63 | endif | |
64 | ||
65 | ## Java compiler. | |
66 | JAVAC = javac $(JAVADEFS) | |
67 | JAVAFLAGS = | |
68 | ||
69 | ## Scala compiler. | |
70 | ## | |
71 | ## Unfortunately, `fsc' is a total security disaster. On the other hand, | |
72 | ## `scalac' is rather slow. If we're running with a custom `TMPDIR', and the | |
73 | ## `noip' wrapper <https://git.distorted.org.uk/~mdw/preload-hacks/> is | |
74 | ## available, then we can tame `fsc' enough that it's probably safe to run; | |
75 | ## otherwise, it's too risky to enable by default. | |
76 | noip-available-p := $(shell noip echo t 2>/dev/null || echo nil) | |
77 | ifeq ($(noip-available-p), t) | |
78 | NOIP = noip | |
79 | endif | |
80 | ifeq "$(explicit-tmpdir-p),$(noip-available-p)" "t,t" | |
81 | SCALAC = $(NOIP) fsc $(JAVADEFS) | |
82 | SCALAC_RESET = $(SCALAC) -reset | |
83 | else | |
84 | SCALAC = scalac $(JAVADEFS) | |
85 | SCALAC_RESET = | |
86 | endif | |
5c2b1515 MW |
87 | SCALAFLAGS = -optimise -feature -deprecation -Xfatal-warnings \ |
88 | -Xlint -Xlint:-package-object-classes \ | |
9c15124f MW |
89 | -Yinline-warnings:false |
90 | SCALA_REPL = $(NOIP) scala $(JAVADEFS) | |
91 | ||
92 | ## Silent-rules is on by default. | |
3a2f1a4b | 93 | V = 0 |
9c15124f MW |
94 | |
95 | ## Allow local overrides at this point. | |
96 | -include local.mk | |
97 | ||
98 | ###-------------------------------------------------------------------------- | |
99 | ### Silent-rules machinery. | |
100 | ||
3a2f1a4b MW |
101 | v_tag = $(call v_tag_$V,$1) |
102 | v_tag_0 = @printf " %-8s %s\n" "$1" "$@"; | |
9c15124f | 103 | |
3a2f1a4b MW |
104 | V_AT = $(V_AT_$V) |
105 | V_AT_0 = @ | |
106 | ||
9c15124f MW |
107 | ###-------------------------------------------------------------------------- |
108 | ### External native packages. | |
3a2f1a4b | 109 | |
3bb2303d MW |
110 | EXTPREFIX = $(abs_builddir)/$(OUTDIR)/inst |
111 | ||
112 | join-paths = $(if $(filter /%,$2),$2,$1/$2) | |
113 | ext-srcdir = $(or $($1_SRCDIR),../$1) | |
114 | ||
115 | PKG_CONFIG = PKG_CONFIG_LIBDIR=$(OUTDIR)/inst/lib/pkgconfig \ | |
116 | pkg-config --static | |
117 | ||
118 | PKGS_CFLAGS := $(foreach p,$(PKGS),$(shell $(PKG_CONFIG) --cflags $p)) | |
119 | PKGS_LIBS := $(foreach p,$(PKGS),$(shell $(PKG_CONFIG) --libs $p)) | |
8eabb4ff | 120 | |
9c15124f MW |
121 | ALL_CFLAGS = $(CFLAGS) -fPIC \ |
122 | $(addprefix -I,$(JNI_INCLUDES)) \ | |
3bb2303d MW |
123 | -I$(OUTDIR)/inst/include \ |
124 | -I$(call ext-srcdir,tripe)/common \ | |
125 | -I$(call ext-srcdir,tripe)/priv \ | |
126 | -I$(call ext-srcdir,tripe)/server \ | |
127 | -I$(OUTDIR)/build/tripe/config \ | |
9c15124f | 128 | $(PKGS_CFLAGS) |
3a2f1a4b | 129 | |
3bb2303d MW |
130 | LIBS = $(OUTDIR)/build/tripe/server/libtripe.a \ |
131 | $(OUTDIR)/build/tripe/priv/libpriv.a \ | |
132 | $(OUTDIR)/build/tripe/common/libcommon.a \ | |
133 | -L$(OUTDIR)/inst/lib $(PKGS_LIBS) | |
3a2f1a4b | 134 | |
9c15124f MW |
135 | ###-------------------------------------------------------------------------- |
136 | ### Various other tweaks and overrides. | |
3a2f1a4b | 137 | |
8eabb4ff MW |
138 | ## Hack around https://issues.scala-lang.org/browse/SI-9689 |
139 | SCALAFLAGS += -Yno-load-impl-class | |
140 | ||
9c15124f MW |
141 | ###-------------------------------------------------------------------------- |
142 | ### And now we can start building things. | |
143 | ||
3a2f1a4b MW |
144 | all:: |
145 | .PHONY: all | |
146 | ||
9c15124f MW |
147 | CLEANFILES = |
148 | clean:: | |
149 | .PHONY: clean | |
150 | ||
151 | ###-------------------------------------------------------------------------- | |
152 | ### Native C code. | |
3a2f1a4b | 153 | |
2812483e | 154 | out/%.o: %.c |
a5ec891a | 155 | $(call v_tag,CC)mkdir -p $(OUTDIR)/ && \ |
9c15124f MW |
156 | $(CC) -c $(ALL_CFLAGS) -MMD -o$@ $< |
157 | ||
158 | ALL_SOURCES = | |
159 | DISTFILES += $(ALL_SOURCES) | |
160 | ||
a5ec891a MW |
161 | objects = $(patsubst %.c,$(OUTDIR)/%$2,$1) |
162 | CLEANFILES += $(OUTDIR)/*.o $(OUTDIR)/*.d | |
2812483e | 163 | |
9c15124f MW |
164 | ###-------------------------------------------------------------------------- |
165 | ### Java classes. | |
166 | ||
167 | ## Java and Scala source files turn into multiple `.class' files with | |
168 | ## unpredictable names. Rather than try to guess stable outputs for these | |
169 | ## sources, we make artificial `timestamp' files and uses these in our | |
170 | ## dependencies. | |
a5ec891a MW |
171 | CLASSDIR = $(OUTDIR)/cls |
172 | stamps = $(patsubst %,$(OUTDIR)/%.$1-stamp,$2) | |
9c15124f MW |
173 | |
174 | clean::; rm -rf $(CLASSDIR) | |
a5ec891a | 175 | CLEANFILES += $(OUTDIR)/*.class-stamp |
9c15124f MW |
176 | |
177 | ## Compiling actual Java code. Note that this confuses the resident Scala | |
178 | ## compiler, so we have to reset it here. | |
179 | CLSISH += java | |
a5ec891a MW |
180 | $(OUTDIR)/%.class-stamp: %.java |
181 | $(call v_tag,JAVAC)mkdir -p $(CLASSDIR)/ && \ | |
2a02a846 MW |
182 | $(JAVAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(JAVAFLAGS) $< && \ |
183 | echo built >$@ | |
9c15124f MW |
184 | $(V_AT)$(SCALAC_RESET) |
185 | ||
186 | ## Compiling Scala code. | |
187 | CLSEXT += scala | |
a5ec891a MW |
188 | $(OUTDIR)/%.class-stamp: %.scala |
189 | $(call v_tag,SCALAC)mkdir -p $(CLASSDIR)/ && \ | |
2a02a846 MW |
190 | $(SCALAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(SCALAFLAGS) $< && \ |
191 | echo built >$@ | |
3a2f1a4b | 192 | |
9c15124f MW |
193 | ###-------------------------------------------------------------------------- |
194 | ### Native-code libraries. | |
195 | ||
3bb2303d MW |
196 | SHLIBS += tripe |
197 | tripe_SOURCES = jni.c | |
3a2f1a4b | 198 | |
a5ec891a | 199 | shlibfile = $(patsubst %,$(OUTDIR)/lib%.so,$1) |
9c15124f MW |
200 | SHLIBFILES = $(call shlibfile,$(SHLIBS)) |
201 | TARGETS += $(SHLIBFILES) | |
202 | ALL_SOURCES += $(foreach l,$(SHLIBS),$($l_SOURCES)) | |
203 | ||
3bb2303d MW |
204 | $(call objects,$(tripe_SOURCES),.o): $(call stamps,ext,tripe) |
205 | ||
a5ec891a | 206 | $(SHLIBFILES): $(OUTDIR)/lib%.so: $$(call objects,$$($$*_SOURCES),.o) |
8eabb4ff MW |
207 | $(call v_tag,LD)$(LD) $(LDFLAGS.so) -o$@ $^ $(LIBS) |
208 | ||
9c15124f MW |
209 | ###-------------------------------------------------------------------------- |
210 | ### Java classes. | |
211 | ||
212 | ## Writing things out longhand is tedious. `CLASSES' is a list of entries of | |
213 | ## the form `SRC[:DEP,...]', saying that `SRC.ext', being a source file | |
214 | ## capable of being built into `.class' files and setting `SRC.stamp', should | |
215 | ## be so built, and that it depends on other similar sources named DEP having | |
216 | ## been so built. | |
2812483e | 217 | CLASSES += util |
9c15124f MW |
218 | CLASSES += sys:util |
219 | CLASSES += admin:sys,util | |
220 | CLASSES += tar:util | |
04a5abae MW |
221 | CLASSES += progress:sys,util |
222 | CLASSES += keys:progress,tar,sys,util | |
223 | CLASSES += terminal:progress,sys,util | |
9c15124f MW |
224 | |
225 | ## Machinery for parsing the `CLASSES' list. | |
226 | COMMA = , | |
227 | class-name = $(firstword $(subst :, ,$1)) | |
228 | class-deps = $(subst $(COMMA), ,$(word 2,$(subst :, ,$1))) | |
229 | ||
230 | CLASS_NAMES = $(foreach c,$(CLASSES),$(call class-name,$c)) | |
231 | ||
a5ec891a | 232 | all:: $(call stamps,class,$(CLASS_NAMES)) |
9c15124f | 233 | |
a5ec891a MW |
234 | $(foreach c,$(CLASSES),$(eval $(call stamps,class,$(call class-name,$c)): \ |
235 | $(call stamps,class,$(call class-deps,$c)))) | |
9c15124f MW |
236 | |
237 | DISTFILES += $(foreach c,$(CLASSES),\ | |
238 | $(foreach e,$(CLSEXT),\ | |
239 | $(wildcard $(call class-name,$c).$e))) | |
240 | ||
241 | ###-------------------------------------------------------------------------- | |
a5ec891a MW |
242 | ### External packages. |
243 | ||
a5ec891a MW |
244 | EXTERNALS += adns |
245 | adns_CONFIG = --disable-dynamic | |
246 | ||
247 | EXTERNALS += mLib | |
248 | mLib_DEPS = adns | |
249 | mLib_CONFIG = --enable-static --disable-shared --with-adns | |
250 | ||
251 | EXTERNALS += catacomb | |
252 | catacomb_DEPS = mLib | |
253 | catacomb_CONFIG = --enable-static --disable-shared | |
254 | ||
255 | EXTERNALS += tripe | |
256 | tripe_DEPS = mLib catacomb | |
257 | tripe_CONFIG = --without-wireshark --with-adns --with-tunnel=slip | |
258 | ||
259 | all:: $(call stamps,ext,$(EXTERNALS)) | |
260 | CLEANFILES += $(OUTDIR)/*.ext-stamp | |
261 | clean::; rm -rf $(OUTDIR)/inst $(OUTDIR)/build | |
262 | ||
263 | $(call stamps,ext,$(EXTERNALS)): \ | |
264 | $(OUTDIR)/%.ext-stamp: $$(call stamps,ext,$$($$*_DEPS)) | |
265 | $(V_AT)rm -rf $(OUTDIR)/build/$*/ | |
266 | $(V_AT)mkdir -p $(OUTDIR)/build/$*/ | |
267 | cd $(OUTDIR)/build/$*/ && \ | |
268 | $(call join-paths,../../..,$(call ext-srcdir,$*))/configure \ | |
269 | --prefix=$(EXTPREFIX) \ | |
270 | $($*_CONFIG) \ | |
271 | CFLAGS="-O2 -g -fPIC -Wall -I$(EXTPREFIX)/include" \ | |
272 | LDFLAGS="-L$(EXTPREFIX)/lib" \ | |
273 | PKG_CONFIG="pkg-config --static" \ | |
274 | PKG_CONFIG_LIBDIR=$(EXTPREFIX)/lib/pkgconfig | |
275 | $(MAKE) -C$(OUTDIR)/build/$*/ | |
276 | $(MAKE) -C$(OUTDIR)/build/$*/ -s install | |
277 | $(V_AT)echo done >$@ | |
278 | ||
279 | ###-------------------------------------------------------------------------- | |
9c15124f MW |
280 | ### Distribution arrangements. |
281 | ||
282 | DISTFILES += COPYING | |
283 | DISTFILES += Makefile | |
284 | DISTFILES += auto-version | |
285 | ||
286 | distdir = $(PACKAGE)-$(VERSION) | |
287 | DISTTAR = $(distdir).tar.gz | |
288 | ||
289 | distdir: | |
a5ec891a MW |
290 | rm -rf $(OUTDIR)/$(distdir) |
291 | mkdir $(OUTDIR)/$(distdir)/ | |
292 | echo $(VERSION) >$(OUTDIR)/$(distdir)/RELEASE | |
9c15124f | 293 | set -e; for i in $(DISTFILES); do \ |
a5ec891a MW |
294 | case $$i in */*) mkdir -p $(OUTDIR)/$(distdir)/$${i%/*}/ ;; esac; \ |
295 | cp $$i $(OUTDIR)/$(distdir)/; \ | |
9c15124f MW |
296 | done |
297 | .PHONY: distdir | |
298 | ||
299 | dist: distdir | |
a5ec891a | 300 | set -e; cd $(OUTDIR)/; tar chozf ../$(DISTTAR) $(distdir) |
9c15124f MW |
301 | rm -rf $(distdir) |
302 | .PHONY: dist | |
303 | ||
304 | ###-------------------------------------------------------------------------- | |
305 | ### Finishing touches. | |
3a2f1a4b MW |
306 | |
307 | all:: $(TARGETS) | |
9c15124f MW |
308 | |
309 | clean::; rm -f $(CLEANFILES) $(TARGETS) | |
3a2f1a4b | 310 | |
04a5abae | 311 | repl: all |
9c15124f MW |
312 | $(SCALA_REPL) -cp $(CLASSDIR) -Djava.library.path=$(OUTDIR) |
313 | .PHONY: repl | |
2812483e | 314 | |
9c15124f | 315 | -include $(call objects,$(ALL_SOURCES),.d) |
3a2f1a4b | 316 | |
9c15124f | 317 | ###----- That's all, folks -------------------------------------------------- |