Skip to content

Commit

Permalink
Curl extension for the Node.js build of PHP.wasm (#1273)
Browse files Browse the repository at this point in the history
Ships the Node.js version of PHP built with `--with-libcurl` option to support the curl extension.

It also changes two nuances in the overall PHP build process:

* It replaces the `select(2)` function using `-Wl,--wrap=select` emcc
option instead of patching PHP source code – this enables supporting
asynchronous `select(2)` in curl without additional patches.
* Brings the `__wrap_select` implementation more in line with
`select(2)`, add support for `POLLERR`.
* Adds support for polling file descriptors that represent neither child
processes nor streams in `poll(2)` – that's because `libcurl` polls
`/dev/urandom`.

Builds on top of and supersedes
#1133

## Debugging Asyncify problems

The [typical way of resolving Asyncify
crashes](https://wordpress.github.io/wordpress-playground/architecture/wasm-asyncify/)
didn't work during the work on this PR. Functions didn't come up in the
error messages and even raw stack traces. The reasons are unclear.

[The JSPI build of
PHP](#1339) was
more helpful as it enabled logging the current stack trace in all the
asynchronous calls, which quickly revealed all the missing
`ASYNCIFY_ONLY` functions. This is the way to debug any future issues
until we fully migrate to JSPI.

## Testing Instructions

Confirm the CI checks pass. This PR ships a few new tests specifically
targeting networking with curl.


## Related resources

* #85
* #1093

---------

Co-authored-by: Adam Zieliński <adam@adamziel.com>
Co-authored-by: MHO <yannick@chillpills.io>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent cf118a4 commit 3fd0315
Show file tree
Hide file tree
Showing 44 changed files with 10,680 additions and 2,470 deletions.
18 changes: 16 additions & 2 deletions packages/php-wasm/compile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ libzip/dist/1.9.2/root/lib/lib/libzip.a: base-image libz
libpng16: libpng16/dist/root/lib/lib/libpng16.a
libpng16/dist/root/lib/lib/libpng16.a: base-image libz
mkdir -p ./libpng16/dist/root/lib
docker build -f ./libpng16/Dockerfile -t playground-php-wasm:libpng .
docker build -f ./libpng16/Dockerfile -t playground-php-wasm:libpng .
docker cp $$(docker create playground-php-wasm:libpng):/root/install/lib ./libpng16/dist/root/lib
docker cp $$(docker create playground-php-wasm:libpng):/root/install/include ./libpng16/dist/root/lib

Expand Down Expand Up @@ -88,7 +88,21 @@ oniguruma/dist/root/lib/lib/libonig.a: base-image
docker cp $$(docker create playground-php-wasm:oniguruma):/root/lib/lib ./oniguruma/dist/root/lib/
docker cp $$(docker create playground-php-wasm:oniguruma):/root/lib/include ./oniguruma/dist/root/lib

all: libz libzip libpng16 libxml2 libopenssl libsqlite3 libiconv libncurses libedit bison2.7 oniguruma
libcurl: libcurl/dist/root/lib/lib/libcurl.a
libcurl/dist/root/lib/lib/libcurl.a: base-image libz libopenssl
mkdir -p ./libcurl/dist/root/lib
docker build -f ./libcurl/Dockerfile -t playground-php-wasm:libcurl . --progress=plain
docker cp $$(docker create playground-php-wasm:libcurl):/root/curl-7.69.1/lib/.libs ./libcurl/dist/root/lib/lib
docker cp $$(docker create playground-php-wasm:libcurl):/root/curl-7.69.1/include/ ./libcurl/dist/root/lib

libcurl7.15: libcurl7.15/dist/root/lib/lib/libcurl.a
libcurl7.15/dist/root/lib/lib/libcurl.a: base-image libz libopenssl
mkdir -p ./libcurl7.15/dist/root/lib
docker build -f ./libcurl7.15/Dockerfile -t playground-php-wasm:libcurl7.15 . --progress=plain
docker cp $$(docker create playground-php-wasm:libcurl):/root/curl-7.15.5/lib/.libs ./libcurl7.15/dist/root/lib/lib
docker cp $$(docker create playground-php-wasm:libcurl):/root/curl-7.15.5/include/ ./libcurl7.15/dist/root/lib

all: libz libzip libpng16 libxml2 libopenssl libsqlite3 libiconv libncurses libedit bison2.7 oniguruma libcurl libcurl7.15
clean:
rm -rf ./libz/dist
rm -rf ./libzip/dist
Expand Down
1 change: 1 addition & 0 deletions packages/php-wasm/compile/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const platformDefaults = {
WITH_OPENSSL: 'yes',
},
node: {
WITH_CURL: 'yes',
WITH_FILEINFO: 'yes',
WITH_ICONV: 'yes',
WITH_LIBXML: 'yes',
Expand Down
66 changes: 66 additions & 0 deletions packages/php-wasm/compile/libcurl/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
FROM playground-php-wasm:base

RUN mkdir -p /root/lib/include /root/lib/lib
COPY ./libz/dist/root/lib/include /root/lib/include
COPY ./libz/dist/root/lib/lib /root/lib/lib
COPY ./libopenssl/dist/root/lib/include /root/lib/include
COPY ./libopenssl/dist/root/lib/lib /root/lib/lib

ARG CURL_VERSION="curl-7.69.1"

RUN /root/copy-lib.sh lib-libz
RUN set -euxo pipefail && \
source /root/emsdk/emsdk_env.sh && \
wget https://curl.haxx.se/download/$CURL_VERSION.tar.gz && \
tar xf $CURL_VERSION.tar.gz

WORKDIR /root/$CURL_VERSION

RUN CPPFLAGS="-I/root/lib/include " \
LDFLAGS="-L/root/lib/lib " \
PKG_CONFIG_PATH=$PKG_CONFIG_PATH \
source /root/emsdk/emsdk_env.sh && \
emconfigure ./configure \
--build i386-pc-linux-gnu \
--target wasm32-unknown-emscripten \
--prefix=/root/lib/ \
--disable-shared \
--enable-static \
--with-ssl \
--with-openssl=/root/lib \
--enable-https \
--enable-http \
--disable-pop3 \
--disable-imap \
--disable-smb \
--disable-smtp \
--disable-telnet \
--disable-gopher \
--disable-ftp \
--disable-ftps \
--disable-rtsp \
--disable-tftp \
--disable-pthreads \
--disable-threaded-resolver \
--with-zlib=/root/lib

RUN cp /root/emsdk/upstream/bin/wasm-ld /root/emsdk/upstream/bin/wasm-ld-original && \
echo $'#!/bin/bash\n\
if [[ " $@ " =~ " -o curl " ]]; then \n\
echo '' > /root/curl-7.69.1/src/curl; \n\
echo '' > /root/curl-7.69.1/curl; \n\
fi; \n\
/root/emsdk/upstream/bin/wasm-ld-original "$@" || true; \n\
exit 0; \n' > /root/emsdk/upstream/bin/wasm-ld && \
chmod a+x /root/emsdk/upstream/bin/wasm-ld


RUN source /root/emsdk/emsdk_env.sh && \
EMCC_SKIP="-lc -lz -lcurl -lssl " \
EMCC_FLAGS="-sSIDE_MODULE -Wl,--wrap=select " emmake make -i || true

RUN source /root/emsdk/emsdk_env.sh && \
EMCC_SKIP="-lc -lz -lcurl -lssl " \
EMCC_FLAGS="-sSIDE_MODULE -Wl,--wrap=select " emmake make install -i || true

RUN ls -R /root/lib
Loading

0 comments on commit 3fd0315

Please sign in to comment.