scx_rustland_core: generate source files in-tree

There is no need to generate source code in a temporary directory with
RustLandBuilder(), we can simply generate code in-tree and exclude the
generated source files from .gitignore.

Having the generated source files in-tree can help to debug potential
build issues (and it also allows to drop the the tempfile crate
dependency).

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
This commit is contained in:
Andrea Righi 2024-02-28 12:56:40 +01:00
parent 06d8170f9f
commit 0d1c6555a4
4 changed files with 20 additions and 33 deletions

View File

@ -10,6 +10,7 @@ description = "Framework to implement sched_ext schedulers running in user space
include = [
"src/bpf/intf.h",
"src/bpf/main.bpf.c",
"src/bpf.rs",
]
[dependencies]
@ -17,7 +18,6 @@ anyhow = "1.0"
libbpf-rs = "0.22.0"
libc = "0.2.137"
buddy-alloc = "0.5.1"
tempfile = "3.10.1"
scx_utils = { path = "../scx_utils", version = "0.6" }
[build-dependencies]

View File

@ -5,63 +5,46 @@
use anyhow::Result;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use tempfile::tempdir;
use std::path::Path;
use scx_utils::BpfBuilder;
pub struct RustLandBuilder {
inner_builder: BpfBuilder,
temp_dir: tempfile::TempDir,
}
impl RustLandBuilder {
pub fn new() -> Result<Self> {
Ok(Self {
inner_builder: BpfBuilder::new()?,
temp_dir: tempdir()?,
})
}
fn create_temp_file(
&mut self,
file_name: &str,
content: &[u8],
) -> Result<PathBuf, Box<dyn Error>> {
let mut temp_file = self.temp_dir.as_ref().to_path_buf();
temp_file.push(file_name);
let mut file = File::create(&temp_file)?;
file.write_all(content)?;
Ok(temp_file)
}
fn get_bpf_rs_content(&self) -> &'static str {
include_str!("bpf.rs")
fn create_file(&self, file_name: &str, content: &[u8]) {
let path = Path::new(file_name);
let mut file = File::create(&path).expect("Unable to create file");
file.write_all(content).expect("Unable to write to file");
}
pub fn build(&mut self) -> Result<()> {
// Embed the BPF source files in the crate.
// Embed the BPF source files.
let intf = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bpf/intf.h"));
let skel = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bpf/main.bpf.c"));
let bpf = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bpf.rs"));
let intf_path = self.create_temp_file("intf.h", intf).unwrap();
let skel_path = self.create_temp_file("main.bpf.c", skel).unwrap();
// Generate BPF backend code (C).
self.create_file("intf.h", intf);
self.create_file("main.bpf.c", skel);
self.inner_builder
.enable_intf(intf_path.to_str().unwrap(), "bpf_intf.rs");
self.inner_builder
.enable_skel(skel_path.to_str().unwrap(), "bpf");
self.inner_builder.enable_intf("intf.h", "bpf_intf.rs");
self.inner_builder.enable_skel("main.bpf.c", "bpf");
let content = self.get_bpf_rs_content();
let path = Path::new("src/bpf.rs");
let mut file = File::create(&path).expect("Unable to create file");
file.write_all(content.as_bytes()).expect("Unable to write to file");
// Generate user-space BPF connector code (Rust).
self.create_file("src/bpf.rs", bpf);
// Build the scheduler.
self.inner_builder.build()
}
}

View File

@ -1,4 +1,6 @@
src/bpf/.output
intf.h
main.bpf.c
bpf.rs
Cargo.lock
target

View File

@ -1,4 +1,6 @@
src/bpf/.output
intf.h
main.bpf.c
bpf.rs
Cargo.lock
target