|
1 | 1 | use cc::Build;
|
| 2 | +use std::ffi::OsStr; |
2 | 3 | use std::fmt::Display;
|
3 | 4 | use std::{fmt, env};
|
4 | 5 | use std::path::{Path, PathBuf};
|
@@ -240,26 +241,72 @@ impl Builder {
|
240 | 241 |
|
241 | 242 | self.verify_paths()?;
|
242 | 243 |
|
243 |
| - // FreeRTOS header files |
244 |
| - b.include(self.freertos_include_dir()); |
245 |
| - // FreeRTOS port header files (e.g. portmacro.h) |
246 |
| - b.include(self.get_freertos_port_dir()); |
247 |
| - b.include(self.freertos_config_dir.clone()); |
248 |
| - b.file(self.heap_c_file()); |
249 |
| - self.freertos_files().iter().for_each(|f| { |
250 |
| - b.file(f); |
251 |
| - }); |
252 |
| - self.freertos_port_files().iter().for_each(|f| { |
253 |
| - b.file(f); |
254 |
| - }); |
255 |
| - self.freertos_shim_files().iter().for_each(|f| { |
256 |
| - b.file(f); |
257 |
| - }); |
| 244 | + add_include_with_rerun(&mut b, self.freertos_include_dir()); // FreeRTOS header files |
| 245 | + add_include_with_rerun(&mut b, self.get_freertos_port_dir()); // FreeRTOS port header files (e.g. portmacro.h) |
| 246 | + add_include_with_rerun(&mut b, &self.freertos_config_dir); // User's FreeRTOSConfig.h |
| 247 | + |
| 248 | + add_build_files_with_rerun(&mut b, self.freertos_files()); // Non-port C files |
| 249 | + add_build_files_with_rerun(&mut b, self.freertos_port_files()); // Port C files |
| 250 | + add_build_files_with_rerun(&mut b, self.freertos_shim_files()); // Shim C file |
| 251 | + add_build_file_with_rerun(&mut b, self.heap_c_file()); // Heap C file |
| 252 | + |
| 253 | + println!("cargo:rerun-if-env-changed={ENV_KEY_FREERTOS_SRC}"); |
| 254 | + println!("cargo:rerun-if-env-changed={ENV_KEY_FREERTOS_CONFIG}"); |
| 255 | + println!("cargo:rerun-if-env-changed={ENV_KEY_FREERTOS_SHIM}"); |
258 | 256 |
|
259 | 257 | b.try_compile("freertos").map_err(|e| Error::new(&format!("{}", e)))?;
|
260 | 258 |
|
261 | 259 | Ok(())
|
262 | 260 | }
|
| 261 | + |
| 262 | + /// Add a single file to the build. This also tags the file with cargo:rerun-if-changed so that cargo will re-run |
| 263 | + /// the build script if the file changes. If you don't want this additional behavior, use get_cc().file() to |
| 264 | + /// directly add a file to the build instead. |
| 265 | + pub fn add_build_file<P: AsRef<Path>>(&mut self, file: P) { |
| 266 | + add_build_file_with_rerun(self.get_cc(), file); |
| 267 | + } |
| 268 | + |
| 269 | + /// Add multiple files to the build. This also tags the files with cargo:rerun-if-changed so that cargo will re-run |
| 270 | + /// the build script if the files change. If you don't want this additional behavior, use get_cc().files() to |
| 271 | + /// directly add files to the build instead. |
| 272 | + pub fn add_build_files<P>(&mut self, files: P) |
| 273 | + where |
| 274 | + P: IntoIterator, |
| 275 | + P::Item: AsRef<Path>, |
| 276 | + { |
| 277 | + add_build_files_with_rerun(self.get_cc(), files); |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +fn add_build_file_with_rerun<P: AsRef<Path>>(build: &mut Build, file: P) { |
| 282 | + build.file(&file); |
| 283 | + println!("cargo:rerun-if-changed={}", file.as_ref().display()); |
| 284 | +} |
| 285 | + |
| 286 | +fn add_build_files_with_rerun<P>(build: &mut Build, files: P) |
| 287 | +where |
| 288 | + P: IntoIterator, |
| 289 | + P::Item: AsRef<Path>, |
| 290 | +{ |
| 291 | + for file in files.into_iter() { |
| 292 | + add_build_file_with_rerun(build, file); |
| 293 | + } |
| 294 | +} |
| 295 | + |
| 296 | +fn add_include_with_rerun<P: AsRef<Path>>(build: &mut Build, dir: P) { |
| 297 | + build.include(&dir); |
| 298 | + |
| 299 | + WalkDir::new(&dir) |
| 300 | + .follow_links(false) |
| 301 | + .max_depth(1) |
| 302 | + .into_iter() |
| 303 | + .filter_map(|e| e.ok()) |
| 304 | + .for_each(|entry| { |
| 305 | + let f_name = entry.path(); |
| 306 | + if f_name.extension() == Some(OsStr::new("h")) { |
| 307 | + println!("cargo:rerun-if-changed={}", f_name.display()); |
| 308 | + } |
| 309 | + }); |
263 | 310 | }
|
264 | 311 |
|
265 | 312 | #[test]
|
|
0 commit comments