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 Compiling from nargo on M2 Mac #1

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions barretenberg_wrapper/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ pub enum Arch {
X86_64,
Arm,
}
// These constants correspond to the filenames
// in cmake/toolchains
// These constants correspond to the filenames in cmake/toolchains
//
// There are currently no toolchains for windows
// Please use WSL
// There are currently no toolchain for windows, please use WSL
const INTEL_APPLE: &str = "x86_64-apple-clang";
const INTEL_LINUX: &str = "x86_64-linux-clang";
const ARM_APPLE: &str = "arm-apple-clang";
Expand Down Expand Up @@ -41,7 +39,6 @@ fn select_arch() -> Arch {
}
}
}

fn select_os() -> OS {
let os = std::env::consts::OS;
match os {
Expand All @@ -55,18 +52,16 @@ fn select_os() -> OS {
}
}
fn select_cpp_stdlib() -> &'static str {
// The name of the c++ stdlib depends on the
// operating system
// The name of the c++ stdlib depends on the OS
match select_os() {
OS::Linux => "stdc++",
OS::Apple => "c++",
}
}
fn set_brew_env_var() {
// The cmake file for macos uses an environment
// variable to figure out where to find
// certain programs installed via brew
if let OS::Apple = select_os() {
fn set_brew_env_var(toolchain: &'static str) {
// The cmake file for macos uses an environment variable
// to figure out where to find certain programs installed via brew
if toolchain == INTEL_APPLE || toolchain == ARM_APPLE {
let output = std::process::Command::new("brew")
.arg("--prefix")
.stdout(std::process::Stdio::piped())
Expand All @@ -76,9 +71,9 @@ fn set_brew_env_var() {
let stdout = String::from_utf8(output.stdout).unwrap();

env::set_var("BREW_PREFIX", stdout.trim());
//
}
}

fn main() {
// Builds the project in ../barretenberg into dst
println!("cargo:rerun-if-changed=../barretenberg");
Expand All @@ -90,7 +85,7 @@ fn main() {
// TODO: We could check move this to a bash script along with
// TODO: checks that check that all the necessary dependencies are
// TODO installed via llvm
set_brew_env_var();
set_brew_env_var(toolchain);

let dst = cmake::Config::new("../barretenberg")
.very_verbose(true)
Expand All @@ -105,8 +100,8 @@ fn main() {

// Link C++ std lib
println!("cargo:rustc-link-lib={}", select_cpp_stdlib());
// Link lib OMP
link_lib_omp();
// Link lib OpenMP
link_lib_omp(toolchain);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass retrieved toolchain down to minimise runs of select_toolchain().


// println!(
// "cargo:rustc-link-search={}/build/src/aztec/bb",
Expand Down Expand Up @@ -203,7 +198,6 @@ fn main() {
println!("cargo:rustc-link-lib=static=stdlib_pedersen");

// Generate bindings from a header file and place them in a bindings.rs file

let bindings = bindgen::Builder::default()
// Clang args so that we can use relative include paths
.clang_args(&["-I../barretenberg/src/aztec", "-I../..", "-I../", "-xc++"])
Expand All @@ -216,17 +210,18 @@ fn main() {
.expect("Couldn't write bindings");
}

fn link_lib_omp() {
//
// we are using clang, so we need to tell the linker where
// to search for lomp.
if let OS::Linux = select_os() {
let llvm_dir = find_llvm_linux_path();
println!("cargo:rustc-link-search={}/lib", llvm_dir);
} else if let ARM_APPLE = select_toolchain() {
println!("cargo:rustc-link-search=/opt/homebrew/lib")
fn link_lib_omp(toolchain: &'static str) {
// We are using clang, so we need to tell the linker where to search for lomp
match toolchain {
INTEL_LINUX | ARM_LINUX => {
let llvm_dir = find_llvm_linux_path();
println!("cargo:rustc-link-search={}/lib", llvm_dir)
}
INTEL_APPLE => println!("cargo:rustc-link-search=/usr/local/lib"),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Intel Mac Homebrew path

ARM_APPLE => println!("cargo:rustc-link-search=/opt/homebrew/lib"),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the error from clang on macOS failing to find lomp when no search path is provided:

error: linking with `cc` failed: exit status: 1
  = note: ld: library not found for -lomp
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

&_ => unimplemented!("lomp linking of {} is not supported", toolchain),
}
if let ARM_LINUX = select_toolchain() {
if toolchain == ARM_LINUX {
// only arm linux uses gcc
println!("cargo:rustc-link-lib=gomp")
} else {
Expand All @@ -238,7 +233,6 @@ fn find_llvm_linux_path() -> String {
// Most linux systems will have the `find` application
//
// This assumes that there is a single llvm-X folder in /usr/lib

let output = std::process::Command::new("sh")
.arg("-c")
.arg("find /usr/lib -type d -name \"*llvm-*\" -print -quit")
Expand Down