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

3
BUILD_DIR := build
4
5
RELEASE_OPTIMIZE_FLAGS ?= -g -O3
DEBUG_OPTIMIZE_FLAGS ?= -g -O0
6
JS_OPTIMIZE_FLAGS ?= -O3
7
FUZZING_OPTIMIZE_FLAGS ?= -O3
Richard van der Hoff's avatar
Richard van der Hoff committed
8
CC = gcc
9
EMCC = emcc
10
11
AFL_CC = afl_gcc
AFL_CXX = afl-g++
12
13
RELEASE_TARGET := $(BUILD_DIR)/libolm.so
DEBUG_TARGET := $(BUILD_DIR)/libolm_debug.so
14
15
16
17
JS_TARGET := javascript/olm.js

JS_EXPORTED_FUNCTIONS := javascript/exported_functions.json

Richard van der Hoff's avatar
Richard van der Hoff committed
18
PUBLIC_HEADERS := include/olm/olm.h
Richard van der Hoff's avatar
Richard van der Hoff committed
19

20
21
22
23
24
SOURCES := $(wildcard src/*.cpp) $(wildcard src/*.c) \
    lib/crypto-algorithms/sha256.c \
    lib/crypto-algorithms/aes.c \
    lib/curve25519-donna/curve25519-donna.c

25
FUZZER_SOURCES := $(wildcard fuzzers/fuzz_*.cpp) $(wildcard fuzzers/fuzz_*.c)
Richard van der Hoff's avatar
Richard van der Hoff committed
26
TEST_SOURCES := $(wildcard tests/test_*.cpp) $(wildcard tests/test_*.c)
27
28
29
30
31
32
33
34
35

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))
36
37
JS_PRE := $(wildcard javascript/*pre.js)
JS_POST := $(wildcard javascript/*post.js)
Richard van der Hoff's avatar
Richard van der Hoff committed
38
39

CPPFLAGS += -Iinclude -Ilib
40
41
# we rely on <stdint.h>, which was introduced in C99
CFLAGS += -Wall -Werror -std=c99 -fPIC
42
43
CXXFLAGS += -Wall -Werror -std=c++11 -fPIC
LDFLAGS += -Wall -Werror
Richard van der Hoff's avatar
Richard van der Hoff committed
44

45
46
47
48
49
50
51
52
53
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)

54
55
56
57
58
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
59
60
61
62
63
# generate .d files when compiling
CPPFLAGS += -MMD

### per-target variables

64
65
66
67
68
69
70
$(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
71
72

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

75
76
77
78
79
80
81
$(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)

82
83
84
85
$(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
86
87
### top-level targets

88
lib: $(RELEASE_TARGET)
89
90
.PHONY: lib

91
92
93
94
95
96
97
98
99
$(RELEASE_TARGET): $(RELEASE_OBJECTS)
	$(CXX) $(LDFLAGS) --shared -fPIC \
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(RELEASE_OBJECTS)

debug: $(DEBUG_TARGET)
.PHONY: debug

$(DEBUG_TARGET): $(DEBUG_OBJECTS)
100
	$(CXX) $(LDFLAGS) --shared -fPIC \
101
102
103
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(DEBUG_OBJECTS)

104
105
106
107
108
js: $(JS_TARGET)
.PHONY: js

$(JS_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS)
	$(EMCC_LINK) \
109
110
               --pre-js $(JS_PRE) --post-js $(JS_POST) \
               -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
111
112
               $(JS_OBJECTS) -o $@

Richard van der Hoff's avatar
Richard van der Hoff committed
113
114
115
116
117
build_tests: $(TEST_BINARIES)

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

121
122
123
fuzzers: $(FUZZER_BINARIES) $(FUZZER_DEBUG_BINARIES)
.PHONY: fuzzers

124
125
126
127
$(JS_EXPORTED_FUNCTIONS): $(PUBLIC_HEADERS)
	perl -MJSON -ne '/(olm_[^( ]*)\(/ && push @f, "_$$1"; END { print encode_json \@f }' $^ > $@.tmp
	mv $@.tmp $@

128
all: test js lib debug
129
130
131
132
133
.PHONY: all

clean:;
	rm -rf $(BUILD_DIR)
.PHONY: clean
134

Richard van der Hoff's avatar
Richard van der Hoff committed
135
### rules for building objects
136
137
$(BUILD_DIR)/release/%.o: %.c
	mkdir -p $(dir $@)
138
139
	$(COMPILE.c) $(OUTPUT_OPTION) $<

140
141
$(BUILD_DIR)/release/%.o: %.cpp
	mkdir -p $(dir $@)
142
143
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

144
145
$(BUILD_DIR)/debug/%.o: %.c
	mkdir -p $(dir $@)
Richard van der Hoff's avatar
Richard van der Hoff committed
146
147
	$(COMPILE.c) $(OUTPUT_OPTION) $<

148
149
$(BUILD_DIR)/debug/%.o: %.cpp
	mkdir -p $(dir $@)
Richard van der Hoff's avatar
Richard van der Hoff committed
150
151
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

152
153
$(BUILD_DIR)/javascript/%.o: %.c
	mkdir -p $(dir $@)
154
155
	$(EMCC.c) $(OUTPUT_OPTION) $<

156
157
$(BUILD_DIR)/javascript/%.o: %.cpp
	mkdir -p $(dir $@)
158
159
	$(EMCC.cc) $(OUTPUT_OPTION) $<

160
$(BUILD_DIR)/tests/%: tests/%.c $(DEBUG_OBJECTS)
161
	mkdir -p $(dir $@)
162
	$(LINK.c) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
163

164
$(BUILD_DIR)/tests/%: tests/%.cpp $(DEBUG_OBJECTS)
165
	mkdir -p $(dir $@)
166
	$(LINK.cc) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
167

168
169
$(BUILD_DIR)/fuzzers/objects/%.o: %.c
	mkdir -p $(dir $@)
170
171
	$(AFL.c) $(OUTPUT_OPTION) $<

172
173
$(BUILD_DIR)/fuzzers/objects/%.o: %.cpp
	mkdir -p $(dir $@)
174
175
176
177
178
179
180
181
182
183
184
185
186
	$(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
187
188
189

### dependencies

190
191
-include $(RELEASE_OBJECTS:.o=.d)
-include $(DEBUG_OBJECTS:.o=.d)
192
-include $(JS_OBJECTS:.o=.d)
Richard van der Hoff's avatar
Richard van der Hoff committed
193
-include $(TEST_BINARIES:=.d)
194
195
196
-include $(FUZZER_OBJECTS:.o=.d)
-include $(FUZZER_BINARIES:=.d)
-include $(FUZZER_DEBUG_BINARIES:=.d)