-
-
Notifications
You must be signed in to change notification settings - Fork 809
/
Copy pathMakefile
277 lines (208 loc) · 5.33 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# VARIABLES #
ifndef VERBOSE
QUIET := @
endif
# Determine the OS:
#
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
# [2]: http://stackoverflow.com/a/27776822/2225624
OS ?= $(shell uname)
ifneq (, $(findstring MINGW,$(OS)))
OS := WINNT
else
ifneq (, $(findstring MSYS,$(OS)))
OS := WINNT
else
ifneq (, $(findstring CYGWIN,$(OS)))
OS := WINNT
endif
endif
endif
# Determine the absolute path of the Makefile (see http://blog.jgc.org/2007/01/what-makefile-am-i-in.html):
this_dir := $(dir $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
# Remove the trailing slash:
this_dir := $(patsubst %/,%,$(this_dir))
# Define the command for Node.js:
NODE ?= node
# Define the Node.js directory path. Notes:
#
# 1. The directory is expected to contain include header files.
# 2. This only works on Mac and Linux.
NODE_DIR ?= $(shell $(NODE) -e 'console.log( path.resolve( process.execPath, "..", ".." ) );')
# Determine path to NAN header files:
INCLUDE_NAN ?= $(shell node -e 'require( "nan" );')
# Define the program used for compiling C source files:
ifdef C_COMPILER
CC := $(C_COMPILER)
else
CC := gcc
endif
# Define the command-line options when compiling C files:
CFLAGS ?= \
-std=c99 \
-O3 \
-Wall \
-pedantic
# Define the program used for compiling C++ source files:
ifdef CXX_COMPILER
CXX := $(CXX_COMPILER)
else
CXX := g++
endif
# Define the command-line options when compiling C++ files:
CXXFLAGS ?= \
-std=c++11 \
-O3 \
-Wall \
-pedantic
# Define the program used for compiling WebAssembly:
ifdef EMCC_COMPILER
EMCC := $(EMCC_COMPILER)
else
EMCC := emcc
endif
# Define the command-line options when compiling WebAssembly:
EMCCFLAGS ?= $(CFLAGS)
# Shared `emcc` flags:
EMCC_SHARED_FLAGS := \
-s EXPORTED_FUNCTIONS="['_stdlib_base_pdiff']" \
-s STRICT=1 \
-s NO_FILESYSTEM=1 \
-s ERROR_ON_UNDEFINED_SYMBOLS=1 \
-s ERROR_ON_MISSING_LIBRARIES=1
# WebAssembly specific flags:
# TODO: Remove `-s LEGALIZE_JS_FFI=0`, as this setting is a hack to force WASM only mode (see https://github.com/kripken/emscripten/issues/5370).
EMCC_WASM_FLAGS := $(EMCC_SHARED_FLAGS) \
-s SIDE_MODULE=1 \
-s WASM=1 \
-s LEGALIZE_JS_FFI=0
# Define the program for generating WebAssembly text files:
WASM2WAT ?= wasm2wat
# Determine whether to generate [position independent code][1]:
#
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
ifeq ($(OS), WINNT)
fPIC ?=
else
fPIC ?= -fPIC
endif
# Define the program for linking compiled files:
ifdef LINKER
LD := $(LINKER)
else
LD := g++
endif
# Define the command-line options when linking compiled files:
ifeq ($(OS), Darwin)
LDFLAGS ?= \
-undefined dynamic_lookup \
-Wl,-no_pie \
-Wl,-search_paths_first
else
LDFLAGS ?=
endif
# List of external includes:
INCLUDE ?=
# List of external source files:
SOURCE_FILES ?=
# List of external object files:
OBJECT_FILES ?=
# List of libraries (e.g., `-lopenblas -lpthread`):
LIBRARIES ?=
# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
LIBPATH ?=
# List of C targets:
c_objects := pdiff.o
# List of C++ add-on targets:
addon_objects := addon.o
# List of WebAssembly targets:
wasm_objects := pdiff.wasm
# List of WebAssembly text file targets:
wat_objects := pdiff.wat
# TARGETS #
# Default target.
#
# This target is the default target.
all: $(c_objects) $(addon_objects) addon.node wasm
.PHONY: all
# Compile C source.
#
# This target compiles C source files.
$(c_objects): %.o: %.c
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -I ../include -c -o $@ $<
# Compile add-ons.
#
# This target compiles C++ add-on source files.
$(addon_objects): %.o: %.cpp
$(QUIET) $(CXX) \
$(CXXFLAGS) \
$(fPIC) \
-I "$(NODE_DIR)/include/node" \
-I "$(NODE_DIR)/src" \
-I "$(NODE_DIR)/deps/uv/include" \
-I "$(NODE_DIR)/deps/v8/include" \
-I "$(INCLUDE_NAN)" \
$(INCLUDE) \
-I ../include \
-c \
-o $@ \
$<
# Compile WebAssembly binaries.
#
# This target compiles WebAssembly binaries.
$(wasm_objects): %.wasm: %.c
$(QUIET) $(EMCC) \
$(EMCCFLAGS) \
$(EMCC_WASM_FLAGS) \
$(fPIC) \
$(INCLUDE) \
-I ../include \
-o $*.js \
$(SOURCE_FILES) \
$< \
$(LIBPATH) \
$(LIBRARIES)
$(QUIET) rm -f $*.js
# Generate WebAssembly text files.
#
# This target generates WebAssembly text files from WebAssembly binaries.
$(wat_objects): %.wat: %.wasm
$(QUIET) $(WASM2WAT) \
-o $*.wat \
$<
# Generate add-on.
#
# This target generates a Node.js add-on by creating a shared object which can be linked to by other libraries and executables.
addon.node: $(addon_objects)
$(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(OBJECT_FILES) $(c_objects) $< $(LIBPATH) $(LIBRARIES)
# Generate WebAssembly.
#
# This target generates WebAssembly files.
wasm: clean-wasm $(wasm_objects) $(wat_objects)
.PHONY: wasm
# List libraries.
#
# This target lists compiled libraries.
list: $(c_objects)
$(QUIET) for file in $^; do \
echo \"$(this_dir)/$$file\"; \
done
.PHONY: list
# Clean-up add-on.
#
# This target removes generated files for building an add-on.
clean-addon:
$(QUIET) -rm -f *.o *.node
.PHONY: clean-addon
# Clean-up WebAssembly.
#
# This target removes generated files for compiling to WebAssembly.
clean-wasm:
$(QUIET) -rm -f *.wasm *.wat
.PHONY: clean-wasm
# Perform clean-up.
#
# This target removes generated files.
clean: clean-addon
.PHONY: clean