From 70bf010965fe4c706d22861d1b25aa1b087a16bb Mon Sep 17 00:00:00 2001 From: Globallager <72797635+Globallager@users.noreply.github.com> Date: Tue, 11 Oct 2022 17:52:14 +0800 Subject: [PATCH 1/2] Fix Compiling on M1/M2 Macs Assume `libomp` installed with `brew`. --- barretenberg_wrapper/build.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/barretenberg_wrapper/build.rs b/barretenberg_wrapper/build.rs index 1bb40c4cd..0752370fe 100644 --- a/barretenberg_wrapper/build.rs +++ b/barretenberg_wrapper/build.rs @@ -33,6 +33,7 @@ fn select_arch() -> Arch { let arch = std::env::consts::ARCH; match arch { "arm" => Arch::Arm, + "aarch64" => Arch::Arm, "x86_64" => Arch::X86_64, _ => { // For other arches, we default to x86_64 @@ -219,12 +220,11 @@ fn link_lib_omp() { // // we are using clang, so we need to tell the linker where // to search for lomp. - // - // For macOS, we do not need to supply the search path - // however for linux we do 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") } if let ARM_LINUX = select_toolchain() { // only arm linux uses gcc From 2f7ca8a8d364942d7a6de0f6546a32635868353d Mon Sep 17 00:00:00 2001 From: Globallager <72797635+Globallager@users.noreply.github.com> Date: Thu, 13 Oct 2022 14:56:21 +0800 Subject: [PATCH 2/2] Update Toolchain Checks - Add Intel Mac Homebrew path - Optimise toolchain equality checks / matching - Reformat comments --- barretenberg_wrapper/build.rs | 50 +++++++++++++++-------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/barretenberg_wrapper/build.rs b/barretenberg_wrapper/build.rs index 0752370fe..a8b148d6e 100644 --- a/barretenberg_wrapper/build.rs +++ b/barretenberg_wrapper/build.rs @@ -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"; @@ -41,7 +39,6 @@ fn select_arch() -> Arch { } } } - fn select_os() -> OS { let os = std::env::consts::OS; match os { @@ -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()) @@ -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"); @@ -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) @@ -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); // println!( // "cargo:rustc-link-search={}/build/src/aztec/bb", @@ -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++"]) @@ -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"), + ARM_APPLE => println!("cargo:rustc-link-search=/opt/homebrew/lib"), + &_ => 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 { @@ -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")