Skip to content

PHP WASM: Use Emscripten's zlib and remove bundled copy #335

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

Open
eliot-akira opened this issue May 12, 2023 · 2 comments
Open

PHP WASM: Use Emscripten's zlib and remove bundled copy #335

eliot-akira opened this issue May 12, 2023 · 2 comments
Labels

Comments

@eliot-akira
Copy link
Collaborator

eliot-akira commented May 12, 2023

Just making a note here to serve as a reminder for the future.

Something I noticed in a comment that gives an overview of the patches applied by WordPress Playground:

zlib – Playground uses a 10 years old Emscripten-compatible port of zlib because my initial attempt to compile the upstream version as WASM failed. I'm sure this old port can be removed, especially since Emscripten comes with -s USE_ZLIB=1 switch these days, but I didn't get there yet. PRs welcome!

Currently a copy of this ported zlib lives in the Playground repo, in the folder packages/php-wasm/compile/build-assets/zlib. The README file says this is zlib version 1.2.5, while the port itself is at 1.2.8.

The USE_ZLIB option in Emscripten (added in version 1.26.1) uses a Python script to download and patch zlib from its original source repo, which is at version 1.2.13.


According to the documentation, Building Projects: Emscripten Ports:

When you request that a port be used, emcc will fetch it from the remote server, set it up and build it locally, then link it with your project, add necessary include to your build commands, etc.

The Dockerfile for PHP WASM is daunting ("impressive, overwhelming, inspiring fear"), but here's my guess at what's necessary to remove the bundled copy and use Emscripten's zlib.

  • /root/.emcc-php-wasm-flags - Add -s USE_ZLIB=1 and remove -I /root/zlib
  • /root/.php-configure-flags - Update --with-zlib-dir=/root/lib to the destination path where Emscripten builds zlib?
  • Copy the same built zlib to the image emscripten-libz and/or /root/libz for steps that need it?

From this pull request in Emscripten, Use common include directory for all ports #9983:

All ports now install their headers into a shared directory under EM_CACHE.

It seems the built zlib is at {$EM_CACHE}/ports-builds/zlib.

@adamziel
Copy link
Collaborator

Thank you for creating this issue @eliot-akira!

@eliot-akira
Copy link
Collaborator Author

eliot-akira commented May 17, 2023

I learned that Emscripten has an undocumented tool called embuilder as a way to manually build bundled ports like zlib.

Here's its source and man page.

The command to invoke:

embuilder build zlib

It puts the downloaded source and an additional file zconf.h into the directory:

{$EM_CACHE}/ports/zlib/zlib-{$ZLIB_VERSION}

The zlib version is hardcoded in the script here. There doesn't seem to be a way to get it from outside of emscripten/embuilder.

The built file libz.a is placed in the following directory (defined here).

{$EM_CACHE}/ports-builds/zlib

The cache directory defaults to emscripten/cache but can be overridden using the EM_CACHE environment variable (according to emcc docs here).


All that considered, perhaps it's easier to manually update the copy of zlib bundled with the Playground, instead of trying to use what Emscripten generates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants