Makefile 8.44 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

17
RELEASE_TARGET := $(BUILD_DIR)/libolm.so.$(VERSION)
18
STATIC_RELEASE_TARGET := $(BUILD_DIR)/libolm.a
19
DEBUG_TARGET := $(BUILD_DIR)/libolm_debug.so.$(VERSION)
20
21
22
JS_TARGET := javascript/olm.js

JS_EXPORTED_FUNCTIONS := javascript/exported_functions.json
David Baker's avatar
David Baker committed
23
JS_EXTRA_EXPORTED_RUNTIME_METHODS := ALLOC_STACK
David Baker's avatar
David Baker committed
24
JS_EXTERNS := javascript/externs.js
25

26
PUBLIC_HEADERS := include/olm/olm.h include/olm/outbound_group_session.h include/olm/inbound_group_session.h include/olm/pk.h
Richard van der Hoff's avatar
Richard van der Hoff committed
27

28
29
30
31
32
SOURCES := $(wildcard src/*.cpp) $(wildcard src/*.c) \
    lib/crypto-algorithms/sha256.c \
    lib/crypto-algorithms/aes.c \
    lib/curve25519-donna/curve25519-donna.c

33
FUZZER_SOURCES := $(wildcard fuzzers/fuzz_*.cpp) $(wildcard fuzzers/fuzz_*.c)
Richard van der Hoff's avatar
Richard van der Hoff committed
34
TEST_SOURCES := $(wildcard tests/test_*.cpp) $(wildcard tests/test_*.c)
35
36
37
38
39
40
41
42
43

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))
44
JS_PRE := $(wildcard javascript/*pre.js)
45
46
JS_POST := javascript/olm_outbound_group_session.js \
    javascript/olm_inbound_group_session.js \
47
    javascript/olm_pk.js \
48
    javascript/olm_post.js
49
DOCS := tracing/README.html \
50
    docs/megolm.html \
51
    docs/olm.html \
52
    docs/signing.html \
53
54
    README.html \
    CHANGELOG.html
Richard van der Hoff's avatar
Richard van der Hoff committed
55

56
57
58
59
CPPFLAGS += -Iinclude -Ilib \
    -DOLMLIB_VERSION_MAJOR=$(MAJOR) -DOLMLIB_VERSION_MINOR=$(MINOR) \
    -DOLMLIB_VERSION_PATCH=$(PATCH)

60
61
# we rely on <stdint.h>, which was introduced in C99
CFLAGS += -Wall -Werror -std=c99 -fPIC
62
63
CXXFLAGS += -Wall -Werror -std=c++11 -fPIC
LDFLAGS += -Wall -Werror
Richard van der Hoff's avatar
Richard van der Hoff committed
64

David Baker's avatar
David Baker committed
65
EMCCFLAGS = --closure 1 --memory-init-file 0 -s NO_FILESYSTEM=1 -s INVOKE_RUN=0 -s MODULARIZE=1
66
67
68
69
70
71
72
73
# NO_BROWSER is kept for compatibility with emscripten 1.35.24, but is no
# longer needed.
EMCCFLAGS += -s NO_BROWSER=1

EMCC.c = $(EMCC) $(CFLAGS) $(CPPFLAGS) -c
EMCC.cc = $(EMCC) $(CXXFLAGS) $(CPPFLAGS) -c
EMCC_LINK = $(EMCC) $(LDFLAGS) $(EMCCFLAGS)

74
75
76
77
78
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
79
80
81
82
83
# generate .d files when compiling
CPPFLAGS += -MMD

### per-target variables

84
85
86
87
88
89
90
$(RELEASE_OBJECTS): CFLAGS += $(RELEASE_OPTIMIZE_FLAGS)
$(RELEASE_OBJECTS): CXXFLAGS += $(RELEASE_OPTIMIZE_FLAGS)
$(RELEASE_TARGET): LDFLAGS += $(RELEASE_OPTIMIZE_FLAGS)

$(DEBUG_OBJECTS): CFLAGS += $(DEBUG_OPTIMIZE_FLAGS)
$(DEBUG_OBJECTS): CXXFLAGS += $(DEBUG_OPTIMIZE_FLAGS)
$(DEBUG_TARGET): LDFLAGS += $(DEBUG_OPTIMIZE_FLAGS)
Richard van der Hoff's avatar
Richard van der Hoff committed
91
92

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

95
96
97
98
99
100
101
$(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)

102
103
104
105
$(JS_OBJECTS): CFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_OBJECTS): CXXFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_TARGET): LDFLAGS += $(JS_OPTIMIZE_FLAGS)

106
107
108
109
110
111
112
113
114
115
116
117
118
### 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
119
120
### top-level targets

121
lib: $(RELEASE_TARGET)
122
123
.PHONY: lib

124
125
$(RELEASE_TARGET): $(RELEASE_OBJECTS)
	$(CXX) $(LDFLAGS) --shared -fPIC \
126
            -Wl,-soname,libolm.so.$(MAJOR) \
127
128
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(RELEASE_OBJECTS)
129
	ln -sf libolm.so.$(VERSION) $(BUILD_DIR)/libolm.so.$(MAJOR)
130

131
debug: $(DEBUG_TARGET)
132
133
134
.PHONY: debug

$(DEBUG_TARGET): $(DEBUG_OBJECTS)
135
	$(CXX) $(LDFLAGS) --shared -fPIC \
136
            -Wl,-soname,libolm_debug.so.$(MAJOR) \
137
138
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(DEBUG_OBJECTS)
139
	ln -sf libolm_debug.so.$(VERSION) $(BUILD_DIR)/libolm_debug.so.$(MAJOR)
140

141
142
143
144
static: $(STATIC_RELEASE_TARGET)
.PHONY: static

$(STATIC_RELEASE_TARGET): $(RELEASE_OBJECTS)
145
	$(AR) rcs $@ $^
146

147
148
149
150
js: $(JS_TARGET)
.PHONY: js

$(JS_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS)
David Baker's avatar
David Baker committed
151
	EMCC_CLOSURE_ARGS="--externs $(JS_EXTERNS)" $(EMCC_LINK) \
152
153
               $(foreach f,$(JS_PRE),--pre-js $(f)) \
               $(foreach f,$(JS_POST),--post-js $(f)) \
154
               -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
David Baker's avatar
David Baker committed
155
               -s "EXTRA_EXPORTED_RUNTIME_METHODS=$(JS_EXTRA_EXPORTED_RUNTIME_METHODS)" \
156
157
               $(JS_OBJECTS) -o $@

Richard van der Hoff's avatar
Richard van der Hoff committed
158
159
160
161
162
build_tests: $(TEST_BINARIES)

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

166
167
168
fuzzers: $(FUZZER_BINARIES) $(FUZZER_DEBUG_BINARIES)
.PHONY: fuzzers

169
$(JS_EXPORTED_FUNCTIONS): $(PUBLIC_HEADERS)
170
	perl -MJSON -ne '$$f{"_$$1"}=1 if /(olm_[^( ]*)\(/; END { @f=sort keys %f; print encode_json \@f }' $^ > $@.tmp
171
172
	mv $@.tmp $@

173
all: test js lib debug doc
174
175
.PHONY: all

176
install-headers: $(PUBLIC_HEADERS)
177
	test -d $(DESTDIR)$(PREFIX)/include/olm || $(call mkdir,$(DESTDIR)$(PREFIX)/include/olm)
178
	install -Dm644 $(PUBLIC_HEADERS) $(DESTDIR)$(PREFIX)/include/olm/
179
.PHONY: install-headers
180
181

install-debug: debug install-headers
182
	test -d $(DESTDIR)$(PREFIX)/lib || $(call mkdir,$(DESTDIR)$(PREFIX)/lib)
183
184
185
186
187
	install -Dm755 $(DEBUG_TARGET) $(DESTDIR)$(PREFIX)/lib/libolm_debug.so.$(VERSION)
	ln -s libolm_debug.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm_debug.so.$(MAJOR)
	ln -s libolm_debug.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm_debug.so
.PHONY: install-debug

188
install: lib install-headers
189
	test -d $(DESTDIR)$(PREFIX)/lib || $(call mkdir,$(DESTDIR)$(PREFIX)/lib)
190
191
192
193
194
	install -Dm755 $(RELEASE_TARGET) $(DESTDIR)$(PREFIX)/lib/libolm.so.$(VERSION)
	ln -s libolm.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm.so.$(MAJOR)
	ln -s libolm.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libolm.so
.PHONY: install

195
clean:;
196
	rm -rf $(BUILD_DIR) $(DOCS)
197
.PHONY: clean
198

199
200
201
doc: $(DOCS)
.PHONY: doc

Richard van der Hoff's avatar
Richard van der Hoff committed
202
### rules for building objects
203
$(BUILD_DIR)/release/%.o: %.c
204
	$(call mkdir,$(dir $@))
205
206
	$(COMPILE.c) $(OUTPUT_OPTION) $<

207
$(BUILD_DIR)/release/%.o: %.cpp
208
	$(call mkdir,$(dir $@))
209
210
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

211
$(BUILD_DIR)/debug/%.o: %.c
212
	$(call mkdir,$(dir $@))
Richard van der Hoff's avatar
Richard van der Hoff committed
213
214
	$(COMPILE.c) $(OUTPUT_OPTION) $<

215
$(BUILD_DIR)/debug/%.o: %.cpp
216
	$(call mkdir,$(dir $@))
Richard van der Hoff's avatar
Richard van der Hoff committed
217
218
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

219
$(BUILD_DIR)/javascript/%.o: %.c
220
	$(call mkdir,$(dir $@))
221
222
	$(EMCC.c) $(OUTPUT_OPTION) $<

223
$(BUILD_DIR)/javascript/%.o: %.cpp
224
	$(call mkdir,$(dir $@))
225
226
	$(EMCC.cc) $(OUTPUT_OPTION) $<

227
$(BUILD_DIR)/tests/%: tests/%.c $(DEBUG_OBJECTS)
228
	$(call mkdir,$(dir $@))
229
	$(LINK.c) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
230

231
$(BUILD_DIR)/tests/%: tests/%.cpp $(DEBUG_OBJECTS)
232
	$(call mkdir,$(dir $@))
233
	$(LINK.cc) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
234

235
$(BUILD_DIR)/fuzzers/objects/%.o: %.c
236
	$(call mkdir,$(dir $@))
237
238
	$(AFL.c) $(OUTPUT_OPTION) $<

239
$(BUILD_DIR)/fuzzers/objects/%.o: %.cpp
240
	$(call mkdir,$(dir $@))
241
242
243
244
245
246
247
248
249
250
251
252
253
	$(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
254

255
256
257
%.html: %.rst
	rst2html $< $@

Richard van der Hoff's avatar
Richard van der Hoff committed
258
259
### dependencies

260
261
-include $(RELEASE_OBJECTS:.o=.d)
-include $(DEBUG_OBJECTS:.o=.d)
262
-include $(JS_OBJECTS:.o=.d)
Richard van der Hoff's avatar
Richard van der Hoff committed
263
-include $(TEST_BINARIES:=.d)
264
265
266
-include $(FUZZER_OBJECTS:.o=.d)
-include $(FUZZER_BINARIES:=.d)
-include $(FUZZER_DEBUG_BINARIES:=.d)