Skip to content

Commit 1a7d2a8

Browse files
autarchclaude
andcommitted
Always use cross for musl targets on Linux
Ubuntu runners lack musl-g++, which causes C++ compilation to fail for musl targets even when the host architecture matches. Forcing cross for all musl targets on Linux fixes this since cross's Docker images have a complete musl toolchain including C++. Fixes GH #22. Also adds a C++ file to test-project (compiled via the cc crate) to catch regressions of this in CI, and removes the now-dead musl-tools install step from action.yml. Reported by @RaulTrombin (Raul Victor Trombin). GH #22. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f481cb5 commit 1a7d2a8

9 files changed

Lines changed: 59 additions & 25 deletions

File tree

.github/workflows/test.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ jobs:
2525

2626
- name: Linux-x86_64
2727
runs-on: ubuntu-24.04
28-
# It's important to use a musl target here to make sure that the step to install
29-
# musl-tools works.
3028
target: x86_64-unknown-linux-musl
3129
cache-cross-binary: true
3230
expect-file-re: "ELF.+x86-64"
33-
expect-cross: ""
34-
expect-stripped: "--expect-stripped"
31+
expect-cross: "--expect-cross"
32+
expect-stripped: ""
3533
can-execute: true
3634

3735
- name: Linux-x86_64 (beta)
@@ -40,8 +38,8 @@ jobs:
4038
toolchain: beta
4139
cache-cross-binary: true
4240
expect-file-re: "ELF.+x86-64"
43-
expect-cross: ""
44-
expect-stripped: "--expect-stripped"
41+
expect-cross: "--expect-cross"
42+
expect-stripped: ""
4543
can-execute: true
4644

4745
- name: Linux-x86_64 (nightly)
@@ -50,8 +48,8 @@ jobs:
5048
toolchain: nightly
5149
cache-cross-binary: true
5250
expect-file-re: "ELF.+x86-64"
53-
expect-cross: ""
54-
expect-stripped: "--expect-stripped"
51+
expect-cross: "--expect-cross"
52+
expect-stripped: ""
5553
can-execute: true
5654

5755
- name: Linux-x86_64 (force cross)

Changes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
- Fixed a bug in calculating the `cross` binary's hash.
44
- Fixed cache key handling to deal with spaces in cache key elements, for example in the OS version.
55
Reported by @gdubicki (Greg Dubicki). GH #50. Fixes #51.
6+
- This action now always uses `cross` for `musl` targets on Linux, even when the host architecture
7+
matches the target. This fixes C++ compilation failures caused by the lack of `musl-g++` on Ubuntu
8+
runners. Reported by @RaulTrombin (Raul Victor Trombin). GH #22.
69

710
## 1.0.6 - 2026-03-15
811

action.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,6 @@ runs:
157157
env:
158158
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
159159

160-
- name: Install musl-tools on Linux if target includes "musl"
161-
shell: bash
162-
run: |
163-
if dpkg -l musl-tools | grep -q "^ii\s*musl-tools"; then
164-
exit 0
165-
fi
166-
sudo apt-get update --yes && \
167-
sudo apt-get install --yes musl-tools
168-
if:
169-
steps.determine-cross-compile.outputs.needs-cross != 'true' && contains(inputs.target,
170-
'musl')
171-
172160
- name: Set build command
173161
id: set-build-command
174162
shell: bash

set-cross-compile.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ def check_needs_cross(target: str) -> bool:
5353

5454
target = target.lower()
5555

56-
# Check for x86_64 Linux targets on x86_64 Linux host
56+
# Check for x86_64 Linux targets on x86_64 Linux host. We always use cross for musl targets
57+
# because the Ubuntu runner lacks a C++ compiler for musl (musl-g++).
5758
if (
58-
re.search(r"x86_64.+linux-(?:gnu|musl)", target)
59+
re.search(r"x86_64.+linux-gnu", target)
5960
and "x86_64" in system_info
6061
and "linux" in system_info
6162
):
@@ -66,9 +67,10 @@ def check_needs_cross(target: str) -> bool:
6667
# because then we need 32-bit C headers, 32-bit C libs to link to, etc.
6768

6869
# Check if both host and target are ARM Linux. I'm assuming here that for things like
69-
# "arm-linux-androideabi" or "armv7-unknown-linux-ohos" we'd still need cross.
70+
# "arm-linux-androideabi" or "armv7-unknown-linux-ohos" we'd still need cross. We always use
71+
# cross for musl targets because the Ubuntu runner lacks a C++ compiler for musl (musl-g++).
7072
if (
71-
re.search(r"(?:aarch64|arm).+linux-(?:gnu|musl)", target)
73+
re.search(r"(?:aarch64|arm).+linux-gnu", target)
7274
and ("arm" in system_info or "aarch64" in system_info)
7375
and "linux" in system_info
7476
):

test-project/Cargo.lock

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-project/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ path = "src/bin1.rs"
1515
name = "bin2"
1616
path = "src/bin2.rs"
1717

18+
[build-dependencies]
19+
cc = "1"
20+
1821
# workaround for https://github.com/cross-rs/cross/issues/1345
1922
[package.metadata.cross.target.x86_64-unknown-netbsd]
2023
pre-build = [

test-project/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
cc::Build::new()
3+
.cpp(true)
4+
.file("src/hello.cpp")
5+
.compile("hello");
6+
}

test-project/src/bin1.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
extern "C" {
2+
fn hello_cpp() -> i32;
3+
}
4+
15
fn main() {
6+
let _ = unsafe { hello_cpp() };
27
println!("Hello, world!");
38
}
49

test-project/src/hello.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extern "C" int hello_cpp() {
2+
int a = 3, b = 1;
3+
return a < b ? a : b;
4+
}

0 commit comments

Comments
 (0)