Skip to content

Commit 0571c43

Browse files
committed
ci: add musl build step
1 parent 3f6595d commit 0571c43

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

.github/workflows/build.yml

Lines changed: 24 additions & 3 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,13 +181,15 @@ jobs:
162181
- name: Build
163182
env:
164183
EXT_PHP_RS_TEST: ""
165-
run: cargo build --release --features closure,anyhow,runtime --workspace
184+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
185+
run: cargo build --release --features closure,anyhow,runtime --workspace ${{ 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,runtime --no-fail-fast
171-
190+
env:
191+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
192+
run: cargo test --release --workspace --features closure,anyhow,runtime --no-fail-fast ${{ matrix.target == 'musl' && '--target x86_64-unknown-linux-musl' || '' }}
172193
test-embed:
173194
name: Test with embed
174195
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

crates/cli/build.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,18 @@
55
66
fn main() {
77
println!("cargo:rustc-link-arg-bins=-rdynamic");
8+
9+
// On musl targets, allow undefined symbols for PHP runtime functions
10+
// sigsetjmp and PHP symbols will be resolved when the binary runs with PHP loaded
11+
#[cfg(target_env = "musl")]
12+
{
13+
println!("cargo:rustc-link-arg-bins=-Wl,--unresolved-symbols=ignore-all");
14+
}
15+
16+
// On macOS, use dynamic lookup for undefined symbols
17+
// This allows PHP symbols to be resolved at runtime
18+
#[cfg(target_os = "macos")]
19+
{
20+
println!("cargo:rustc-link-arg-bins=-Wl,-undefined,dynamic_lookup");
21+
}
822
}

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)