diff --git a/php-rust/.cargo/config.toml b/php-rust/.cargo/config.toml new file mode 100644 index 00000000..0bd26212 --- /dev/null +++ b/php-rust/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.x86_64-unknown-linux-musl] +rustflags = ["-C", "target-feature=+crt-static"] diff --git a/php-rust/crates/php-types/build.rs b/php-rust/crates/php-types/build.rs index 5b77a118..4795aa42 100644 --- a/php-rust/crates/php-types/build.rs +++ b/php-rust/crates/php-types/build.rs @@ -1,4 +1,53 @@ fn main() { + println!("cargo:rerun-if-env-changed=PHPR_MUSL_PREFIX"); + if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("musl") { + link_musl_static(); + } else { + link_system_dylibs(); + } + // libxml2 >= 2.12 renamed the XPath value-stack entry points `valuePop`/ + // `valuePush` to `xmlXPathValuePop`/`xmlXPathValuePush`, and the old names + // are gone by 2.15 (PR #3: Arch, libxml2 2.15.4). The macOS system libxml2 + // is 2.9.13 and exports only the old names; Linux distributions ship both + // eras (Debian/Ubuntu 2.9.x, Fedora/Arch >= 2.12), so the choice follows the + // installed version, not the target OS. src/xsltio.rs picks the symbol + // names with `cfg(libxml_xpath_new_names)`. + println!("cargo:rustc-check-cfg=cfg(libxml_xpath_new_names)"); + println!("cargo:rerun-if-env-changed=PHPR_LIBXML_NEW_XPATH"); + println!("cargo:rerun-if-env-changed=PKG_CONFIG_PATH"); + if libxml_uses_new_xpath_names() { + println!("cargo:rustc-cfg=libxml_xpath_new_names"); + } + println!("cargo:rerun-if-changed=build.rs"); +} + +/// Static musl build: the system libraries are glibc-built shared objects, so +/// a static phpr links the PIC archives that `scripts/build-musl-deps.sh` +/// installs in `$PHPR_MUSL_PREFIX` (default `~/.local/phpr-musl`). Archives +/// carry no dependency info, so gd's codecs and libxml2's zlib are listed +/// explicitly, each after the libraries that use it. +fn link_musl_static() { + let prefix = musl_prefix(); + let lib = format!("{prefix}/lib"); + if !std::path::Path::new(&lib).join("libgd.a").exists() { + panic!("static musl libraries not found in {lib}: run scripts/build-musl-deps.sh"); + } + println!("cargo:rustc-link-search=native={lib}"); + for name in [ + "gd", "avif", "aom", "webp", "sharpyuv", "jpeg", "png16", "tidy", "exslt", "xslt", + "xml2", "z", + ] { + println!("cargo:rustc-link-lib=static={name}"); + } +} + +fn musl_prefix() -> String { + std::env::var("PHPR_MUSL_PREFIX").unwrap_or_else(|_| { + format!("{}/.local/phpr-musl", std::env::var("HOME").unwrap_or_default()) + }) +} + +fn link_system_dylibs() { // macOS: gd lives in a Homebrew keg, not the default library path. // Linux: libgd is in the system library path; no explicit search needed. if cfg!(target_os = "macos") { @@ -18,20 +67,6 @@ fn main() { println!("cargo:rustc-link-lib=dylib=xslt"); println!("cargo:rustc-link-lib=dylib=exslt"); println!("cargo:rustc-link-lib=dylib=xml2"); - // libxml2 >= 2.12 renamed the XPath value-stack entry points `valuePop`/ - // `valuePush` to `xmlXPathValuePop`/`xmlXPathValuePush`, and the old names - // are gone by 2.15 (PR #3: Arch, libxml2 2.15.4). The macOS system libxml2 - // is 2.9.13 and exports only the old names; Linux distributions ship both - // eras (Debian/Ubuntu 2.9.x, Fedora/Arch >= 2.12), so the choice follows the - // installed version, not the target OS. src/xsltio.rs picks the symbol - // names with `cfg(libxml_xpath_new_names)`. - println!("cargo:rustc-check-cfg=cfg(libxml_xpath_new_names)"); - println!("cargo:rerun-if-env-changed=PHPR_LIBXML_NEW_XPATH"); - println!("cargo:rerun-if-env-changed=PKG_CONFIG_PATH"); - if libxml_uses_new_xpath_names() { - println!("cargo:rustc-cfg=libxml_xpath_new_names"); - } - println!("cargo:rerun-if-changed=build.rs"); } @@ -46,10 +81,14 @@ fn libxml_uses_new_xpath_names() -> bool { if cfg!(target_os = "macos") { return false; } - let probes: [(&str, &[&str]); 2] = [ - ("pkg-config", &["--modversion", "libxml-2.0"]), - ("xml2-config", &["--version"]), - ]; + // musl: the libxml2 that gets linked is the prefix's, not the system's. + let musl_xml2_config = format!("{}/bin/xml2-config", musl_prefix()); + let musl = std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("musl"); + let probes: Vec<(&str, &[&str])> = if musl { + vec![(musl_xml2_config.as_str(), &["--version"])] + } else { + vec![("pkg-config", &["--modversion", "libxml-2.0"]), ("xml2-config", &["--version"])] + }; for (cmd, args) in probes { let Ok(out) = std::process::Command::new(cmd).args(args).output() else { continue }; if !out.status.success() { diff --git a/php-rust/scripts/build-musl-deps.sh b/php-rust/scripts/build-musl-deps.sh new file mode 100755 index 00000000..49052d80 --- /dev/null +++ b/php-rust/scripts/build-musl-deps.sh @@ -0,0 +1,164 @@ +#!/usr/bin/env bash +# Build the C libraries phpr links (gd, tidy, libxslt/libexslt, libxml2 and gd's +# codecs) as static, position-independent musl archives, so that +# cargo build --release --target x86_64-unknown-linux-musl -p php-cli +# produces a fully static phpr. Distributions ship these libraries only as +# glibc-built shared objects, which cannot go into a static musl binary. +# +# Everything lands in $PHPR_MUSL_PREFIX (default ~/.local/phpr-musl), outside the +# repo; crates/php-types/build.rs reads the same variable on musl targets. +# +# Requirements (Linux x86_64 host): +# - musl C toolchain providing `musl-gcc` (Arch: `pacman -S musl`, +# Debian/Ubuntu: `apt install musl-tools`) +# - the Rust target: `rustup target add x86_64-unknown-linux-musl` (for the +# toolchain pinned in rust-toolchain.toml) +# - cmake, make, nasm (libaom's x86 assembly), curl, tar with xz support +# +# Process: +# 1. scripts/build-musl-deps.sh # once; download + build what is missing +# 2. cargo build --release --target x86_64-unknown-linux-musl -p php-cli +# -> target/x86_64-unknown-linux-musl/release/phpr (static-pie) +# scripts/build-musl-deps.sh --clean # wipe the prefix and rebuild everything +set -euo pipefail + +P="${PHPR_MUSL_PREFIX:-$HOME/.local/phpr-musl}" +SRC="$P/src" +JOBS="$(nproc)" + +if [[ "${1:-}" == "--clean" ]]; then + rm -rf "$P" +fi +mkdir -p "$SRC" "$P/lib" "$P/include" + +for tool in musl-gcc cmake nasm curl make; do + command -v "$tool" >/dev/null || { echo "missing tool: $tool" >&2; exit 1; } +done + +export CC=musl-gcc +export CFLAGS="-O2 -fPIC" +export CPPFLAGS="-I$P/include" +export LDFLAGS="-L$P/lib" +# Only the prefix's .pc files: never let a glibc system library leak in. +export PKG_CONFIG_LIBDIR="$P/lib/pkgconfig" +export PKG_CONFIG_PATH="" + +CMAKE_COMMON=( + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_C_COMPILER=musl-gcc + -DCMAKE_INSTALL_PREFIX="$P" + -DCMAKE_INSTALL_LIBDIR=lib + -DCMAKE_PREFIX_PATH="$P" + -DCMAKE_FIND_ROOT_PATH="$P" + -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY + -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY + -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY + -DCMAKE_POSITION_INDEPENDENT_CODE=ON + -DBUILD_SHARED_LIBS=OFF +) + +# fetch : download and unpack into $SRC/ +fetch() { + local url="$1" dir="$2" tarball="$SRC/${2}.tar" + [[ -d "$SRC/$dir" ]] && return 0 + echo ">> fetch $dir" + curl -fsSL "$url" -o "$tarball" + mkdir -p "$SRC/$dir" + tar -xf "$tarball" -C "$SRC/$dir" --strip-components=1 + rm -f "$tarball" +} + +# step : skip a library already installed in the prefix +step() { + if [[ -f "$P/lib/$2" ]]; then + echo ">> $1: already built" + return 1 + fi + echo ">> build $1" +} + +if step zlib libz.a; then + fetch https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz zlib + (cd "$SRC/zlib" && ./configure --prefix="$P" --static && make -j"$JOBS" && make install) +fi + +if step libpng libpng16.a; then + fetch https://github.com/pnggroup/libpng/archive/refs/tags/v1.6.47.tar.gz libpng + cmake -S "$SRC/libpng" -B "$SRC/libpng/_build" "${CMAKE_COMMON[@]}" \ + -DPNG_SHARED=OFF -DPNG_STATIC=ON -DPNG_TESTS=OFF -DPNG_TOOLS=OFF + cmake --build "$SRC/libpng/_build" -j"$JOBS" && cmake --install "$SRC/libpng/_build" +fi + +if step libjpeg-turbo libjpeg.a; then + fetch https://github.com/libjpeg-turbo/libjpeg-turbo/releases/download/3.1.0/libjpeg-turbo-3.1.0.tar.gz libjpeg-turbo + cmake -S "$SRC/libjpeg-turbo" -B "$SRC/libjpeg-turbo/_build" "${CMAKE_COMMON[@]}" \ + -DENABLE_SHARED=OFF -DENABLE_STATIC=ON -DWITH_TURBOJPEG=OFF + cmake --build "$SRC/libjpeg-turbo/_build" -j"$JOBS" && cmake --install "$SRC/libjpeg-turbo/_build" +fi + +if step libwebp libwebp.a; then + fetch https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.5.0.tar.gz libwebp + cmake -S "$SRC/libwebp" -B "$SRC/libwebp/_build" "${CMAKE_COMMON[@]}" \ + -DWEBP_BUILD_ANIM_UTILS=OFF -DWEBP_BUILD_CWEBP=OFF -DWEBP_BUILD_DWEBP=OFF \ + -DWEBP_BUILD_GIF2WEBP=OFF -DWEBP_BUILD_IMG2WEBP=OFF -DWEBP_BUILD_VWEBP=OFF \ + -DWEBP_BUILD_WEBPINFO=OFF -DWEBP_BUILD_WEBPMUX=OFF -DWEBP_BUILD_EXTRAS=OFF + cmake --build "$SRC/libwebp/_build" -j"$JOBS" && cmake --install "$SRC/libwebp/_build" +fi + +# AVIF: libaom serves as both decoder and encoder (gdImageCreateFromAvifPtr and +# gdImageAvifPtrEx), so one codec covers what the system libavif provides. +if step libaom libaom.a; then + fetch https://storage.googleapis.com/aom-releases/libaom-3.12.1.tar.gz libaom + # NASM 3.x reworded `nasm -hf`, so aom's probe for "-Ox" fails although the + # flag still works: relax the probe to the bare "-O" option. + sed -i 's/MATCHES "-Ox"/MATCHES "-O"/' "$SRC/libaom/build/cmake/aom_optimization.cmake" + cmake -S "$SRC/libaom" -B "$SRC/libaom/_build" "${CMAKE_COMMON[@]}" \ + -DENABLE_DOCS=OFF -DENABLE_EXAMPLES=OFF -DENABLE_TESTS=OFF -DENABLE_TOOLS=OFF \ + -DENABLE_TESTDATA=OFF -DCONFIG_TUNE_VMAF=0 -DCONFIG_TUNE_BUTTERAUGLI=0 + cmake --build "$SRC/libaom/_build" -j"$JOBS" && cmake --install "$SRC/libaom/_build" +fi + +if step libavif libavif.a; then + fetch https://github.com/AOMediaCodec/libavif/archive/refs/tags/v1.3.0.tar.gz libavif + cmake -S "$SRC/libavif" -B "$SRC/libavif/_build" "${CMAKE_COMMON[@]}" \ + -DAVIF_CODEC_AOM=SYSTEM -DAVIF_LIBYUV=OFF -DAVIF_LIBSHARPYUV=OFF \ + -DAVIF_LIBXML2=OFF -DAVIF_BUILD_APPS=OFF -DAVIF_BUILD_TESTS=OFF + cmake --build "$SRC/libavif/_build" -j"$JOBS" && cmake --install "$SRC/libavif/_build" +fi + +if step libgd libgd.a; then + fetch https://github.com/libgd/libgd/releases/download/gd-2.3.3/libgd-2.3.3.tar.xz libgd + # Only the library and headers: gd's sample tools do not link statically + # (libtool puts -lz ahead of libpng16.a) and phpr does not need them. + (cd "$SRC/libgd" && ./configure --prefix="$P" --disable-shared --enable-static \ + --with-zlib="$P" --with-png="$P" --with-jpeg="$P" --with-webp="$P" --with-avif="$P" \ + --without-freetype --without-fontconfig --without-xpm --without-tiff \ + --without-heif --without-liq --without-raqm --without-x \ + && make -j"$JOBS" -C src libgd.la && make -C src install-libLTLIBRARIES install-includeHEADERS) +fi + +if step libxml2 libxml2.a; then + fetch https://download.gnome.org/sources/libxml2/2.15/libxml2-2.15.1.tar.xz libxml2 + (cd "$SRC/libxml2" && ./configure --prefix="$P" --disable-shared --enable-static \ + --without-python --without-icu --without-lzma --with-zlib="$P" \ + && make -j"$JOBS" && make install) +fi + +if step libxslt libxslt.a; then + fetch https://download.gnome.org/sources/libxslt/1.1/libxslt-1.1.45.tar.xz libxslt + (cd "$SRC/libxslt" && ./configure --prefix="$P" --disable-shared --enable-static \ + --without-python --without-crypto --with-libxml-prefix="$P" \ + && make -j"$JOBS" && make install) +fi + +if step tidy libtidy.a; then + fetch https://github.com/htacg/tidy-html5/archive/refs/tags/5.8.0.tar.gz tidy + cmake -S "$SRC/tidy" -B "$SRC/tidy/_build" "${CMAKE_COMMON[@]}" \ + -DBUILD_SHARED_LIB=OFF -DSUPPORT_CONSOLE_APP=OFF -DBUILD_DOCUMENTATION=OFF \ + -DCMAKE_POLICY_VERSION_MINIMUM=3.5 + cmake --build "$SRC/tidy/_build" -j"$JOBS" && cmake --install "$SRC/tidy/_build" + # tidy-html5 names its static archive libtidy_static.a on some versions. + [[ -f "$P/lib/libtidy.a" ]] || cp "$P/lib/libtidy_static.a" "$P/lib/libtidy.a" +fi + +echo ">> done: static musl libraries in $P/lib"