Skip to content

Fix musl compilation: Add musl as a prefix fallback #1453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2884,14 +2884,21 @@ impl Build {
"qcc".to_string()
}
} else if self.get_is_cross_compile()? {
let prefix = self.prefix_for_target(&raw_target);
match prefix {
Some(prefix) => {
self.prefix_for_target(&raw_target)
.filter_map(|prefix| {
let cc = if target.abi == "llvm" { clang } else { gnu };
format!("{}-{}", prefix, cc)
}
None => default.to_string(),
}
let prefixed_cc = format!("{}-{}", prefix, cc);
let status = Command::new(&prefixed_cc)
.arg("--version")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.ok()?;
status.success().then_some(prefixed_cc)
})
.next()
.unwrap_or_else(|| default.to_string())
} else {
default.to_string()
};
Expand Down Expand Up @@ -3335,16 +3342,17 @@ impl Build {
name = format!("g{}", tool).into();
self.cmd(&name)
} else if self.get_is_cross_compile()? {
match self.prefix_for_target(&self.get_raw_target()?) {
Some(prefix) => {
let chosen = self
.prefix_for_target(&self.get_raw_target()?)
.filter_map(|prefix| {
// GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both.
// Prefer -ar if it exists, as builds of `-gcc-ar` have been observed to be
// outright broken (such as when targeting freebsd with `--disable-lto`
// toolchain where the archiver attempts to load the LTO plugin anyway but
// fails to find one).
//
// The same applies to ranlib.
let chosen = ["", "-gcc"]
["", "-gcc"]
.iter()
.filter_map(|infix| {
let target_p = format!("{prefix}{infix}-{tool}");
Expand All @@ -3358,15 +3366,12 @@ impl Build {
status.success().then_some(target_p)
})
.next()
.unwrap_or_else(|| tool.to_string());
name = chosen.into();
self.cmd(&name)
}
None => {
name = tool.into();
self.cmd(&name)
}
}
})
.next()
.unwrap_or_else(|| tool.to_string());

name = chosen.into();
self.cmd(&name)
} else {
name = tool.into();
self.cmd(&name)
Expand All @@ -3378,22 +3383,23 @@ impl Build {
}

// FIXME: Use parsed target instead of raw target.
fn prefix_for_target(&self, target: &str) -> Option<Cow<'static, str>> {
fn prefix_for_target(&self, target: &str) -> impl Iterator<Item = Cow<'static, str>> {
// CROSS_COMPILE is of the form: "arm-linux-gnueabi-"
self.getenv("CROSS_COMPILE")
.as_deref()
.map(|s| s.to_string_lossy().trim_end_matches('-').to_owned())
.map(Cow::Owned)
.or_else(|| {
.into_iter()
.chain(
// Put aside RUSTC_LINKER's prefix to be used as second choice, after CROSS_COMPILE
self.getenv("RUSTC_LINKER").and_then(|var| {
var.to_string_lossy()
.strip_suffix("-gcc")
.map(str::to_string)
.map(Cow::Owned)
})
})
.or_else(|| {
}),
)
.chain(
match target {
// Note: there is no `aarch64-pc-windows-gnu` target, only `-gnullvm`
"aarch64-pc-windows-gnullvm" => Some("aarch64-w64-mingw32"),
Expand Down Expand Up @@ -3514,8 +3520,13 @@ impl Build {
"x86_64-unknown-netbsd" => Some("x86_64--netbsd"),
_ => None,
Copy link
Collaborator

@madsmtm madsmtm Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all of this might be unnecessary? We should just use:

"x86_64-unknown-linux-musl" => self.find_working_gnu_prefix(&["x86_64-linux-musl", "musl"]),

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will open another PR for this (since it's easier to start from scratch)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by #1455

}
.map(Cow::Borrowed)
})
.map(Cow::Borrowed),
)
.chain(
(target == "x86_64-unknown-linux-musl")
.then_some("musl")
.map(Cow::Borrowed),
)
}

/// Some platforms have multiple, compatible, canonical prefixes. Look through
Expand Down
Loading