Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4013,7 +4013,14 @@ impl Build {
for obj in objs {
match obj.strip_prefix(dir) {
Ok(relative) => cmd.arg(relative),
Err(_) if obj.is_relative() => cmd.arg(current_dir.join(obj)),
// A relative object that exists from the current
// directory is passed by its absolute path. Any other
// name is passed as given, so the archiver looks for it
// in the folder it runs in and then along `LIB`, where it
// finds system libraries such as `Ole32.lib`.
Err(_) if obj.is_relative() && obj.exists() => {
cmd.arg(current_dir.join(obj))
}
Err(_) => cmd.arg(obj),
};
}
Expand Down
36 changes: 31 additions & 5 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4624,10 +4624,12 @@ fn msvc_create_archive() {
fn msvc_archive_members_are_relative_to_out_dir() {
let test = Test::msvc();
let elsewhere = test.td.path().parent().unwrap().join("elsewhere.o");
// Tests run in the package folder, so `Cargo.toml` exists from the current
// directory. The shim doesn't read objects.
test.gcc()
.file("foo.c")
.object(&elsewhere)
.object("relative.o")
.object("Cargo.toml")
.compile("foo");

// lib.exe names each member by the path it is given, so the objects in
Expand All @@ -4643,7 +4645,30 @@ fn msvc_archive_members_are_relative_to_out_dir() {
ar.must_have(out)
.must_have("9bb0a3195b9dd27a-foo.o")
.must_have(&elsewhere)
.must_have(env::current_dir().unwrap().join("relative.o"));
.must_have(env::current_dir().unwrap().join("Cargo.toml"));
}

#[test]
fn msvc_archive_passes_unknown_names_as_given() {
let test = Test::msvc();
test.gcc()
.file("foo.c")
.object("Ole32.lib")
.object("missing/foo.obj")
.compile("foo");

// Names that aren't found from the current directory are passed as given,
// and lib.exe looks for them in OUT_DIR and then along `LIB`.
let ar = test.cmd(1);
assert_eq!(
ar.cwd.canonicalize().unwrap(),
test.td.path().canonicalize().unwrap()
);
let current_dir = env::current_dir().unwrap();
ar.must_have("Ole32.lib")
.must_not_have(current_dir.join("Ole32.lib"))
.must_have("missing/foo.obj")
.must_not_have(current_dir.join("missing/foo.obj"));
}

#[test]
Expand Down Expand Up @@ -4725,15 +4750,16 @@ fn msvc_archive_arm64ec_members_are_relative_to_out_dir() {
fn msvc_archive_batches_are_relative_to_out_dir() {
let test = Test::msvc();
// The archiver takes about 4000 bytes of paths at a time, so these go in
// two batches: `foo.o` and the first, then the second and `relative.o`.
// two batches: `foo.o` and the first, then the second and `Cargo.toml`,
// which exists from the current directory.
let objects: Vec<_> = (0..2)
.map(|i| test.td.path().join(format!("{i:0>3000}.o")))
.collect();
test.gcc()
.file("foo.c")
.object(&objects[0])
.object(&objects[1])
.object("relative.o")
.object("Cargo.toml")
.compile("foo");

// Every batch runs in OUT_DIR, and a relative object from elsewhere is
Expand All @@ -4750,7 +4776,7 @@ fn msvc_archive_batches_are_relative_to_out_dir() {
.must_have(objects[0].file_name().unwrap());
test.cmd(2)
.must_have(objects[1].file_name().unwrap())
.must_have(env::current_dir().unwrap().join("relative.o"));
.must_have(env::current_dir().unwrap().join("Cargo.toml"));
}

#[test]
Expand Down
Loading