davidcole1340 / ext-php-rs

Bindings for the Zend API to build PHP extensions natively in Rust.
Apache License 2.0
598 stars 64 forks source link

Support for musl #132

Open joehoyle opened 2 years ago

joehoyle commented 2 years ago

I'm trying to build a PHP extension for Alpine Linux, which uses musl. My understanding is that I need to set my Cargo.toml to build a staticlib rather than cdylib in this case.

I'm running into issues with building ext-php-rs, I think the first issue is that bindgen is set to dynamically link libclang, which fails with "Dynamic loading not supported". It does appear from https://gitlab.alpinelinux.org/alpine/aports/-/issues/12226 that it's possible for bindgen to work with clang-static.

After cloning ext-php-rs and trying that, I am now getting an error:

  = note: /usr/lib/gcc/aarch64-alpine-linux-musl/10.3.1/../../../../aarch64-alpine-linux-musl/bin/ld: /root/.rustup/toolchains/stable-aarch64-unknown-linux-musl/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-182d699ce6400069.rlib(cpu_model.o): undefined reference to symbol 'getauxval'
          /usr/lib/gcc/aarch64-alpine-linux-musl/10.3.1/../../../../aarch64-alpine-linux-musl/bin/ld: /lib/libc.musl-aarch64.so.1: error adding symbols: DSO missing from command line
          collect2: error: ld returned 1 exit status

I'm a bit out of my depth with this though! If anyone else has tried or had experience with building for musl, and pointers would be much appreciated! Thanks

PineappleIOnic commented 2 years ago

We also needed support for musl for the extension we were writing and managed to figure out a way to build our extension.

You don't need to use staticlib and it's actually better if you don't since with staticlib you'll need to compile the extension into PHP, Our solution was to build it on a GNU based linux distro then target x86_64-unknown-linux-musl while also using zigbuild to fix a issue that the rust linker has with musl.

If you have any more questions just lmk and I'm more than happy to answer them.

PineappleIOnic commented 2 years ago

I also forgot to mention we use the -C target-feature=-crt-static rust compiler flags

sashaaro commented 1 year ago

I compile as below in ubuntu

sudo apt-get install musl-tools
rustup target add x86_64-unknown-linux-musl
RUSTFLAGS="-C target-feature=-crt-static" cargo build --target x86_64-unknown-linux-musl --release

Compiled .so file including PHP in image php:8.1.11-fpm-alpine3.16

docker run --rm -it --name=php php:8.1.11-fpm-alpine3.16 sh
echo "extension=libreqwest.so" >> /usr/local/etc/php/conf.d/20-reqwest.ini
docker cp ./../reqwest-ext/target/x86_64-unknown-linux-musl/release/libreqwest.so php:/usr/local/lib/php/extensions/no-debug-non-zts-20210902/

I receive the next error while php -v Error loading shared library ld-linux-x86-64.so.2: No such file or directory

Warning: PHP Startup:
Unable to load dynamic library 'libreqwest.so' (tried: /usr/local/lib/php/extensions/no-debug-zts-20220829/libreqwest.so 
(Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /usr/local/lib/php/extensions/no-debug-zts-20220829/libreqwest.so)),
/usr/local/lib/php/extensions/no-debug-zts-20220829/libreqwest.so.so 
(Error loading shared library /usr/local/lib/php/extensions/no-debug-zts-20220829/libreqwest.so.so: No such file or directory)) in
Unknown on line 0
PineappleIOnic commented 1 year ago

Have you double checked that is the correct name of the file outputted by your compile? also if you don't mind , could you share the repo your compiling with me so I can test it myself? @sashaaro

sashaaro commented 1 year ago

https://github.com/sashaaro/reqwest-php-extension

sashaaro commented 1 year ago
docker run --rm -it -v "$(pwd)":/app -w /app messense/rust-musl-cross:x86_64-musl
cd .. && git clone https://github.com/php/php-src && cd php-src && git checkout php-8.2.0
apt install re2c
apt-get install -y pkg-config
make clean
./buildconf --force
./configure --enable-zts --without-sqlite3 --without-pdo-sqlite
make -j4
make install
cd /app
RUSTFLAGS="-C target-feature=-crt-static" cargo build --release

Compiled .so library in target/x86_64-unknown-linux-musl work fine in alpine with php8.2 zth