Skip to content

Commit 24ed855

Browse files
committed
ci: add musl build step
1 parent 3f6595d commit 24ed855

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

.github/workflows/build.yml

Lines changed: 21 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,23 @@ 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
104112
env:
105113
CARGO_TERM_COLOR: always
106114
steps:
107115
- name: Checkout code
108116
uses: actions/checkout@v5
117+
- name: Install musl toolchain
118+
if: matrix.target == 'musl'
119+
run: |
120+
sudo apt-get update
121+
sudo apt-get install -y musl-tools musl-dev
109122
- name: Setup PHP
110123
uses: shivammathur/setup-php@v2
111124
with:
@@ -118,6 +131,9 @@ jobs:
118131
with:
119132
toolchain: ${{ matrix.rust }}
120133
components: rustfmt, clippy
134+
- name: Add musl target
135+
if: matrix.target == 'musl'
136+
run: rustup target add x86_64-unknown-linux-musl
121137
- run: rustup show
122138
- name: Cache cargo dependencies
123139
uses: Swatinem/rust-cache@v2
@@ -162,13 +178,15 @@ jobs:
162178
- name: Build
163179
env:
164180
EXT_PHP_RS_TEST: ""
165-
run: cargo build --release --features closure,anyhow,runtime --workspace
181+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
182+
run: cargo build --release --features closure,anyhow,runtime --workspace ${{ matrix.target == 'musl' && '--target x86_64-unknown-linux-musl' || '' }}
166183
# Test
167184
- name: Test inline examples
168185
# Macos fails on unstable rust. We skip the inline examples test for now.
169186
if: "!(contains(matrix.os, 'macos') && matrix.rust == 'nightly')"
170-
run: cargo test --release --workspace --features closure,anyhow,runtime --no-fail-fast
171-
187+
env:
188+
RUSTFLAGS: ${{ matrix.target == 'musl' && '-C target-feature=-crt-static' || '' }}
189+
run: cargo test --release --workspace --features closure,anyhow,runtime --no-fail-fast ${{ matrix.target == 'musl' && '--target x86_64-unknown-linux-musl' || '' }}
172190
test-embed:
173191
name: Test with embed
174192
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@
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+
}
815
}

0 commit comments

Comments
 (0)