Skip to content

Commit 8d471c8

Browse files
authored
Rollup merge of rust-lang#125886 - GuillaumeGomez:migrate-run-make-issue-15460, r=jieyouxu
Migrate run make issue 15460 Part of rust-lang#121876. r? `@jieyouxu`
2 parents 78e8ca4 + 36aba94 commit 8d471c8

File tree

9 files changed

+64
-8
lines changed

9 files changed

+64
-8
lines changed

src/tools/compiletest/src/runtest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,6 +3369,7 @@ impl<'test> TestCx<'test> {
33693369
cmd.env("IS_MSVC", "1")
33703370
.env("IS_WINDOWS", "1")
33713371
.env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
3372+
.env("MSVC_LIB_PATH", format!("{}", lib.display()))
33723373
.env("CC", format!("'{}' {}", self.config.cc, cflags))
33733374
.env("CXX", format!("'{}' {}", &self.config.cxx, cxxflags));
33743375
} else {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,50 @@ pub fn bin_name(name: &str) -> String {
160160
if is_windows() { format!("{name}.exe") } else { name.to_string() }
161161
}
162162

163+
fn ar<P: AsRef<str>, P2: AsRef<str>>(obj_path: P, lib_path: P2, caller_line_number: u32) {
164+
let mut ar = Command::new(env::var("AR").unwrap());
165+
ar.current_dir(tmp_dir()).arg("crus").arg(lib_path.as_ref()).arg(obj_path.as_ref());
166+
let output = ar.output().unwrap();
167+
if !output.status.success() {
168+
handle_failed_output(&ar, output, caller_line_number);
169+
}
170+
}
171+
172+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
173+
#[track_caller]
174+
pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
175+
let caller_location = std::panic::Location::caller();
176+
let caller_line_number = caller_location.line();
177+
178+
let obj_file = format!("{lib_name}.o");
179+
let src = format!("{lib_name}.c");
180+
let lib_name = if is_msvc() {
181+
let lib_path = format!("lib{lib_name}.lib");
182+
// First compiling `.c` to `.o`.
183+
cc().arg("-c").out_exe(lib_name).input(src).run();
184+
// Generating `.lib` from `.o`.
185+
let mut msvc_lib = Command::new(env::var("MSVC_LIB_PATH").unwrap());
186+
msvc_lib
187+
.current_dir(tmp_dir())
188+
.arg("-nologo")
189+
.arg(&format!("-out:{}", cygpath_windows(&lib_path)))
190+
.arg(&obj_file);
191+
let output = msvc_lib.output().unwrap();
192+
if !output.status.success() {
193+
handle_failed_output(&msvc_lib, output, caller_line_number);
194+
}
195+
lib_path
196+
} else {
197+
let lib_path = format!("lib{lib_name}.a");
198+
// First compiling `.c` to `.o`.
199+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
200+
// Generating `.a` from `.o`.
201+
ar(obj_file, &lib_path, caller_line_number);
202+
lib_path
203+
};
204+
tmp_dir().join(lib_name)
205+
}
206+
163207
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
164208
/// available on the platform!
165209
#[track_caller]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ impl Rustc {
190190
self
191191
}
192192

193+
pub fn extra_filename(&mut self, extra: &str) -> &mut Self {
194+
self.cmd.arg(format!("-Cextra-filename={extra}"));
195+
self
196+
}
197+
193198
/// Specify a stdin input
194199
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
195200
self.stdin = Some(input.as_ref().to_vec().into_boxed_slice());

src/tools/tidy/src/allowed_run_make_makefiles.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ run-make/issue-107094/Makefile
8585
run-make/issue-10971-temps-dir/Makefile
8686
run-make/issue-109934-lto-debuginfo/Makefile
8787
run-make/issue-14698/Makefile
88-
run-make/issue-15460/Makefile
8988
run-make/issue-18943/Makefile
9089
run-make/issue-20626/Makefile
9190
run-make/issue-22131/Makefile

tests/run-make/issue-15460/Makefile

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/15460>.
2+
3+
//@ ignore-cross-compile
4+
5+
use run_make_support::{build_native_static_lib, run, rustc};
6+
7+
fn main() {
8+
build_native_static_lib("foo");
9+
10+
rustc().input("foo.rs").extra_filename("-383hf8").arg("-Cprefer-dynamic").run();
11+
rustc().input("bar.rs").run();
12+
13+
run("bar");
14+
}

0 commit comments

Comments
 (0)