Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat(nix): Switch to which clang (#88)
Browse files Browse the repository at this point in the history
Co-authored-by: Koby <[email protected]>
  • Loading branch information
kobyhallx and Koby authored Mar 16, 2023
1 parent c424cd3 commit 21184b0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
40 changes: 26 additions & 14 deletions barretenberg-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,20 @@ fn link_lib_omp(os: &OS) {
// We are using clang, so we need to tell the linker where to search for lomp
match os {
OS::Linux => {
let llvm_dir = find_llvm_linux_path();
println!("cargo:rustc-link-search={llvm_dir}/lib")
let clang_path_string =
which_clang("clang").expect("Err: Expected to find clang installation on $PATH");
let clang_path = std::path::Path::new(clang_path_string.as_str());
let llvm_path = clang_path
.parent()
.expect("Expected to find parent directory of {clang_path_string}");
let llvm_lib_path = llvm_path.join("lib");
llvm_path
.try_exists()
.expect("Err: Clang lib path does not exist.");
println!(
"cargo:rustc-link-search={}",
llvm_lib_path.to_str().unwrap()
);
}
OS::Apple => {
if let Some(brew_prefix) = find_brew_prefix() {
Expand All @@ -141,19 +153,19 @@ fn link_lib_omp(os: &OS) {
println!("cargo:rustc-link-lib=omp");
}

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")
.stdout(std::process::Stdio::piped())
fn which_clang(clang_command: &'static str) -> Option<String> {
let which_clang_command = std::process::Command::new("which")
.arg(clang_command)
.output()
.expect("Failed to execute command to run `find`");
// This should be the path to llvm
let path_to_llvm = String::from_utf8(output.stdout).unwrap();
path_to_llvm.trim().to_owned()
.expect("Failed to execute which clang command");

if which_clang_command.status.success() {
let path =
String::from_utf8(which_clang_command.stdout).expect("Invalid UTF-8 output from which");
Some(path.trim().to_owned())
} else {
None
}
}

fn find_brew_prefix() -> Option<String> {
Expand Down
17 changes: 15 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,24 @@

packages.default = barretenberg-backend;

devShells.default = pkgs.mkShell ({
# llvmPackages should be aligned to selection from libbarretenberg
# better if we get rid of llvm targets and override them from input
devShells.default = pkgs.mkShell.override { stdenv = pkgs.llvmPackages.stdenv; } ({
inputsFrom = builtins.attrValues self.checks;

buildInputs = packages.default.buildInputs;
nativeBuildInputs = packages.default.nativeBuildInputs;
nativeBuildInputs = with pkgs; packages.default.nativeBuildInputs ++ [
which
starship
git
cargo
rustc
];

shellHook = ''
eval "$(starship init bash)"
'';

} // environment);
});
}

0 comments on commit 21184b0

Please sign in to comment.