Skip to content

Commit

Permalink
Add test case for 'cargo:rustc-link-arg=-defaultlib:oldnames'.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Nov 3, 2023
1 parent 9680ff6 commit 581ea58
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
15 changes: 14 additions & 1 deletion cc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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/{}.{}",
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cc-test/src/NMakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions cc-test/src/msvcrt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdlib.h>
#include <string.h>
void msvcrt() { free(strdup("")); }
11 changes: 11 additions & 0 deletions cc-test/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 581ea58

Please sign in to comment.