Skip to content

Commit 57911a4

Browse files
committed
ci: add musl build step
1 parent 73f3d21 commit 57911a4

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

.github/workflows/build.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ jobs:
9191
rust: [stable, nightly]
9292
clang: ["15", "17"]
9393
phpts: [ts, nts]
94+
target: [gnu, musl]
9495
exclude:
9596
# ext-php-rs requires nightly Rust when on Windows.
9697
- os: windows-latest
@@ -101,11 +102,26 @@ jobs:
101102
clang: "15"
102103
- os: windows-latest
103104
clang: "15"
105+
# musl only supported on Linux with stable Rust
106+
- os: windows-latest
107+
target: musl
108+
- os: macos-latest
109+
target: musl
110+
- target: musl
111+
rust: nightly
112+
# Skip PHP 8.0 for musl (EOL)
113+
- target: musl
114+
php: "8.0"
104115
env:
105116
CARGO_TERM_COLOR: always
106117
steps:
107118
- name: Checkout code
108119
uses: actions/checkout@v5
120+
- name: Install musl toolchain
121+
if: matrix.target == 'musl'
122+
run: |
123+
sudo apt-get update
124+
sudo apt-get install -y musl-tools musl-dev
109125
- name: Setup PHP
110126
uses: shivammathur/setup-php@v2
111127
with:
@@ -118,6 +134,9 @@ jobs:
118134
with:
119135
toolchain: ${{ matrix.rust }}
120136
components: rustfmt, clippy
137+
- name: Add musl target
138+
if: matrix.target == 'musl'
139+
run: rustup target add x86_64-unknown-linux-musl
121140
- run: rustup show
122141
- name: Cache cargo dependencies
123142
uses: Swatinem/rust-cache@v2
@@ -162,12 +181,15 @@ jobs:
162181
- name: Build
163182
env:
164183
EXT_PHP_RS_TEST: ""
165-
run: cargo build --release --features closure,anyhow --workspace ${{ matrix.php == '8.0' && '--no-default-features' || '' }}
184+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
185+
run: cargo build --release --features closure,anyhow --workspace ${{ matrix.php == '8.0' && '--no-default-features' || '' }} ${{ matrix.target == 'musl' && '--target x86_64-unknown-linux-musl' || '' }}
166186
# Test
167187
- name: Test inline examples
168188
# Macos fails on unstable rust. We skip the inline examples test for now.
169189
if: "!(contains(matrix.os, 'macos') && matrix.rust == 'nightly')"
170-
run: cargo test --release --workspace --features closure,anyhow --no-fail-fast ${{ matrix.php == '8.0' && '--no-default-features' || '' }}
190+
env:
191+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
192+
run: cargo test --release --workspace --features closure,anyhow --no-fail-fast ${{ matrix.php == '8.0' && '--no-default-features' || '' }} ${{ matrix.target == 'musl' && '--target x86_64-unknown-linux-musl' || '' }}
171193
build-zts:
172194
name: Build with ZTS
173195
runs-on: ubuntu-latest

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ best resource at the moment. This can be viewed at [docs.rs].
126126
1.57 at the time of writing.
127127
- Clang 5.0 or later.
128128

129+
### Alpine Linux (musl) Requirements
130+
131+
Building for Alpine Linux (musl libc) is supported on stable Rust with the following
132+
requirements:
133+
134+
- Install musl toolchain: `sudo apt-get install musl-tools musl-dev`
135+
- Add musl target: `rustup target add x86_64-unknown-linux-musl`
136+
- Build with dynamic linking flag:
137+
```bash
138+
RUSTFLAGS="-C target-feature=-crt-static" \
139+
cargo build --target x86_64-unknown-linux-musl
140+
```
141+
142+
**Note**: Building for musl requires dynamic CRT linking (`-crt-static` flag) to produce
143+
the `cdylib` output required for PHP extensions.
144+
129145
### Windows Requirements
130146

131147
- Extensions can only be compiled for PHP installations sourced from

unix_build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ impl<'a> PHPProvider<'a> for Provider<'a> {
6565
#[cfg(feature = "embed")]
6666
println!("cargo:rustc-link-lib=php");
6767

68+
// On macOS, allow undefined symbols for PHP extensions
69+
// PHP symbols will be resolved at runtime when the extension is loaded by PHP
70+
#[cfg(target_os = "macos")]
71+
{
72+
println!("cargo:rustc-cdylib-link-arg=-Wl,-undefined,dynamic_lookup");
73+
}
74+
6875
Ok(())
6976
}
7077
}

0 commit comments

Comments
 (0)