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

Richard van der Hoff's avatar
Richard van der Hoff committed
3
4
MAJOR := 1
MINOR := 0
5
6
PATCH := 0
VERSION := $(MAJOR).$(MINOR).$(PATCH)
7
PREFIX ?= /usr/local
8
BUILD_DIR := build
9
10
RELEASE_OPTIMIZE_FLAGS ?= -g -O3
DEBUG_OPTIMIZE_FLAGS ?= -g -O0
11
JS_OPTIMIZE_FLAGS ?= -O3
12
FUZZING_OPTIMIZE_FLAGS ?= -O3
Richard van der Hoff's avatar
Richard van der Hoff committed
13
CC = gcc
14
EMCC = emcc
15
AFL_CC = afl-gcc
16
AFL_CXX = afl-g++
17
18
19
20
RELEASE_TARGET := $(BUILD_DIR)/libolm.so.$(VERSION)
RELEASE_SYMLINKS := $(BUILD_DIR)/libolm.so.$(MAJOR) $(BUILD_DIR)/libolm.so
DEBUG_TARGET := $(BUILD_DIR)/libolm_debug.so.$(VERSION)
DEBUG_SYMLINKS := $(BUILD_DIR)/libolm_debug.so.$(MAJOR) $(BUILD_DIR)/libolm_debug.so
21
22
23
24
JS_TARGET := javascript/olm.js

JS_EXPORTED_FUNCTIONS := javascript/exported_functions.json

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

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

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

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

CPPFLAGS += -Iinclude -Ilib
53
54
# we rely on <stdint.h>, which was introduced in C99
CFLAGS += -Wall -Werror -std=c99 -fPIC
55
56
CXXFLAGS += -Wall -Werror -std=c++11 -fPIC
LDFLAGS += -Wall -Werror
Richard van der Hoff's avatar
Richard van der Hoff committed
57

58
59
60
61
62
63
64
65
66
EMCCFLAGS = --closure 1 --memory-init-file 0 -s NO_FILESYSTEM=1 -s INVOKE_RUN=0
# 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)

67
68
69
70
71
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
72
73
74
75
76
# generate .d files when compiling
CPPFLAGS += -MMD

### per-target variables

77
78
79
80
81
82
83
$(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
84
85

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

88
89
90
91
92
93
94
$(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)

95
96
97
98
$(JS_OBJECTS): CFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_OBJECTS): CXXFLAGS += $(JS_OPTIMIZE_FLAGS)
$(JS_TARGET): LDFLAGS += $(JS_OPTIMIZE_FLAGS)

Richard van der Hoff's avatar
Richard van der Hoff committed
99
100
### top-level targets

101
lib: $(RELEASE_TARGET) $(RELEASE_SYMLINKS)
102
103
.PHONY: lib

104
105
$(RELEASE_TARGET): $(RELEASE_OBJECTS)
	$(CXX) $(LDFLAGS) --shared -fPIC \
106
            -Wl,-soname,libolm.so.$(MAJOR) \
107
108
109
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(RELEASE_OBJECTS)

110
111
112
113
$(RELEASE_SYMLINKS):
	ln -s libolm.so.$(VERSION) $@

debug: $(DEBUG_TARGET) $(DEBUG_SYMLINKS)
114
115
116
.PHONY: debug

$(DEBUG_TARGET): $(DEBUG_OBJECTS)
117
	$(CXX) $(LDFLAGS) --shared -fPIC \
118
            -Wl,-soname,libolm_debug.so.$(MAJOR) \
119
120
121
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(DEBUG_OBJECTS)

122
123
124
$(DEBUG_SYMLINKS):
	ln -s libolm_debug.so.$(VERSION) $@

125
126
127
128
129
js: $(JS_TARGET)
.PHONY: js

$(JS_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS)
	$(EMCC_LINK) \
130
131
               $(foreach f,$(JS_PRE),--pre-js $(f)) \
               $(foreach f,$(JS_POST),--post-js $(f)) \
132
               -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
133
134
               $(JS_OBJECTS) -o $@

Richard van der Hoff's avatar
Richard van der Hoff committed
135
136
137
138
139
build_tests: $(TEST_BINARIES)

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

143
144
145
fuzzers: $(FUZZER_BINARIES) $(FUZZER_DEBUG_BINARIES)
.PHONY: fuzzers

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

150
all: test js lib debug doc
151
152
.PHONY: all

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
install-debug: debug
	test -d $(DESTDIR)$(PREFIX) || mkdir -p $(DESTDIR)$(PREFIX)
	test -d $(DESTDIR)$(PREFIX)/lib || mkdir $(DESTDIR)$(PREFIX)/lib
	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

install: lib
	test -d $(DESTDIR)$(PREFIX) || mkdir -p $(DESTDIR)$(PREFIX)
	test -d $(DESTDIR)$(PREFIX)/lib || mkdir $(DESTDIR)$(PREFIX)/lib
	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

169
clean:;
170
	rm -rf $(BUILD_DIR) $(DOCS)
171
.PHONY: clean
172

173
174
175
doc: $(DOCS)
.PHONY: doc

Richard van der Hoff's avatar
Richard van der Hoff committed
176
### rules for building objects
177
178
$(BUILD_DIR)/release/%.o: %.c
	mkdir -p $(dir $@)
179
180
	$(COMPILE.c) $(OUTPUT_OPTION) $<

181
182
$(BUILD_DIR)/release/%.o: %.cpp
	mkdir -p $(dir $@)
183
184
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

185
186
$(BUILD_DIR)/debug/%.o: %.c
	mkdir -p $(dir $@)
Richard van der Hoff's avatar
Richard van der Hoff committed
187
188
	$(COMPILE.c) $(OUTPUT_OPTION) $<

189
190
$(BUILD_DIR)/debug/%.o: %.cpp
	mkdir -p $(dir $@)
Richard van der Hoff's avatar
Richard van der Hoff committed
191
192
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

193
194
$(BUILD_DIR)/javascript/%.o: %.c
	mkdir -p $(dir $@)
195
196
	$(EMCC.c) $(OUTPUT_OPTION) $<

197
198
$(BUILD_DIR)/javascript/%.o: %.cpp
	mkdir -p $(dir $@)
199
200
	$(EMCC.cc) $(OUTPUT_OPTION) $<

201
$(BUILD_DIR)/tests/%: tests/%.c $(DEBUG_OBJECTS)
202
	mkdir -p $(dir $@)
203
	$(LINK.c) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
204

205
$(BUILD_DIR)/tests/%: tests/%.cpp $(DEBUG_OBJECTS)
206
	mkdir -p $(dir $@)
207
	$(LINK.cc) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
208

209
210
$(BUILD_DIR)/fuzzers/objects/%.o: %.c
	mkdir -p $(dir $@)
211
212
	$(AFL.c) $(OUTPUT_OPTION) $<

213
214
$(BUILD_DIR)/fuzzers/objects/%.o: %.cpp
	mkdir -p $(dir $@)
215
216
217
218
219
220
221
222
223
224
225
226
227
	$(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
228

229
230
231
%.html: %.rst
	rst2html $< $@

Richard van der Hoff's avatar
Richard van der Hoff committed
232
233
### dependencies

234
235
-include $(RELEASE_OBJECTS:.o=.d)
-include $(DEBUG_OBJECTS:.o=.d)
236
-include $(JS_OBJECTS:.o=.d)
Richard van der Hoff's avatar
Richard van der Hoff committed
237
-include $(TEST_BINARIES:=.d)
238
239
240
-include $(FUZZER_OBJECTS:.o=.d)
-include $(FUZZER_BINARIES:=.d)
-include $(FUZZER_DEBUG_BINARIES:=.d)