From 43833395ca2d6d93033b7d350d5f7a6901bd6941 Mon Sep 17 00:00:00 2001 From: Nikita Baksalyar Date: Sun, 10 Apr 2022 01:07:21 +0100 Subject: [PATCH] Support LLVM14 Signed-off-by: Nikita Baksalyar --- Cargo.lock | 16 +++++++++++++++- README.md | 32 ++++++++++++++++++++------------ TUTORIAL.md | 10 +++++----- cargo-bpf/Cargo.toml | 6 ++++-- cargo-bpf/build.rs | 2 +- cargo-bpf/src/llvm.rs | 2 ++ 6 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b1cbbf3..5b0d3ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,7 +121,8 @@ dependencies = [ "lazy_static", "libbpf-sys", "libc", - "llvm-sys", + "llvm-sys 130.0.3", + "llvm-sys 140.0.0", "proc-macro2", "quote", "redbpf", @@ -480,6 +481,19 @@ dependencies = [ "semver 0.11.0", ] +[[package]] +name = "llvm-sys" +version = "140.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edbec78fa56ea7a1ff451683a51b8ecf79a65ca9e88a4be6c4b0a6fc300d2a6" +dependencies = [ + "cc", + "lazy_static", + "libc", + "regex", + "semver 1.0.7", +] + [[package]] name = "log" version = "0.4.16" diff --git a/README.md b/README.md index 54494d7..567f06b 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,9 @@ programs using Rust. It includes: `LLVM` is required in your build system to compile BPF bytecode using RedBPF. -- **LLVM 13** - It is needed to compile BPF bytecode. +- **LLVM 14 or 13** + It is needed to compile BPF bytecode. If you're using Rust 1.60 or later, LLVM 14 is required. + If you need to use LLVM 13, use the feature `llvm13-0`. - One of the followings: 1. The Linux kernel headers @@ -74,7 +75,7 @@ programs using Rust. It includes: ### On Ubuntu 20.04 LTS -Install LLVM 13 and the Linux kernel headers +Install LLVM 14 and the Linux kernel headers ```console # apt-get update \ && apt-get -y install \ @@ -85,12 +86,15 @@ Install LLVM 13 and the Linux kernel headers libelf-dev \ linux-headers-generic \ pkg-config \ - && wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && ./llvm.sh 13 && rm -f ./llvm.sh -# llvm-config-13 --version | grep 13 + && wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && ./llvm.sh 14 && rm -f ./llvm.sh +# llvm-config-14 --version | grep 14 ``` ### On Fedora 35 +Currently, only LLVM 13 is available in the official Fedora repositories. +Consider building LLVM from source until Fedora 36 is released. + Install LLVM 13 and the Linux kernel headers ```console # dnf install -y \ @@ -126,16 +130,19 @@ Install LLVM 13 and the Linux kernel headers # llvm-config --version | grep -q '^13' ``` +At the time of writing, LLVM 14 is not available in the official package repository. +If you need to use Rust 1.60 or later, please build LLVM from source. + ### Building LLVM from source If your Linux distro does not support the latest LLVM as pre-built packages yet, you may build LLVM from the LLVM source code. ```console -$ tar -xaf llvm-13.0.0.src.tar.xz -$ mkdir -p llvm-13.0.0.src/build -$ cd llvm-13.0.0.src/build -$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-13-release -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_LLVM_DYLIB=1 +$ tar -xaf llvm-14.0.0.src.tar.xz +$ mkdir -p llvm-14.0.0.src/build +$ cd llvm-14.0.0.src/build +$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-14-release -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_LLVM_DYLIB=1 $ cmake --build . --target install ``` @@ -143,8 +150,8 @@ Then you can use your LLVM by specifying the custom installation path when installing `cargo-bpf` or building RedBPF like this: ```console -$ LLVM_SYS_130_PREFIX=$HOME/llvm-13-release/ cargo install cargo-bpf -$ LLVM_SYS_130_PREFIX=$HOME/llvm-13-release/ cargo build +$ LLVM_SYS_140_PREFIX=$HOME/llvm-14-release/ cargo install cargo-bpf +$ LLVM_SYS_140_PREFIX=$HOME/llvm-14-release/ cargo build ``` Make sure correct `-DCMAKE_BUILD_TYPE` is specified. Typically `Debug` type is @@ -235,7 +242,8 @@ There are two LLVM versions involved in compiling BPF programs: | Rust version | LLVM version of the rustc | Valid LLVM version of system | |:-------------|:-------------------------:|:-----------------------------| -| 1.56 ~ | LLVM 13 | LLVM 13 and newer | +| 1.56 ~ 1.60 | LLVM 13 | LLVM 13 and newer | +| 1.60 ~ | LLVM 14 | LLVM 14 and newer | ## Docker images for RedBPF build test diff --git a/TUTORIAL.md b/TUTORIAL.md index 0577812..53f1293 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -45,14 +45,14 @@ For some reasons, you may want to build LLVM from source code. When you build LLVM, consider building LLVM with `Release` build mode. -For example, when you build LLVM13 from source code, you can pass +For example, when you build LLVM14 from source code, you can pass `-DCMAKE_BUILD_TYPE=Release` to the `cmake` command as below: ```console -$ tar -xaf llvm-13.0.0.src.tar.xz -$ mkdir -p llvm-13.0.0.src/build -$ cd llvm-13.0.0.src/build -$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-13-release -DCMAKE_BUILD_TYPE=Release +$ tar -xaf llvm-14.0.0.src.tar.xz +$ mkdir -p llvm-14.0.0.src/build +$ cd llvm-14.0.0.src/build +$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-14-release -DCMAKE_BUILD_TYPE=Release $ cmake --build . --target install ``` diff --git a/cargo-bpf/Cargo.toml b/cargo-bpf/Cargo.toml index f3418a0..961502d 100644 --- a/cargo-bpf/Cargo.toml +++ b/cargo-bpf/Cargo.toml @@ -39,6 +39,7 @@ anyhow = "1.0" # if `strict-versioning` feature is off, llvm-sys-130 can be linked to # libLLVM13 and newer libraries. llvm-sys-130 = { version = "130", optional = true, package = "llvm-sys" } +llvm-sys-140 = { version = "140", optional = true, package = "llvm-sys" } cfg-if = "1.0" rustversion = "1.0" rustc_version = "0.4.0" @@ -53,11 +54,12 @@ cfg-if = "1.0.0" default = ["command-line", "llvm-sys"] bindings = ["libbpf-sys", "bpf-sys", "bindgen", "syn", "quote", "proc-macro2", "tempfile"] build = ["bindings", "libc", "toml_edit", "redbpf"] -docsrs-llvm = ["llvm-sys-130/no-llvm-linking", "llvm-sys-130/disable-alltargets-init"] +docsrs-llvm = ["llvm-sys-140/no-llvm-linking", "llvm-sys-140/disable-alltargets-init"] build-c = [] command-line = ["build", "clap", "redbpf/load", "futures", "tokio", "hexdump"] -llvm-sys = ["llvm-sys-130"] # Use current default LLVM version or higher +llvm-sys = ["llvm-sys-140"] # Use current default LLVM version or higher llvm13 = ["llvm-sys-130"] +llvm14 = ["llvm-sys-140"] [package.metadata.docs.rs] all-features = false diff --git a/cargo-bpf/build.rs b/cargo-bpf/build.rs index 5918380..b8c891b 100644 --- a/cargo-bpf/build.rs +++ b/cargo-bpf/build.rs @@ -56,7 +56,7 @@ fn print_cargo_bpf_llvm_version() { fn main() { cfg_if::cfg_if! { - if #[cfg(all(feature = "llvm-sys-130", not(feature = "docsrs-llvm")))] { + if #[cfg(all(any(feature = "llvm-sys-130", feature = "llvm-sys-140"), not(feature = "docsrs-llvm")))] { print_cargo_bpf_llvm_version(); } else { println!("cargo:rustc-env=CARGO_BPF_LLVM_VERSION=0.0.0"); diff --git a/cargo-bpf/src/llvm.rs b/cargo-bpf/src/llvm.rs index 0e95ae2..f65f3a4 100644 --- a/cargo-bpf/src/llvm.rs +++ b/cargo-bpf/src/llvm.rs @@ -22,6 +22,8 @@ second. cfg_if::cfg_if! { if #[cfg(feature = "llvm-sys-130")] { use llvm_sys_130 as llvm_sys; + } else if #[cfg(feature = "llvm-sys-140")] { + use llvm_sys_140 as llvm_sys; } else { compile_error!("Specify --features llvm13"); }