Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Support LLVM14
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Baksalyar <[email protected]>
  • Loading branch information
nbaksalyar committed Apr 17, 2022
1 parent c864a69 commit ec184b8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
16 changes: 15 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

- One of the followings:
1. The Linux kernel headers
Expand All @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -126,25 +130,28 @@ 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
```

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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
6 changes: 4 additions & 2 deletions cargo-bpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cargo-bpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions cargo-bpf/src/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit ec184b8

Please sign in to comment.