1- use std:: { borrow:: Cow , collections:: HashMap , path:: Path , vec} ;
21#[ cfg( unix) ]
3- use std:: { env, path:: PathBuf } ;
4-
2+ use crate :: WSL_PLUGIN_API_HEADER_FILE_NAME ;
53use bindgen:: callbacks:: { ParseCallbacks , TypeKind } ;
6-
74use cfg_if:: cfg_if;
5+ #[ cfg( unix) ]
6+ use cow_utils:: CowUtils ;
7+ use std:: { borrow:: Cow , collections:: HashMap , path:: Path , vec} ;
8+ #[ cfg( unix) ]
9+ use std:: { env, fs, io:: Write , path:: PathBuf } ;
10+
811#[ derive( Debug ) ]
912struct BindgenCallback ;
1013
@@ -47,21 +50,29 @@ fn rust_to_llvm_target() -> HashMap<&'static str, &'static str> {
4750
4851/// If the host is not Windows, replace `Windows.h` with `windows.h` in a temporary file.
4952#[ cfg( unix) ]
50- fn preprocess_header < P : AsRef < Path > > (
51- header_path : P ,
52- ) -> Result < PathBuf , Box < dyn std:: error:: Error > > {
53- use std:: { fs, io:: Write , path:: PathBuf } ;
54-
55- use crate :: WSL_PLUGIN_API_HEADER_FILE_NAME ;
56-
53+ fn preprocess_header < ' a , P : ' a + AsRef < Path > > (
54+ header_path : & ' a P ,
55+ ) -> Result < Cow < ' a , Path > , Box < dyn std:: error:: Error > > {
5756 let content = fs:: read_to_string ( & header_path) ?;
58- let modified_content = content. replace ( "Windows.h" , "windows.h" ) ;
57+ let target_env = std:: env:: var ( "CARGO_CFG_TARGET_ENV" ) . unwrap_or_default ( ) ;
58+ let new_content = if target_env == "msvc" {
59+ content. cow_replace ( "windows.h" , "Windows.h" )
60+ } else {
61+ content. cow_replace ( "Windows.h" , "windows.h" )
62+ } ;
5963
60- let out_dir: PathBuf = env:: var ( "OUT_DIR" ) ?. into ( ) ;
61- let comp_h_file_path = out_dir. join ( format ! ( "unix_{}" , WSL_PLUGIN_API_HEADER_FILE_NAME ) ) ;
62- fs:: File :: create ( & comp_h_file_path) ?. write_all ( modified_content. as_bytes ( ) ) ?;
63- println ! ( "Using modified header file at: {:?}" , & comp_h_file_path) ;
64- Ok ( comp_h_file_path)
64+ let result = match new_content {
65+ Cow :: Borrowed ( _) => Cow :: Borrowed ( header_path. as_ref ( ) ) ,
66+ Cow :: Owned ( ref modified_content) => {
67+ let out_dir: PathBuf = env:: var ( "OUT_DIR" ) ?. into ( ) ;
68+ let out_path = out_dir. join ( format ! ( "unix_{}" , WSL_PLUGIN_API_HEADER_FILE_NAME ) ) ;
69+ let mut file = fs:: File :: create ( & out_path) ?;
70+ file. write_all ( modified_content. as_bytes ( ) ) ?;
71+ println ! ( "Using modified header file at: {:?}" , & out_path) ;
72+ Cow :: Owned ( out_path)
73+ }
74+ } ;
75+ Ok ( result)
6576}
6677
6778pub ( crate ) fn process < P : AsRef < Path > , S : AsRef < str > > (
@@ -72,14 +83,15 @@ pub(crate) fn process<P: AsRef<Path>, S: AsRef<str>>(
7283 let host = host. as_ref ( ) ;
7384 let target = target. as_ref ( ) ;
7485 // Here we use cow to have the same type and avoiding clowning the PathBuff
75- cfg_if ! {
76- if #[ cfg( unix) ] {
77- let header_file_path: Cow <' _, Path > = Cow :: Owned ( preprocess_header( header_file_path) ?) ;
78- }
79- else {
80- let header_file_path: Cow <' _, Path > = Cow :: Borrowed ( header_file_path. as_ref( ) ) ;
86+ let header_file_path: Cow < ' _ , Path > = {
87+ cfg_if ! {
88+ if #[ cfg( unix) ] {
89+ preprocess_header( & header_file_path) ?
90+ } else {
91+ Cow :: Borrowed ( header_file_path. as_ref( ) )
92+ }
8193 }
82- }
94+ } ;
8395 let mut builder = bindgen:: Builder :: default ( )
8496 . header ( header_file_path. to_str ( ) . unwrap ( ) )
8597 . raw_line ( "use windows::core::*;" )
@@ -91,8 +103,6 @@ pub(crate) fn process<P: AsRef<Path>, S: AsRef<str>>(
91103 . raw_line ( "#[allow(clippy::upper_case_acronyms)] type DWORD = u32;" )
92104 . raw_line ( r#"#[cfg(feature = "hooks-field-names")]"# )
93105 . raw_line ( "use struct_field_names_as_array::FieldNamesAsSlice;" )
94- . derive_debug ( true )
95- . derive_copy ( true )
96106 . allowlist_item ( "WSL.*" )
97107 . allowlist_item ( "Wsl.*" )
98108 . clang_arg ( "-fparse-all-comments" )
0 commit comments