Skip to content

Commit d8ddd13

Browse files
committed
Set cargo:rerun-if-changed for C files and cargo:rerun-if-env-changed for custom environment variables
1 parent d60e434 commit d8ddd13

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

freertos-cargo-build/src/lib.rs

+62-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cc::Build;
2+
use std::ffi::OsStr;
23
use std::fmt::Display;
34
use std::{fmt, env};
45
use std::path::{Path, PathBuf};
@@ -240,26 +241,72 @@ impl Builder {
240241

241242
self.verify_paths()?;
242243

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}");
258256

259257
b.try_compile("freertos").map_err(|e| Error::new(&format!("{}", e)))?;
260258

261259
Ok(())
262260
}
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+
});
263310
}
264311

265312
#[test]

freertos-rust-examples/build.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ fn main() {
2020
b.freertos_config("examples/win");
2121
// TODO: in future all FreeRTOS API should be implemented by the freertos-rust crate
2222
// until then, we need to compile some C code manually
23-
b.get_cc().file("examples/win/hooks.c");
24-
b.get_cc().file("examples/win/Run-time-stats-utils.c");
23+
b.add_build_file("examples/win/hooks.c");
24+
b.add_build_file("examples/win/Run-time-stats-utils.c");
2525

2626
if target_env == "msvc" {
2727
println!("cargo:rustc-link-lib=static=winmm");
@@ -31,8 +31,8 @@ fn main() {
3131
if target == "x86_64-unknown-linux-gnu" {
3232
b.freertos_config("examples/linux");
3333

34-
b.get_cc().file("examples/linux/hooks.c");
35-
// b.get_cc().file("examples/linux/Run-time-stats-utils.c"); // Unimplemented yet..
34+
b.add_build_file("examples/linux/hooks.c");
35+
// b.add_build_file("examples/linux/Run-time-stats-utils.c"); // Unimplemented yet..
3636
}
3737

3838
if target == "thumbv7m-none-eabi" {

0 commit comments

Comments
 (0)