From 7067db450abfa9ea60756702d1248cdb88e843d7 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 7 Jul 2023 11:18:51 -0400 Subject: [PATCH] xtask: Avoid lossy string conversion --- xtask/src/build_ebpf.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/xtask/src/build_ebpf.rs b/xtask/src/build_ebpf.rs index 465980c5..3a100fae 100644 --- a/xtask/src/build_ebpf.rs +++ b/xtask/src/build_ebpf.rs @@ -1,5 +1,8 @@ use std::{ - env, fs, + borrow::Cow, + env, + ffi::{OsStr, OsString}, + fs, path::{Path, PathBuf}, process::Command, }; @@ -77,9 +80,12 @@ fn build_rust_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> { fn get_libbpf_headers>(libbpf_dir: P, include_path: P) -> anyhow::Result<()> { let dir = include_path.as_ref(); fs::create_dir_all(dir)?; + let mut includedir = OsString::new(); + includedir.push("INCLUDEDIR="); + includedir.push(dir.as_os_str()); let status = Command::new("make") .current_dir(libbpf_dir.as_ref().join("src")) - .arg(format!("INCLUDEDIR={}", dir.as_os_str().to_string_lossy())) + .arg(includedir) .arg("install_headers") .status() .expect("failed to build get libbpf headers"); @@ -119,17 +125,18 @@ fn compile_with_clang>( out: P, include_path: P, ) -> anyhow::Result<()> { - let clang = match env::var("CLANG") { - Ok(val) => val, - Err(_) => String::from("/usr/bin/clang"), + let clang: Cow<'_, _> = match env::var_os("CLANG") { + Some(val) => val.into(), + None => OsStr::new("/usr/bin/clang").into(), }; - let arch = match std::env::consts::ARCH { + let arch = match env::consts::ARCH { "x86_64" => "x86", "aarch64" => "arm64", - _ => std::env::consts::ARCH, + arch => arch, }; let mut cmd = Command::new(clang); - cmd.arg(format!("-I{}", include_path.as_ref().to_string_lossy())) + cmd.arg("-I") + .arg(include_path.as_ref()) .arg("-g") .arg("-O2") .arg("-target")