-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
250 lines (211 loc) · 7.48 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
# # Building process
# 1. Compile C/C++ codes and libraries using Emscripten.
# 2. Packing JS files as UMD using webpack.docs
# 3. Copy files to $(DIST)
# Change the port you like. You can run the dev server by using "make run"
DEV_SERVER_PORT := 9000
# Used by build-docs target
ifdef PRODUCTION
export BASE_URL := https://cdn.jsdelivr.net/npm/opus-media-recorder@latest
else
export BASE_URL := https://localhost:$(DEV_SERVER_PORT)
endif
# Path Settings
LIB_DIR := $(abspath lib)
SRC_DIR := $(abspath src)
EXAMPLE_DIR := $(abspath example)
DOCS_DIR := $(abspath docs)
# This is used by /lib/Makefile
export BUILD_DIR := $(abspath build)
export LIB_BUILD_DIR := $(abspath $(BUILD_DIR)/emscripten)
DIST_DIR := .
# Expected files
OUTPUT_FILES = OpusMediaRecorder.js WaveEncoder.js \
OggOpusEncoder.js OggOpusEncoder.wasm \
WebMOpusEncoder.js WebMOpusEncoder.wasm \
encoderWorker.js commonFunctions.js
# Add UMD libraries
OUTPUT_FILES += OpusMediaRecorder.umd.js encoderWorker.umd.js
FINAL_TARGETS_BUILD = $(addprefix $(BUILD_DIR)/,$(OUTPUT_FILES))
# Production only section
# .bin files - Some bundlers needs extension other than .wasm
OUTPUT_FILES_WASM := $(filter %.wasm, $(OUTPUT_FILES))
OUTPUT_FILES_BIN = $(OUTPUT_FILES_WASM:%.wasm=%.bin)
# Production only files: dist
FINAL_TARGETS_DIST = $(addprefix $(DIST_DIR)/,$(OUTPUT_FILES) $(OUTPUT_FILES_BIN))
ifndef PRODUCTION
# Development only section
# Debugging map files
OUTPUT_FILES += OggOpusEncoder.wasm.map WebMOpusEncoder.wasm.map
endif
ifdef PRODUCTION
all: check_emcc $(FINAL_TARGETS_BUILD) $(FINAL_TARGETS_DIST) build-docs
else
all: check_emcc $(FINAL_TARGETS_BUILD) build-docs
endif
################################################################################
# 1. Emscripten compilation
################################################################################
# Reference: https://github.com/kripken/emscripten/blob/master/src/settings.js
# Emscripten compiler (emcc) options
export EMCC_DEBUG=1
EMCC_OPTS = -std=c++11 \
-fno-exceptions \
-Oz \
--llvm-opts 3 \
--llvm-lto 1 \
-s WASM=1 \
-s MODULARIZE=1 \
-s FILESYSTEM=0 \
-s MALLOC="emmalloc" \
--source-map-base http://localhost:$(DEV_SERVER_PORT)/
# -s EXPORT_ES6=1 -- I'm not using ES6 import yet.
# -s ENVIRONMENT='worker' -- Enabling it will delete node.js
# codes like require('fs') needed by
# some dunblers like browserify.
# -s "BINARYEN_METHOD='asmjs,native-wasm'" -- In case we need asm.js
# --closure 1 -- Gets error
# -s DYNAMIC_EXECUTION=0 -- Seems to be only for asm.js
# -DNDEBUG -- This will casue Firefox unable to play WebM - See Issue #9.
DEFAULT_EXPORTS:='_malloc','_free'
OPUS_EXPORTS:='_opus_encoder_create', \
'_opus_encode_float', \
'_opus_encoder_ctl', \
'_opus_encoder_destroy'
SPEEX_EXPORTS:='_speex_resampler_init', \
'_speex_resampler_process_interleaved_float', \
'_speex_resampler_destroy'
# WebIDL
WEBIDL = Container.webidl
WEBIDL_GLUE_BASE = $(addsuffix _glue,$(addprefix $(LIB_BUILD_DIR)/,$(WEBIDL)))
WEBIDL_GLUE_JS = $(addsuffix .js,$(WEBIDL_GLUE_BASE))
# OGG/WebM Common
EMCC_INCLUDE_DIR = $(SRC_DIR) \
$(LIB_DIR)/ogg/include \
$(LIB_DIR)/yuv/include \
$(LIB_DIR)/vpx \
$(LIB_DIR)/webm \
$(LIB_BUILD_DIR) \
./
# Emscripten options for Debugging
ifndef PRODUCTION
EMCC_OPTS += -g4 \
-s EXCEPTION_DEBUG=1 \
-s ASSERTIONS=2 \
-s STACK_OVERFLOW_CHECK=1 \
-s VERBOSE=1 \
-s DETERMINISTIC=1 \
-s RUNTIME_LOGGING=1 \
-s ALLOW_MEMORY_GROWTH=1
# -s DISABLE_EXCEPTION_CATCHING=0 \
# -s "TOTAL_STACK=5*1024*1024"
# -s "TOTAL_MEMORY=16777216"
EMCC_OPTS := $(filter-out -DNDEBUG,$(EMCC_OPTS))
endif
# C compiled static libraries
export OPUS_OBJ = $(LIB_BUILD_DIR)/libopus.a
export OGG_OBJ = $(LIB_BUILD_DIR)/libogg.a
export SPEEX_OBJ = $(LIB_BUILD_DIR)/libspeexdsp.a
export YUV_OBJ = $(LIB_BUILD_DIR)/libyuv.a
export VPX_OBJ = $(LIB_BUILD_DIR)/libvpx.a
export WEBM_OBJ = $(LIB_BUILD_DIR)/libwebm.a
LIB_OBJS = $(OPUS_OBJ) $(OGG_OBJ) $(SPEEX_OBJ) $(YUV_OBJ) $(VPX_OBJ) $(WEBM_OBJ)
###########
# Targets #
###########
# 1.1 Static library targets
$(LIB_OBJS):
make -C $(LIB_DIR) $@
# 1.2 C++ - WebIDL - JavaScript glue code targets
$(WEBIDL_GLUE_JS): $(addprefix $(SRC_DIR)/,$(WEBIDL)) $(LIB_BUILD_DIR)
python $(EMSCRIPTEN)/tools/webidl_binder.py \
$< \
$(WEBIDL_GLUE_BASE)
# $(BUILD_DIR)/OggOpusEncoder.js
# $(BUILD_DIR)/WebMOpusEncoder.js
$(BUILD_DIR)/%OpusEncoder.js $(BUILD_DIR)/%OpusEncoder.wasm $(BUILD_DIR)/%OpusEncoder.wasm.map: $(SRC_DIR)/%Container.cpp $(SRC_DIR)/%Container_webidl_js_binder.cpp $(SRC_DIR)/%Container.hpp $(SRC_DIR)/OpusEncoder.js $(WEBIDL_GLUE_JS) $(SRC_DIR)/ContainerInterface.cpp $(LIB_OBJS)
emcc -o $@ \
$(EMCC_OPTS) \
-s EXPORTED_FUNCTIONS="[$(DEFAULT_EXPORTS),$(OPUS_EXPORTS),$(SPEEX_EXPORTS)]" \
$(addprefix -I,$(EMCC_INCLUDE_DIR)) \
$(word 1,$^) \
$(word 2,$^) \
$(SRC_DIR)/ContainerInterface.cpp \
$(LIB_OBJS) \
--pre-js $(SRC_DIR)/OpusEncoder.js \
--post-js $(WEBIDL_GLUE_JS)
################################################################################
# 2. UMD compilation using webpack
################################################################################
# For JavaScript build
NPM_FLAGS = -d
# Options for production
ifdef PRODUCTION
NPM_FLAGS := $(filter-out -d,$(NPM_FLAGS))
NPM_FLAGS += -p
endif
# 2.1 Copy extra JS files to /build/emscripten
$(BUILD_DIR)/%.js: $(SRC_DIR)/%.js $(BUILD_DIR)
cp $< $@
# 2.2 UMD library
$(BUILD_DIR)/%.umd.js: $(BUILD_DIR)/%.js $(BUILD_DIR)/commonFunctions.js
npm run webpack -- --config webpack.config.js \
$(NPM_FLAGS) \
--output-library $(basename $(notdir $<)) \
--output-library-target umd \
$< \
-o $@
# 2.2 UMD Web Worker
$(BUILD_DIR)/%Worker.umd.js: $(BUILD_DIR)/%Worker.js $(BUILD_DIR)/commonFunctions.js
npm run webpack -- --config webpack.worker.config.js \
$(NPM_FLAGS) \
$< \
-o $@
################################################################################
# 3. Production files
################################################################################
$(DIST_DIR)/%: $(BUILD_DIR)/%
cp $< $@
$(DIST_DIR)/%.bin: $(BUILD_DIR)/%.wasm
cp $< $@
################################################################################
# etc.
################################################################################
.PHONY : all check_emcc serve build-docs clean-lib clean-js clean
ifndef EMSCRIPTEN
$(error EMSCRIPTEN is undefined. Enable Escripten SDK.)
endif
cc_version = $(shell $(1) --version | head -n1 | cut -d" " -f5)
define check_version
@if test "$$(printf '%s\n' "$(1)" "$(2)" | sort -V | head -n 1)" != "$(1)"; then \
exit 0; \
else \
echo $(3); \
exit 1; \
fi
endef
check_emcc:
@which emcc > /dev/null
@echo Building with emcc version: $(call cc_version, emcc)
$(call check_version, $(call cc_version, emcc), 1.38.24, 'emcc(emscripten) version must be 1.38.25 or higher')
$(BUILD_DIR) $(LIB_BUILD_DIR):
mkdir -p $@
build-docs:
OUTPUT_DIR=$(DOCS_DIR) make -C $(EXAMPLE_DIR)/example_template
OUTPUT_DIR=$(EXAMPLE_DIR)/webpack make -C $(EXAMPLE_DIR)/example_template
serve: all
# Run server
npm run serve -- --port $(DEV_SERVER_PORT)
clean-lib:
# Clean /lib
make -C $(LIB_DIR) clean
clean-js:
# Delete build files, not touch subdirectory like LIB_BUILD_DIR
-rm $(BUILD_DIR)/*
# Delete WebIDL parser outputs
-rm WebIDLGrammar.pkl parser.out
# Delete Dist files
-rm $(FINAL_TARGETS_DIST)
clean: clean-lib clean-js
# Delete build folder
-rm -rf $(BUILD_DIR)