-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Makefile
193 lines (152 loc) · 8.15 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
# Note: Last built with version 2.0.15 of Emscripten
# TODO: Emit a file showing which version of emcc and SQLite was used to compile the emitted output.
# TODO: Create a release on Github with these compiled assets rather than checking them in
# TODO: Consider creating different files based on browser vs module usage: https://github.com/vuejs/vue/tree/dev/dist
# I got this handy makefile syntax from : https://github.com/mandel59/sqlite-wasm (MIT License) Credited in LICENSE
# To use another version of Sqlite, visit https://www.sqlite.org/download.html and copy the appropriate values here:
SQLITE_AMALGAMATION = sqlite-amalgamation-3450200
SQLITE_AMALGAMATION_ZIP_URL = https://www.sqlite.org/2024/sqlite-amalgamation-3450200.zip
SQLITE_AMALGAMATION_ZIP_SHA3 = 8d9c553b52c7b1656a97fec7907cb00fd1419ac45104e45c76b7c2b81ffe0a9d
# Note that extension-functions.c hasn't been updated since 2010-02-06, so likely doesn't need to be updated
EXTENSION_FUNCTIONS = extension-functions.c
EXTENSION_FUNCTIONS_URL = https://www.sqlite.org/contrib/download/extension-functions.c?get=25
EXTENSION_FUNCTIONS_SHA1 = c68fa706d6d9ff98608044c00212473f9c14892f
EMCC=emcc
SQLITE_COMPILATION_FLAGS = \
-Oz \
-DSQLITE_OMIT_LOAD_EXTENSION \
-DSQLITE_DISABLE_LFS \
-DSQLITE_ENABLE_FTS3 \
-DSQLITE_ENABLE_FTS3_PARENTHESIS \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_ENABLE_NORMALIZE
# When compiling to WASM, enabling memory-growth is not expected to make much of an impact, so we enable it for all builds
# Since tihs is a library and not a standalone executable, we don't want to catch unhandled Node process exceptions
# So, we do : `NODEJS_CATCH_EXIT=0`, which fixes issue: https://github.com/sql-js/sql.js/issues/173 and https://github.com/sql-js/sql.js/issues/262
EMFLAGS = \
-s RESERVED_FUNCTION_POINTERS=64 \
-s ALLOW_TABLE_GROWTH=1 \
-s EXPORTED_FUNCTIONS=@src/exported_functions.json \
-s EXPORTED_RUNTIME_METHODS=@src/exported_runtime_methods.json \
-s SINGLE_FILE=0 \
-s NODEJS_CATCH_EXIT=0 \
-s NODEJS_CATCH_REJECTION=0 \
-s STACK_SIZE=5MB
EMFLAGS_ASM = \
-s WASM=0
EMFLAGS_ASM_MEMORY_GROWTH = \
-s WASM=0 \
-s ALLOW_MEMORY_GROWTH=1
EMFLAGS_WASM = \
-s WASM=1 \
-s ALLOW_MEMORY_GROWTH=1
EMFLAGS_OPTIMIZED= \
-Oz \
-flto \
--closure 1
EMFLAGS_DEBUG = \
-s ASSERTIONS=2 \
-O1
BITCODE_FILES = out/sqlite3.o out/extension-functions.o
OUTPUT_WRAPPER_FILES = src/shell-pre.js src/shell-post.js
SOURCE_API_FILES = src/api.js
EMFLAGS_PRE_JS_FILES = \
--pre-js src/api.js
EXPORTED_METHODS_JSON_FILES = src/exported_functions.json src/exported_runtime_methods.json
all: optimized debug worker
.PHONY: debug
debug: dist/sql-asm-debug.js dist/sql-wasm-debug.js
dist/sql-asm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
$(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_ASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
mv $@ out/tmp-raw.js
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
rm out/tmp-raw.js
dist/sql-wasm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
$(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_WASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
mv $@ out/tmp-raw.js
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
rm out/tmp-raw.js
.PHONY: optimized
optimized: dist/sql-asm.js dist/sql-wasm.js dist/sql-asm-memory-growth.js
dist/sql-asm.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
$(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_ASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
mv $@ out/tmp-raw.js
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
rm out/tmp-raw.js
dist/sql-wasm.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
$(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_WASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
mv $@ out/tmp-raw.js
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
rm out/tmp-raw.js
dist/sql-asm-memory-growth.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
$(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_ASM_MEMORY_GROWTH) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
mv $@ out/tmp-raw.js
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
rm out/tmp-raw.js
# Web worker API
.PHONY: worker
worker: dist/worker.sql-asm.js dist/worker.sql-asm-debug.js dist/worker.sql-wasm.js dist/worker.sql-wasm-debug.js
dist/worker.sql-asm.js: dist/sql-asm.js src/worker.js
cat $^ > $@
dist/worker.sql-asm-debug.js: dist/sql-asm-debug.js src/worker.js
cat $^ > $@
dist/worker.sql-wasm.js: dist/sql-wasm.js src/worker.js
cat $^ > $@
dist/worker.sql-wasm-debug.js: dist/sql-wasm-debug.js src/worker.js
cat $^ > $@
# Building it this way gets us a wrapper that _knows_ it's in worker mode, which is nice.
# However, since we can't tell emcc that we don't need the wasm generated, and just want the wrapper, we have to pay to have the .wasm generated
# even though we would have already generated it with our sql-wasm.js target above.
# This would be made easier if this is implemented: https://github.com/emscripten-core/emscripten/issues/8506
# dist/worker.sql-wasm.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) src/api.js src/worker.js $(EXPORTED_METHODS_JSON_FILES) dist/sql-wasm-debug.wasm
# $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) -s ENVIRONMENT=worker -s $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js src/api.js -o out/sql-wasm.js
# mv out/sql-wasm.js out/tmp-raw.js
# cat src/shell-pre.js out/tmp-raw.js src/shell-post.js src/worker.js > $@
# #mv out/sql-wasm.wasm dist/sql-wasm.wasm
# rm out/tmp-raw.js
# dist/worker.sql-wasm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) src/api.js src/worker.js $(EXPORTED_METHODS_JSON_FILES) dist/sql-wasm-debug.wasm
# $(EMCC) -s ENVIRONMENT=worker $(EMFLAGS) $(EMFLAGS_DEBUG) -s ENVIRONMENT=worker -s WASM_BINARY_FILE=sql-wasm-foo.debug $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js src/api.js -o out/sql-wasm-debug.js
# mv out/sql-wasm-debug.js out/tmp-raw.js
# cat src/shell-pre.js out/tmp-raw.js src/shell-post.js src/worker.js > $@
# #mv out/sql-wasm-debug.wasm dist/sql-wasm-debug.wasm
# rm out/tmp-raw.js
out/sqlite3.o: sqlite-src/$(SQLITE_AMALGAMATION)
mkdir -p out
# Generate llvm bitcode
$(EMCC) $(SQLITE_COMPILATION_FLAGS) -c sqlite-src/$(SQLITE_AMALGAMATION)/sqlite3.c -o $@
# Since the extension-functions.c includes other headers in the sqlite_amalgamation, we declare that this depends on more than just extension-functions.c
out/extension-functions.o: sqlite-src/$(SQLITE_AMALGAMATION)
mkdir -p out
# Generate llvm bitcode
$(EMCC) $(SQLITE_COMPILATION_FLAGS) -c sqlite-src/$(SQLITE_AMALGAMATION)/extension-functions.c -o $@
# TODO: This target appears to be unused. If we re-instatate it, we'll need to add more files inside of the JS folder
# module.tar.gz: test package.json AUTHORS README.md dist/sql-asm.js
# tar --create --gzip $^ > $@
## cache
cache/$(SQLITE_AMALGAMATION).zip:
mkdir -p cache
curl -LsSf '$(SQLITE_AMALGAMATION_ZIP_URL)' -o $@
cache/$(EXTENSION_FUNCTIONS):
mkdir -p cache
curl -LsSf '$(EXTENSION_FUNCTIONS_URL)' -o $@
## sqlite-src
.PHONY: sqlite-src
sqlite-src: sqlite-src/$(SQLITE_AMALGAMATION) sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTIONS)
sqlite-src/$(SQLITE_AMALGAMATION): cache/$(SQLITE_AMALGAMATION).zip sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTIONS)
mkdir -p sqlite-src/$(SQLITE_AMALGAMATION)
echo '$(SQLITE_AMALGAMATION_ZIP_SHA3) ./cache/$(SQLITE_AMALGAMATION).zip' > cache/check.txt
sha3sum -a 256 -c cache/check.txt
# We don't delete the sqlite_amalgamation folder. That's a job for clean
# Also, the extension functions get copied here, and if we get the order of these steps wrong,
# this step could remove the extension functions, and that's not what we want
unzip -u 'cache/$(SQLITE_AMALGAMATION).zip' -d sqlite-src/
touch $@
sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTIONS): cache/$(EXTENSION_FUNCTIONS)
mkdir -p sqlite-src/$(SQLITE_AMALGAMATION)
echo '$(EXTENSION_FUNCTIONS_SHA1) ./cache/$(EXTENSION_FUNCTIONS)' > cache/check.txt
sha1sum -c cache/check.txt
cp 'cache/$(EXTENSION_FUNCTIONS)' $@
.PHONY: clean
clean:
rm -f out/* dist/* cache/*
rm -rf sqlite-src/