From 581ea58b2a8378440fb445d18dc2d9193fe45bc6 Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Wed, 27 Sep 2023 15:56:32 +0200 Subject: [PATCH] Add test case for 'cargo:rustc-link-arg=-defaultlib:oldnames'. --- cc-test/build.rs | 15 ++++++++++++++- cc-test/src/NMakefile | 3 +-- cc-test/src/msvcrt.c | 3 +++ cc-test/tests/all.rs | 11 +++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 cc-test/src/msvcrt.c diff --git a/cc-test/build.rs b/cc-test/build.rs index 695c75473..ddce80f1a 100644 --- a/cc-test/build.rs +++ b/cc-test/build.rs @@ -7,6 +7,12 @@ fn main() { fs::remove_dir_all(&out).unwrap(); fs::create_dir(&out).unwrap(); + let target = std::env::var("TARGET").unwrap(); + + if target.ends_with("msvc") { + env::set_var("CL", "-Zl"); // drop linker directives + } + cc::Build::new() .file("src/foo.c") .flag_if_supported("-Wall") @@ -21,7 +27,6 @@ fn main() { .include("src/include") .compile("bar"); - let target = std::env::var("TARGET").unwrap(); let file = target.split("-").next().unwrap(); let file = format!( "src/{}.{}", @@ -55,6 +60,14 @@ fn main() { if target.contains("windows") { cc::Build::new().file("src/windows.c").compile("windows"); + if target.ends_with("msvc") && which::which("clang").is_ok() { + cc::Build::new() + .compiler("clang") + .define("_CRT_NONSTDC_NO_WARNINGS", None) + .file("src/msvcrt.c") + .compile("nonstdcrt"); + println!("cargo:rustc-cfg=feature=\"msvcrt\""); + } } // Test that the `windows_registry` module will set PATH by looking for diff --git a/cc-test/src/NMakefile b/cc-test/src/NMakefile index 03c73dff0..6f27979f4 100644 --- a/cc-test/src/NMakefile +++ b/cc-test/src/NMakefile @@ -2,13 +2,12 @@ all: $(OUT_DIR)/msvc.lib $(OUT_DIR)/msvc.exe $(OUT_DIR)/msvc.lib: $(OUT_DIR)/msvc.o lib -nologo -out:$(OUT_DIR)/msvc.lib $(OUT_DIR)/msvc.o - rc -h $(OUT_DIR)/msvc.o: src/msvc.c $(CC) -nologo -c -Fo:$@ src/msvc.c -MD $(OUT_DIR)/msvc.exe: $(OUT_DIR)/msvc2.o - $(CC) -nologo -Fo:$@ $(OUT_DIR)/msvc2.o + $(CC) -nologo -Fo:$@ $(OUT_DIR)/msvc2.o /link msvcrt.lib $(OUT_DIR)/msvc2.o: src/msvc.c $(CC) -nologo -c -Fo:$@ src/msvc.c -DMAIN -MD diff --git a/cc-test/src/msvcrt.c b/cc-test/src/msvcrt.c new file mode 100644 index 000000000..1784c4cca --- /dev/null +++ b/cc-test/src/msvcrt.c @@ -0,0 +1,3 @@ +#include +#include +void msvcrt() { free(strdup("")); } diff --git a/cc-test/tests/all.rs b/cc-test/tests/all.rs index a44730987..5f83aa5a5 100644 --- a/cc-test/tests/all.rs +++ b/cc-test/tests/all.rs @@ -50,6 +50,17 @@ fn msvc_here() { } } +#[test] +#[cfg(feature = "msvcrt")] +fn msvcrt_here() { + extern "C" { + fn msvcrt(); + } + unsafe { + msvcrt(); + } +} + #[test] fn opt_linkage() { unsafe {