Skip to content

Commit

Permalink
Use cmake_package to link against LLVM
Browse files Browse the repository at this point in the history
Currently, we are using the same approach as the llvm-sys package to
facilitate linking against LLVM, replicating a lot of their 'build.rs'
in the process. The process uses the `llvm-config` binary to find the
necessary libraries and include directories to link against LLVM.

This method has some downsides, mainly not being able to find the binary
if it is not in the PATH and `llvm-config` not properly reporting that
FFI needs to be linked. We noticed that these problems do not occur for
the CMake build of the C++ Nyxstone library. This commit introduces the
`cmake_package` crate to use CMake's `find_library` function to find the
LLVM libraries.
  • Loading branch information
stuxnot committed Nov 21, 2024
1 parent 882e5a0 commit 56ce359
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 440 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jobs:
env:
working-dir: ./bindings/rust
NYXSTONE_LLVM_PREFIX: "/usr/lib/llvm-15/"
NYXSTONE_LINK_FFI: "1"
steps:
- uses: actions/checkout@v4
- name: Packages
Expand Down Expand Up @@ -43,10 +42,10 @@ jobs:
- name: Packages
run: brew install llvm@15
- name: Build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@15)" NYXSTONE_LINK_FFI=1 cargo build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@15)" cargo build
working-directory: ${{ env.working-dir }}
- name: Run tests
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@15)" NYXSTONE_LINK_FFI=1 cargo test
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@15)" cargo test
working-directory: ${{ env.working-dir }}

mac-llvm-16:
Expand All @@ -58,10 +57,10 @@ jobs:
- name: Packages
run: brew install llvm@16
- name: Build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@16)" NYXSTONE_LINK_FFI=1 cargo build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@16)" cargo build
working-directory: ${{ env.working-dir }}
- name: Run tests
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@16)" NYXSTONE_LINK_FFI=1 cargo test
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@16)" cargo test
working-directory: ${{ env.working-dir }}

mac-llvm-17:
Expand All @@ -73,10 +72,10 @@ jobs:
- name: Packages
run: brew install llvm@17
- name: Build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@17)" NYXSTONE_LINK_FFI=1 cargo build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@17)" cargo build
working-directory: ${{ env.working-dir }}
- name: Run tests
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@17)" NYXSTONE_LINK_FFI=1 cargo test
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@17)" cargo test
working-directory: ${{ env.working-dir }}

mac-llvm-18:
Expand All @@ -88,9 +87,9 @@ jobs:
- name: Packages
run: brew install llvm@18
- name: Build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@18)" NYXSTONE_LINK_FFI=1 cargo build
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@18)" cargo build
working-directory: ${{ env.working-dir }}
- name: Run tests
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@18)" NYXSTONE_LINK_FFI=1 cargo test
run: RUSTFLAGS="-L$(brew --prefix zstd)/lib" RUSTDOCFLAGS="-L$(brew --prefix zstd)/lib" NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@18)" cargo test
working-directory: ${{ env.working-dir }}

1 change: 1 addition & 0 deletions bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ clap = { version = "4.5", features = ["derive"] }
[build-dependencies]
cxx-build = "1.0.94"
anyhow = { version = "1.0.68", default-features = true }
cmake-package = "0.1.2"

[lib]
path = "src/lib.rs"
9 changes: 4 additions & 5 deletions bindings/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ Official bindings for the Nyxstone assembler/disassembler engine.

## Building

The project can be build via `cargo build`, as long as LLVM with a major version in the range 15-18 is installed in the `$PATH` or the environment variable `$NYXSTONE_LLVM_PREFIX` points to the installation location of a LLVM library.

LLVM might be linked against FFI, but not correctly report this fact via `llvm-config`. If your LLVM does link FFI and
Nyxstone fails to run, set the `NYXSTONE_LINK_FFI` environment variable to `1`, which will ensure that Nyxstone
links against `libffi`.
The project can be build via `cargo build`, as long as LLVM with a major version in the range 15 to 18 is availabe to CMake
and cmake is installed on the system. If your installation of LLVM is installed in a non-standard location, you can set
`NYXSTONE_LLVM_PREFIX` to tell Nyxstone or use the `CMAKE_INCLUDE_PATH` environment variable to add the directory to the
CMake search path.

## Installation

Expand Down
Loading

0 comments on commit 56ce359

Please sign in to comment.