Overhaul the build machinery.
[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
33## Where to put the object files.
34OUTDIR = out/
35
36## Native C compiler.
37CC = gcc
38CFLAGS = -O2 -g -Wall -pedantic -Werror
39
40## Native linker.
41LD = gcc
42LDFLAGS.so = -shared
43
44## External `pkg-config' packages required.
45PKGS = mLib catacomb
46
47## Java development kit.
48JDKDIR = /usr/lib/jvm/default-java
49JDK_PLAT = linux
50JNI_INCLUDES = $(JDKDIR)/include $(JDKDIR)/include/$(JDK_PLAT)
51
52## Default arguments for the Java runtime.
53JAVADEFS =
54
55## The Java runtime, for some reason, hardcodes its default for
56## `java.io.tmpdir', inviting security problems. If the user has defined a
57## `TMPDIR', then persuade Java to use it.
58explicit-tmpdir-p = $(if $(filter-out undefined,$(origin TMPDIR)),t,nil)
59ifeq ($(explicit-tmpdir-p), t)
60 JAVADEFS += -Djava.io.tmpdir=$(TMPDIR)
61endif
62
63## Java compiler.
64JAVAC = javac $(JAVADEFS)
65JAVAFLAGS =
66
67## Scala compiler.
68##
69## Unfortunately, `fsc' is a total security disaster. On the other hand,
70## `scalac' is rather slow. If we're running with a custom `TMPDIR', and the
71## `noip' wrapper <https://git.distorted.org.uk/~mdw/preload-hacks/> is
72## available, then we can tame `fsc' enough that it's probably safe to run;
73## otherwise, it's too risky to enable by default.
74noip-available-p := $(shell noip echo t 2>/dev/null || echo nil)
75ifeq ($(noip-available-p), t)
76 NOIP = noip
77endif
78ifeq "$(explicit-tmpdir-p),$(noip-available-p)" "t,t"
79 SCALAC = $(NOIP) fsc $(JAVADEFS)
80 SCALAC_RESET = $(SCALAC) -reset
81else
82 SCALAC = scalac $(JAVADEFS)
83 SCALAC_RESET =
84endif
85SCALAFLAGS = -optimise -Xlint -Xlint:-package-object-classes \
86 -Yinline-warnings:false
87SCALA_REPL = $(NOIP) scala $(JAVADEFS)
88
89## Silent-rules is on by default.
3a2f1a4b 90V = 0
9c15124f
MW
91
92## Allow local overrides at this point.
93-include local.mk
94
95###--------------------------------------------------------------------------
96### Silent-rules machinery.
97
3a2f1a4b
MW
98v_tag = $(call v_tag_$V,$1)
99v_tag_0 = @printf " %-8s %s\n" "$1" "$@";
9c15124f 100
3a2f1a4b
MW
101V_AT = $(V_AT_$V)
102V_AT_0 = @
103
9c15124f
MW
104###--------------------------------------------------------------------------
105### External native packages.
3a2f1a4b 106
8eabb4ff
MW
107PKGS_CFLAGS := $(foreach p,$(PKGS),$(shell pkg-config --cflags $p))
108PKGS_LIBS := $(foreach p,$(PKGS),$(shell pkg-config --libs $p))
109
9c15124f
MW
110ALL_CFLAGS = $(CFLAGS) -fPIC \
111 $(addprefix -I,$(JNI_INCLUDES)) \
112 $(PKGS_CFLAGS)
3a2f1a4b 113
8eabb4ff 114LIBS = $(PKGS_LIBS)
3a2f1a4b 115
9c15124f
MW
116###--------------------------------------------------------------------------
117### Various other tweaks and overrides.
3a2f1a4b 118
8eabb4ff
MW
119## Hack around https://issues.scala-lang.org/browse/SI-9689
120SCALAFLAGS += -Yno-load-impl-class
121
9c15124f
MW
122###--------------------------------------------------------------------------
123### And now we can start building things.
124
3a2f1a4b
MW
125all::
126.PHONY: all
127
9c15124f
MW
128CLEANFILES =
129clean::
130.PHONY: clean
131
132###--------------------------------------------------------------------------
133### Native C code.
3a2f1a4b 134
2812483e 135out/%.o: %.c
9c15124f
MW
136 $(call v_tag,CC)mkdir -p $(OUTDIR) && \
137 $(CC) -c $(ALL_CFLAGS) -MMD -o$@ $<
138
139ALL_SOURCES =
140DISTFILES += $(ALL_SOURCES)
141
142objects = $(patsubst %.c,$(OUTDIR)%$2,$1)
2812483e
MW
143CLEANFILES += $(OUTDIR)*.o $(OUTDIR)*.d
144
9c15124f
MW
145###--------------------------------------------------------------------------
146### Java classes.
147
148## Java and Scala source files turn into multiple `.class' files with
149## unpredictable names. Rather than try to guess stable outputs for these
150## sources, we make artificial `timestamp' files and uses these in our
151## dependencies.
2812483e 152CLASSDIR = $(OUTDIR)cls/
9c15124f
MW
153stamps = $(patsubst %,$(OUTDIR)%.stamp,$1)
154
155clean::; rm -rf $(CLASSDIR)
156CLEANFILES += $(OUTDIR)*.stamp
157
158## Compiling actual Java code. Note that this confuses the resident Scala
159## compiler, so we have to reset it here.
160CLSISH += java
2812483e 161$(OUTDIR)%.stamp: %.java
2a02a846
MW
162 $(call v_tag,JAVAC)mkdir -p $(CLASSDIR) && \
163 $(JAVAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(JAVAFLAGS) $< && \
164 echo built >$@
9c15124f
MW
165 $(V_AT)$(SCALAC_RESET)
166
167## Compiling Scala code.
168CLSEXT += scala
2812483e 169$(OUTDIR)%.stamp: %.scala
2a02a846
MW
170 $(call v_tag,SCALAC)mkdir -p $(CLASSDIR) && \
171 $(SCALAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(SCALAFLAGS) $< && \
172 echo built >$@
3a2f1a4b 173
9c15124f
MW
174###--------------------------------------------------------------------------
175### Native-code libraries.
176
177SHLIBS += toy
178toy_SOURCES = jni.c
3a2f1a4b 179
9c15124f
MW
180shlibfile = $(patsubst %,$(OUTDIR)lib%.so,$1)
181SHLIBFILES = $(call shlibfile,$(SHLIBS))
182TARGETS += $(SHLIBFILES)
183ALL_SOURCES += $(foreach l,$(SHLIBS),$($l_SOURCES))
184
185$(SHLIBFILES): $(OUTDIR)lib%.so: $$(call objects,$$($$*_SOURCES),.o)
8eabb4ff
MW
186 $(call v_tag,LD)$(LD) $(LDFLAGS.so) -o$@ $^ $(LIBS)
187
9c15124f
MW
188###--------------------------------------------------------------------------
189### Java classes.
190
191## Writing things out longhand is tedious. `CLASSES' is a list of entries of
192## the form `SRC[:DEP,...]', saying that `SRC.ext', being a source file
193## capable of being built into `.class' files and setting `SRC.stamp', should
194## be so built, and that it depends on other similar sources named DEP having
195## been so built.
2812483e 196CLASSES += util
9c15124f
MW
197CLASSES += sys:util
198CLASSES += admin:sys,util
199CLASSES += tar:util
200CLASSES += keys:tar,sys,util
201CLASSES += main:sys
202
203## Machinery for parsing the `CLASSES' list.
204COMMA = ,
205class-name = $(firstword $(subst :, ,$1))
206class-deps = $(subst $(COMMA), ,$(word 2,$(subst :, ,$1)))
207
208CLASS_NAMES = $(foreach c,$(CLASSES),$(call class-name,$c))
209
210all:: $(call stamps,$(CLASS_NAMES))
211
212$(foreach c,$(CLASSES),$(eval $(call stamps,$(call class-name,$c)): \
213 $(call stamps,$(call class-deps,$c))))
214
215DISTFILES += $(foreach c,$(CLASSES),\
216 $(foreach e,$(CLSEXT),\
217 $(wildcard $(call class-name,$c).$e)))
218
219###--------------------------------------------------------------------------
220### Distribution arrangements.
221
222DISTFILES += COPYING
223DISTFILES += Makefile
224DISTFILES += auto-version
225
226distdir = $(PACKAGE)-$(VERSION)
227DISTTAR = $(distdir).tar.gz
228
229distdir:
230 rm -rf $(OUTDIR)$(distdir)
231 mkdir $(OUTDIR)$(distdir)
232 echo $(VERSION) >$(OUTDIR)$(distdir)/RELEASE
233 set -e; for i in $(DISTFILES); do \
234 case $$i in */*) mkdir -p $(OUTDIR)$(distdir)/$${i%/*} ;; esac; \
235 cp $$i $(OUTDIR)$(distdir)/; \
236 done
237.PHONY: distdir
238
239dist: distdir
240 set -e; cd $(OUTDIR); tar chozf ../$(DISTTAR) $(distdir)
241 rm -rf $(distdir)
242.PHONY: dist
243
244###--------------------------------------------------------------------------
245### Finishing touches.
3a2f1a4b
MW
246
247all:: $(TARGETS)
9c15124f
MW
248
249clean::; rm -f $(CLEANFILES) $(TARGETS)
3a2f1a4b 250
2812483e 251repl: $(TARGETS)
9c15124f
MW
252 $(SCALA_REPL) -cp $(CLASSDIR) -Djava.library.path=$(OUTDIR)
253.PHONY: repl
2812483e 254
9c15124f 255-include $(call objects,$(ALL_SOURCES),.d)
3a2f1a4b 256
9c15124f 257###----- That's all, folks --------------------------------------------------