From c9deef5da0033b1c328b975c83ee9cf1bb9481b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Thu, 11 Jul 2024 10:17:45 -0700 Subject: [PATCH] Properly rebuild static libraries on file changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When one of the submodules is changed libbpf-sys is not automatically rebuilt, but it should be. Emit the necessary directives to make that happen. Signed-off-by: Daniel Müller --- build.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/build.rs b/build.rs index 7014ead..7fb9f0f 100644 --- a/build.rs +++ b/build.rs @@ -3,11 +3,21 @@ use std::env; use std::ffi; use std::fs; +use std::fs::read_dir; use std::path; +use std::path::Path; use std::process; use nix::fcntl; + +fn emit_rerun_directives_for_contents(dir: &Path) { + for result in read_dir(dir).unwrap() { + let file = result.unwrap(); + println!("cargo:rerun-if-changed={}", file.path().display()); + } +} + #[cfg(feature = "bindgen")] fn generate_bindings(src_dir: path::PathBuf) { use std::collections::HashSet; @@ -195,10 +205,11 @@ fn main() { } fn make_zlib(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path) { + let src_dir = src_dir.join("zlib"); // lock README such that if two crates are trying to compile // this at the same time (eg libbpf-rs libbpf-cargo) // they wont trample each other - let file = std::fs::File::open(src_dir.join("zlib/README")).unwrap(); + let file = std::fs::File::open(src_dir.join("README")).unwrap(); let _lock = fcntl::Flock::lock(file, fcntl::FlockArg::LockExclusive).unwrap(); let status = process::Command::new("./configure") @@ -209,7 +220,7 @@ fn make_zlib(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path) { .arg(out_dir) .env("CC", compiler.path()) .env("CFLAGS", compiler.cflags_env()) - .current_dir(&src_dir.join("zlib")) + .current_dir(&src_dir) .status() .expect("could not execute make"); @@ -219,7 +230,7 @@ fn make_zlib(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path) { .arg("install") .arg("-j") .arg(&format!("{}", num_cpus())) - .current_dir(&src_dir.join("zlib")) + .current_dir(&src_dir) .status() .expect("could not execute make"); @@ -227,11 +238,12 @@ fn make_zlib(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path) { let status = process::Command::new("make") .arg("distclean") - .current_dir(&src_dir.join("zlib")) + .current_dir(&src_dir) .status() .expect("could not execute make"); assert!(status.success(), "make failed"); + emit_rerun_directives_for_contents(&src_dir); } fn make_elfutils(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path) { @@ -310,6 +322,7 @@ fn make_elfutils(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path .expect("could not execute make"); assert!(status.success(), "make failed"); + emit_rerun_directives_for_contents(&src_dir.join("elfutils").join("src")); } fn make_libbpf( @@ -318,6 +331,7 @@ fn make_libbpf( src_dir: &path::Path, out_dir: &path::Path, ) { + let src_dir = src_dir.join("libbpf/src"); // create obj_dir if it doesn't exist let obj_dir = path::PathBuf::from(&out_dir.join("obj").into_os_string()); let _ = fs::create_dir(&obj_dir); @@ -333,7 +347,7 @@ fn make_libbpf( .env("DESTDIR", out_dir) .env("CC", compiler.path()) .env("CFLAGS", cflags) - .current_dir(&src_dir.join("libbpf/src")) + .current_dir(&src_dir) .status() .expect("could not execute make"); @@ -341,11 +355,12 @@ fn make_libbpf( let status = process::Command::new("make") .arg("clean") - .current_dir(&src_dir.join("libbpf/src")) + .current_dir(&src_dir) .status() .expect("could not execute make"); assert!(status.success(), "make failed"); + emit_rerun_directives_for_contents(&src_dir); } fn num_cpus() -> usize {