diff --git a/test/integration-test/Cargo.toml b/test/integration-test/Cargo.toml index 24975368..174d66fb 100644 --- a/test/integration-test/Cargo.toml +++ b/test/integration-test/Cargo.toml @@ -14,7 +14,6 @@ env_logger = "0.10" futures-core = "0.3" inventory = "0.3" integration-test-macros = { path = "../integration-test-macros" } -lazy_static = "1" libc = { version = "0.2.105" } log = "0.4" object = { version = "0.31", default-features = false, features = ["std", "read_core", "elf"] } diff --git a/test/integration-test/src/tests/mod.rs b/test/integration-test/src/tests/mod.rs index 93e6aaa3..50f92820 100644 --- a/test/integration-test/src/tests/mod.rs +++ b/test/integration-test/src/tests/mod.rs @@ -1,8 +1,7 @@ use anyhow::bail; -use lazy_static::lazy_static; use libc::{uname, utsname}; use regex::Regex; -use std::{ffi::CStr, mem}; +use std::{cell::OnceCell, ffi::CStr, mem}; pub mod bpf_probe_read; pub mod btf_relocations; @@ -22,15 +21,15 @@ pub struct IntegrationTest { } pub(crate) fn kernel_version() -> anyhow::Result<(u8, u8, u8)> { - lazy_static! { - static ref RE: Regex = Regex::new(r"^([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap(); - } + static mut RE: OnceCell = OnceCell::new(); + let re = + unsafe { &mut RE }.get_or_init(|| Regex::new(r"^([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap()); let mut data: utsname = unsafe { mem::zeroed() }; let ret = unsafe { uname(&mut data) }; assert!(ret >= 0, "libc::uname failed."); let release_cstr = unsafe { CStr::from_ptr(data.release.as_ptr()) }; let release = release_cstr.to_string_lossy(); - if let Some(caps) = RE.captures(&release) { + if let Some(caps) = re.captures(&release) { let major = caps.get(1).unwrap().as_str().parse().unwrap(); let minor = caps.get(2).unwrap().as_str().parse().unwrap(); let patch = caps.get(3).unwrap().as_str().parse().unwrap(); diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 671a987c..592534e6 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -12,5 +12,4 @@ syn = "2" quote = "1" proc-macro2 = "1" indoc = "2.0" -lazy_static = "1" serde_json = "1" diff --git a/xtask/src/build_ebpf.rs b/xtask/src/build_ebpf.rs index cf2b50f8..465980c5 100644 --- a/xtask/src/build_ebpf.rs +++ b/xtask/src/build_ebpf.rs @@ -7,7 +7,7 @@ use std::{ use anyhow::{bail, Context}; use clap::Parser; -use crate::utils::WORKSPACE_ROOT; +use crate::utils::workspace_root; #[derive(Debug, Copy, Clone)] pub enum Architecture { @@ -52,7 +52,7 @@ pub fn build_ebpf(opts: BuildEbpfOptions) -> anyhow::Result<()> { } fn build_rust_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> { - let mut dir = PathBuf::from(WORKSPACE_ROOT.to_string()); + let mut dir = PathBuf::from(workspace_root()); dir.push("test/integration-ebpf"); let target = format!("--target={}", opts.target); @@ -88,10 +88,10 @@ fn get_libbpf_headers>(libbpf_dir: P, include_path: P) -> anyhow: } fn build_c_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> { - let mut src = PathBuf::from(WORKSPACE_ROOT.to_string()); + let mut src = PathBuf::from(workspace_root()); src.push("test/integration-ebpf/src/bpf"); - let mut out_path = PathBuf::from(WORKSPACE_ROOT.to_string()); + let mut out_path = PathBuf::from(workspace_root()); out_path.push("target"); out_path.push(opts.target.to_string()); out_path.push("release"); diff --git a/xtask/src/utils.rs b/xtask/src/utils.rs index fcfb918c..e1e3eb2a 100644 --- a/xtask/src/utils.rs +++ b/xtask/src/utils.rs @@ -1,17 +1,15 @@ -use lazy_static::lazy_static; use serde_json::Value; -use std::process::Command; +use std::{cell::OnceCell, process::Command}; -lazy_static! { - pub static ref WORKSPACE_ROOT: String = workspace_root(); -} - -fn workspace_root() -> String { - let output = Command::new("cargo").arg("metadata").output().unwrap(); - if !output.status.success() { - panic!("unable to run cargo metadata") - } - let stdout = String::from_utf8(output.stdout).unwrap(); - let v: Value = serde_json::from_str(&stdout).unwrap(); - v["workspace_root"].as_str().unwrap().to_string() +pub fn workspace_root() -> &'static str { + static mut WORKSPACE_ROOT: OnceCell = OnceCell::new(); + unsafe { &mut WORKSPACE_ROOT }.get_or_init(|| { + let output = Command::new("cargo").arg("metadata").output().unwrap(); + if !output.status.success() { + panic!("unable to run cargo metadata") + } + let stdout = String::from_utf8(output.stdout).unwrap(); + let v: Value = serde_json::from_str(&stdout).unwrap(); + v["workspace_root"].as_str().unwrap().to_string() + }) }