Compiling PHP
The build pipeline lives in a Dockerfile. It was originally forked from seanmorris/php-wasm
In broad strokes, that Dockerfile:
- Installs all the necessary linux packages (like
build-essential) - Downloads PHP and the required libraries, e.g.
sqlite3. - Applies a few patches.
- Compiles everything using Emscripten, a drop-in replacement for the C compiler.
- Compiles
php_wasm.c– a convenient API for JavaScript. - Outputs a
php.wasmfile and one or more JavaScript loaders, depending on the configuration. - Transforms the Emscripten's default
php.jsoutput into an ESM module with additional features.
To find out more about each step, refer directly to the Dockerfile.
Building
With Docker running and the repository dependencies installed, run these commands from the repository root:
# Build all supported PHP versions for the web, in both JSPI and Asyncify modes.
npx nx recompile-php:all php-wasm-web
# Build only PHP 8.4 for the web, in JSPI mode.
npx nx recompile-php:jspi php-wasm-web -- --PHP_VERSION=8.4
Replace php-wasm-web with php-wasm-node to build for Node.js, or recompile-php:jspi with recompile-php:asyncify to build the Asyncify variant. The output goes to packages/php-wasm/web-builds/<major>-<minor>/<mode>/ or packages/php-wasm/node-builds/<major>-<minor>/<mode>/.
Debug builds
Use --WITH_DEBUG=yes to build PHP.wasm with readable JavaScript output and DWARF debug information for stepping through C code in a WebAssembly debugger:
# Build PHP 8.4 for debugging in the browser.
npx nx recompile-php:jspi php-wasm-web -- --PHP_VERSION=8.4 --WITH_DEBUG=yes
# Build PHP 8.4 for debugging in Node.js.
npx nx recompile-php:jspi php-wasm-node -- --PHP_VERSION=8.4 --WITH_DEBUG=yes
The same option works with recompile-php:asyncify. Debug builds produce larger files and run more slowly than optimized builds. They replace the selected version's artifacts in the output directory described above. Rebuild with --WITH_DEBUG=no --WITH_SOURCEMAPS=no to restore an optimized build.
For WebAssembly source maps, use --WITH_SOURCEMAPS=yes:
npx nx recompile-php:jspi php-wasm-web -- --PHP_VERSION=8.4 --WITH_SOURCEMAPS=yes
This generates a php.wasm.map file and copies the source files needed for debugging into the build output. For web builds, the source map URL points to the local development server at http://127.0.0.1:5400; run npm run dev to serve it.
Emscripten options
The build script translates these options into compiler flags in the PHP Dockerfile:
| Flag | Purpose | When Playground uses it |
|---|---|---|
-O0 | Disables optimization of the final WebAssembly and JavaScript output. | WITH_DEBUG=yes or WITH_SOURCEMAPS=yes, replacing the default -O3. |
-g2 | Keeps function names and readable JavaScript, without retaining DWARF information in the final module. | Node.js builds when neither debug option is enabled. |
-g3 | Retains DWARF information for source-level debugging. | WITH_DEBUG=yes or WITH_SOURCEMAPS=yes. |
-gsource-map | Generates a WebAssembly source map from compiler debug information. | WITH_SOURCEMAPS=yes. |
See the Emscripten compiler reference for details on these flags.