Makefile 11 KB
Newer Older
Richard van der Hoff's avatar
Richard van der Hoff committed
1
2
#!/usr/bin/make -f

3
include common.mk
4
VERSION := $(MAJOR).$(MINOR).$(PATCH)
5
PREFIX ?= /usr/local
6
BUILD_DIR := build
7
8
RELEASE_OPTIMIZE_FLAGS ?= -g -O3
DEBUG_OPTIMIZE_FLAGS ?= -g -O0
9
JS_OPTIMIZE_FLAGS ?= -O3
10
FUZZING_OPTIMIZE_FLAGS ?= -O3
Richard van der Hoff's avatar
Richard van der Hoff committed
11
CC = gcc
12
EMCC = emcc
stoically's avatar
stoically committed
13
EMAR = emar
14
AFL_CC = afl-gcc
15
AFL_CXX = afl-g++
16
AR = ar
17

David Baker's avatar
David Baker committed
18
19
20
21
22
23
24
25
26
27
28
UNAME := $(shell uname)
ifeq ($(UNAME),Darwin)
	SO := dylib
	OLM_LDFLAGS :=
else
	SO := so
	OLM_LDFLAGS := -Wl,-soname,libolm.so.$(MAJOR) \
                       -Wl,--version-script,version_script.ver
endif

RELEASE_TARGET := $(BUILD_DIR)/libolm.$(SO).$(VERSION)
29
STATIC_RELEASE_TARGET := $(BUILD_DIR)/libolm.a
David Baker's avatar
David Baker committed
30
DEBUG_TARGET := $(BUILD_DIR)/libolm_debug.$(SO).$(VERSION)
David Baker's avatar
David Baker committed
31
32
JS_WASM_TARGET := javascript/olm.js
JS_ASMJS_TARGET := javascript/olm_legacy.js
stoically's avatar
stoically committed
33
WASM_TARGET := $(BUILD_DIR)/wasm/libolm.a
34
35

JS_EXPORTED_FUNCTIONS := javascript/exported_functions.json
David Baker's avatar
David Baker committed
36
JS_EXTRA_EXPORTED_RUNTIME_METHODS := ALLOC_STACK
David Baker's avatar
David Baker committed
37
JS_EXTERNS := javascript/externs.js
38

39
PUBLIC_HEADERS := include/olm/olm.h include/olm/outbound_group_session.h include/olm/inbound_group_session.h include/olm/pk.h include/olm/sas.h
Richard van der Hoff's avatar
Richard van der Hoff committed
40

41
42
43
44
45
SOURCES := $(wildcard src/*.cpp) $(wildcard src/*.c) \
    lib/crypto-algorithms/sha256.c \
    lib/crypto-algorithms/aes.c \
    lib/curve25519-donna/curve25519-donna.c

46
FUZZER_SOURCES := $(wildcard fuzzers/fuzz_*.cpp) $(wildcard fuzzers/fuzz_*.c)
Richard van der Hoff's avatar
Richard van der Hoff committed
47
TEST_SOURCES := $(wildcard tests/test_*.cpp) $(wildcard tests/test_*.c)
48
49
50
51
52
53
54
55
56

OBJECTS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))
RELEASE_OBJECTS := $(addprefix $(BUILD_DIR)/release/,$(OBJECTS))
DEBUG_OBJECTS := $(addprefix $(BUILD_DIR)/debug/,$(OBJECTS))
FUZZER_OBJECTS := $(addprefix $(BUILD_DIR)/fuzzers/objects/,$(OBJECTS))
FUZZER_BINARIES := $(addprefix $(BUILD_DIR)/,$(basename $(FUZZER_SOURCES)))
FUZZER_DEBUG_BINARIES := $(patsubst $(BUILD_DIR)/fuzzers/fuzz_%,$(BUILD_DIR)/fuzzers/debug_%,$(FUZZER_BINARIES))
TEST_BINARIES := $(patsubst tests/%,$(BUILD_DIR)/tests/%,$(basename $(TEST_SOURCES)))
JS_OBJECTS := $(addprefix $(BUILD_DIR)/javascript/,$(OBJECTS))
stoically's avatar
stoically committed
57
WASM_OBJECTS := $(addprefix $(BUILD_DIR)/wasm/,$(OBJECTS))
David Baker's avatar
David Baker committed
58
59
60
61

# pre & post are the js-pre/js-post options to emcc.
# They are injected inside the modularised code and
# processed by the optimiser.
62
JS_PRE := $(wildcard javascript/*pre.js)
63
64
JS_POST := javascript/olm_outbound_group_session.js \
    javascript/olm_inbound_group_session.js \
65
    javascript/olm_pk.js \
66
    javascript/olm_sas.js \
67
    javascript/olm_post.js
David Baker's avatar
David Baker committed
68
69
70
71
72
73
74

# The prefix & suffix are just added onto the start & end
# of what comes out emcc, so are outside of the modularised
# code and not seen by the opimiser.
JS_PREFIX := javascript/olm_prefix.js
JS_SUFFIX := javascript/olm_suffix.js

75
DOCS := tracing/README.html \
76
    docs/megolm.html \
77
    docs/olm.html \
78
    docs/signing.html \
79
80
    README.html \
    CHANGELOG.html
Richard van der Hoff's avatar
Richard van der Hoff committed
81

82
83
84
85
CPPFLAGS += -Iinclude -Ilib \
    -DOLMLIB_VERSION_MAJOR=$(MAJOR) -DOLMLIB_VERSION_MINOR=$(MINOR) \
    -DOLMLIB_VERSION_PATCH=$(PATCH)

86
# we rely on <stdint.h>, which was introduced in C99
David Baker's avatar
David Baker committed
87
88
CFLAGS += -Wall -Werror -std=c99
CXXFLAGS += -Wall -Werror -std=c++11
89
LDFLAGS += -Wall -Werror
Richard van der Hoff's avatar
Richard van der Hoff committed
90

David Baker's avatar
David Baker committed
91
92
93
CFLAGS_NATIVE = -fPIC
CXXFLAGS_NATIVE = -fPIC

David Baker's avatar
David Baker committed
94
EMCCFLAGS = --closure 1 --memory-init-file 0 -s NO_FILESYSTEM=1 -s INVOKE_RUN=0 -s MODULARIZE=1
95

David Baker's avatar
David Baker committed
96
97
98
99
100
101
# Olm generally doesn't need a lot of memory to encrypt / decrypt its usual
# payloads (ie. Matrix messages), but we do need about 128K of heap to encrypt
# a 64K event (enough to store the ciphertext and the plaintext, bearing in
# mind that the plaintext can only be 48K because base64). We also have about
# 36K of statics. So let's have 256K of memory.
# (This can't be changed by the app with wasm since it's baked into the wasm).
David Baker's avatar
David Baker committed
102
103
104
# (emscripten also mandates at least 16MB of memory for asm.js now, so
# we don't use this for the legacy build.)
EMCCFLAGS_WASM += -s TOTAL_STACK=65536 -s TOTAL_MEMORY=262144
David Baker's avatar
David Baker committed
105

David Baker's avatar
David Baker committed
106
EMCCFLAGS_ASMJS += -s WASM=0
David Baker's avatar
David Baker committed
107

108
109
110
111
EMCC.c = $(EMCC) $(CFLAGS) $(CPPFLAGS) -c
EMCC.cc = $(EMCC) $(CXXFLAGS) $(CPPFLAGS) -c
EMCC_LINK = $(EMCC) $(LDFLAGS) $(EMCCFLAGS)

112
113
114
115
116
AFL.c = $(AFL_CC) $(CFLAGS) $(CPPFLAGS) -c
AFL.cc = $(AFL_CXX) $(CXXFLAGS) $(CPPFLAGS) -c
AFL_LINK.c = $(AFL_CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS)
AFL_LINK.cc = $(AFL_CXX) $(LDFLAGS) $(CXXFLAGS) $(CPPFLAGS)

Richard van der Hoff's avatar
Richard van der Hoff committed
117
118
119
120
121
# generate .d files when compiling
CPPFLAGS += -MMD

### per-target variables

David Baker's avatar
David Baker committed
122
123
$(RELEASE_OBJECTS): CFLAGS += $(RELEASE_OPTIMIZE_FLAGS) $(CFLAGS_NATIVE)
$(RELEASE_OBJECTS): CXXFLAGS += $(RELEASE_OPTIMIZE_FLAGS) $(CXXFLAGS_NATIVE)
124
125
$(RELEASE_TARGET): LDFLAGS += $(RELEASE_OPTIMIZE_FLAGS)

David Baker's avatar
David Baker committed
126
127
$(DEBUG_OBJECTS): CFLAGS += $(DEBUG_OPTIMIZE_FLAGS) $(CFLAGS_NATIVE)
$(DEBUG_OBJECTS): CXXFLAGS += $(DEBUG_OPTIMIZE_FLAGS) $(CXXFLAGS_NATIVE)
128
$(DEBUG_TARGET): LDFLAGS += $(DEBUG_OPTIMIZE_FLAGS)
Richard van der Hoff's avatar
Richard van der Hoff committed
129
130

$(TEST_BINARIES): CPPFLAGS += -Itests/include
Richard van der Hoff's avatar
Richard van der Hoff committed
131
$(TEST_BINARIES): LDFLAGS += $(DEBUG_OPTIMIZE_FLAGS) -L$(BUILD_DIR)
Richard van der Hoff's avatar
Richard van der Hoff committed
132

133
134
135
136
137
138
139
$(FUZZER_OBJECTS): CFLAGS += $(FUZZER_OPTIMIZE_FLAGS)
$(FUZZER_OBJECTS): CXXFLAGS += $(FUZZER_OPTIMIZE_FLAGS)
$(FUZZER_BINARIES): CPPFLAGS += -Ifuzzers/include
$(FUZZER_BINARIES): LDFLAGS += $(FUZZER_OPTIMIZE_FLAGS) -L$(BUILD_DIR)
$(FUZZER_DEBUG_BINARIES): CPPFLAGS += -Ifuzzers/include
$(FUZZER_DEBUG_BINARIES): LDFLAGS += $(DEBUG_OPTIMIZE_FLAGS)

140
141
$(JS_OBJECTS): CFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_OBJECTS): CXXFLAGS += $(JS_OPTIMIZE_FLAGS)
David Baker's avatar
David Baker committed
142
143
$(JS_WASM_TARGET): LDFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_ASMJS_TARGET): LDFLAGS += $(JS_OPTIMIZE_FLAGS)
144

145
146
147
148
149
150
151
152
153
154
155
156
157
### Fix to make mkdir work on windows and linux
ifeq ($(shell echo "check_quotes"),"check_quotes")
   WINDOWS := yes
else
   WINDOWS := no
endif

ifeq ($(WINDOWS),yes)
   mkdir = mkdir $(subst /,\,$(1)) > nul 2>&1 || (exit 0)
else
   mkdir = mkdir -p $(1)
endif

Richard van der Hoff's avatar
Richard van der Hoff committed
158
159
### top-level targets

160
lib: $(RELEASE_TARGET)
161
162
.PHONY: lib

163
164
$(RELEASE_TARGET): $(RELEASE_OBJECTS)
	$(CXX) $(LDFLAGS) --shared -fPIC \
David Baker's avatar
David Baker committed
165
            $(OLM_LDFLAGS) \
166
            $(OUTPUT_OPTION) $(RELEASE_OBJECTS)
David Baker's avatar
David Baker committed
167
	ln -sf libolm.$(SO).$(VERSION) $(BUILD_DIR)/libolm.$(SO).$(MAJOR)
168
	ln -sf libolm.$(SO).$(VERSION) $(BUILD_DIR)/libolm.$(SO)
169

170
debug: $(DEBUG_TARGET)
171
172
173
.PHONY: debug

$(DEBUG_TARGET): $(DEBUG_OBJECTS)
174
	$(CXX) $(LDFLAGS) --shared -fPIC \
David Baker's avatar
David Baker committed
175
            $(OLM_LDFLAGS) \
176
            $(OUTPUT_OPTION) $(DEBUG_OBJECTS)
David Baker's avatar
David Baker committed
177
	ln -sf libolm_debug.$(SO).$(VERSION) $(BUILD_DIR)/libolm_debug.$(SO).$(MAJOR)
178

179
180
181
182
static: $(STATIC_RELEASE_TARGET)
.PHONY: static

$(STATIC_RELEASE_TARGET): $(RELEASE_OBJECTS)
183
	$(AR) rcs $@ $^
184

David Baker's avatar
David Baker committed
185
js: $(JS_WASM_TARGET) $(JS_ASMJS_TARGET)
186
187
.PHONY: js

stoically's avatar
stoically committed
188
189
190
191
192
193
wasm: $(WASM_TARGET)
.PHONY: wasm

$(WASM_TARGET): $(WASM_OBJECTS)
	$(EMAR) rcs $@ $^

David Baker's avatar
David Baker committed
194
195
196
# Note that the output file we give to emcc determines the name of the
# wasm file baked into the js, hence messing around outputting to olm.js
# and then renaming it.
David Baker's avatar
David Baker committed
197
$(JS_WASM_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS) $(JS_PREFIX) $(JS_SUFFIX)
David Baker's avatar
David Baker committed
198
	EMCC_CLOSURE_ARGS="--externs $(JS_EXTERNS)" $(EMCC_LINK) \
David Baker's avatar
David Baker committed
199
200
201
202
203
204
205
206
207
208
209
	       $(EMCCFLAGS_WASM) \
               $(foreach f,$(JS_PRE),--pre-js $(f)) \
               $(foreach f,$(JS_POST),--post-js $(f)) \
               -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
               -s "EXTRA_EXPORTED_RUNTIME_METHODS=$(JS_EXTRA_EXPORTED_RUNTIME_METHODS)" \
               $(JS_OBJECTS) -o $@
	       mv $@ javascript/olmtmp.js
	       cat $(JS_PREFIX) javascript/olmtmp.js $(JS_SUFFIX) > $@
	       rm javascript/olmtmp.js

$(JS_ASMJS_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS) $(JS_PREFIX) $(JS_SUFFIX)
David Baker's avatar
David Baker committed
210
	EMCC_CLOSURE_ARGS="--externs $(JS_EXTERNS)" $(EMCC_LINK) \
David Baker's avatar
David Baker committed
211
	       $(EMCCFLAGS_ASMJS) \
212
213
               $(foreach f,$(JS_PRE),--pre-js $(f)) \
               $(foreach f,$(JS_POST),--post-js $(f)) \
214
               -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
David Baker's avatar
David Baker committed
215
               -s "EXTRA_EXPORTED_RUNTIME_METHODS=$(JS_EXTRA_EXPORTED_RUNTIME_METHODS)" \
216
               $(JS_OBJECTS) -o $@
David Baker's avatar
David Baker committed
217
218
219
	       mv $@ javascript/olmtmp.js
	       cat $(JS_PREFIX) javascript/olmtmp.js $(JS_SUFFIX) > $@
	       rm javascript/olmtmp.js
220

Richard van der Hoff's avatar
Richard van der Hoff committed
221
222
223
224
225
build_tests: $(TEST_BINARIES)

test: build_tests
	for i in $(TEST_BINARIES); do \
	    echo $$i; \
226
	    $$i || exit $$?; \
Richard van der Hoff's avatar
Richard van der Hoff committed
227
228
	done

229
230
231
fuzzers: $(FUZZER_BINARIES) $(FUZZER_DEBUG_BINARIES)
.PHONY: fuzzers

232
$(JS_EXPORTED_FUNCTIONS): $(PUBLIC_HEADERS)
233
	./exports.py $^ > $@.tmp
234
235
	mv $@.tmp $@

236
all: test js lib debug doc
237
238
.PHONY: all

239
install-headers: $(PUBLIC_HEADERS)
240
	test -d $(DESTDIR)$(PREFIX)/include/olm || $(call mkdir,$(DESTDIR)$(PREFIX)/include/olm)
241
	install $(PUBLIC_HEADERS) $(DESTDIR)$(PREFIX)/include/olm/
242
.PHONY: install-headers
243
244

install-debug: debug install-headers
245
	test -d $(DESTDIR)$(PREFIX)/lib || $(call mkdir,$(DESTDIR)$(PREFIX)/lib)
246
	install $(DEBUG_TARGET) $(DESTDIR)$(PREFIX)/lib/libolm_debug.$(SO).$(VERSION)
247
248
	ln -sf libolm_debug.$(SO).$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm_debug.$(SO).$(MAJOR)
	ln -sf libolm_debug.$(SO).$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm_debug.$(SO)
249
250
.PHONY: install-debug

251
install: lib install-headers
252
	test -d $(DESTDIR)$(PREFIX)/lib || $(call mkdir,$(DESTDIR)$(PREFIX)/lib)
253
	install $(RELEASE_TARGET) $(DESTDIR)$(PREFIX)/lib/libolm.$(SO).$(VERSION)
254
255
	ln -sf libolm.$(SO).$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm.$(SO).$(MAJOR)
	ln -sf libolm.$(SO).$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm.$(SO)
256
257
.PHONY: install

258
clean:;
259
	rm -rf $(BUILD_DIR) $(DOCS)
260
.PHONY: clean
261

262
263
264
doc: $(DOCS)
.PHONY: doc

Richard van der Hoff's avatar
Richard van der Hoff committed
265
### rules for building objects
266
$(BUILD_DIR)/release/%.o: %.c
267
	$(call mkdir,$(dir $@))
268
269
	$(COMPILE.c) $(OUTPUT_OPTION) $<

270
$(BUILD_DIR)/release/%.o: %.cpp
271
	$(call mkdir,$(dir $@))
272
273
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

274
$(BUILD_DIR)/debug/%.o: %.c
275
	$(call mkdir,$(dir $@))
Richard van der Hoff's avatar
Richard van der Hoff committed
276
277
	$(COMPILE.c) $(OUTPUT_OPTION) $<

278
$(BUILD_DIR)/debug/%.o: %.cpp
279
	$(call mkdir,$(dir $@))
Richard van der Hoff's avatar
Richard van der Hoff committed
280
281
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

282
$(BUILD_DIR)/javascript/%.o: %.c
283
	$(call mkdir,$(dir $@))
284
285
	$(EMCC.c) $(OUTPUT_OPTION) $<

286
$(BUILD_DIR)/javascript/%.o: %.cpp
287
	$(call mkdir,$(dir $@))
288
289
	$(EMCC.cc) $(OUTPUT_OPTION) $<

stoically's avatar
stoically committed
290
291
292
293
294
295
296
297
$(BUILD_DIR)/wasm/%.o: %.c
	$(call mkdir,$(dir $@))
	$(EMCC.c) $(OUTPUT_OPTION) $<

$(BUILD_DIR)/wasm/%.o: %.cpp
	$(call mkdir,$(dir $@))
	$(EMCC.cc) $(OUTPUT_OPTION) $<

298
$(BUILD_DIR)/tests/%: tests/%.c $(DEBUG_OBJECTS)
299
	$(call mkdir,$(dir $@))
300
	$(LINK.c) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
301

302
$(BUILD_DIR)/tests/%: tests/%.cpp $(DEBUG_OBJECTS)
303
	$(call mkdir,$(dir $@))
304
	$(LINK.cc) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
305

306
$(BUILD_DIR)/fuzzers/objects/%.o: %.c
307
	$(call mkdir,$(dir $@))
308
309
	$(AFL.c) $(OUTPUT_OPTION) $<

310
$(BUILD_DIR)/fuzzers/objects/%.o: %.cpp
311
	$(call mkdir,$(dir $@))
312
313
314
315
316
317
318
319
320
321
322
323
324
	$(AFL.cc) $(OUTPUT_OPTION) $<

$(BUILD_DIR)/fuzzers/fuzz_%: fuzzers/fuzz_%.c $(FUZZER_OBJECTS)
	$(AFL_LINK.c) $< $(FUZZER_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@

$(BUILD_DIR)/fuzzers/fuzz_%: fuzzers/fuzz_%.cpp $(FUZZER_OBJECTS)
	$(AFL_LINK.cc) $< $(FUZZER_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@

$(BUILD_DIR)/fuzzers/debug_%: fuzzers/fuzz_%.c $(DEBUG_OBJECTS)
	$(LINK.c) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@

$(BUILD_DIR)/fuzzers/debug_%: fuzzers/fuzz_%.cpp $(DEBUG_OBJECTS)
	$(LINK.cc) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
325

326
327
328
%.html: %.rst
	rst2html $< $@

Richard van der Hoff's avatar
Richard van der Hoff committed
329
330
### dependencies

331
332
-include $(RELEASE_OBJECTS:.o=.d)
-include $(DEBUG_OBJECTS:.o=.d)
333
-include $(JS_OBJECTS:.o=.d)
Richard van der Hoff's avatar
Richard van der Hoff committed
334
-include $(TEST_BINARIES:=.d)
335
336
337
-include $(FUZZER_OBJECTS:.o=.d)
-include $(FUZZER_BINARIES:=.d)
-include $(FUZZER_DEBUG_BINARIES:=.d)