Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nix): Use wasi-sdk 12 to provide barretenberg-wasm in overlay #315

Merged
merged 14 commits into from
Apr 5, 2023
6 changes: 5 additions & 1 deletion .github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
target: [llvm12, wasm32, cross-aarch64]
target: [default, wasm32, cross-aarch64]

steps:
- name: Checkout
Expand All @@ -32,6 +32,10 @@ jobs:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- name: Check nix flake
run: |
nix flake check

- name: Build barretenberg as ${{ matrix.target }}
run: |
nix build -L .#${{ matrix.target }}
Expand Down
35 changes: 35 additions & 0 deletions barretenberg-wasm.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{ stdenv, cmake, ninja, binaryen, callPackage }:
let
toolchain_file = ./cpp/cmake/toolchains/wasm32-wasi.cmake;
wasi-sdk = callPackage ./wasi-sdk.nix { };
in
stdenv.mkDerivation
{
pname = "barretenberg.wasm";
version = "0.1.0";

src = ./cpp;

nativeBuildInputs = [ cmake ninja binaryen wasi-sdk ];

buildInputs = [ ];

cmakeFlags = [
"-DTESTING=OFF"
"-DBENCHMARKS=OFF"
"-DCMAKE_TOOLCHAIN_FILE=${toolchain_file}"
"-DCMAKE_C_COMPILER=${wasi-sdk}/bin/clang"
"-DCMAKE_CXX_COMPILER=${wasi-sdk}/bin/clang++"
"-DCMAKE_AR=${wasi-sdk}/bin/llvm-ar"
"-DCMAKE_RANLIB=${wasi-sdk}/bin/llvm-ranlib"
"-DCMAKE_SYSROOT=${wasi-sdk}/share/wasi-sysroot"
"-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER"
"-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY"
"-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY"
"-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY"
"-DCMAKE_C_COMPILER_WORKS=ON"
"-DCMAKE_CXX_COMPILER_WORKS=ON"
];

enableParallelBuilding = true;
}
15 changes: 5 additions & 10 deletions barretenberg.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ overrideCC, stdenv, llvmPackages, cmake, ninja, lib, callPackage, binaryen, gcc11 }:
{ overrideCC, stdenv, llvmPackages, cmake, ninja, lib, callPackage, gcc11 }:
let
targetPlatform = stdenv.targetPlatform;
buildEnv =
Expand All @@ -18,21 +18,16 @@ buildEnv.mkDerivation

src = ./cpp;

nativeBuildInputs = [ cmake ninja ]
++ optionals targetPlatform.isWasm [ binaryen ];
nativeBuildInputs = [ cmake ninja ];

buildInputs = [ ]
++ optionals (targetPlatform.isDarwin || targetPlatform.isLinux) [
llvmPackages.openmp
];
buildInputs = [ llvmPackages.openmp ];

cmakeFlags = [
"-DTESTING=OFF"
"-DBENCHMARKS=OFF"
"-DCMAKE_TOOLCHAIN_FILE=${toolchain_file}"
]
++ optionals (targetPlatform.isDarwin || targetPlatform.isLinux)
[ "-DCMAKE_BUILD_TYPE=RelWithAssert" ];
"-DCMAKE_BUILD_TYPE=RelWithAssert"
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
];

NIX_CFLAGS_COMPILE =
optionals targetPlatform.isDarwin [ " -fno-aligned-allocation" ];
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

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

27 changes: 13 additions & 14 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

outputs = { self, nixpkgs, flake-utils }:
let
barretenbergOverlay = self: super: {
# It seems that llvmPackages_11 can't build WASI, so default to llvmPackages_12
barretenberg = super.callPackage ./barretenberg.nix {
llvmPackages = self.llvmPackages_12;
barretenbergOverlay = final: prev: {
barretenberg = prev.callPackage ./barretenberg.nix { };
barretenberg-wasm = prev.callPackage ./barretenberg-wasm.nix { };
barretenberg-transcript00 = prev.fetchurl {
url = "http://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat";
sha256 = "sha256-D5SzlCb1pX0aF3QmJPfTFwoy4Z1sXhbyAigUOdvkhpU=";
};
};
in
Expand All @@ -33,10 +35,7 @@
value = pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsLLVM.barretenberg;
} ++ optional (pkgs.hostPlatform.isx86_64 && pkgs.hostPlatform.isDarwin) {
name = "cross-aarch64";
value = pkgs.pkgsCross.aarch64-darwin.barretenberg.override {
# llvmPackages_12 seems to fail when we try to cross-compile but llvmPackages_11 works
llvmPackages = pkgs.llvmPackages_11;
};
value = pkgs.pkgsCross.aarch64-darwin.barretenberg;
}
);

Expand All @@ -53,20 +52,19 @@
in
rec {
packages = {
llvm11 = pkgs.barretenberg.override {
llvmPackages = pkgs.llvmPackages_11;
llvm11 = pkgs.barretenberg;
llvm12 = pkgs.barretenberg.override {
llvmPackages = pkgs.llvmPackages_12;
};
llvm12 = pkgs.barretenberg;
llvm13 = pkgs.barretenberg.override {
llvmPackages = pkgs.llvmPackages_13;
};
llvm14 = pkgs.barretenberg.override {
llvmPackages = pkgs.llvmPackages_14;
};
wasm32 = pkgs.pkgsCross.wasi32.barretenberg;
wasm32 = pkgs.barretenberg-wasm;

# Defaulting to llvm12 so we can ensure consistent shells
default = packages.llvm12;
default = packages.llvm11;
} // crossTargets;

# Provide legacyPackages with our overlay so we can run
Expand All @@ -87,6 +85,7 @@

wasm32 = pkgs.mkShell.override
{
# TODO: This derivations forces wasi-sdk 12 so the stdenv will have the wrong tools
stdenv = packages.wasm32.stdenv;
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
}
({
Expand Down
48 changes: 48 additions & 0 deletions wasi-sdk.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copied from https://github.com/ereslibre/nixities/blob/2c60af777fc863f90e6e4eeffcf3465def93a1f3/packages/wasi-sdk/default.nix
# with a fix for the autoPatchelfHook needing libstdc++.so and some refactor
{ lib, pkgs, stdenv }:
let
pname = "wasi-sdk";
version = "12";
in
pkgs.stdenv.mkDerivation {
inherit pname version;

sourceRoot = "${pname}-${version}.0";
dontBuild = true;
dontConfigure = true;
dontStrip = true;

nativeBuildInputs =
lib.optional stdenv.isLinux (with pkgs; [ autoPatchelfHook ]);

# Needed by autoPatchelfHook to have libstdc++
# see https://discourse.nixos.org/t/autopatchelfhook-not-patching-all-dependencies/14634/6
buildInputs =
lib.optional stdenv.isLinux [ stdenv.cc.cc.lib ];

installPhase = ''
mkdir -p $out/{bin,lib,share}
mv bin/* $out/bin/
mv lib/* $out/lib/
mv share/* $out/share/
'';

src =
let
tarball =
if stdenv.hostPlatform.isDarwin then {
suffix = "macos";
hash = "sha256-juJfnD/eYY/upcV62tOFFSYmeEtra1L7Vj5e2DK/U+8=";
} else {
suffix = "linux";
hash = "sha256-+kdpTXW/b86Y++eScZMpiyXuA9reJ/ykU9fdUwN4lzo=";
};
in

pkgs.fetchurl {
url =
"https://github.com/WebAssembly/${pname}/releases/download/${pname}-${version}/${pname}-${version}.0-${tarball.suffix}.tar.gz";
hash = tarball.hash;
};
}