From 4ad706982735eafd066ac3bf44275185dce7130f Mon Sep 17 00:00:00 2001 From: Kenta Iwasaki Date: Tue, 31 Aug 2021 02:27:20 +0900 Subject: [PATCH] cargo-build-bpf: allow sdk path to be set by environment variables In many Linux distros such as NixOS, the directory in which packages are installed is assumed to be read-only. To work around this, it is expected that the filepaths which packaged CLI tools take in are able to be freely configured through either 1) command-line flags, 2) environment variables, or 3) a configuration file. In this commit, environment variables 'BPF_SDK_PATH' and 'BPF_OUT_PATH', which map respectively to command-line flags '--bpf-sdk-path' and '--bpf-out-dir', are now handled in cargo-build-bpf. Additionally, given that arbitrary filepaths may now be set in which the BPF SDK is located, the requirement in which '$BPF_SDK_PATH/dependencies/bpf-tools' must strictly be a symbolic link to the directory '$HOME/.cache/solana/${bpf-tools.version}/bpf-tools has been relaxed. Ideally, the directory in which bpf-tools is expected to be downloaded to and stored should be configurable. Though, this commit serves as a temporary fix which enables NixOS users to now start being able to build applications with the Solana SDK. --- sdk/cargo-build-bpf/src/main.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/sdk/cargo-build-bpf/src/main.rs b/sdk/cargo-build-bpf/src/main.rs index 07ce2df19b..68ab04cf69 100644 --- a/sdk/cargo-build-bpf/src/main.rs +++ b/sdk/cargo-build-bpf/src/main.rs @@ -131,7 +131,13 @@ fn install_if_missing( .join("solana") .join(version) .join(package); - if !target_path.is_dir() { + + if !target_path.is_dir() + && !target_path + .symlink_metadata() + .map(|metadata| metadata.file_type().is_symlink()) + .unwrap_or(false) + { if target_path.exists() { fs::remove_file(&target_path).map_err(|err| err.to_string())?; } @@ -150,7 +156,7 @@ fn install_if_missing( .map_err(|err| err.to_string())?; fs::remove_file(file).map_err(|err| err.to_string())?; } - // Make a symbolyc link source_path -> target_path in the + // Make a symbolic link source_path -> target_path in the // sdk/bpf/dependencies directory if no valid link found. let source_base = config.bpf_sdk.join("dependencies"); if !source_base.exists() { @@ -158,17 +164,7 @@ fn install_if_missing( } let source_path = source_base.join(package); // Check whether the correct symbolic link exists. - let invalid_link = if let Ok(link_target) = source_path.read_link() { - if link_target != target_path { - fs::remove_file(&source_path).map_err(|err| err.to_string())?; - true - } else { - false - } - } else { - true - }; - if invalid_link { + if source_path.read_link().is_err() { #[cfg(unix)] std::os::unix::fs::symlink(target_path, source_path).map_err(|err| err.to_string())?; #[cfg(windows)] @@ -659,6 +655,7 @@ fn main() { .version(crate_version!()) .arg( Arg::with_name("bpf_out_dir") + .env("BPF_OUT_PATH") .long("bpf-out-dir") .value_name("DIRECTORY") .takes_value(true) @@ -666,6 +663,7 @@ fn main() { ) .arg( Arg::with_name("bpf_sdk") + .env("BPF_SDK_PATH") .long("bpf-sdk") .value_name("PATH") .takes_value(true)