Browse Source

codegen: avoid lossy string conversion

This can't possibly work right if lossy conversion is required.
Tamir Duberstein 1 month ago
parent
commit
ee7861a6ed
2 changed files with 13 additions and 27 deletions
  1. 7 21
      xtask/src/codegen/aya.rs
  2. 6 6
      xtask/src/codegen/aya_ebpf_bindings.rs

+ 7 - 21
xtask/src/codegen/aya.rs

@@ -15,23 +15,9 @@ fn codegen_internal_btf_bindings(libbpf_dir: &Path) -> Result<(), anyhow::Error>
     let generated = dir.join("src/generated");
 
     let mut bindgen = bindgen::user_builder()
-        .clang_arg(format!(
-            "-I{}",
-            libbpf_dir
-                .join("include/uapi")
-                .canonicalize()
-                .unwrap()
-                .to_string_lossy()
-        ))
-        .clang_arg(format!(
-            "-I{}",
-            libbpf_dir
-                .join("include")
-                .canonicalize()
-                .unwrap()
-                .to_string_lossy()
-        ))
-        .header(libbpf_dir.join("src/libbpf_internal.h").to_string_lossy())
+        .clang_args(["-I", libbpf_dir.join("include/uapi").to_str().unwrap()])
+        .clang_args(["-I", libbpf_dir.join("include").to_str().unwrap()])
+        .header(libbpf_dir.join("src/libbpf_internal.h").to_str().unwrap())
         .constified_enum_module("bpf_core_relo_kind");
 
     let types = ["bpf_core_relo", "btf_ext_header"];
@@ -177,9 +163,9 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
 
     let builder = || {
         bindgen::user_builder()
-            .header(dir.join("include/linux_wrapper.h").to_string_lossy())
-            .clang_args(&["-I", &*libbpf_dir.join("include/uapi").to_string_lossy()])
-            .clang_args(&["-I", &*libbpf_dir.join("include").to_string_lossy()])
+            .header(dir.join("include/linux_wrapper.h").to_str().unwrap())
+            .clang_args(["-I", libbpf_dir.join("include/uapi").to_str().unwrap()])
+            .clang_args(["-I", libbpf_dir.join("include").to_str().unwrap()])
     };
 
     for arch in Architecture::supported() {
@@ -209,7 +195,7 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
             Architecture::S390X => s390x_sysroot,
             Architecture::Mips => mips_sysroot,
         };
-        bindgen = bindgen.clang_args(&["-I", &*sysroot.to_string_lossy()]);
+        bindgen = bindgen.clang_args(["-I", sysroot.to_str().unwrap()]);
 
         for x in &types {
             bindgen = bindgen.allowlist_type(x);

+ 6 - 6
xtask/src/codegen/aya_ebpf_bindings.rs

@@ -29,13 +29,13 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
 
     let builder = || {
         let mut bindgen = bindgen::bpf_builder()
-            .header(&*dir.join("include/bindings.h").to_string_lossy())
+            .header(dir.join("include/bindings.h").to_str().unwrap())
             // aya-tool uses aya_ebpf::cty. We can't use that here since aya-bpf
             // depends on aya-ebpf-bindings so it would create a circular dep.
             .ctypes_prefix("::aya_ebpf_cty")
-            .clang_args(&["-I", &*libbpf_dir.join("include/uapi").to_string_lossy()])
-            .clang_args(&["-I", &*libbpf_dir.join("include").to_string_lossy()])
-            .clang_args(&["-I", &*libbpf_dir.join("src").to_string_lossy()])
+            .clang_args(["-I", libbpf_dir.join("include/uapi").to_str().unwrap()])
+            .clang_args(["-I", libbpf_dir.join("include").to_str().unwrap()])
+            .clang_args(["-I", libbpf_dir.join("src").to_str().unwrap()])
             // BPF_F_LINK is defined twice. Once in an anonymous enum
             // which bindgen will constify, and once via #define macro
             // which generates a duplicate const.
@@ -95,7 +95,7 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
             Architecture::S390X => "s390x-unknown-linux-gnu",
             Architecture::Mips => "mips-unknown-linux-gnu",
         };
-        bindgen = bindgen.clang_args(&["-target", target]);
+        bindgen = bindgen.clang_args(["-target", target]);
 
         // Set the sysroot. This is needed to ensure that the correct arch
         // specific headers are imported.
@@ -108,7 +108,7 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
             Architecture::S390X => s390x_sysroot,
             Architecture::Mips => mips_sysroot,
         };
-        bindgen = bindgen.clang_args(&["-I", &*sysroot.to_string_lossy()]);
+        bindgen = bindgen.clang_args(["-I", sysroot.to_str().unwrap()]);
 
         let bindings = bindgen
             .generate()