chiark / gitweb /
Makefile: Make the Scala compiler more petty.
[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
5c2b1515
MW
85SCALAFLAGS = -optimise -feature -deprecation -Xfatal-warnings \
86 -Xlint -Xlint:-package-object-classes \
9c15124f
MW
87 -Yinline-warnings:false
88SCALA_REPL = $(NOIP) scala $(JAVADEFS)
89
90## Silent-rules is on by default.
3a2f1a4b 91V = 0
9c15124f
MW
92
93## Allow local overrides at this point.
94-include local.mk
95
96###--------------------------------------------------------------------------
97### Silent-rules machinery.
98
3a2f1a4b
MW
99v_tag = $(call v_tag_$V,$1)
100v_tag_0 = @printf " %-8s %s\n" "$1" "$@";
9c15124f 101
3a2f1a4b
MW
102V_AT = $(V_AT_$V)
103V_AT_0 = @
104
9c15124f
MW
105###--------------------------------------------------------------------------
106### External native packages.
3a2f1a4b 107
8eabb4ff
MW
108PKGS_CFLAGS := $(foreach p,$(PKGS),$(shell pkg-config --cflags $p))
109PKGS_LIBS := $(foreach p,$(PKGS),$(shell pkg-config --libs $p))
110
9c15124f
MW
111ALL_CFLAGS = $(CFLAGS) -fPIC \
112 $(addprefix -I,$(JNI_INCLUDES)) \
113 $(PKGS_CFLAGS)
3a2f1a4b 114
8eabb4ff 115LIBS = $(PKGS_LIBS)
3a2f1a4b 116
9c15124f
MW
117###--------------------------------------------------------------------------
118### Various other tweaks and overrides.
3a2f1a4b 119
8eabb4ff
MW
120## Hack around https://issues.scala-lang.org/browse/SI-9689
121SCALAFLAGS += -Yno-load-impl-class
122
9c15124f
MW
123###--------------------------------------------------------------------------
124### And now we can start building things.
125
3a2f1a4b
MW
126all::
127.PHONY: all
128
9c15124f
MW
129CLEANFILES =
130clean::
131.PHONY: clean
132
133###--------------------------------------------------------------------------
134### Native C code.
3a2f1a4b 135
2812483e 136out/%.o: %.c
9c15124f
MW
137 $(call v_tag,CC)mkdir -p $(OUTDIR) && \
138 $(CC) -c $(ALL_CFLAGS) -MMD -o$@ $<
139
140ALL_SOURCES =
141DISTFILES += $(ALL_SOURCES)
142
143objects = $(patsubst %.c,$(OUTDIR)%$2,$1)
2812483e
MW
144CLEANFILES += $(OUTDIR)*.o $(OUTDIR)*.d
145
9c15124f
MW
146###--------------------------------------------------------------------------
147### Java classes.
148
149## Java and Scala source files turn into multiple `.class' files with
150## unpredictable names. Rather than try to guess stable outputs for these
151## sources, we make artificial `timestamp' files and uses these in our
152## dependencies.
2812483e 153CLASSDIR = $(OUTDIR)cls/
9c15124f
MW
154stamps = $(patsubst %,$(OUTDIR)%.stamp,$1)
155
156clean::; rm -rf $(CLASSDIR)
157CLEANFILES += $(OUTDIR)*.stamp
158
159## Compiling actual Java code. Note that this confuses the resident Scala
160## compiler, so we have to reset it here.
161CLSISH += java
2812483e 162$(OUTDIR)%.stamp: %.java
2a02a846
MW
163 $(call v_tag,JAVAC)mkdir -p $(CLASSDIR) && \
164 $(JAVAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(JAVAFLAGS) $< && \
165 echo built >$@
9c15124f
MW
166 $(V_AT)$(SCALAC_RESET)
167
168## Compiling Scala code.
169CLSEXT += scala
2812483e 170$(OUTDIR)%.stamp: %.scala
2a02a846
MW
171 $(call v_tag,SCALAC)mkdir -p $(CLASSDIR) && \
172 $(SCALAC) -d $(CLASSDIR) -cp $(CLASSDIR) $(SCALAFLAGS) $< && \
173 echo built >$@
3a2f1a4b 174
9c15124f
MW
175###--------------------------------------------------------------------------
176### Native-code libraries.
177
178SHLIBS += toy
179toy_SOURCES = jni.c
3a2f1a4b 180
9c15124f
MW
181shlibfile = $(patsubst %,$(OUTDIR)lib%.so,$1)
182SHLIBFILES = $(call shlibfile,$(SHLIBS))
183TARGETS += $(SHLIBFILES)
184ALL_SOURCES += $(foreach l,$(SHLIBS),$($l_SOURCES))
185
186$(SHLIBFILES): $(OUTDIR)lib%.so: $$(call objects,$$($$*_SOURCES),.o)
8eabb4ff
MW
187 $(call v_tag,LD)$(LD) $(LDFLAGS.so) -o$@ $^ $(LIBS)
188
9c15124f
MW
189###--------------------------------------------------------------------------
190### Java classes.
191
192## Writing things out longhand is tedious. `CLASSES' is a list of entries of
193## the form `SRC[:DEP,...]', saying that `SRC.ext', being a source file
194## capable of being built into `.class' files and setting `SRC.stamp', should
195## be so built, and that it depends on other similar sources named DEP having
196## been so built.
2812483e 197CLASSES += util
9c15124f
MW
198CLASSES += sys:util
199CLASSES += admin:sys,util
200CLASSES += tar:util
201CLASSES += keys:tar,sys,util
202CLASSES += main:sys
203
204## Machinery for parsing the `CLASSES' list.
205COMMA = ,
206class-name = $(firstword $(subst :, ,$1))
207class-deps = $(subst $(COMMA), ,$(word 2,$(subst :, ,$1)))
208
209CLASS_NAMES = $(foreach c,$(CLASSES),$(call class-name,$c))
210
211all:: $(call stamps,$(CLASS_NAMES))
212
213$(foreach c,$(CLASSES),$(eval $(call stamps,$(call class-name,$c)): \
214 $(call stamps,$(call class-deps,$c))))
215
216DISTFILES += $(foreach c,$(CLASSES),\
217 $(foreach e,$(CLSEXT),\
218 $(wildcard $(call class-name,$c).$e)))
219
220###--------------------------------------------------------------------------
221### Distribution arrangements.
222
223DISTFILES += COPYING
224DISTFILES += Makefile
225DISTFILES += auto-version
226
227distdir = $(PACKAGE)-$(VERSION)
228DISTTAR = $(distdir).tar.gz
229
230distdir:
231 rm -rf $(OUTDIR)$(distdir)
232 mkdir $(OUTDIR)$(distdir)
233 echo $(VERSION) >$(OUTDIR)$(distdir)/RELEASE
234 set -e; for i in $(DISTFILES); do \
235 case $$i in */*) mkdir -p $(OUTDIR)$(distdir)/$${i%/*} ;; esac; \
236 cp $$i $(OUTDIR)$(distdir)/; \
237 done
238.PHONY: distdir
239
240dist: distdir
241 set -e; cd $(OUTDIR); tar chozf ../$(DISTTAR) $(distdir)
242 rm -rf $(distdir)
243.PHONY: dist
244
245###--------------------------------------------------------------------------
246### Finishing touches.
3a2f1a4b
MW
247
248all:: $(TARGETS)
9c15124f
MW
249
250clean::; rm -f $(CLEANFILES) $(TARGETS)
3a2f1a4b 251
2812483e 252repl: $(TARGETS)
9c15124f
MW
253 $(SCALA_REPL) -cp $(CLASSDIR) -Djava.library.path=$(OUTDIR)
254.PHONY: repl
2812483e 255
9c15124f 256-include $(call objects,$(ALL_SOURCES),.d)
3a2f1a4b 257
9c15124f 258###----- That's all, folks --------------------------------------------------