@@ -44,9 +44,6 @@ impl ToTokens for RawString {
44
44
pub fn build ( stream : proc_macro:: TokenStream ) -> proc_macro:: TokenStream {
45
45
let build = parse_macro_input ! ( stream as BuildMacro ) ;
46
46
let tokens = RawString ( build. to_tokens_string ( ) ) ;
47
- let target_dir = std:: env:: var ( "PATH" ) . expect ( "No `PATH` env variable set" ) ;
48
- let end = target_dir. find ( ';' ) . expect ( "Path not ending in `;`" ) ;
49
- let target_dir = RawString ( target_dir[ ..end] . to_string ( ) ) ;
50
47
51
48
let tokens = quote ! {
52
49
{
@@ -59,6 +56,8 @@ pub fn build(stream: proc_macro::TokenStream) -> proc_macro::TokenStream {
59
56
) ;
60
57
61
58
path. push( "windows.rs" ) ;
59
+ println!( "cargo:rerun-if-changed={}" , path. to_str( ) . expect( "`OUT_DIR` not a UTF-8 string" ) ) ;
60
+
62
61
:: std:: fs:: write( & path, #tokens) . expect( "Could not write generated code to windows.rs" ) ;
63
62
64
63
let mut cmd = :: std:: process:: Command :: new( "rustfmt" ) ;
@@ -68,49 +67,27 @@ pub fn build(stream: proc_macro::TokenStream) -> proc_macro::TokenStream {
68
67
fn copy( source: & :: std:: path:: Path , destination: & mut :: std:: path:: PathBuf ) {
69
68
if let :: std:: result:: Result :: Ok ( entries) = :: std:: fs:: read_dir( source) {
70
69
for entry in entries. filter_map( |entry| entry. ok( ) ) {
71
- if let :: std:: result:: Result :: Ok ( entry_type) = entry. file_type( ) {
72
- let path = entry. path( ) ;
73
- if let :: std:: option:: Option :: Some ( last_path_component) = path. file_name( ) {
74
- let _ = :: std:: fs:: create_dir_all( & destination) ;
75
- destination. push( last_path_component) ;
76
- if entry_type. is_file( ) {
77
- let _ = :: std:: fs:: copy( path, & destination) ;
78
- } else if entry_type. is_dir( ) {
79
- let _ = :: std:: fs:: create_dir( & destination) ;
80
- copy( & path, destination) ;
81
- }
82
- destination. pop( ) ;
83
- }
84
- }
85
- }
86
- }
87
- }
88
-
89
- fn copy_to_profile( source: & :: std:: path:: Path , destination: & :: std:: path:: Path , profile: & str ) {
90
- if let :: std:: result:: Result :: Ok ( files) = :: std:: fs:: read_dir( destination) {
91
- for file in files. filter_map( |file| file. ok( ) ) {
92
- if let :: std:: result:: Result :: Ok ( file_type) = file. file_type( ) {
93
- if file_type. is_dir( ) {
94
- let mut path = file. path( ) ;
95
- if let :: std:: option:: Option :: Some ( filename) = path. file_name( ) {
96
- if filename == profile {
97
- copy( source, & mut path) ;
98
- } else {
99
- copy_to_profile( source, & path, profile) ;
100
- }
101
- }
70
+ let path = entry. path( ) ;
71
+ if let :: std:: option:: Option :: Some ( last_path_component) = path. file_name( ) {
72
+ let _ = :: std:: fs:: create_dir_all( & destination) ;
73
+ destination. push( last_path_component) ;
74
+ if path. is_file( ) {
75
+ let _ = :: std:: fs:: copy( path, & destination) ;
76
+ } else if path. is_dir( ) {
77
+ let _ = :: std:: fs:: create_dir( & destination) ;
78
+ copy( & path, destination) ;
102
79
}
80
+ destination. pop( ) ;
103
81
}
104
82
}
105
83
}
106
84
}
107
85
108
86
let mut source : :: std:: path:: PathBuf = :: std:: env:: var( "CARGO_MANIFEST_DIR" ) . expect( "No `CARGO_MANIFEST_DIR` env variable set" ) . into( ) ;
109
87
source. push( ".windows" ) ;
88
+ println!( "cargo:rerun-if-changed={}" , source. to_str( ) . expect( "`CARGO_MANIFEST_DIR` not a UTF-8 string" ) ) ;
110
89
111
90
if source. exists( ) {
112
- println!( "cargo:rerun-if-changed={}" , source. to_str( ) . expect( "`CARGO_MANIFEST_DIR` not a valid path" ) ) ;
113
-
114
91
// The `target_arch` cfg is not set for build scripts so we need to sniff it out from the environment variable.
115
92
source. push( match :: std:: env:: var( "CARGO_CFG_TARGET_ARCH" ) . expect( "No `CARGO_CFG_TARGET_ARCH` env variable set" ) . as_str( ) {
116
93
"x86_64" => "x64" ,
@@ -124,17 +101,12 @@ pub fn build(stream: proc_macro::TokenStream) -> proc_macro::TokenStream {
124
101
println!( "cargo:rustc-link-search=native={}" , source. to_str( ) . expect( "`CARGO_MANIFEST_DIR` not a valid path" ) ) ;
125
102
}
126
103
127
- let mut destination : :: std:: path:: PathBuf = #target_dir. into( ) ;
104
+ let mut destination: :: std:: path:: PathBuf = :: std:: env:: var( "OUT_DIR" ) . expect( "No `OUT_DIR` env variable set" ) . into( ) ;
105
+ // Of `target/<profile>/build/<crate_name>/out`, pop the last 3 folders
106
+ destination. pop( ) ;
128
107
destination. pop( ) ;
129
108
destination. pop( ) ;
130
109
131
- let profile = :: std:: env:: var( "PROFILE" ) . expect( "No `PROFILE` env variable set" ) ;
132
- copy_to_profile( & source, & destination, & profile) ;
133
-
134
- destination. push( ".windows" ) ;
135
- destination. push( "winmd" ) ;
136
- source. pop( ) ;
137
- source. push( "winmd" ) ;
138
110
copy( & source, & mut destination) ;
139
111
}
140
112
}
0 commit comments