Skip to content

Commit

Permalink
Rollup merge of rust-lang#125886 - GuillaumeGomez:migrate-run-make-is…
Browse files Browse the repository at this point in the history
…sue-15460, r=jieyouxu

Migrate run make issue 15460

Part of rust-lang#121876.

r? `@jieyouxu`
  • Loading branch information
jieyouxu authored Jun 2, 2024
2 parents 78e8ca4 + 36aba94 commit 8d471c8
Showing 9 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
@@ -3369,6 +3369,7 @@ impl<'test> TestCx<'test> {
cmd.env("IS_MSVC", "1")
.env("IS_WINDOWS", "1")
.env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
.env("MSVC_LIB_PATH", format!("{}", lib.display()))
.env("CC", format!("'{}' {}", self.config.cc, cflags))
.env("CXX", format!("'{}' {}", &self.config.cxx, cxxflags));
} else {
44 changes: 44 additions & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -160,6 +160,50 @@ pub fn bin_name(name: &str) -> String {
if is_windows() { format!("{name}.exe") } else { name.to_string() }
}

fn ar<P: AsRef<str>, P2: AsRef<str>>(obj_path: P, lib_path: P2, caller_line_number: u32) {
let mut ar = Command::new(env::var("AR").unwrap());
ar.current_dir(tmp_dir()).arg("crus").arg(lib_path.as_ref()).arg(obj_path.as_ref());
let output = ar.output().unwrap();
if !output.status.success() {
handle_failed_output(&ar, output, caller_line_number);
}
}

/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
#[track_caller]
pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();

let obj_file = format!("{lib_name}.o");
let src = format!("{lib_name}.c");
let lib_name = if is_msvc() {
let lib_path = format!("lib{lib_name}.lib");
// First compiling `.c` to `.o`.
cc().arg("-c").out_exe(lib_name).input(src).run();
// Generating `.lib` from `.o`.
let mut msvc_lib = Command::new(env::var("MSVC_LIB_PATH").unwrap());
msvc_lib
.current_dir(tmp_dir())
.arg("-nologo")
.arg(&format!("-out:{}", cygpath_windows(&lib_path)))
.arg(&obj_file);
let output = msvc_lib.output().unwrap();
if !output.status.success() {
handle_failed_output(&msvc_lib, output, caller_line_number);
}
lib_path
} else {
let lib_path = format!("lib{lib_name}.a");
// First compiling `.c` to `.o`.
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
// Generating `.a` from `.o`.
ar(obj_file, &lib_path, caller_line_number);
lib_path
};
tmp_dir().join(lib_name)
}

/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
/// available on the platform!
#[track_caller]
5 changes: 5 additions & 0 deletions src/tools/run-make-support/src/rustc.rs
Original file line number Diff line number Diff line change
@@ -190,6 +190,11 @@ impl Rustc {
self
}

pub fn extra_filename(&mut self, extra: &str) -> &mut Self {
self.cmd.arg(format!("-Cextra-filename={extra}"));
self
}

/// Specify a stdin input
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
self.stdin = Some(input.as_ref().to_vec().into_boxed_slice());
1 change: 0 additions & 1 deletion src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
@@ -85,7 +85,6 @@ run-make/issue-107094/Makefile
run-make/issue-10971-temps-dir/Makefile
run-make/issue-109934-lto-debuginfo/Makefile
run-make/issue-14698/Makefile
run-make/issue-15460/Makefile
run-make/issue-18943/Makefile
run-make/issue-20626/Makefile
run-make/issue-22131/Makefile
7 changes: 0 additions & 7 deletions tests/run-make/issue-15460/Makefile

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions tests/run-make/link-native-static-lib-to-dylib/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Regression test for <https://github.com/rust-lang/rust/issues/15460>.

//@ ignore-cross-compile

use run_make_support::{build_native_static_lib, run, rustc};

fn main() {
build_native_static_lib("foo");

rustc().input("foo.rs").extra_filename("-383hf8").arg("-Cprefer-dynamic").run();
rustc().input("bar.rs").run();

run("bar");
}

0 comments on commit 8d471c8

Please sign in to comment.