Port issue-11908 to rmake · rust-lang/rust@81f7e54
@@ -40,12 +40,17 @@ pub fn target() -> String {
40404141/// Check if target is windows-like.
4242pub fn is_windows() -> bool {
43-env::var_os("IS_WINDOWS").is_some()
43+target().contains("windows")
4444}
45454646/// Check if target uses msvc.
4747pub fn is_msvc() -> bool {
48- env::var_os("IS_MSVC").is_some()
48+target().contains("msvc")
49+}
50+51+/// Check if target uses macOS.
52+pub fn is_darwin() -> bool {
53+target().contains("darwin")
4954}
50555156/// 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+if is_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+pub fn dynamic_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+pub fn dynamic_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+if is_darwin() {
119+format!("lib{name}.dylib")
120+} else if is_windows() {
121+format!("{name}.dll")
122+} else {
123+format!("lib{name}.so")
124+}
125+}
8612687-if target().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+pub fn rust_lib(name: &str) -> PathBuf {
130+tmp_dir().join(format!("lib{name}.rlib"))
88131}
8913290133/// Construct the binary name based on platform.