|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * SPDX-FileCopyrightText: 2026 VEY-OSS Developers. |
| 4 | + */ |
| 5 | + |
| 6 | +use std::path::PathBuf; |
| 7 | +use std::process::Command; |
| 8 | +use std::{env, fs}; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + let mut out = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 12 | + out.push("udp.skel.rs"); |
| 13 | + |
| 14 | + compile_single("udp"); |
| 15 | +} |
| 16 | + |
| 17 | +fn compile_single(name: &str) { |
| 18 | + let mut source = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 19 | + source.push("src"); |
| 20 | + source.push("bpf"); |
| 21 | + source.push(format!("{name}.bpf.c")); |
| 22 | + |
| 23 | + println!("cargo:rerun-if-changed={}", source.display()); |
| 24 | + |
| 25 | + let mut out = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 26 | + out.push(format!("{name}.bpf.o")); |
| 27 | + |
| 28 | + let mut cmd = Command::new("clang"); |
| 29 | + |
| 30 | + let mut arch_path = PathBuf::from("/usr/lib/linux/uapi"); |
| 31 | + if let Ok(true) = fs::exists(&arch_path) { |
| 32 | + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); |
| 33 | + match target_arch.as_str() { |
| 34 | + "aarch64" => arch_path.push("arm64"), |
| 35 | + "x86_64" => arch_path.push("x86"), |
| 36 | + "loongarch64" => arch_path.push("loongaarch"), |
| 37 | + "powerpc64" | "powerpc64le" => arch_path.push("powerpc"), |
| 38 | + "riscv64a23" | "riscv64gc" => arch_path.push("riscv"), |
| 39 | + "s390x" => arch_path.push("s390"), |
| 40 | + "sparc64" => arch_path.push("sparc"), |
| 41 | + "mips64" | "mips64el" | "mipsisa64r6" | "mipsisa64r6el" => arch_path.push("mips"), |
| 42 | + arch => { |
| 43 | + panic!("Unsupported architecture: {arch}"); |
| 44 | + } |
| 45 | + } |
| 46 | + cmd.arg("-I").arg(&arch_path); |
| 47 | + } |
| 48 | + |
| 49 | + cmd.arg("-g") |
| 50 | + .arg("-O2") |
| 51 | + .arg("-target") |
| 52 | + .arg("bpf") |
| 53 | + .arg("-Wno-visibility") |
| 54 | + .arg("-c") |
| 55 | + .arg(source) |
| 56 | + .arg("-o") |
| 57 | + .arg(out); |
| 58 | + |
| 59 | + let output = cmd.output().unwrap(); |
| 60 | + if !output.stderr.is_empty() { |
| 61 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 62 | + for line in stderr.split("\n") { |
| 63 | + if !line.trim().is_empty() { |
| 64 | + println!("cargo:warning=STDERR:{}", line); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + if !output.status.success() { |
| 69 | + panic!("{name} bpf compile failed"); |
| 70 | + } |
| 71 | +} |
0 commit comments