13
13
extern crate filetime;
14
14
15
15
use std:: { fs, env} ;
16
+ use std:: fs:: File ;
16
17
use std:: process:: { Command , Stdio } ;
17
18
use std:: path:: { Path , PathBuf } ;
18
19
@@ -148,19 +149,29 @@ pub fn up_to_date(src: &Path, dst: &Path) -> bool {
148
149
}
149
150
}
150
151
152
+ #[ must_use]
151
153
pub struct NativeLibBoilerplate {
152
- pub skip_build : bool ,
153
154
pub src_dir : PathBuf ,
154
155
pub out_dir : PathBuf ,
155
- pub timestamp : PathBuf ,
156
156
}
157
157
158
+ impl Drop for NativeLibBoilerplate {
159
+ fn drop ( & mut self ) {
160
+ t ! ( File :: create( self . out_dir. join( "rustbuild.timestamp" ) ) ) ;
161
+ }
162
+ }
163
+
164
+ // Perform standard preparations for native libraries that are build only once for all stages.
165
+ // Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
166
+ // updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
167
+ // If Err is returned, then everything is up-to-date and further build actions can be skipped.
168
+ // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
169
+ // of scope, so all the build actions should be completed until then.
158
170
pub fn native_lib_boilerplate ( src_name : & str ,
159
171
out_name : & str ,
160
172
link_name : & str ,
161
- timestamp_name : & str ,
162
173
search_subdir : & str )
163
- -> NativeLibBoilerplate {
174
+ -> Result < NativeLibBoilerplate , ( ) > {
164
175
let current_dir = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ;
165
176
let src_dir = current_dir. join ( ".." ) . join ( src_name) ;
166
177
rerun_if_changed_anything_in_dir ( & src_dir) ;
@@ -171,15 +182,11 @@ pub fn native_lib_boilerplate(src_name: &str,
171
182
println ! ( "cargo:rustc-link-lib=static={}" , link_name) ;
172
183
println ! ( "cargo:rustc-link-search=native={}" , out_dir. join( search_subdir) . display( ) ) ;
173
184
174
- let timestamp = out_dir. join ( timestamp_name) ;
175
- let skip_build = up_to_date ( Path :: new ( "build.rs" ) , & timestamp) &&
176
- up_to_date ( & src_dir, & timestamp) ;
177
-
178
- NativeLibBoilerplate {
179
- skip_build : skip_build,
180
- src_dir : src_dir,
181
- out_dir : out_dir,
182
- timestamp : timestamp,
185
+ let timestamp = out_dir. join ( "rustbuild.timestamp" ) ;
186
+ if !up_to_date ( Path :: new ( "build.rs" ) , & timestamp) || !up_to_date ( & src_dir, & timestamp) {
187
+ Ok ( NativeLibBoilerplate { src_dir : src_dir, out_dir : out_dir } )
188
+ } else {
189
+ Err ( ( ) )
183
190
}
184
191
}
185
192
0 commit comments