Skip to content

Commit

Permalink
Merge pull request #8 from noir-lang/phated/rust-bindgen-nix
Browse files Browse the repository at this point in the history
feat(wrapper): Flake bindgen cleanup
  • Loading branch information
kobyhallx authored Feb 6, 2023
2 parents 3bc4718 + ee24b2c commit 4ddc09b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
32 changes: 26 additions & 6 deletions .github/workflows/cross-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ jobs:
strategy:
fail-fast: false
matrix:
target:
[
# llvm11, llvm12, llvm13, llvm14, wasm32,
cross-aarch64-linux
target: [
# llvm11, llvm12, llvm13, llvm14, wasm32,
cross-aarch64-linux,
]

steps:
Expand All @@ -24,8 +23,9 @@ jobs:
- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- name: Build ${{ matrix.target }}
- name: Build barretenberg on ${{ matrix.target }}
run: |
nix build -L .#${{ matrix.target }}
Expand All @@ -39,4 +39,24 @@ jobs:
with:
name: ${{ matrix.target }}
path: ./artifacts/
retention-days: 3
retention-days: 3

bb_wrapper:
runs-on: ubuntu-22.04

steps:
- name: Checkout
uses: actions/checkout@v3

- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- name: Build barretenberg_wrapper
working-directory: ./barretenberg_wrapper
# This removes the lockfile as a workaround to relative file paths causing a failure in flakes
run: |
rm flake.lock
nix build -L
./result/bin/barretenberg_wrapper
31 changes: 26 additions & 5 deletions .github/workflows/cross-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
fail-fast: false
matrix:
target: [
# llvm11, llvm12, llvm13, llvm14, wasm32,
cross-aarch64-darwin
]
# llvm11, llvm12, llvm13, llvm14, wasm32,
cross-aarch64-darwin,
]

steps:
- name: Checkout
Expand All @@ -23,8 +23,9 @@ jobs:
- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- name: Build ${{ matrix.target }}
- name: Build barretenberg on ${{ matrix.target }}
run: |
nix build -L .#${{ matrix.target }}
Expand All @@ -38,4 +39,24 @@ jobs:
with:
name: ${{ matrix.target }}
path: ./artifacts/
retention-days: 3
retention-days: 3

bb_wrapper:
runs-on: macos-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- name: Build barretenberg_wrapper
working-directory: ./barretenberg_wrapper
# This removes the lockfile as a workaround to relative file paths causing a failure in flakes
run: |
rm flake.lock
nix build -L
./result/bin/barretenberg_wrapper
26 changes: 6 additions & 20 deletions barretenberg_wrapper/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ARM_LINUX: &str = "aarch64-linux";
const CC_ENV_KEY: &&str = &"CC";
const CXX_ENV_KEY: &&str = &"CXX";

const LIBB_ENV_KEY: &str = "LIBBARRETENBERG";
const BINGDEN_ENV_KEY: &str = "BINDGEN_EXTRA_CLANG_ARGS";

fn select_toolchain() -> &'static str {
let arch = select_arch();
Expand Down Expand Up @@ -112,16 +112,16 @@ fn set_compiler(toolchain: &'static str) {
fn main() {
// TODO: Passing value like that is consistent with cargo but feels hacky from nix perspective

let libbarretenberg = env::var(LIBB_ENV_KEY).unwrap_or_default();
let bindgen_flags = env::var(BINGDEN_ENV_KEY).unwrap_or_default();
let bindings;

// Link C++ std lib
println!("cargo:rustc-link-lib={}", select_cpp_stdlib());

if libbarretenberg.is_empty() {
if bindgen_flags.is_empty() {
println!(
"cargo:info={} environment variable not set. Using fixed Barretenberg path `../barretenberg`",
LIBB_ENV_KEY
BINGDEN_ENV_KEY
);
// Builds the project in ../barretenberg into dst
println!("cargo:rerun-if-changed=../barretenberg");
Expand Down Expand Up @@ -238,24 +238,10 @@ fn main() {
.generate()
.expect("Unable to generate bindings");
} else {
println!("cargo:rustc-link-lib=omp");

// :info isn't "cargo correct" but will show value when this errors and will help with debugging
println!("cargo:info={} is {}", LIBB_ENV_KEY, libbarretenberg);

println!("cargo:rustc-link-search={}/lib", libbarretenberg);

bindings = bindgen::Builder::default()
// Clang args so that we can use relative include paths
.clang_args(&[
"-xc++",
format!("-I{}/include/aztec", libbarretenberg.as_str()).as_str(),
])
.header(format!(
// TODO: would this be cleaner with if we would have cmake install target?
"{}/include/aztec/bb/bb.hpp",
libbarretenberg.as_str()
))
.clang_args(&["-xc++"])
.header_contents("wrapper.h", "#include <aztec/bb/bb.hpp>")
.generate()
.expect("Unable to generate bindings");
}
Expand Down
13 changes: 5 additions & 8 deletions barretenberg_wrapper/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,17 @@

doCheck = false;


LIBBARRETENBERG = libbarretenberg;

# Bindegn needs this
# Bindegn needs these
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
BINDGEN_EXTRA_CLANG_ARGS = "-I${libbarretenberg}/include/aztec -L${libbarretenberg}";
RUSTFLAGS = "-L${libbarretenberg}/lib -lomp";

buildInputs = [
pkgs.llvmPackages.openmp
pkgs.llvmPackages.openmp
libbarretenberg
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
];


};
in rec {
checks = { inherit barretenberg_wrapper_crate; };
Expand All @@ -80,7 +77,7 @@

buildInputs = packages.default.buildInputs ;

nativeBuildInputs = with pkgs; [
nativeBuildInputs = with pkgs; [
cargo
rustc ];
};
Expand Down

0 comments on commit 4ddc09b

Please sign in to comment.