Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror clang's link stage on Windows. #866

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 5 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,11 @@ impl Build {
atlmfc_lib.display()
));
}

if compiler.family == ToolFamily::Clang {
// Mirror what clang does at link stage
self.print(&format_args!("cargo:rustc-link-arg=-defaultlib:oldnames"));
}
}

if self.link_lib_modifiers.is_empty() {
Expand Down Expand Up @@ -1712,13 +1717,6 @@ impl Build {
cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into());
}

if cmd.family == ToolFamily::Clang && target.contains("windows") {
// Disambiguate mingw and msvc on Windows. Problem is that
// depending on the origin clang can default to a mismatchig
// run-time.
cmd.push_cc_arg(format!("--target={}", target).into());
}

if cmd.family == ToolFamily::Clang && target.contains("android") {
// For compatibility with code that doesn't use pre-defined `__ANDROID__` macro.
// If compiler used via ndk-build or cmake (officially supported build methods)
Expand Down