Makefile 4.53 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
Richard van der Hoff's avatar
Richard van der Hoff committed
7
CC = gcc
8
EMCC = emcc
9
10
RELEASE_TARGET := $(BUILD_DIR)/libolm.so
DEBUG_TARGET := $(BUILD_DIR)/libolm_debug.so
11
12
13
14
15
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
16
17

SOURCES := $(wildcard src/*.cpp) $(wildcard src/*.c)
18
19
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))))
Richard van der Hoff's avatar
Richard van der Hoff committed
20
TEST_SOURCES := $(wildcard tests/test_*.cpp) $(wildcard tests/test_*.c)
21
22
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))))
23
24
JS_PRE := $(wildcard javascript/*pre.js)
JS_POST := $(wildcard javascript/*post.js)
Richard van der Hoff's avatar
Richard van der Hoff committed
25
26

CPPFLAGS += -Iinclude -Ilib
27
28
CFLAGS += -Wall -std=c89 -fPIC
CXXFLAGS += -Wall -std=c++11 -fPIC
Richard van der Hoff's avatar
Richard van der Hoff committed
29
30
LDFLAGS += -Wall

31
32
33
34
35
36
37
38
39
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)

Richard van der Hoff's avatar
Richard van der Hoff committed
40
41
42
43
44
# generate .d files when compiling
CPPFLAGS += -MMD

### per-target variables

45
46
47
48
49
50
51
$(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
52
53
54
55

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

56
57
58
59
$(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
60
61
### top-level targets

62
lib: $(RELEASE_TARGET)
63
64
.PHONY: lib

Mark Haines's avatar
Mark Haines committed
65
66
# Make sure that the build directory exists.
# We can't check the build directory into git because it is empty.
67
68
69
70
makedirs:
	mkdir -p $(BUILD_DIR)/release $(BUILD_DIR)/debug $(BUILD_DIR)/javascript $(BUILD_DIR)/tests
.PHONY: makedirs

71
72
73
74
75
76
77
78
79
$(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)
80
	$(CXX) $(LDFLAGS) --shared -fPIC \
81
82
83
            -Wl,--version-script,version_script.ver \
            $(OUTPUT_OPTION) $(DEBUG_OBJECTS)

Richard van der Hoff's avatar
Richard van der Hoff committed
84

85
86
87
88
89
js: $(JS_TARGET)
.PHONY: js

$(JS_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS)
	$(EMCC_LINK) \
90
91
               --pre-js $(JS_PRE) --post-js $(JS_POST) \
               -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
92
93
               $(JS_OBJECTS) -o $@

Richard van der Hoff's avatar
Richard van der Hoff committed
94
clean:;
95
96
	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
97
               $(TEST_BINARIES) $(TEST_BINARIES:=.d) \
98
               $(JS_OBJECTS) $(JS_OBJECTS:.bc=.d) $(JS_TARGET) \
99
               $(JS_EXPORTED_FUNCTIONS) \
100
               $(RELEASE_TARGET) $(DEBUG_TARGET)\
Richard van der Hoff's avatar
Richard van der Hoff committed
101
102
103
104
105
106

build_tests: $(TEST_BINARIES)

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

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

114
all: test js lib debug
115
116
.PHONY: lib

Richard van der Hoff's avatar
Richard van der Hoff committed
117
### rules for building objects
118
$(BUILD_DIR)/release/%.o: src/%.c | makedirs
119
120
	$(COMPILE.c) $(OUTPUT_OPTION) $<

121
$(BUILD_DIR)/release/%.o: src/%.cpp | makedirs
122
123
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

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

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

130
$(BUILD_DIR)/javascript/%.js.bc: src/%.c | makedirs
131
132
	$(EMCC.c) $(OUTPUT_OPTION) $<

133
$(BUILD_DIR)/javascript/%.js.bc: src/%.cpp | makedirs
134
135
	$(EMCC.cc) $(OUTPUT_OPTION) $<

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

139
$(BUILD_DIR)/tests/%: tests/%.cpp $(DEBUG_OBJECTS)
140
	$(LINK.cc) $< $(DEBUG_OBJECTS) $(LOADLIBES) $(LDLIBS) -o $@
Richard van der Hoff's avatar
Richard van der Hoff committed
141
142
143
144


### dependencies

145
146
-include $(RELEASE_OBJECTS:.o=.d)
-include $(DEBUG_OBJECTS:.o=.d)
147
-include $(JS_OBJECTS:.bc=.d)
Richard van der Hoff's avatar
Richard van der Hoff committed
148
-include $(TEST_BINARIES:=.d)