Skip to content

Commit

Permalink
integration-ebpf: artifact dependency bpf-linker
Browse files Browse the repository at this point in the history
- integration-ebpf -[artifact-dependency]-> bpf-linker.
- integration-ebpf: remove manual rebuild-if-changed=bpf-linker.
- integration-ebpf: make bpf-linker availble to rustc in build.rs.
- xtask: filter for test binaries now that the build also produces the
  bpf-linker binary.
- github: remove `cargo install bpf-linker` and commentary about cache
  invalidation.
- integration-test: remove bpf-linker installation instructions.
  • Loading branch information
tamird committed Jul 18, 2023
1 parent 6fc09ca commit 0918458
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 60 deletions.
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ linker = "arm-linux-gnueabihf-gcc"

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"

# See https://github.com/rust-lang/rust-analyzer/issues/14510.
#
# Turns out this affects all cargo invocations in the project, not just rust-analyzer.
[unstable]
bindeps = true
3 changes: 0 additions & 3 deletions .github/workflows/build-aya-bpf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ jobs:

- uses: Swatinem/rust-cache@v2

- name: Prereqs
run: cargo install bpf-linker

- uses: taiki-e/install-action@cargo-hack
- name: Build
env:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ jobs:
.tmp/*.qcow2
.tmp/test_rsa
.tmp/test_rsa.pub
# FIXME: we should invalidate the cache on new bpf-linker releases.
# For now we must manually delete the cache when we release a new
# bpf-linker version.
key: tmp-files-${{ hashFiles('test/run.sh') }}

- name: Run integration tests
Expand Down
2 changes: 0 additions & 2 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ common usage behaviours work on real Linux distros
To run locally all you need is:

1. Rust nightly
1. `cargo install bpf-linker`
1. `bpftool` [^1]

### Other OSs

1. A POSIX shell
1. `rustup target add x86_64-unknown-linux-musl`
1. `cargo install bpf-linker`
1. Install `qemu` and `cloud-init-utils` package - or any package that provides `cloud-localds`

## Usage
Expand Down
2 changes: 1 addition & 1 deletion test/integration-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ aya-bpf = { path = "../../bpf/aya-bpf" }
aya-log-ebpf = { path = "../../bpf/aya-log-ebpf" }

[build-dependencies]
xtask = { path = "../../xtask" }
bpf-linker = { git = "https://github.com/aya-rs/bpf-linker.git", artifact = "bin" }

[[bin]]
name = "log"
Expand Down
41 changes: 24 additions & 17 deletions test/integration-ebpf/build.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
use std::{env, path::PathBuf};

use xtask::{create_symlink_to_binary, AYA_BUILD_INTEGRATION_BPF};
use std::{env, fs, path::PathBuf};

fn main() {
println!("cargo:rerun-if-env-changed={}", AYA_BUILD_INTEGRATION_BPF);
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = PathBuf::from(out_dir);

let build_integration_bpf = env::var(AYA_BUILD_INTEGRATION_BPF)
.as_deref()
.map(str::parse)
.map(Result::unwrap)
.unwrap_or_default();
let bpf_linker = env::var("CARGO_BIN_FILE_BPF_LINKER").unwrap();

if build_integration_bpf {
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = PathBuf::from(out_dir);
let bpf_linker_symlink = create_symlink_to_binary(&out_dir, "bpf-linker").unwrap();
println!(
"cargo:rerun-if-changed={}",
bpf_linker_symlink.to_str().unwrap()
);
// There seems to be no way to pass `-Clinker={}` to rustc from here.
//
// We assume rustc is going to look for `bpf-linker` on the PATH, so we can create a symlink and
// put it on the PATH.
let bin_dir = out_dir.join("bin");
fs::create_dir_all(&bin_dir).unwrap();
let bpf_linker_symlink = bin_dir.join("bpf-linker");
match fs::remove_file(&bpf_linker_symlink) {
Ok(()) => {}
Err(err) => {
if err.kind() != std::io::ErrorKind::NotFound {
panic!("failed to remove symlink: {err}")
}
}
}
std::os::unix::fs::symlink(bpf_linker, bpf_linker_symlink).unwrap();
let path = env::var_os("PATH");
let path = path.as_ref();
let paths = std::iter::once(bin_dir).chain(path.into_iter().flat_map(env::split_paths));
let path = env::join_paths(paths).unwrap();
println!("cargo:rustc-env=PATH={}", path.to_str().unwrap());
}
1 change: 0 additions & 1 deletion test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ EOF
exec_vm 'curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
-y --profile minimal --default-toolchain nightly --component rust-src --component clippy'
exec_vm 'echo source ~/.cargo/env >> ~/.bashrc'
exec_vm cargo install bpf-linker --no-default-features
}

scp_vm() {
Expand Down
1 change: 0 additions & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ proc-macro2 = "1"
quote = "1"
syn = "2"
tempfile = "3"
which = { version = "4.4.0", default-features = false }
30 changes: 1 addition & 29 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use anyhow::{anyhow, Context as _, Result};
use std::{
fs,
path::{Path, PathBuf},
process::Command,
};
use which::which;
use std::process::Command;

pub const AYA_BUILD_INTEGRATION_BPF: &str = "AYA_BUILD_INTEGRATION_BPF";
pub const LIBBPF_DIR: &str = "xtask/libbpf";
Expand All @@ -21,26 +16,3 @@ pub fn exec(cmd: &mut Command) -> Result<()> {
None => Err(anyhow!("{cmd:?} terminated by signal")),
}
}

// Create a symlink in the out directory to work around the fact that cargo ignores anything
// in `$CARGO_HOME`, which is also where `cargo install` likes to place binaries. Cargo will
// stat through the symlink and discover that the binary has changed.
//
// This was introduced in https://github.com/rust-lang/cargo/commit/99f841c.
//
// TODO(https://github.com/rust-lang/cargo/pull/12369): Remove this when the fix is available.
pub fn create_symlink_to_binary(out_dir: &Path, binary_name: &str) -> Result<PathBuf> {
let binary = which(binary_name).unwrap();
let symlink = out_dir.join(binary_name);
match fs::remove_file(&symlink) {
Ok(()) => {}
Err(err) => {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err).context(format!("failed to remove symlink {}", symlink.display()));
}
}
}
std::os::unix::fs::symlink(binary, &symlink)
.with_context(|| format!("failed to create symlink {}", symlink.display()))?;
Ok(symlink)
}
9 changes: 6 additions & 3 deletions xtask/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use anyhow::{Context as _, Result};
use cargo_metadata::{Artifact, CompilerMessage, Message, Target};
use cargo_metadata::{Artifact, ArtifactProfile, CompilerMessage, Message, Target};
use clap::Parser;
use xtask::AYA_BUILD_INTEGRATION_BPF;

Expand Down Expand Up @@ -62,10 +62,13 @@ pub fn build(opts: BuildOptions) -> Result<Vec<(String, PathBuf)>> {
Message::CompilerArtifact(Artifact {
executable,
target: Target { name, .. },
profile: ArtifactProfile { test, .. },
..
}) => {
if let Some(executable) = executable {
executables.push((name, executable.into()));
if test {
if let Some(executable) = executable {
executables.push((name, executable.into()));
}
}
}
Message::CompilerMessage(CompilerMessage { message, .. }) => {
Expand Down

0 comments on commit 0918458

Please sign in to comment.