Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile sql.js to wasm instead of "just" asm.js #255

Merged
merged 48 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3fd3247
Fix "ready" promise to handle sync and async loading by Emscripten
Taytay Nov 1, 2018
a99d51e
Now create a js/worker.sql-debug.js file when compiling
Taytay Nov 1, 2018
fe22c2d
Worker and worker test improvements
Taytay Nov 1, 2018
4792726
Fix an error in test_database where it was calling done twice
Taytay Nov 1, 2018
6809ec8
Add npm's package.lock
Taytay Nov 1, 2018
5d5a280
Add note regarding which version of emcc was last used to make
Taytay Nov 1, 2018
7dd4332
Add note about test_worker being expected to fail
Taytay Nov 1, 2018
6cb3145
Commit compiled files
Taytay Nov 1, 2018
de891fa
Remove mistaken line from makefile
Taytay Nov 1, 2018
2614712
Clean up Makefile - fix bug in `make optimized`
Taytay Nov 1, 2018
8ee7001
test_worker now purposefully skips until we can fix the bug with Node…
Taytay Nov 1, 2018
15f287e
Commit latest versions of compiled files
Taytay Nov 1, 2018
e37523f
Compile with WASM=1
Taytay Nov 1, 2018
4b8ed30
Replace invalid "-03" option with "-O3"
Taytay Nov 2, 2018
5142efd
Add extra test scripts to make it more obvious how to test the variou…
Taytay Nov 2, 2018
623c414
Merge branch 'fix/async_loading' into wasm
Taytay Nov 2, 2018
7b07b34
Optimize output and rename *-raw.wasm files
Taytay Nov 2, 2018
53c1c63
Emit both WASM and non-WASM targets
Taytay Nov 2, 2018
cf0e880
Don't use writeStringToMemory
Taytay Nov 4, 2018
508cf1a
Commit the latest compiled assets
Taytay Nov 4, 2018
ee90d9f
Merge branch 'fix/async_loading' into wasm
Taytay Nov 4, 2018
70e44c0
Commit latest compiled assets
Taytay Nov 4, 2018
23e1f67
Add package.json scripts to test the various compiled builds
Taytay Nov 5, 2018
fb9e3d8
The test files now work correctly if run individually
Taytay Nov 5, 2018
cc2de41
The worker test would theoretically work if run by itself
Taytay Nov 5, 2018
bc55c40
Adding the load_sql_file.js that the tests depend upon
Taytay Nov 5, 2018
91966a1
Merge branch 'fix/async_loading' into wasm
Taytay Nov 5, 2018
dd8394b
Add @taytay to AUTHORS
Taytay Jan 7, 2019
b4b749a
Bump to version 1, use sql-wasm.js as default file
Taytay Jan 7, 2019
f451216
The "module" is now an async function that loads and initializes Sql.js
Taytay Jan 7, 2019
6e769a6
Cleanup: Rename files for clarity
Taytay Jan 7, 2019
6b3c46d
Rename sql-memory-growth.js to sql-asm-memory-growth.js
Taytay Jan 7, 2019
36f23f7
Move files from coffee/ and js/ into src/
Taytay Jan 7, 2019
1f709a8
Rename most folders for clarity
Taytay Jan 7, 2019
2e3fa51
Fix bugs with the modularization
Taytay Jan 8, 2019
9d90bb3
WIP: Update the README to show async usage and describe versions of t…
Taytay Jan 8, 2019
babd0a3
Add latest compiled assets
Taytay Jan 8, 2019
d86aca6
Fix loading of worker module
Taytay Apr 25, 2019
77ad0d3
Add and modify existing examples
Taytay Apr 25, 2019
e6cc307
Improve README for 1.0
Taytay Apr 25, 2019
b4df241
Minor: test_blob.js formatting
Taytay Apr 25, 2019
b3d2c37
Remove Node uncaughtException handler
Taytay Apr 26, 2019
3342bba
Add example in Makefile showing how we could compile a worker-only wr…
Taytay Apr 26, 2019
a85a60a
Build with 1.38.30 of Emscripten
Taytay Apr 26, 2019
4e91be7
Upgrade to Sqlite 3.28.0
Taytay Apr 26, 2019
9fa2052
Ignore /sqlite-src and /cache folders
Taytay Apr 26, 2019
6466c31
Add a GUI/index.html to redirect to examples/GUI/index.html
Taytay Apr 26, 2019
15eea72
Rename exported_runtime_methods and exported_functions
Taytay Apr 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
node_modules/
*~

# LLVM bitcode
c/sqlite3.bc
c/extension-functions.bc
# Intermediary js files
js/api.js
js/sql-noopt-raw.js.map
js/sql-optimized.js
js/worker.js
js/sql-raw.js.map
# Intermediary files:
out/
sqlite-src/
cache/
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Ophir LOJKINE <[email protected]> (https://github.com/lovasoa)
@kripken
@hankinsoft
@firien
@dinedal
@dinedal
@taytay
6 changes: 6 additions & 0 deletions GUI/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<!-- Add a link to the new page because there are likely links pointing here from elsewhere -->
This page has moved to: <a href="../examples/GUI/index.html">../examples/GUI/index.html</a>
</body>
</html>
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Some portions of the Makefile taken from:
Copyright 2017 Ryusei Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
197 changes: 167 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,50 +1,187 @@
EMCC=emcc
# Note: Last built with version 1.38.30 of Emscripten

CFLAGS=-O2 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DLONGDOUBLE_TYPE=double -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS
# 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

all: js/sql.js debug js/worker.sql.js memory-growth
# 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-3280000
SQLITE_AMALGAMATION_ZIP_URL = https://www.sqlite.org/2019/sqlite-amalgamation-3280000.zip
SQLITE_AMALGAMATION_ZIP_SHA1 = eb82fcc95104c8e2d9550ab023c1054b9cc40a76

# RESERVED_FUNCTION_POINTERS setting is used for registering custom functions
optimized: EMFLAGS= --memory-init-file 0 -O3 -s INLINING_LIMIT=50 -s RESERVED_FUNCTION_POINTERS=64
optimized: js/sql-optimized.js
# 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

memory-growth: EMFLAGS= --memory-init-file 0 -O3 -s INLINING_LIMIT=50 -s RESERVED_FUNCTION_POINTERS=64 -s ALLOW_MEMORY_GROWTH=1
memory-growth: js/sql-memory-growth.js
EMCC=emcc

debug: EMFLAGS= -O1 -g -s INLINING_LIMIT=10 -s RESERVED_FUNCTION_POINTERS=64
debug: js/sql-debug.js
CFLAGS=-O2 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DLONGDOUBLE_TYPE=double -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS

js/sql.js: optimized
cp js/sql-optimized.js js/sql.js
# 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/kripken/sql.js/issues/173 and https://github.com/kripken/sql.js/issues/262
EMFLAGS = \
--memory-init-file 0 \
-s RESERVED_FUNCTION_POINTERS=64 \
-s EXPORTED_FUNCTIONS=@src/exported_functions.json \
-s EXTRA_EXPORTED_RUNTIME_METHODS=@src/exported_runtime_methods.json \
-s SINGLE_FILE=0 \
-s NODEJS_CATCH_EXIT=0

EMFLAGS_WASM = \
-s WASM=1 \
-s ALLOW_MEMORY_GROWTH=1

EMFLAGS_OPTIMIZED= \
-s INLINING_LIMIT=50 \
-O3 \
--closure 1

EMFLAGS_DEBUG = \
-s INLINING_LIMIT=10 \
-O1

BITCODE_FILES = out/sqlite3.bc out/extension-functions.bc
OUTPUT_WRAPPER_FILES = src/shell-pre.js src/shell-post.js

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) out/api.js src/exported_functions.json src/exported_runtime_methods.json
$(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) -s WASM=0 $(BITCODE_FILES) --pre-js out/api.js -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) out/api.js src/exported_functions.json src/exported_runtime_methods.json
$(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js out/api.js -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) out/api.js src/exported_functions.json src/exported_runtime_methods.json
$(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) -s WASM=0 $(BITCODE_FILES) --pre-js out/api.js -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) out/api.js src/exported_functions.json src/exported_runtime_methods.json
$(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js out/api.js -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) out/api.js src/exported_functions.json src/exported_runtime_methods.json
$(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) -s WASM=0 -s ALLOW_MEMORY_GROWTH=1 $(BITCODE_FILES) --pre-js out/api.js -o $@
mv $@ out/tmp-raw.js
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
rm out/tmp-raw.js

js/sql%.js: js/shell-pre.js js/sql%-raw.js js/shell-post.js
cat $^ > $@

js/sql%-raw.js: c/sqlite3.bc c/extension-functions.bc js/api.js exported_functions
$(EMCC) $(EMFLAGS) -s EXPORTED_FUNCTIONS=@exported_functions -s EXTRA_EXPORTED_RUNTIME_METHODS=@exported_runtime_methods c/extension-functions.bc c/sqlite3.bc --post-js js/api.js -o $@ ;\
# 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

js/api.js: coffee/api.coffee coffee/exports.coffee coffee/api-data.coffee
out/worker.js: src/worker.coffee
cat $^ | coffee --bare --compile --stdio > $@

# Web worker API
worker: js/worker.sql.js
js/worker.js: coffee/worker.coffee
cat $^ | coffee --bare --compile --stdio > $@
dist/worker.sql-asm.js: dist/sql-asm.js out/worker.js
cat $^ > $@

js/worker.sql.js: js/sql.js js/worker.js
dist/worker.sql-asm-debug.js: dist/sql-asm-debug.js out/worker.js
cat $^ > $@

c/sqlite3.bc: c/sqlite3.c
dist/worker.sql-wasm.js: dist/sql-wasm.js out/worker.js
cat $^ > $@

dist/worker.sql-wasm-debug.js: dist/sql-wasm-debug.js out/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) out/api.js out/worker.js src/exported_functions.json src/exported_runtime_methods.json dist/sql-wasm-debug.wasm
# $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) -s ENVIRONMENT=worker -s $(EMFLAGS_WASM) $(BITCODE_FILES) --pre-js out/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 out/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) out/api.js out/worker.js src/exported_functions.json src/exported_runtime_methods.json 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 out/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 out/worker.js > $@
# #mv out/sql-wasm-debug.wasm dist/sql-wasm-debug.wasm
# rm out/tmp-raw.js

out/api.js: src/output-pre.js src/api.coffee src/exports.coffee src/api-data.coffee src/output-post.js
cat src/api.coffee src/exports.coffee src/api-data.coffee | coffee --bare --compile --stdio > $@
cat src/output-pre.js $@ src/output-post.js > out/api-wrapped.js
mv out/api-wrapped.js $@

out/sqlite3.bc: sqlite-src/$(SQLITE_AMALGAMATION)
# Generate llvm bitcode
$(EMCC) $(CFLAGS) c/sqlite3.c -o c/sqlite3.bc
$(EMCC) $(CFLAGS) sqlite-src/$(SQLITE_AMALGAMATION)/sqlite3.c -o $@

out/extension-functions.bc: sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTIONS)
$(EMCC) $(CFLAGS) -s LINKABLE=1 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

.PHONY: clean-cache
clean-cache:
rm -rf 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: clean-sqlite-src
clean-sqlite-src:
rm -rf sqlite

.PHONY: sqlite-src
sqlite-src: sqlite-src/$(SQLITE_AMALGAMATION) sqlite-src/$(EXTENSION_FUNCTIONS)

sqlite-src/$(SQLITE_AMALGAMATION): cache/$(SQLITE_AMALGAMATION).zip
mkdir -p sqlite-src
echo '$(SQLITE_AMALGAMATION_ZIP_SHA1) ./cache/$(SQLITE_AMALGAMATION).zip' > cache/check.txt
sha1sum -c cache/check.txt
rm -rf $@
unzip 'cache/$(SQLITE_AMALGAMATION).zip' -d sqlite-src/
touch $@

c/extension-functions.bc: c/extension-functions.c
$(EMCC) $(CFLAGS) -s LINKABLE=1 c/extension-functions.c -o c/extension-functions.bc
sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTIONS): cache/$(EXTENSION_FUNCTIONS)
mkdir -p sqlite-src
echo '$(EXTENSION_FUNCTIONS_SHA1) ./cache/$(EXTENSION_FUNCTIONS)' > cache/check.txt
sha1sum -c cache/check.txt
cp 'cache/$(EXTENSION_FUNCTIONS)' $@

module.tar.gz: test package.json AUTHORS README.md js/sql.js
tar --create --gzip $^ > $@

clean:
rm -rf js/sql.js js/api.js js/sql*-raw.js js/worker.sql.js js/worker.js js/sql-memory-growth.js c/sqlite3.bc c/extension-functions.bc
.PHONY: clean
clean:
rm -rf out/* dist/*

.PHONY: clean-all
clean-all:
rm -f out/* dist/* cache/*
rm -rf sqlite-src/

Loading