From 80408e0649b29915ad5258367b67c70a91e6b043 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Tue, 28 May 2024 13:21:28 -0400 Subject: [PATCH 1/3] port symlinked-extern to rmake --- src/tools/run-make-support/src/lib.rs | 23 +++++++++++++++-- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/symlinked-extern/rmake.rs | 25 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/run-make/symlinked-extern/rmake.rs diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index b920f9a07db87..d93a950d3e1b5 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -93,6 +93,17 @@ pub fn source_root() -> PathBuf { env_var("SOURCE_ROOT").into() } +/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix. +pub fn create_symlink, Q: AsRef>(original: P, link: Q) { + if is_windows() { + use std::os::windows::fs; + fs::symlink_file(original, link).unwrap(); + } else { + use std::os::unix::fs; + fs::symlink(original, link).unwrap(); + } +} + /// Construct the static library name based on the platform. pub fn static_lib_name(name: &str) -> String { // See tools.mk (irrelevant lines omitted): @@ -114,7 +125,11 @@ pub fn static_lib_name(name: &str) -> String { // ``` assert!(!name.contains(char::is_whitespace), "static library name cannot contain whitespace"); - if is_msvc() { format!("{name}.lib") } else { format!("lib{name}.a") } + if is_msvc() { + format!("{name}.lib") + } else { + format!("lib{name}.a") + } } /// Construct the dynamic library name based on the platform. @@ -161,7 +176,11 @@ pub fn rust_lib_name(name: &str) -> String { /// Construct the binary name based on platform. pub fn bin_name(name: &str) -> String { - if is_windows() { format!("{name}.exe") } else { name.to_string() } + if is_windows() { + format!("{name}.exe") + } else { + name.to_string() + } } /// Return the current working directory. diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index ac89a30f35342..4adfd52b2b38a 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -228,7 +228,6 @@ run-make/std-core-cycle/Makefile run-make/symbol-mangling-hashed/Makefile run-make/symbol-visibility/Makefile run-make/symbols-include-type-name/Makefile -run-make/symlinked-extern/Makefile run-make/symlinked-libraries/Makefile run-make/symlinked-rlib/Makefile run-make/sysroot-crates-are-unstable/Makefile diff --git a/tests/run-make/symlinked-extern/rmake.rs b/tests/run-make/symlinked-extern/rmake.rs new file mode 100644 index 0000000000000..124f1cef8498e --- /dev/null +++ b/tests/run-make/symlinked-extern/rmake.rs @@ -0,0 +1,25 @@ +// Crates that are resolved normally have their path canonicalized and all +// symlinks resolved. This did not happen for paths specified +// using the --extern option to rustc, which could lead to rustc thinking +// that it encountered two different versions of a crate, when it's +// actually the same version found through different paths. + +// This test checks that --extern and symlinks together +// can result in successful compilation. + +//@ ignore-cross-compile + +use run_make_support::{create_symlink, rustc, tmp_dir}; +use std::fs; + +fn main() { + rustc().input("foo.rs").run(); + fs::create_dir_all(tmp_dir().join("other")).unwrap(); + create_symlink(tmp_dir().join("libfoo.rlib"), tmp_dir().join("other")); + rustc().input("bar.rs").library_search_path(tmp_dir()).run(); + rustc() + .input("baz.rs") + .extern_("foo", tmp_dir().join("other/libfoo.rlib")) + .library_search_path(tmp_dir()) + .run(); +} From 59acd234575b0283a6e00ec2e9d8be5f1b2f782d Mon Sep 17 00:00:00 2001 From: Oneirical Date: Tue, 28 May 2024 13:33:14 -0400 Subject: [PATCH 2/3] port symlinked-rlib to rmake --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/symlinked-extern/Makefile | 12 ------------ tests/run-make/symlinked-extern/rmake.rs | 1 + tests/run-make/symlinked-rlib/Makefile | 10 ---------- tests/run-make/symlinked-rlib/rmake.rs | 16 ++++++++++++++++ 5 files changed, 17 insertions(+), 23 deletions(-) delete mode 100644 tests/run-make/symlinked-extern/Makefile delete mode 100644 tests/run-make/symlinked-rlib/Makefile create mode 100644 tests/run-make/symlinked-rlib/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 4adfd52b2b38a..dc1310afde9d8 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -229,7 +229,6 @@ run-make/symbol-mangling-hashed/Makefile run-make/symbol-visibility/Makefile run-make/symbols-include-type-name/Makefile run-make/symlinked-libraries/Makefile -run-make/symlinked-rlib/Makefile run-make/sysroot-crates-are-unstable/Makefile run-make/target-cpu-native/Makefile run-make/target-specs/Makefile diff --git a/tests/run-make/symlinked-extern/Makefile b/tests/run-make/symlinked-extern/Makefile deleted file mode 100644 index 28c764b84e8b7..0000000000000 --- a/tests/run-make/symlinked-extern/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -# ignore-windows -# `ln` is actually `cp` on msys. - -all: - $(RUSTC) foo.rs - mkdir -p $(TMPDIR)/other - ln -nsf $(TMPDIR)/libfoo.rlib $(TMPDIR)/other - $(RUSTC) bar.rs -L $(TMPDIR) - $(RUSTC) baz.rs --extern foo=$(TMPDIR)/other/libfoo.rlib -L $(TMPDIR) diff --git a/tests/run-make/symlinked-extern/rmake.rs b/tests/run-make/symlinked-extern/rmake.rs index 124f1cef8498e..a54989661dc71 100644 --- a/tests/run-make/symlinked-extern/rmake.rs +++ b/tests/run-make/symlinked-extern/rmake.rs @@ -3,6 +3,7 @@ // using the --extern option to rustc, which could lead to rustc thinking // that it encountered two different versions of a crate, when it's // actually the same version found through different paths. +// See https://github.com/rust-lang/rust/pull/16505 // This test checks that --extern and symlinks together // can result in successful compilation. diff --git a/tests/run-make/symlinked-rlib/Makefile b/tests/run-make/symlinked-rlib/Makefile deleted file mode 100644 index a8565f683c3e8..0000000000000 --- a/tests/run-make/symlinked-rlib/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -# ignore-windows -# `ln` is actually `cp` on msys. - -all: - $(RUSTC) foo.rs --crate-type=rlib -o $(TMPDIR)/foo.xxx - ln -nsf $(TMPDIR)/foo.xxx $(TMPDIR)/libfoo.rlib - $(RUSTC) bar.rs -L $(TMPDIR) diff --git a/tests/run-make/symlinked-rlib/rmake.rs b/tests/run-make/symlinked-rlib/rmake.rs new file mode 100644 index 0000000000000..d451c62a2f38a --- /dev/null +++ b/tests/run-make/symlinked-rlib/rmake.rs @@ -0,0 +1,16 @@ +// Rustc did not recognize libraries which were symlinked +// to files having extension other than .rlib. This was fixed +// in #32828. This test creates a symlink to "foo.xxx", which has +// an unusual file extension, and checks that rustc can successfully +// use it as an rlib library. +// See https://github.com/rust-lang/rust/pull/32828 + +//@ ignore-cross-compile + +use run_make_support::{create_symlink, rustc, tmp_dir}; + +fn main() { + rustc().input("foo.rs").crate_type("rlib").output(tmp_dir().join("foo.xxx")).run(); + create_symlink(tmp_dir().join("foo.xxx"), tmp_dir().join("libfoo.rlib")); + rustc().input("bar.rs").library_search_path(tmp_dir()); +} From 2ac5faa509e9511665986a5ac25ed7c4e427aac2 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Tue, 28 May 2024 13:57:09 -0400 Subject: [PATCH 3/3] port symlinked-libraries to rmake --- src/tools/run-make-support/src/lib.rs | 41 +++++++++++-------- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/symlinked-extern/rmake.rs | 15 +++---- tests/run-make/symlinked-libraries/Makefile | 11 ----- tests/run-make/symlinked-libraries/rmake.rs | 16 ++++++++ tests/run-make/symlinked-rlib/rmake.rs | 8 ++-- 6 files changed, 50 insertions(+), 42 deletions(-) delete mode 100644 tests/run-make/symlinked-libraries/Makefile create mode 100644 tests/run-make/symlinked-libraries/rmake.rs diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index d93a950d3e1b5..9ec20bf8fd478 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -94,14 +94,31 @@ pub fn source_root() -> PathBuf { } /// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix. +#[cfg(target_family = "windows")] pub fn create_symlink, Q: AsRef>(original: P, link: Q) { - if is_windows() { - use std::os::windows::fs; - fs::symlink_file(original, link).unwrap(); - } else { - use std::os::unix::fs; - fs::symlink(original, link).unwrap(); + if link.as_ref().exists() { + std::fs::remove_dir(link.as_ref()).unwrap(); + } + use std::os::windows::fs; + fs::symlink_file(original.as_ref(), link.as_ref()).expect(&format!( + "failed to create symlink {:?} for {:?}", + link.as_ref().display(), + original.as_ref().display(), + )); +} + +/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix. +#[cfg(target_family = "unix")] +pub fn create_symlink, Q: AsRef>(original: P, link: Q) { + if link.as_ref().exists() { + std::fs::remove_dir(link.as_ref()).unwrap(); } + use std::os::unix::fs; + fs::symlink(original.as_ref(), link.as_ref()).expect(&format!( + "failed to create symlink {:?} for {:?}", + link.as_ref().display(), + original.as_ref().display(), + )); } /// Construct the static library name based on the platform. @@ -125,11 +142,7 @@ pub fn static_lib_name(name: &str) -> String { // ``` assert!(!name.contains(char::is_whitespace), "static library name cannot contain whitespace"); - if is_msvc() { - format!("{name}.lib") - } else { - format!("lib{name}.a") - } + if is_msvc() { format!("{name}.lib") } else { format!("lib{name}.a") } } /// Construct the dynamic library name based on the platform. @@ -176,11 +189,7 @@ pub fn rust_lib_name(name: &str) -> String { /// Construct the binary name based on platform. pub fn bin_name(name: &str) -> String { - if is_windows() { - format!("{name}.exe") - } else { - name.to_string() - } + if is_windows() { format!("{name}.exe") } else { name.to_string() } } /// Return the current working directory. diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index dc1310afde9d8..339184731970e 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -228,7 +228,6 @@ run-make/std-core-cycle/Makefile run-make/symbol-mangling-hashed/Makefile run-make/symbol-visibility/Makefile run-make/symbols-include-type-name/Makefile -run-make/symlinked-libraries/Makefile run-make/sysroot-crates-are-unstable/Makefile run-make/target-cpu-native/Makefile run-make/target-specs/Makefile diff --git a/tests/run-make/symlinked-extern/rmake.rs b/tests/run-make/symlinked-extern/rmake.rs index a54989661dc71..98f69aefbe622 100644 --- a/tests/run-make/symlinked-extern/rmake.rs +++ b/tests/run-make/symlinked-extern/rmake.rs @@ -10,17 +10,12 @@ //@ ignore-cross-compile -use run_make_support::{create_symlink, rustc, tmp_dir}; -use std::fs; +use run_make_support::{create_symlink, cwd, fs_wrapper, rustc}; fn main() { rustc().input("foo.rs").run(); - fs::create_dir_all(tmp_dir().join("other")).unwrap(); - create_symlink(tmp_dir().join("libfoo.rlib"), tmp_dir().join("other")); - rustc().input("bar.rs").library_search_path(tmp_dir()).run(); - rustc() - .input("baz.rs") - .extern_("foo", tmp_dir().join("other/libfoo.rlib")) - .library_search_path(tmp_dir()) - .run(); + fs_wrapper::create_dir_all("other"); + create_symlink("libfoo.rlib", "other"); + rustc().input("bar.rs").library_search_path(cwd()).run(); + rustc().input("baz.rs").extern_("foo", "other").library_search_path(cwd()).run(); } diff --git a/tests/run-make/symlinked-libraries/Makefile b/tests/run-make/symlinked-libraries/Makefile deleted file mode 100644 index fb0b6127e6f5c..0000000000000 --- a/tests/run-make/symlinked-libraries/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -# ignore-windows -# `ln` is actually `cp` on msys. - -all: - $(RUSTC) foo.rs -C prefer-dynamic - mkdir -p $(TMPDIR)/other - ln -nsf $(TMPDIR)/$(call DYLIB_GLOB,foo) $(TMPDIR)/other - $(RUSTC) bar.rs -L $(TMPDIR)/other diff --git a/tests/run-make/symlinked-libraries/rmake.rs b/tests/run-make/symlinked-libraries/rmake.rs new file mode 100644 index 0000000000000..eaf0c44206a5f --- /dev/null +++ b/tests/run-make/symlinked-libraries/rmake.rs @@ -0,0 +1,16 @@ +// When a directory and a symlink simultaneously exist with the same name, +// setting that name as the library search path should not cause rustc +// to avoid looking in the symlink and cause an error. This test creates +// a directory and a symlink named "other", and places the library in the symlink. +// If it succeeds, the library was successfully found. +// See https://github.com/rust-lang/rust/issues/12459 + +//@ ignore-cross-compile +use run_make_support::{create_symlink, dynamic_lib_name, fs_wrapper, rustc}; + +fn main() { + rustc().input("foo.rs").arg("-Cprefer-dynamic").run(); + fs_wrapper::create_dir_all("other"); + create_symlink(dynamic_lib_name("foo"), "other"); + rustc().input("bar.rs").library_search_path("other").run(); +} diff --git a/tests/run-make/symlinked-rlib/rmake.rs b/tests/run-make/symlinked-rlib/rmake.rs index d451c62a2f38a..3759ca25928a1 100644 --- a/tests/run-make/symlinked-rlib/rmake.rs +++ b/tests/run-make/symlinked-rlib/rmake.rs @@ -7,10 +7,10 @@ //@ ignore-cross-compile -use run_make_support::{create_symlink, rustc, tmp_dir}; +use run_make_support::{create_symlink, cwd, rustc}; fn main() { - rustc().input("foo.rs").crate_type("rlib").output(tmp_dir().join("foo.xxx")).run(); - create_symlink(tmp_dir().join("foo.xxx"), tmp_dir().join("libfoo.rlib")); - rustc().input("bar.rs").library_search_path(tmp_dir()); + rustc().input("foo.rs").crate_type("rlib").output("foo.xxx").run(); + create_symlink("foo.xxx", "libfoo.rlib"); + rustc().input("bar.rs").library_search_path(cwd()).run(); }