From 2a6d2a6402e40e2a25b812bfb659824ab83fcec6 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 14 Jul 2023 18:15:10 -0400 Subject: [PATCH 1/2] integration-test: transitive dep on Rust ebpf Emit "cargo:rerun-if-changed={}" for each transitive dependency on integration-ebpf. In a normal world we'd just add integration-ebpf to our build-dependencies, but cargo ignores this because integration-ebpf has no library targets. --- test/integration-test/build.rs | 52 ++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/test/integration-test/build.rs b/test/integration-test/build.rs index 3d9fa08e..7a802748 100644 --- a/test/integration-test/build.rs +++ b/test/integration-test/build.rs @@ -1,4 +1,5 @@ use std::{ + collections::{HashMap, HashSet}, env, ffi::OsString, fmt::Write as _, @@ -9,7 +10,7 @@ use std::{ }; use cargo_metadata::{ - Artifact, CompilerMessage, Message, Metadata, MetadataCommand, Package, Target, + Artifact, CompilerMessage, Dependency, Message, Metadata, MetadataCommand, Package, Target, }; fn main() { @@ -25,6 +26,17 @@ fn main() { } }; + const INTEGRATION_EBPF_PACKAGE: &str = "integration-ebpf"; + + let Metadata { packages, .. } = MetadataCommand::new().no_deps().exec().unwrap(); + let packages: HashMap = packages + .into_iter() + .map(|package| { + let Package { name, .. } = &package; + (name.clone(), package) + }) + .collect(); + let manifest_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap(); let manifest_dir = PathBuf::from(manifest_dir); let out_dir = env::var_os("OUT_DIR").unwrap(); @@ -117,14 +129,29 @@ fn main() { } } - let ebpf_dir = manifest_dir.parent().unwrap().join("integration-ebpf"); - println!("cargo:rerun-if-changed={}", ebpf_dir.to_str().unwrap()); - let target = format!("{target}-unknown-none"); + // Teach cargo about our dependencies. + let mut visited = HashSet::new(); + let mut frontier = vec![INTEGRATION_EBPF_PACKAGE]; + while let Some(package) = frontier.pop() { + if !visited.insert(package) { + continue; + } + let Package { dependencies, .. } = packages.get(package).unwrap(); + for Dependency { name, path, .. } in dependencies { + if let Some(path) = path { + println!("cargo:rerun-if-changed={}", path.as_str()); + frontier.push(name); + } + } + } + let mut cmd = Command::new("cargo"); - cmd.current_dir(&ebpf_dir).args([ + cmd.args([ "build", + "-p", + "integration-ebpf", "-Z", "build-std=core", "--release", @@ -185,18 +212,13 @@ fn main() { fs::write(&dst, []).unwrap_or_else(|err| panic!("failed to create {dst:?}: {err}")); } - let Metadata { packages, .. } = MetadataCommand::new().no_deps().exec().unwrap(); - for Package { name, targets, .. } in packages { - if name != "integration-ebpf" { + let Package { targets, .. } = packages.get(INTEGRATION_EBPF_PACKAGE).unwrap(); + for Target { name, kind, .. } in targets { + if *kind != ["bin"] { continue; } - for Target { name, kind, .. } in targets { - if kind != ["bin"] { - continue; - } - let dst = out_dir.join(name); - fs::write(&dst, []).unwrap_or_else(|err| panic!("failed to create {dst:?}: {err}")); - } + let dst = out_dir.join(name); + fs::write(&dst, []).unwrap_or_else(|err| panic!("failed to create {dst:?}: {err}")); } } } From 581d5c8f5f73b0fff553279ba8e848d407be2291 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 14 Jul 2023 18:18:57 -0400 Subject: [PATCH 2/2] integration-test: bust cache on bpf-linker This is useful when iterating on bpf-linker locally, which is otherwise an undeclared input to this build. We could use an artifact dependency[0] here, but they are unstable and bpf-linker requires gymnastics to build correctly (for now). [0] https://doc.rust-lang.org/cargo/reference/unstable.html#artifact-dependencies-dependency-declarations --- test/integration-test/Cargo.toml | 3 ++- test/integration-test/build.rs | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/integration-test/Cargo.toml b/test/integration-test/Cargo.toml index 8fb11bda..82ccfcdf 100644 --- a/test/integration-test/Cargo.toml +++ b/test/integration-test/Cargo.toml @@ -24,4 +24,5 @@ tokio = { version = "1.24", default-features = false, features = [ ] } [build-dependencies] -cargo_metadata = "0.15.4" +cargo_metadata = { version = "0.15.4", default-features = false } +which = { version = "4.4.0", default-features = false } diff --git a/test/integration-test/build.rs b/test/integration-test/build.rs index 7a802748..8f5596f0 100644 --- a/test/integration-test/build.rs +++ b/test/integration-test/build.rs @@ -12,6 +12,7 @@ use std::{ use cargo_metadata::{ Artifact, CompilerMessage, Dependency, Message, Metadata, MetadataCommand, Package, Target, }; +use which::which; fn main() { const AYA_BUILD_INTEGRATION_BPF: &str = "AYA_BUILD_INTEGRATION_BPF"; @@ -147,6 +148,9 @@ fn main() { } } + let bpf_linker = which("bpf-linker").unwrap(); + println!("cargo:rerun-if-changed={}", bpf_linker.to_str().unwrap()); + let mut cmd = Command::new("cargo"); cmd.args([ "build",