Makefile 6.62 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
18
JS_TARGET := javascript/olm.js

JS_EXPORTED_FUNCTIONS := javascript/exported_functions.json

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

SOURCES := $(wildcard src/*.cpp) $(wildcard src/*.c)
21
22
RELEASE_OBJECTS := $(patsubst src/%,$(BUILD_DIR)/release/%,$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES))))
DEBUG_OBJECTS := $(patsubst src/%,$(BUILD_DIR)/debug/%,$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES))))
23
24
25
26
FUZZER_OBJECTS := $(patsubst src/%,$(BUILD_DIR)/fuzzers/objects/%,$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES))))
FUZZER_SOURCES := $(wildcard fuzzers/fuzz_*.cpp) $(wildcard fuzzers/fuzz_*.c)
FUZZER_BINARIES := $(patsubst fuzzers/%,$(BUILD_DIR)/fuzzers/%,$(patsubst %.c,%,$(patsubst %.cpp,%,$(FUZZER_SOURCES))))
FUZZER_DEBUG_BINARIES := $(patsubst $(BUILD_DIR)/fuzzers/fuzz_%,$(BUILD_DIR)/fuzzers/debug_%,$(FUZZER_BINARIES))
Richard van der Hoff's avatar
Richard van der Hoff committed
27
TEST_SOURCES := $(wildcard tests/test_*.cpp) $(wildcard tests/test_*.c)
28
29
TEST_BINARIES := $(patsubst tests/%,$(BUILD_DIR)/tests/%,$(patsubst %.c,%,$(patsubst %.cpp,%,$(TEST_SOURCES))))
JS_OBJECTS := $(patsubst src/%,$(BUILD_DIR)/javascript/%,$(patsubst %.c,%.js.bc,$(patsubst %.cpp,%.js.bc,$(SOURCES))))
30
31
JS_PRE := $(wildcard javascript/*pre.js)
JS_POST := $(wildcard javascript/*post.js)
Richard van der Hoff's avatar
Richard van der Hoff committed
32
33

CPPFLAGS += -Iinclude -Ilib
34
35
CFLAGS += -Wall -std=c89 -fPIC
CXXFLAGS += -Wall -std=c++11 -fPIC
Richard van der Hoff's avatar
Richard van der Hoff committed
36
37
LDFLAGS += -Wall

38
39
40
41
42
43
44
45
46
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)

47
48
49
50
51
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
52
53
54
55
56
# generate .d files when compiling
CPPFLAGS += -MMD

### per-target variables

57
58
59
60
61
62
63
$(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
64
65
66
67

$(TEST_BINARIES): CPPFLAGS += -Itests/include
$(TEST_BINARIES): LDFLAGS += $(TEST_OPTIMIZE_FLAGS) -L$(BUILD_DIR)

68
69
70
71
72
73
74
$(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)

75
76
77
78
$(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
79
80
### top-level targets

81
lib: $(RELEASE_TARGET)
82
83
.PHONY: lib

Mark Haines's avatar
Mark Haines committed
84
85
# Make sure that the build directory exists.
# We can't check the build directory into git because it is empty.
86
makedirs:
87
88
	mkdir -p $(BUILD_DIR)/release $(BUILD_DIR)/debug $(BUILD_DIR)/javascript\
            $(BUILD_DIR)/tests $(BUILD_DIR)/fuzzers/objects
89
90
.PHONY: makedirs

91

92
93
94
95
96
97
98
99
100
$(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)
101
	$(CXX) $(LDFLAGS) --shared -fPIC \
102
103
104
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(DEBUG_OBJECTS)

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

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

Richard van der Hoff's avatar
Richard van der Hoff committed
114
clean:;
115
116
	rm -rf $(RELEASE_OBJECTS) $(RELEASE_OBJECTS:.o=.d) \
               $(DEBUG_OBJECTS) $(DEBUG_OBJECTS:.o=.d) \
Richard van der Hoff's avatar
Richard van der Hoff committed
117
               $(TEST_BINARIES) $(TEST_BINARIES:=.d) \
118
               $(JS_OBJECTS) $(JS_OBJECTS:.bc=.d) $(JS_TARGET) \
119
               $(JS_EXPORTED_FUNCTIONS)\
120
               $(RELEASE_TARGET) $(DEBUG_TARGET)\
121
122
123
               $(FUZZER_OBJECTS) $(FUZZER_OBJECTS:.o=.d)\
               $(FUZZER_BINARIES) $(FUZZER_BINARIES:=.d)\
               $(FUZZER_DEBUG_BINARIES) $(FUZZER_DEBUG_BINARIES:=.d)\
Richard van der Hoff's avatar
Richard van der Hoff committed
124
125
126
127
128
129

build_tests: $(TEST_BINARIES)

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

133
134
135
fuzzers: $(FUZZER_BINARIES) $(FUZZER_DEBUG_BINARIES)
.PHONY: fuzzers

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

140
all: test js lib debug
141
142
.PHONY: lib

Richard van der Hoff's avatar
Richard van der Hoff committed
143
### rules for building objects
144
$(BUILD_DIR)/release/%.o: src/%.c | makedirs
145
146
	$(COMPILE.c) $(OUTPUT_OPTION) $<

147
$(BUILD_DIR)/release/%.o: src/%.cpp | makedirs
148
149
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

150
$(BUILD_DIR)/debug/%.o: src/%.c | makedirs
Richard van der Hoff's avatar
Richard van der Hoff committed
151
152
	$(COMPILE.c) $(OUTPUT_OPTION) $<

153
$(BUILD_DIR)/debug/%.o: src/%.cpp | makedirs
Richard van der Hoff's avatar
Richard van der Hoff committed
154
155
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

156
$(BUILD_DIR)/javascript/%.js.bc: src/%.c | makedirs
157
158
	$(EMCC.c) $(OUTPUT_OPTION) $<

159
$(BUILD_DIR)/javascript/%.js.bc: src/%.cpp | makedirs
160
161
	$(EMCC.cc) $(OUTPUT_OPTION) $<

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

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

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
$(BUILD_DIR)/fuzzers/objects/%.o: src/%.c | makedirs
	$(AFL.c) $(OUTPUT_OPTION) $<

$(BUILD_DIR)/fuzzers/objects/%.o: src/%.cpp | makedirs
	$(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
185
186
187

### dependencies

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