Skip to content

Commit

Permalink
Properly rebuild static libraries on file changes
Browse files Browse the repository at this point in the history
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 <deso@posteo.net>
  • Loading branch information
d-e-s-o authored and alexforster committed Jul 26, 2024
1 parent 32a2eb8 commit c9deef5
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand All @@ -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");

Expand All @@ -219,19 +230,20 @@ 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");

assert!(status.success(), "make failed");

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) {
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -333,19 +347,20 @@ 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");

assert!(status.success(), "make failed");

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 {
Expand Down

0 comments on commit c9deef5

Please sign in to comment.