Skip to content

Commit 102af30

Browse files
committed
[WIP] Fix MinGW linking errors when mixing Rust and other languages
1 parent a916ac2 commit 102af30

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,6 +3391,7 @@ dependencies = [
33913391
"bitflags",
33923392
"cc",
33933393
"jobserver",
3394+
"lazy_static 1.3.0",
33943395
"libc",
33953396
"log",
33963397
"memmap",

src/librustc_codegen_ssa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ bitflags = "1.2.1"
1414
cc = "1.0.1"
1515
num_cpus = "1.0"
1616
memmap = "0.7"
17+
lazy_static = "1"
1718
log = "0.4.5"
1819
libc = "0.2.44"
1920
jobserver = "0.1.11"

src/librustc_codegen_ssa/back/link.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,48 @@ pub fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary
918918
}
919919
}
920920

921+
fn get_compiler_libs_path(sess: &Session) -> Option<PathBuf> {
922+
use std::sync::Mutex;
923+
924+
lazy_static::lazy_static! {
925+
static ref SYSTEM_LIBS: Mutex<Option<PathBuf>> = Mutex::new(None);
926+
}
927+
928+
let system_libs = SYSTEM_LIBS.lock().unwrap().clone();
929+
if let Some(compiler_libs_path) = system_libs {
930+
println!("cache: hit");
931+
return Some(compiler_libs_path);
932+
} else {
933+
println!("cache: miss");
934+
let compiler = if let Some(linker) = &sess.opts.cg.linker {
935+
linker.clone().into_os_string()
936+
} else if let Some(linker) = &sess.target.target.options.linker {
937+
linker.into()
938+
} else {
939+
return None;
940+
};
941+
if let Ok(output) = Command::new(compiler).arg("-print-file-name=crt2.o").output() {
942+
if let Some(compiler_libs_path) =
943+
PathBuf::from(std::str::from_utf8(&output.stdout).unwrap()).parent()
944+
{
945+
let compiler_libs_path = fix_windows_verbatim_for_gcc(compiler_libs_path);
946+
*SYSTEM_LIBS.lock().unwrap() = Some(compiler_libs_path.clone());
947+
return Some(compiler_libs_path);
948+
}
949+
}
950+
}
951+
None
952+
}
953+
921954
pub fn get_file_path(sess: &Session, name: &str) -> PathBuf {
955+
if sess.target.target.llvm_target.contains("windows-gnu") {
956+
if let Some(compiler_libs_path) = get_compiler_libs_path(sess) {
957+
let file_path = compiler_libs_path.join(name);
958+
if file_path.exists() {
959+
return file_path;
960+
}
961+
}
962+
}
922963
let fs = sess.target_filesearch(PathKind::Native);
923964
let file_path = fs.get_lib_path().join(name);
924965
if file_path.exists() {
@@ -1100,6 +1141,12 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
11001141
// target descriptor
11011142
let t = &sess.target.target;
11021143

1144+
if sess.target.target.llvm_target.contains("windows-gnu") {
1145+
if let Some(compiler_libs_path) = get_compiler_libs_path(sess) {
1146+
cmd.include_path(&compiler_libs_path);
1147+
}
1148+
}
1149+
11031150
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
11041151

11051152
for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) {

0 commit comments

Comments
 (0)