Skip to content

Commit 73202bd

Browse files
committed
fix llvm-ar as archiver for msvc targets
this is somewhat a nasty hack here, but it does work. when (cross) compiling for msvc and you use llvm-ar as the archiver, then you need to use gnu-style flags. currently it attemps to run llvm-ar like: ``` running: "/usr/bin/llvm-ar" "-out:/workspaces/ars/build_windows_clang_x86_64_relnoopt/./cargo/build/x86_64-pc-windows-msvc/release/build/link-cplusplus-b04e59fc086748c2/out/liblink-cplusplus.a" "-nologo" "/workspaces/ars/build_windows_clang_x86_64_relnoopt/./cargo/build/x86_64-pc-windows-msvc/release/build/link-cplusplus-b04e59fc086748c2/out/0466faf07398e270-dummy.o" ``` which doesn't work, as llvm-ar takes in GNU-style ar flags. i'm definitely open to a more robust way to get the archiver "Family" (maybe something similar to how we detect compiler family? idk) if that would be better
1 parent 30c6b72 commit 73202bd

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,7 +2398,8 @@ impl Build {
23982398
}
23992399

24002400
let target = self.get_target()?;
2401-
if target.contains("msvc") {
2401+
let (mut ar, cmd, _any_flags) = self.get_ar()?;
2402+
if target.contains("msvc") && ! ar.get_program().to_str().unwrap().contains("llvm-") {
24022403
// The Rust compiler will look for libfoo.a and foo.lib, but the
24032404
// MSVC linker will also be passed foo.lib, so be sure that both
24042405
// exist for now.
@@ -2421,7 +2422,6 @@ impl Build {
24212422
// Non-msvc targets (those using `ar`) need a separate step to add
24222423
// the symbol table to archives since our construction command of
24232424
// `cq` doesn't add it for us.
2424-
let (mut ar, cmd, _any_flags) = self.get_ar()?;
24252425

24262426
// NOTE: We add `s` even if flags were passed using $ARFLAGS/ar_flag, because `s`
24272427
// here represents a _mode_, not an arbitrary flag. Further discussion of this choice
@@ -2435,8 +2435,8 @@ impl Build {
24352435
fn assemble_progressive(&self, dst: &Path, objs: &[&Path]) -> Result<(), Error> {
24362436
let target = self.get_target()?;
24372437

2438-
if target.contains("msvc") {
2439-
let (mut cmd, program, any_flags) = self.get_ar()?;
2438+
let (mut cmd, program, any_flags) = self.get_ar()?;
2439+
if target.contains("msvc") && !cmd.get_program().to_str().unwrap().contains("llvm-") {
24402440
// NOTE: -out: here is an I/O flag, and so must be included even if $ARFLAGS/ar_flag is
24412441
// in use. -nologo on the other hand is just a regular flag, and one that we'll skip if
24422442
// the caller has explicitly dictated the flags they want. See
@@ -2455,8 +2455,6 @@ impl Build {
24552455
cmd.args(objs);
24562456
run(&mut cmd, &program, &self.cargo_output)?;
24572457
} else {
2458-
let (mut ar, cmd, _any_flags) = self.get_ar()?;
2459-
24602458
// Set an environment variable to tell the OSX archiver to ensure
24612459
// that all dates listed in the archive are zero, improving
24622460
// determinism of builds. AFAIK there's not really official
@@ -2479,12 +2477,12 @@ impl Build {
24792477
//
24802478
// In any case if this doesn't end up getting read, it shouldn't
24812479
// cause that many issues!
2482-
ar.env("ZERO_AR_DATE", "1");
2480+
cmd.env("ZERO_AR_DATE", "1");
24832481

24842482
// NOTE: We add cq here regardless of whether $ARFLAGS/ar_flag have been used because
24852483
// it dictates the _mode_ ar runs in, which the setter of $ARFLAGS/ar_flag can't
24862484
// dictate. See https://github.com/rust-lang/cc-rs/pull/763 for further discussion.
2487-
run(ar.arg("cq").arg(dst).args(objs), &cmd, &self.cargo_output)?;
2485+
run(cmd.arg("cq").arg(dst).args(objs), &program, &self.cargo_output)?;
24882486
}
24892487

24902488
Ok(())

0 commit comments

Comments
 (0)