Skip to content

Commit c340325

Browse files
committed
Remap dylib paths into the sysroot to be relative to the sysroot
Like for rlibs, the paths on the linker command line need to be relative paths if the sysroot was specified by the user to be a relative path. Dylibs put the path in /LIBPATH instead of into the file path of the library itself, so we rehome the libpath and adjust the rehoming function to be able to support both use cases, rlibs and dylibs.
1 parent 4b83156 commit c340325

File tree

1 file changed

+22
-16
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+22
-16
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -2682,27 +2682,27 @@ fn add_upstream_native_libraries(
26822682
}
26832683
}
26842684

2685-
// Rehome sysroot lib paths to be relative to the sysroot, which may be a relative
2686-
// path specified by the user. If the sysroot is a relative path, and the sysroot rlibs
2687-
// are specified as an absolute path, the linker command line can be non-deterministic
2688-
// due to the paths including the current working directory. The linker command line
2689-
// needs to be deterministic since it appears inside the PDB file generated by the MSVC
2690-
// linker. See https://github.com/rust-lang/rust/issues/112586.
2691-
fn rehome_sysroot_rlib_path<'a>(sess: &'a Session, rlib_path: PathBuf) -> PathBuf {
2685+
// Rehome lib paths (which exclude the library file name) that point into the sysroot lib directory
2686+
// to be relative to the sysroot directory, which may be a relative path specified by the user.
2687+
//
2688+
// If the sysroot is a relative path, and the sysroot libs are specified as an absolute path, the
2689+
// linker command line can be non-deterministic due to the paths including the current working
2690+
// directory. The linker command line needs to be deterministic since it appears inside the PDB
2691+
// file generated by the MSVC linker. See https://github.com/rust-lang/rust/issues/112586.
2692+
//
2693+
// The returned path will always have `fix_windows_verbatim_for_gcc()` applied to it.
2694+
fn rehome_sysroot_lib_dir<'a>(sess: &'a Session, lib_dir: &Path) -> PathBuf {
26922695
let sysroot_lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
26932696
let canonical_sysroot_lib_path =
26942697
{ try_canonicalize(&sysroot_lib_path).unwrap_or_else(|_| sysroot_lib_path.clone()) };
26952698

2696-
let mut canonical_rlib_dir = try_canonicalize(&rlib_path).unwrap_or_else(|_| rlib_path.clone());
2697-
canonical_rlib_dir.pop();
2698-
2699-
if canonical_rlib_dir == canonical_sysroot_lib_path {
2700-
// The `susroot_lib_path` returned by `target_filesearch().get_lib_path()` has
2699+
let canonical_lib_dir = try_canonicalize(lib_dir).unwrap_or_else(|_| lib_dir.to_path_buf());
2700+
if canonical_lib_dir == canonical_sysroot_lib_path {
2701+
// This path, returned by `target_filesearch().get_lib_path()`, has
27012702
// already had `fix_windows_verbatim_for_gcc()` applied if needed.
27022703
sysroot_lib_path
2703-
.join(rlib_path.file_name().expect("rlib path has no file name path component"))
27042704
} else {
2705-
rlib_path
2705+
fix_windows_verbatim_for_gcc(&lib_dir)
27062706
}
27072707
}
27082708

@@ -2737,7 +2737,13 @@ fn add_static_crate<'a>(
27372737
let cratepath = &src.rlib.as_ref().unwrap().0;
27382738

27392739
let mut link_upstream = |path: &Path| {
2740-
cmd.link_rlib(&rehome_sysroot_rlib_path(sess, fix_windows_verbatim_for_gcc(path)));
2740+
let rlib_path = if let Some(dir) = path.parent() {
2741+
let file_name = path.file_name().expect("rlib path has no file name path component");
2742+
rehome_sysroot_lib_dir(sess, &dir).join(file_name)
2743+
} else {
2744+
fix_windows_verbatim_for_gcc(path)
2745+
};
2746+
cmd.link_rlib(&rlib_path);
27412747
};
27422748

27432749
if !are_upstream_rust_objects_already_included(sess)
@@ -2806,7 +2812,7 @@ fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
28062812
// what its name is
28072813
let parent = cratepath.parent();
28082814
if let Some(dir) = parent {
2809-
cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
2815+
cmd.include_path(&rehome_sysroot_lib_dir(sess, dir));
28102816
}
28112817
let stem = cratepath.file_stem().unwrap().to_str().unwrap();
28122818
// Convert library file-stem into a cc -l argument.

0 commit comments

Comments
 (0)