Skip to content

Commit

Permalink
tools/build.rs: Only run when compiling for *-windows-msvc, and e…
Browse files Browse the repository at this point in the history
…rror if trying to cross compile to `*-windows-msvc`.
  • Loading branch information
kkysen committed Oct 22, 2024
1 parent 416843b commit a61cac8
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions tools/build.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
use std::env;

fn main() {
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();

let os = os.as_str();
let env = env.as_str();

match (os, env) {
("windows", "msvc") => {
if !cfg!(all(target_os = "windows", target_env = "msvc")) {
panic!("Cannot cross compile to *-windows-msvc");
}
}
#[allow(clippy::needless_return)]
_ => return,
}

// NOTE: we rely on libraries that are only distributed for Windows so
// targeting Windows/MSVC is not supported when cross compiling.
#[cfg(all(target_os = "windows", target_env = "msvc"))]
{
use cc::windows_registry;
use std::env;

let os = env::var("CARGO_CFG_TARGET_OS").expect("missing CARGO_CFG_TARGET_OS");
let target = env::var("TARGET").expect("missing TARGET");
if os == "windows" {
// for sprintf, snprintf, etc.
println!("cargo:rustc-link-lib=static=oldnames");
let tool = windows_registry::find_tool(&target, "cl.exe")
.expect("couldn't find cl.exe; are the Visual Studio C++ tools installed?");
let lib_paths = &tool
.env()
.iter()
.find(|(key, _val)| key == "LIB")
.expect("LIB path not found")
.1;
for path in lib_paths.to_str().unwrap().split(';') {
if path != "" {
println!("cargo:rustc-link-search={path}");
}
// for sprintf, snprintf, etc.
let target = env::var("TARGET").unwrap();
println!("cargo:rustc-link-lib=static=oldnames");
let tool = windows_registry::find_tool(&target, "cl.exe")
.expect("couldn't find cl.exe; are the Visual Studio C++ tools installed?");
let lib_paths = &tool
.env()
.iter()
.find(|(key, _val)| key == "LIB")
.expect("LIB path not found")
.1;
for path in lib_paths.to_str().unwrap().split(';') {
if path != "" {
println!("cargo:rustc-link-search={path}");
}

let getopt = "getopt";
cc::Build::new()
.files([&"../tools/compat/getopt.c"])
.include("../include/compat")
.debug(cfg!(debug_assertions))
.compile(&getopt);
// cc automatically outputs the following line
// println!("cargo:rustc-link-lib=static={getopt}");
}

let getopt = "getopt";
cc::Build::new()
.files([&"../tools/compat/getopt.c"])
.include("../include/compat")
.debug(cfg!(debug_assertions))
.compile(&getopt);
// cc automatically outputs the following line
// println!("cargo:rustc-link-lib=static={getopt}");
}
}

0 comments on commit a61cac8

Please sign in to comment.