Skip to content

Commit 6c00252

Browse files
committed
ci: add musl build step
1 parent f3f0875 commit 6c00252

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

.github/workflows/build.yml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ jobs:
8585
name: Build and Test
8686
runs-on: ${{ matrix.os }}
8787
strategy:
88+
fail-fast: false
8889
matrix:
8990
os: [ubuntu-latest, macos-latest, windows-latest]
9091
php: ["8.0", "8.1", "8.2", "8.3", "8.4"]
9192
rust: [stable, nightly]
9293
clang: ["15", "17"]
9394
phpts: [ts, nts]
95+
target: [gnu, musl]
9496
exclude:
9597
# ext-php-rs requires nightly Rust when on Windows.
9698
- os: windows-latest
@@ -101,11 +103,26 @@ jobs:
101103
clang: "15"
102104
- os: windows-latest
103105
clang: "15"
106+
# musl only supported on Linux with stable Rust
107+
- os: windows-latest
108+
target: musl
109+
- os: macos-latest
110+
target: musl
111+
- target: musl
112+
rust: nightly
113+
# Skip PHP 8.0 for musl (EOL)
114+
- target: musl
115+
php: "8.0"
104116
env:
105117
CARGO_TERM_COLOR: always
106118
steps:
107119
- name: Checkout code
108120
uses: actions/checkout@v5
121+
- name: Install musl toolchain
122+
if: matrix.target == 'musl'
123+
run: |
124+
sudo apt-get update
125+
sudo apt-get install -y musl-tools musl-dev
109126
- name: Setup PHP
110127
uses: shivammathur/setup-php@v2
111128
with:
@@ -118,6 +135,9 @@ jobs:
118135
with:
119136
toolchain: ${{ matrix.rust }}
120137
components: rustfmt, clippy
138+
- name: Add musl target
139+
if: matrix.target == 'musl'
140+
run: rustup target add x86_64-unknown-linux-musl
121141
- run: rustup show
122142
- name: Cache cargo dependencies
123143
uses: Swatinem/rust-cache@v2
@@ -162,12 +182,15 @@ jobs:
162182
- name: Build
163183
env:
164184
EXT_PHP_RS_TEST: ""
165-
run: cargo build --release --features closure,anyhow,runtime --workspace ${{ matrix.php == '8.0' && '--no-default-features' || '' }}
185+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
186+
run: cargo build --release --features closure,anyhow,runtime --workspace ${{ matrix.php == '8.0' && '--no-default-features' || '' }} ${{ matrix.target == 'musl' && '--target x86_64-unknown-linux-musl' || '' }}
166187
# Test
167188
- name: Test inline examples
168189
# Macos fails on unstable rust. We skip the inline examples test for now.
169190
if: "!(contains(matrix.os, 'macos') && matrix.rust == 'nightly')"
170-
run: cargo test --release --workspace --features closure,anyhow,runtime --no-fail-fast ${{ matrix.php == '8.0' && '--no-default-features' || '' }}
191+
env:
192+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
193+
run: cargo test --release --workspace --features closure,anyhow,runtime --no-fail-fast ${{ matrix.php == '8.0' && '--no-default-features' || '' }} ${{ matrix.target == 'musl' && '--target x86_64-unknown-linux-musl' || '' }}
171194
build-zts:
172195
name: Build with ZTS
173196
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

tests/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
// On macOS, allow undefined symbols for PHP extensions
3+
// PHP symbols will be resolved at runtime when the extension is loaded by PHP
4+
#[cfg(target_os = "macos")]
5+
{
6+
println!("cargo:rustc-link-arg=-Wl,-undefined,dynamic_lookup");
7+
}
8+
}

0 commit comments

Comments
 (0)