More progress. More work.
[tripe-android] / Makefile
CommitLineData
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
25PACKAGE = tripe-android
26VERSION := $(shell ./auto-version)
27
28.SECONDEXPANSION: #sorry
29
30###--------------------------------------------------------------------------
31### Build parameters.
32
a5ec891a
MW
33abs_builddir := $(shell pwd)
34
9c15124f 35## Where to put the object files.
a5ec891a 36OUTDIR = out
9c15124f
MW
37
38## Native C compiler.
39CC = gcc
40CFLAGS = -O2 -g -Wall -pedantic -Werror
41
42## Native linker.
43LD = gcc
44LDFLAGS.so = -shared
45
46## External `pkg-config' packages required.
47PKGS = mLib catacomb
48
49## Java development kit.
50JDKDIR = /usr/lib/jvm/default-java
51JDK_PLAT = linux
52JNI_INCLUDES = $(JDKDIR)/include $(JDKDIR)/include/$(JDK_PLAT)
53
54## Default arguments for the Java runtime.
55JAVADEFS =
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.
60explicit-tmpdir-p = $(if $(filter-out undefined,$(origin TMPDIR)),t,nil)
61ifeq ($(explicit-tmpdir-p), t)
62 JAVADEFS += -Djava.io.tmpdir=$(TMPDIR)
63endif
64
65## Java compiler.
66JAVAC = javac $(JAVADEFS)
67JAVAFLAGS =
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.
76noip-available-p := $(shell noip echo t 2>/dev/null || echo nil)
77ifeq ($(noip-available-p), t)
78 NOIP = noip
79endif
80ifeq "$(explicit-tmpdir-p),$(noip-available-p)" "t,t"
81 SCALAC = $(NOIP) fsc $(JAVADEFS)
82 SCALAC_RESET = $(SCALAC) -reset
83else
84 SCALAC = scalac $(JAVADEFS)
85 SCALAC_RESET =
86endif
5c2b1515
MW
87SCALAFLAGS = -optimise -feature -deprecation -Xfatal-warnings \
88 -Xlint -Xlint:-package-object-classes \
9c15124f
MW
89 -Yinline-warnings:false
90SCALA_REPL = $(NOIP) scala $(JAVADEFS)
91
92## Silent-rules is on by default.
3a2f1a4b 93V = 0
9c15124f
MW
94
95## Allow local overrides at this point.
96-include local.mk
97
98###--------------------------------------------------------------------------
99### Silent-rules machinery.
100
3a2f1a4b
MW
101v_tag = $(call v_tag_$V,$1)
102v_tag_0 = @printf " %-8s %s\n" "$1" "$@";
9c15124f 103
3a2f1a4b
MW
104V_AT = $(V_AT_$V)
105V_AT_0 = @
106
9c15124f
MW
107###--------------------------------------------------------------------------
108### External native packages.
3a2f1a4b 109
8eabb4ff
MW
110PKGS_CFLAGS := $(foreach p,$(PKGS),$(shell pkg-config --cflags $p))
111PKGS_LIBS := $(foreach p,$(PKGS),$(shell pkg-config --libs $p))
112
9c15124f
MW
113ALL_CFLAGS = $(CFLAGS) -fPIC \
114 $(addprefix -I,$(JNI_INCLUDES)) \
115 $(PKGS_CFLAGS)
3a2f1a4b 116
8eabb4ff 117LIBS = $(PKGS_LIBS)
3a2f1a4b 118
9c15124f
MW
119###--------------------------------------------------------------------------
120### Various other tweaks and overrides.
3a2f1a4b 121
8eabb4ff
MW
122## Hack around https://issues.scala-lang.org/browse/SI-9689
123SCALAFLAGS += -Yno-load-impl-class
124
9c15124f
MW
125###--------------------------------------------------------------------------
126### And now we can start building things.
127
3a2f1a4b
MW
128all::
129.PHONY: all
130
9c15124f
MW
131CLEANFILES =
132clean::
133.PHONY: clean
134
135###--------------------------------------------------------------------------
136### Native C code.
3a2f1a4b 137
2812483e 138out/%.o: %.c
a5ec891a 139 $(call v_tag,CC)mkdir -p $(OUTDIR)/ && \
9c15124f
MW
140 $(CC) -c $(ALL_CFLAGS) -MMD -o$@ $<
141
142ALL_SOURCES =
143DISTFILES += $(ALL_SOURCES)
144
a5ec891a
MW
145objects = $(patsubst %.c,$(OUTDIR)/%$2,$1)
146CLEANFILES += $(OUTDIR)/*.o $(OUTDIR)/*.d
2812483e 147
9c15124f
MW
148###--------------------------------------------------------------------------
149### Java classes.
150
151## Java and Scala source files turn into multiple `.class' files with
152## unpredictable names. Rather than try to guess stable outputs for these
153## sources, we make artificial `timestamp' files and uses these in our
154## dependencies.
a5ec891a
MW
155CLASSDIR = $(OUTDIR)/cls
156stamps = $(patsubst %,$(OUTDIR)/%.$1-stamp,$2)
9c15124f
MW
157
158clean::; rm -rf $(CLASSDIR)
a5ec891a 159CLEANFILES += $(OUTDIR)/*.class-stamp
9c15124f
MW
160
161## Compiling actual Java code. Note that this confuses the resident Scala
162## compiler, so we have to reset it here.
163CLSISH += java
a5ec891a
MW
164$(OUTDIR)/%.class-stamp: %.java
165 $(call v_tag,JAVAC)mkdir -p $(CLASSDIR)/ && \
2a02a846
MW
166 $(JAVAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(JAVAFLAGS) $< && \
167 echo built >$@
9c15124f
MW
168 $(V_AT)$(SCALAC_RESET)
169
170## Compiling Scala code.
171CLSEXT += scala
a5ec891a
MW
172$(OUTDIR)/%.class-stamp: %.scala
173 $(call v_tag,SCALAC)mkdir -p $(CLASSDIR)/ && \
2a02a846
MW
174 $(SCALAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(SCALAFLAGS) $< && \
175 echo built >$@
3a2f1a4b 176
9c15124f
MW
177###--------------------------------------------------------------------------
178### Native-code libraries.
179
180SHLIBS += toy
181toy_SOURCES = jni.c
3a2f1a4b 182
a5ec891a 183shlibfile = $(patsubst %,$(OUTDIR)/lib%.so,$1)
9c15124f
MW
184SHLIBFILES = $(call shlibfile,$(SHLIBS))
185TARGETS += $(SHLIBFILES)
186ALL_SOURCES += $(foreach l,$(SHLIBS),$($l_SOURCES))
187
a5ec891a 188$(SHLIBFILES): $(OUTDIR)/lib%.so: $$(call objects,$$($$*_SOURCES),.o)
8eabb4ff
MW
189 $(call v_tag,LD)$(LD) $(LDFLAGS.so) -o$@ $^ $(LIBS)
190
9c15124f
MW
191###--------------------------------------------------------------------------
192### Java classes.
193
194## Writing things out longhand is tedious. `CLASSES' is a list of entries of
195## the form `SRC[:DEP,...]', saying that `SRC.ext', being a source file
196## capable of being built into `.class' files and setting `SRC.stamp', should
197## be so built, and that it depends on other similar sources named DEP having
198## been so built.
2812483e 199CLASSES += util
9c15124f
MW
200CLASSES += sys:util
201CLASSES += admin:sys,util
202CLASSES += tar:util
04a5abae
MW
203CLASSES += progress:sys,util
204CLASSES += keys:progress,tar,sys,util
205CLASSES += terminal:progress,sys,util
9c15124f
MW
206CLASSES += main:sys
207
208## Machinery for parsing the `CLASSES' list.
209COMMA = ,
210class-name = $(firstword $(subst :, ,$1))
211class-deps = $(subst $(COMMA), ,$(word 2,$(subst :, ,$1)))
212
213CLASS_NAMES = $(foreach c,$(CLASSES),$(call class-name,$c))
214
a5ec891a 215all:: $(call stamps,class,$(CLASS_NAMES))
9c15124f 216
a5ec891a
MW
217$(foreach c,$(CLASSES),$(eval $(call stamps,class,$(call class-name,$c)): \
218 $(call stamps,class,$(call class-deps,$c))))
9c15124f
MW
219
220DISTFILES += $(foreach c,$(CLASSES),\
221 $(foreach e,$(CLSEXT),\
222 $(wildcard $(call class-name,$c).$e)))
223
224###--------------------------------------------------------------------------
a5ec891a
MW
225### External packages.
226
227EXTPREFIX = $(abs_builddir)/$(OUTDIR)/inst
228
229join-paths = $(if $(filter /%,$2),$2,$1/$2)
230ext-srcdir = $(or $($1_SRCDIR),../$1)
231
232EXTERNALS += adns
233adns_CONFIG = --disable-dynamic
234
235EXTERNALS += mLib
236mLib_DEPS = adns
237mLib_CONFIG = --enable-static --disable-shared --with-adns
238
239EXTERNALS += catacomb
240catacomb_DEPS = mLib
241catacomb_CONFIG = --enable-static --disable-shared
242
243EXTERNALS += tripe
244tripe_DEPS = mLib catacomb
245tripe_CONFIG = --without-wireshark --with-adns --with-tunnel=slip
246
247all:: $(call stamps,ext,$(EXTERNALS))
248CLEANFILES += $(OUTDIR)/*.ext-stamp
249clean::; rm -rf $(OUTDIR)/inst $(OUTDIR)/build
250
251$(call stamps,ext,$(EXTERNALS)): \
252 $(OUTDIR)/%.ext-stamp: $$(call stamps,ext,$$($$*_DEPS))
253 $(V_AT)rm -rf $(OUTDIR)/build/$*/
254 $(V_AT)mkdir -p $(OUTDIR)/build/$*/
255 cd $(OUTDIR)/build/$*/ && \
256 $(call join-paths,../../..,$(call ext-srcdir,$*))/configure \
257 --prefix=$(EXTPREFIX) \
258 $($*_CONFIG) \
259 CFLAGS="-O2 -g -fPIC -Wall -I$(EXTPREFIX)/include" \
260 LDFLAGS="-L$(EXTPREFIX)/lib" \
261 PKG_CONFIG="pkg-config --static" \
262 PKG_CONFIG_LIBDIR=$(EXTPREFIX)/lib/pkgconfig
263 $(MAKE) -C$(OUTDIR)/build/$*/
264 $(MAKE) -C$(OUTDIR)/build/$*/ -s install
265 $(V_AT)echo done >$@
266
267###--------------------------------------------------------------------------
9c15124f
MW
268### Distribution arrangements.
269
270DISTFILES += COPYING
271DISTFILES += Makefile
272DISTFILES += auto-version
273
274distdir = $(PACKAGE)-$(VERSION)
275DISTTAR = $(distdir).tar.gz
276
277distdir:
a5ec891a
MW
278 rm -rf $(OUTDIR)/$(distdir)
279 mkdir $(OUTDIR)/$(distdir)/
280 echo $(VERSION) >$(OUTDIR)/$(distdir)/RELEASE
9c15124f 281 set -e; for i in $(DISTFILES); do \
a5ec891a
MW
282 case $$i in */*) mkdir -p $(OUTDIR)/$(distdir)/$${i%/*}/ ;; esac; \
283 cp $$i $(OUTDIR)/$(distdir)/; \
9c15124f
MW
284 done
285.PHONY: distdir
286
287dist: distdir
a5ec891a 288 set -e; cd $(OUTDIR)/; tar chozf ../$(DISTTAR) $(distdir)
9c15124f
MW
289 rm -rf $(distdir)
290.PHONY: dist
291
292###--------------------------------------------------------------------------
293### Finishing touches.
3a2f1a4b
MW
294
295all:: $(TARGETS)
9c15124f
MW
296
297clean::; rm -f $(CLEANFILES) $(TARGETS)
3a2f1a4b 298
04a5abae 299repl: all
9c15124f
MW
300 $(SCALA_REPL) -cp $(CLASSDIR) -Djava.library.path=$(OUTDIR)
301.PHONY: repl
2812483e 302
9c15124f 303-include $(call objects,$(ALL_SOURCES),.d)
3a2f1a4b 304
9c15124f 305###----- That's all, folks --------------------------------------------------