Skip to content

Commit 9e7aff7

Browse files
committed
Auto merge of #125031 - Oneirical:dynamic-libs, r=jieyouxu
Migrate `run-make/issue-11908` to new `rmake.rs` format Part of #121876 and the associated [Google Summer of Code project](https://blog.rust-lang.org/2024/05/01/gsoc-2024-selected-projects.html). Set as draft, because I have a few concerns: - [x] I am not sure if `target().contains("darwin")` is a good way of checking that the target is on OSX. - [x] I find it strange that the `dylib` part of the test adapts to different target platforms, but not the `rlib` part. Is `rlib` named the same on all platforms?
2 parents 0160bff + 81f7e54 commit 9e7aff7

8 files changed

Lines changed: 79 additions & 37 deletions

File tree

‎src/tools/run-make-support/src/lib.rs‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ pub fn target() -> String {
4040

4141
/// Check if target is windows-like.
4242
pubfnis_windows() -> bool{
43-
env::var_os("IS_WINDOWS").is_some()
43+
target().contains("windows")
4444
}
4545

4646
/// Check if target uses msvc.
4747
pubfnis_msvc() -> bool{
48-
env::var_os("IS_MSVC").is_some()
48+
target().contains("msvc")
49+
}
50+
51+
/// Check if target uses macOS.
52+
pubfnis_darwin() -> bool{
53+
target().contains("darwin")
4954
}
5055

5156
/// Construct a path to a static library under `$TMPDIR` given the library name. This will return a
@@ -82,9 +87,47 @@ pub fn static_lib_name(name: &str) -> String {
8287
// endif
8388
// endif
8489
// ```
85-
assert!(!name.contains(char::is_whitespace),"name cannot contain whitespace");
90+
assert!(!name.contains(char::is_whitespace),"static library name cannot contain whitespace");
91+
92+
ifis_msvc(){format!("{name}.lib")}else{format!("lib{name}.a")}
93+
}
94+
95+
/// Construct a path to a dynamic library under `$TMPDIR` given the library name. This will return a
96+
/// path with `$TMPDIR` joined with platform-and-compiler-specific library name.
97+
pubfndynamic_lib(name:&str) -> PathBuf{
98+
tmp_dir().join(dynamic_lib_name(name))
99+
}
100+
101+
/// Construct the dynamic library name based on the platform.
102+
pubfndynamic_lib_name(name:&str) -> String{
103+
// See tools.mk (irrelevant lines omitted):
104+
//
105+
// ```makefile
106+
// ifeq ($(UNAME),Darwin)
107+
// DYLIB = $(TMPDIR)/lib$(1).dylib
108+
// else
109+
// ifdef IS_WINDOWS
110+
// DYLIB = $(TMPDIR)/$(1).dll
111+
// else
112+
// DYLIB = $(TMPDIR)/lib$(1).so
113+
// endif
114+
// endif
115+
// ```
116+
assert!(!name.contains(char::is_whitespace),"dynamic library name cannot contain whitespace");
117+
118+
ifis_darwin(){
119+
format!("lib{name}.dylib")
120+
}elseifis_windows(){
121+
format!("{name}.dll")
122+
}else{
123+
format!("lib{name}.so")
124+
}
125+
}
86126

87-
iftarget().contains("msvc"){format!("{name}.lib")}else{format!("lib{name}.a")}
127+
/// Construct a path to a rust library (rlib) under `$TMPDIR` given the library name. This will return a
128+
/// path with `$TMPDIR` joined with the library name.
129+
pubfnrust_lib(name:&str) -> PathBuf{
130+
tmp_dir().join(format!("lib{name}.rlib"))
88131
}
89132

90133
/// Construct the binary name based on platform.

‎src/tools/run-make-support/src/rustc.rs‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl Rustc {
9191
self
9292
}
9393

94-
/// Specify path to the output file.
94+
/// Specify path to the output file. Equivalent to `-o`` in rustc.
9595
pubfnoutput<P:AsRef<Path>>(&mutself,path:P) -> &mutSelf{
9696
self.cmd.arg("-o");
9797
self.cmd.arg(path.as_ref());
@@ -150,13 +150,7 @@ impl Rustc {
150150
self
151151
}
152152

153-
/// Enables link time optimizations in rustc. Equivalent to `-Clto``.
154-
pubfnlto(&mutself) -> &mutSelf{
155-
self.cmd.arg("-Clto");
156-
self
157-
}
158-
159-
/// Add a directory to the library search path.
153+
/// Add a directory to the library search path. Equivalent to `-L`` in rustc.
160154
pubfnlibrary_search_path<P:AsRef<Path>>(&mutself,path:P) -> &mutSelf{
161155
self.cmd.arg("-L");
162156
self.cmd.arg(path.as_ref());

‎src/tools/tidy/src/allowed_run_make_makefiles.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ run-make/invalid-staticlib/Makefile
9494
run-make/issue-107094/Makefile
9595
run-make/issue-10971-temps-dir/Makefile
9696
run-make/issue-109934-lto-debuginfo/Makefile
97-
run-make/issue-11908/Makefile
9897
run-make/issue-14698/Makefile
9998
run-make/issue-15460/Makefile
10099
run-make/issue-18943/Makefile

‎tests/run-make/issue-11908/Makefile‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎tests/run-make/reachable-extern-fn-available-lto/rmake.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
//@ ignore-cross-compile
1111

12-
use run_make_support::{cc, extra_c_flags, run, rustc, static_lib, tmp_dir};
12+
use run_make_support::{cc, extra_c_flags, run, rustc, static_lib};
1313

1414
fnmain(){
1515
let libbar_path = static_lib("bar");
1616
rustc().input("foo.rs").crate_type("rlib").run();
1717
rustc()
1818
.input("bar.rs")
1919
.crate_type("staticlib")
20-
.lto()
20+
.arg("-Clto")
2121
.library_search_path(".")
2222
.output(&libbar_path)
2323
.run();
File renamed without changes.
File renamed without changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// A path which contains the same rlib or dylib in two locations
2+
// should not cause an assertion panic in the compiler.
3+
// This test tries to replicate the linked issue and checks
4+
// if the bugged error makes a resurgence.
5+
6+
// See https://github.com/rust-lang/rust/issues/11908
7+
8+
//@ ignore-cross-compile
9+
10+
use run_make_support::{dynamic_lib, rust_lib, rustc, tmp_dir};
11+
use std::fs;
12+
13+
fnmain(){
14+
let tmp_dir_other = tmp_dir().join("other");
15+
16+
fs::create_dir(&tmp_dir_other);
17+
rustc().input("foo.rs").crate_type("dylib").arg("-Cprefer-dynamic").run();
18+
fs::rename(dynamic_lib("foo"),&tmp_dir_other);
19+
rustc().input("foo.rs").crate_type("dylib").arg("-Cprefer-dynamic").run();
20+
rustc().input("bar.rs").library_search_path(&tmp_dir_other).run();
21+
fs::remove_dir_all(tmp_dir());
22+
23+
fs::create_dir_all(&tmp_dir_other);
24+
rustc().input("foo.rs").crate_type("rlib").run();
25+
fs::rename(rust_lib("foo"),&tmp_dir_other);
26+
rustc().input("foo.rs").crate_type("rlib").run();
27+
rustc().input("bar.rs").library_search_path(tmp_dir_other).run();
28+
}

0 commit comments

Comments
 (0)