Make cargo-build-bpf clean up after failed installation of bpf-tools (#21143)

This commit is contained in:
Dmitri Makarov 2021-11-03 14:06:14 -07:00 committed by GitHub
parent e22c97d541
commit 55d19e61da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 17 deletions

View File

@ -1400,7 +1400,7 @@ fn assert_instruction_count() {
("solana_bpf_rust_noop", 480),
("solana_bpf_rust_param_passing", 146),
("solana_bpf_rust_rand", 487),
("solana_bpf_rust_sanity", 8397),
("solana_bpf_rust_sanity", 8406),
("solana_bpf_rust_secp256k1_recover", 25216),
("solana_bpf_rust_sha", 30704),
]);

View File

@ -119,19 +119,8 @@ fn install_if_missing(
version: &str,
url: &str,
download_file_name: &str,
target_path: &Path,
) -> Result<(), String> {
// Check whether the package is already in ~/.cache/solana.
// Download it and place in the proper location if not found.
let home_dir = PathBuf::from(env::var("HOME").unwrap_or_else(|err| {
eprintln!("Can't get home directory path: {}", err);
exit(1);
}));
let target_path = home_dir
.join(".cache")
.join("solana")
.join(version)
.join(package);
// Check whether the target path is an empty directory. This can
// happen if package download failed on previous run of
// cargo-build-bpf. Remove the target_path directory in this
@ -146,6 +135,8 @@ fn install_if_missing(
fs::remove_dir(&target_path).map_err(|err| err.to_string())?;
}
// Check whether the package is already in ~/.cache/solana.
// Download it and place in the proper location if not found.
if !target_path.is_dir()
&& !target_path
.symlink_metadata()
@ -183,7 +174,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 {
if link_target.ne(target_path) {
fs::remove_file(&source_path).map_err(|err| err.to_string())?;
true
} else {
@ -477,14 +468,41 @@ fn build_bpf_package(config: &Config, target_directory: &Path, package: &cargo_m
} else {
"solana-bpf-tools-linux.tar.bz2"
};
let home_dir = PathBuf::from(env::var("HOME").unwrap_or_else(|err| {
eprintln!("Can't get home directory path: {}", err);
exit(1);
}));
let version = "v1.18";
let package = "bpf-tools";
let target_path = home_dir
.join(".cache")
.join("solana")
.join(version)
.join(package);
install_if_missing(
config,
"bpf-tools",
"v1.18",
package,
version,
"https://github.com/solana-labs/bpf-tools/releases/download",
bpf_tools_download_file_name,
&target_path,
)
.expect("Failed to install bpf-tools");
.unwrap_or_else(|err| {
// The package version directory doesn't contain a valid
// installation, and it should be removed.
let target_path_parent = target_path.parent().expect("Invalid package path");
fs::remove_dir_all(&target_path_parent).unwrap_or_else(|err| {
eprintln!(
"Failed to remove {} while recovering from installation failure: {}",
target_path_parent.to_string_lossy(),
err,
);
exit(1);
});
eprintln!("Failed to install bpf-tools: {}", err);
exit(1);
});
link_bpf_toolchain(config);
let llvm_bin = config