Makefile 10.6 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
13
AFL_CC = afl-gcc
14
AFL_CXX = afl-g++
15
AR = ar
16

David Baker's avatar
David Baker committed
17
18
19
20
21
22
23
24
25
26
27
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)
28
STATIC_RELEASE_TARGET := $(BUILD_DIR)/libolm.a
David Baker's avatar
David Baker committed
29
DEBUG_TARGET := $(BUILD_DIR)/libolm_debug.$(SO).$(VERSION)
David Baker's avatar
David Baker committed
30
31
JS_WASM_TARGET := javascript/olm.js
JS_ASMJS_TARGET := javascript/olm_legacy.js
32
33

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

37
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
38

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

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

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))
David Baker's avatar
David Baker committed
55
56
57
58

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

# 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

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

79
80
81
82
CPPFLAGS += -Iinclude -Ilib \
    -DOLMLIB_VERSION_MAJOR=$(MAJOR) -DOLMLIB_VERSION_MINOR=$(MINOR) \
    -DOLMLIB_VERSION_PATCH=$(PATCH)

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

David Baker's avatar
David Baker committed
88
89
90
CFLAGS_NATIVE = -fPIC
CXXFLAGS_NATIVE = -fPIC

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

David Baker's avatar
David Baker committed
93
94
95
96
97
98
# 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
99
100
101
# (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
102

David Baker's avatar
David Baker committed
103
EMCCFLAGS_ASMJS += -s WASM=0
David Baker's avatar
David Baker committed
104

105
106
107
108
EMCC.c = $(EMCC) $(CFLAGS) $(CPPFLAGS) -c
EMCC.cc = $(EMCC) $(CXXFLAGS) $(CPPFLAGS) -c
EMCC_LINK = $(EMCC) $(LDFLAGS) $(EMCCFLAGS)

109
110
111
112
113
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
114
115
116
117
118
# generate .d files when compiling
CPPFLAGS += -MMD

### per-target variables

David Baker's avatar
David Baker committed
119
120
$(RELEASE_OBJECTS): CFLAGS += $(RELEASE_OPTIMIZE_FLAGS) $(CFLAGS_NATIVE)
$(RELEASE_OBJECTS): CXXFLAGS += $(RELEASE_OPTIMIZE_FLAGS) $(CXXFLAGS_NATIVE)
121
122
$(RELEASE_TARGET): LDFLAGS += $(RELEASE_OPTIMIZE_FLAGS)

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

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

130
131
132
133
134
135
136
$(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)

137
138
$(JS_OBJECTS): CFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_OBJECTS): CXXFLAGS += $(JS_OPTIMIZE_FLAGS)
David Baker's avatar
David Baker committed
139
140
$(JS_WASM_TARGET): LDFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_ASMJS_TARGET): LDFLAGS += $(JS_OPTIMIZE_FLAGS)
141

142
143
144
145
146
147
148
149
150
151
152
153
154
### 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
155
156
### top-level targets

157
lib: $(RELEASE_TARGET)
158
159
.PHONY: lib

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

167
debug: $(DEBUG_TARGET)
168
169
170
.PHONY: debug

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

176
177
178
179
static: $(STATIC_RELEASE_TARGET)
.PHONY: static

$(STATIC_RELEASE_TARGET): $(RELEASE_OBJECTS)
180
	$(AR) rcs $@ $^
181

David Baker's avatar
David Baker committed
182
js: $(JS_WASM_TARGET) $(JS_ASMJS_TARGET)
183
184
.PHONY: js

David Baker's avatar
David Baker committed
185
186
187
# 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
188
$(JS_WASM_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS) $(JS_PREFIX) $(JS_SUFFIX)
David Baker's avatar
David Baker committed
189
	EMCC_CLOSURE_ARGS="--externs $(JS_EXTERNS)" $(EMCC_LINK) \
David Baker's avatar
David Baker committed
190
191
192
193
194
195
196
197
198
199
200
	       $(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
201
	EMCC_CLOSURE_ARGS="--externs $(JS_EXTERNS)" $(EMCC_LINK) \
David Baker's avatar
David Baker committed
202
	       $(EMCCFLAGS_ASMJS) \
203
204
               $(foreach f,$(JS_PRE),--pre-js $(f)) \
               $(foreach f,$(JS_POST),--post-js $(f)) \
205
               -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
David Baker's avatar
David Baker committed
206
               -s "EXTRA_EXPORTED_RUNTIME_METHODS=$(JS_EXTRA_EXPORTED_RUNTIME_METHODS)" \
207
               $(JS_OBJECTS) -o $@
David Baker's avatar
David Baker committed
208
209
210
	       mv $@ javascript/olmtmp.js
	       cat $(JS_PREFIX) javascript/olmtmp.js $(JS_SUFFIX) > $@
	       rm javascript/olmtmp.js
211

Richard van der Hoff's avatar
Richard van der Hoff committed
212
213
214
215
216
build_tests: $(TEST_BINARIES)

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

220
221
222
fuzzers: $(FUZZER_BINARIES) $(FUZZER_DEBUG_BINARIES)
.PHONY: fuzzers

223
$(JS_EXPORTED_FUNCTIONS): $(PUBLIC_HEADERS)
224
	./exports.py $^ > $@.tmp
225
226
	mv $@.tmp $@

227
all: test js lib debug doc
228
229
.PHONY: all

230
install-headers: $(PUBLIC_HEADERS)
231
	test -d $(DESTDIR)$(PREFIX)/include/olm || $(call mkdir,$(DESTDIR)$(PREFIX)/include/olm)
232
	install -Dm644 $(PUBLIC_HEADERS) $(DESTDIR)$(PREFIX)/include/olm/
233
.PHONY: install-headers
234
235

install-debug: debug install-headers
236
	test -d $(DESTDIR)$(PREFIX)/lib || $(call mkdir,$(DESTDIR)$(PREFIX)/lib)
David Baker's avatar
David Baker committed
237
	install -Dm755 $(DEBUG_TARGET) $(DESTDIR)$(PREFIX)/lib/libolm_debug.$(SO).$(VERSION)
238
239
	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)
240
241
.PHONY: install-debug

242
install: lib install-headers
243
	test -d $(DESTDIR)$(PREFIX)/lib || $(call mkdir,$(DESTDIR)$(PREFIX)/lib)
David Baker's avatar
David Baker committed
244
	install -Dm755 $(RELEASE_TARGET) $(DESTDIR)$(PREFIX)/lib/libolm.$(SO).$(VERSION)
245
246
	ln -sf libolm.$(SO).$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm.$(SO).$(MAJOR)
	ln -sf libolm.$(SO).$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm.$(SO)
247
248
.PHONY: install

249
clean:;
250
	rm -rf $(BUILD_DIR) $(DOCS)
251
.PHONY: clean
252

253
254
255
doc: $(DOCS)
.PHONY: doc

Richard van der Hoff's avatar
Richard van der Hoff committed
256
### rules for building objects
257
$(BUILD_DIR)/release/%.o: %.c
258
	$(call mkdir,$(dir $@))
259
260
	$(COMPILE.c) $(OUTPUT_OPTION) $<

261
$(BUILD_DIR)/release/%.o: %.cpp
262
	$(call mkdir,$(dir $@))
263
264
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

265
$(BUILD_DIR)/debug/%.o: %.c
266
	$(call mkdir,$(dir $@))
Richard van der Hoff's avatar
Richard van der Hoff committed
267
268
	$(COMPILE.c) $(OUTPUT_OPTION) $<

269
$(BUILD_DIR)/debug/%.o: %.cpp
270
	$(call mkdir,$(dir $@))
Richard van der Hoff's avatar
Richard van der Hoff committed
271
272
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

273
$(BUILD_DIR)/javascript/%.o: %.c
274
	$(call mkdir,$(dir $@))
275
276
	$(EMCC.c) $(OUTPUT_OPTION) $<

277
$(BUILD_DIR)/javascript/%.o: %.cpp
278
	$(call mkdir,$(dir $@))
279
280
	$(EMCC.cc) $(OUTPUT_OPTION) $<

281
$(BUILD_DIR)/tests/%: tests/%.c $(DEBUG_OBJECTS)
282
	$(call mkdir,$(dir $@))
283
	$(LINK.c) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
284

285
$(BUILD_DIR)/tests/%: tests/%.cpp $(DEBUG_OBJECTS)
286
	$(call mkdir,$(dir $@))
287
	$(LINK.cc) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
288

289
$(BUILD_DIR)/fuzzers/objects/%.o: %.c
290
	$(call mkdir,$(dir $@))
291
292
	$(AFL.c) $(OUTPUT_OPTION) $<

293
$(BUILD_DIR)/fuzzers/objects/%.o: %.cpp
294
	$(call mkdir,$(dir $@))
295
296
297
298
299
300
301
302
303
304
305
306
307
	$(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
308

309
310
311
%.html: %.rst
	rst2html $< $@

Richard van der Hoff's avatar
Richard van der Hoff committed
312
313
### dependencies

314
315
-include $(RELEASE_OBJECTS:.o=.d)
-include $(DEBUG_OBJECTS:.o=.d)
316
-include $(JS_OBJECTS:.o=.d)
Richard van der Hoff's avatar
Richard van der Hoff committed
317
-include $(TEST_BINARIES:=.d)
318
319
320
-include $(FUZZER_OBJECTS:.o=.d)
-include $(FUZZER_BINARIES:=.d)
-include $(FUZZER_DEBUG_BINARIES:=.d)