interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / wasm / README.md

190 lines · 6327 bytes Raw Blame History Rendered
1This directory houses the browser-side
2[Web Assembly (WASM)](https://en.wikipedia.org/wiki/WebAssembly)
3parts of the sqlite3 build.
4
5See also: [](./api/README.md)
6
7It requires [Emscripten][] and that the build environment be set up
8for emscripten. _Release_ builds also require [the wabt tools](#wabt)
91.0.36 or higher, but dev builds do not. A mini-HOWTO for setting that
10up follows...
11
12First, install the Emscripten SDK, as documented
13[here](https://emscripten.org/docs/getting_started/downloads.html) and summarized
14below for Linux environments:
15
16```
17# Clone the emscripten repository:
18$ git clone https://github.com/emscripten-core/emsdk.git
19$ cd emsdk
20
21# Download and install the latest SDK tools:
22$ ./emsdk install latest
23
24# Make the "latest" SDK "active" for the current user:
25$ ./emsdk activate latest
26```
27
28The SDK can later be updated using:
29
30```
31$ git pull
32$ ./emsdk install latest
33$ ./emsdk activate latest
34```
35
36(Sidebar: Emscripten updates can and do _change things_, i.e. _break
37things_, so it's considered _required practice_ to test thoroughly
38after upgrading it! Our build process makes no guarantees about which
39Emscripten version(s) will or won't work, but it's important that
40production builds are built using a compatible version. During active
41development, the EMSDK is frequently updated, the goal being to keep
42`sqlite3.wasm` working with "the latest" EMSDK.)
43
44The SQLite configure script will search for the EMSDK. One way
45to ensure that it finds it is:
46
47```
48# Activate PATH and other environment variables in the current terminal
49# from the emsdk checkout:
50$ source ./emsdk_env.sh
51
52$ which emcc
53/path/to/emsdk/upstream/emscripten/emcc
54
55$ ./configure ...
56```
57
58Optionally, add that `source` part to your login shell's resource file
59(`~/.bashrc` or equivalent).
60
61Another way is to pass the EMSDK dir to configure:
62
63```
64$ ./configure --with-emsdk=/path/to/emsdk
65```
66
67The build tree uses a small wrapper for invoking the `emcc` (the
68Emscripten compiler): `tool/emcc.sh` is generated from
69`tool/emcc.sh.in` using the EMSDK path found by the configure process.
70
71With that in place, the most common build approaches are:
72
73```
74# From the top of the tree:
75$ make fiddle
76```
77
78Or:
79
80```
81$ make sqlite3.c shell.c; # needed in advance of...
82$ cd ext/wasm
83$ make
84```
85
86Those will generate the a number of files required for a handful of
87test and demo applications which can be accessed via
88`index.html`. WASM content cannot, due to XMLHttpRequest security
89limitations, be loaded if the containing HTML file is opened directly
90in the browser (i.e. if it is opened using a `file://` URL), so it
91needs to be served via an HTTP server. For example, using
92[althttpd][]:
93
94```
95$ cd ext/wasm
96$ althttpd --enable-sab --max-age 1 --page index.html
97# Or, more simply, from the ext/wasm dir:
98$ make httpd
99```
100
101That will open the system's browser and visit the index page, from
102which (almost) all of the test and demo applications can be accessed.
103(`ext/wasm/SQLTester` is not listed in that page because it's only of
104real utility when it's used in conjunction with the project's
105proprietary test suite, which most users don't have access to.)
106
107When serving this app via [althttpd][], it must be a version from
1082022-09-26 or newer so that it recognizes the `--enable-sab` flag,
109which causes althttpd to emit two HTTP response headers which are
110required to enable JavaScript's `SharedArrayBuffer` and `Atomics`
111APIs. Those APIs are required in order to enable the [OPFS][]-related
112features in the apps which use them.
113
114
115<a id="targets"></a>
116# Major Makefile Targets
117
118- default, all
119 Build intended for day-to-day development, optimized for reduced
120 build time. i.e. unoptimized debug builds.
121- o0, o1, o2, o3, os, oz
122 A full clean/rebuild with the `-Ox` optimization level indicated by
123 the target name. A clean rebuild is necessary for all components to
124 get the desired optimization level.
125- dist
126 Creates an optimized build and distribution zip file.
127- snapshot
128 Similar to `dist` but the resulting archive is clearly marked as a
129 snapshot/prerelease build.
130
131
132Targets intended only for use by the code maintainers:
133
134- for-testing
135 Create an optimized build for the test server.
136- push-testing
137 Push the current build to the test server. Requires SSH access to
138 the test server and that an SSH alias be set up for it locally.
139- update-docs
140 Updates certain derived/generated files in a local checkout of
141 the [sqlite.org/wasm][www-wasm] site, if found. It uses whatever
142 build is currently sitting in the tree - care must be taken
143 to use an `oz`, `dist`, or `for-testing` build to ensure that
144 the optimized JS/WASM files used.
145
146
147# Testing on a remote machine that is accessed via SSH
148
149*NB: The following are developer notes, last validated on 2023-07-19*
150
151 * Remote: Install git, emsdk, and althttpd
152 * Use a [version of althttpd][althttpd] from
153 September 26, 2022 or newer.
154 * Remote: Install the SQLite source tree. CD to ext/wasm
155 * Remote: "`make`" to build WASM
156 * Remote: `althttpd --enable-sab --port 8080 --popup`
157 * Local: `ssh -L 8180:localhost:8080 remote`
158 * Local: Point your web-browser at http://localhost:8180/index.html
159
160In order to enable [SharedArrayBuffer][], the web-browser requires
161that the two extra Cross-Origin lines be present in HTTP reply headers
162and that the request must come from "localhost" (_or_ over an SSL
163connection). Since the web-server is on a different machine from the
164web-broser, the localhost requirement means that the connection must
165be tunneled using SSH.
166
167
168<a id="wabt"></a>
169# The wabt Tools
170
171_Release_ builds require the wabt tools:
172
173<https://github.com/WebAssembly/wabt>
174
175Specifically, we need `wasm-strip` so that the resulting WASM file is not
176several megabytes.
177
178Pre-built binaries can be downloaded from:
179
180<https://github.com/WebAssembly/wabt/releases>
181
182As of 2025-10-14, versions 1.36.0 or higher are known to work and
1831.34.0 is known to not work with current Emscripten output.
184
185
186[emscripten]: https://emscripten.org
187[althttpd]: https://sqlite.org/althttpd
188[SharedArrayBuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
189[OPFS]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system
190[www-wasm]: https://sqlite.org/wasm