From 7c8c3640f6a59ca2ed458c42b14e17df6956b00a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 14 May 2024 14:26:02 -0700 Subject: [PATCH] Support OUT_DIR located in \\?\ path on Windows --- build.rs | 10 +++++++++- src/cargo.rs | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 0989570..3985e33 100644 --- a/build.rs +++ b/build.rs @@ -7,6 +7,7 @@ fn main() -> io::Result<()> { println!("cargo:rerun-if-changed=src/tests"); println!("cargo:rustc-check-cfg=cfg(trybuild_no_target)"); + println!("cargo:rustc-check-cfg=cfg(host_os, values(\"windows\"))"); let out_dir = env::var_os("OUT_DIR").unwrap(); let target = env::var("TARGET").ok(); @@ -15,5 +16,12 @@ fn main() -> io::Result<()> { Some(target) => format!(r#"Some("{}")"#, target.escape_debug()), None => "None".to_owned(), }; - fs::write(path, value) + fs::write(path, value)?; + + let host = env::var_os("HOST").unwrap(); + if let Some("windows") = host.to_str().unwrap().split('-').nth(2) { + println!("cargo:rustc-cfg=host_os=\"windows\""); + } + + Ok(()) } diff --git a/src/cargo.rs b/src/cargo.rs index 4d9d8e4..9699638 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -183,8 +183,12 @@ fn features(project: &Project) -> Vec { } fn target() -> Vec<&'static str> { + #[cfg(not(host_os = "windows"))] const TARGET: Option<&str> = include!(concat!(env!("OUT_DIR"), "/target")); + #[cfg(host_os = "windows")] + const TARGET: Option<&str> = include!(concat!(env!("OUT_DIR"), "\\target")); + // When --target flag is passed, cargo does not pass RUSTFLAGS to rustc when // building proc-macro and build script even if the host and target triples // are the same. Therefore, if we always pass --target to cargo, tools such