Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 13 additions & 16 deletions cargo/private/cargo_build_script_runner/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::fs::{create_dir_all, read_dir, read_to_string, remove_file, write};
use std::path::{Path, PathBuf};
use std::process::Command;

use cargo_build_script_runner::cargo_manifest_dir::{remove_symlink, symlink, RunfilesMaker};
use cargo_build_script_runner::cargo_manifest_dir::{remove_symlink_if_symlink, symlink, RunfilesMaker};
use cargo_build_script_runner::{BuildScriptOutput, CompileAndLinkFlags, SUPPRESS_WARNINGS_ENV};

fn run_buildrs() -> Result<(), String> {
Expand Down Expand Up @@ -75,10 +75,11 @@ fn run_buildrs() -> Result<(), String> {
.ok_or_else(|| "Failed while getting file name".to_string())?;
let link = manifest_dir.join(file_name);

symlink_if_not_exists(&path, &link)
.map_err(|err| format!("Failed to symlink {path:?} to {link:?}: {err}"))?;

exec_root_links.push(link)
if symlink_if_not_exists(&path, &link)
.map_err(|err| format!("Failed to symlink {path:?} to {link:?}: {err}"))?
{
exec_root_links.push(link);
}
}
}

Expand Down Expand Up @@ -237,7 +238,7 @@ fn run_buildrs() -> Result<(), String> {

if !exec_root_links.is_empty() {
for link in exec_root_links {
remove_symlink(&link).map_err(|e| {
remove_symlink_if_symlink(&link).map_err(|e| {
format!(
"Failed to remove exec_root link '{}' with {:?}",
link.display(),
Expand Down Expand Up @@ -360,9 +361,13 @@ fn set_script_runfiles_env(script_path: &Path, command: &mut Command) {
}

/// Create a symlink from `link` to `original` if `link` doesn't already exist.
fn symlink_if_not_exists(original: &Path, link: &Path) -> Result<(), String> {
fn symlink_if_not_exists(original: &Path, link: &Path) -> Result<bool, String> {
if std::fs::symlink_metadata(link).is_ok() {
return Ok(false);
}

symlink(original, link)
.or_else(swallow_already_exists)
.map(|_| true)
.map_err(|err| format!("Failed to create symlink: {err}"))
}

Expand All @@ -383,14 +388,6 @@ fn resolve_rundir(rundir: &str, exec_root: &Path, manifest_dir: &Path) -> Result
Ok(exec_root.join(rundir_path))
}

fn swallow_already_exists(err: std::io::Error) -> std::io::Result<()> {
if err.kind() == std::io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(err)
}
}

/// A representation of expected command line arguments.
struct Args {
progname: String,
Expand Down
10 changes: 10 additions & 0 deletions cargo/private/cargo_build_script_runner/cargo_manifest_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ pub fn remove_symlink(path: &Path) -> Result<(), std::io::Error> {
}
}

/// Remove `path` only when it is a symlink.
pub fn remove_symlink_if_symlink(path: &Path) -> Result<(), std::io::Error> {
match std::fs::symlink_metadata(path) {
Ok(meta) if meta.file_type().is_symlink() => remove_symlink(path),
Ok(_) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}
}

/// Check if the system supports symlinks by attempting to create one.
fn system_supports_symlinks(test_dir: &Path) -> Result<bool, String> {
let test_file = test_dir.join("cbsr.txt");
Expand Down
14 changes: 8 additions & 6 deletions crate_universe/private/urls.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ the binaries produced for it.

CARGO_BAZEL_URLS = {
"aarch64-apple-darwin": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-aarch64-apple-darwin",
"x86_64-unknown-linux-musl": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-x86_64-unknown-linux-musl",
"x86_64-unknown-linux-gnu": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-aarch64-unknown-linux-musl",
"aarch64-unknown-linux-gnu": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-aarch64-unknown-linux-musl",
"aarch64-unknown-linux-musl": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-aarch64-unknown-linux-musl",
"x86_64-unknown-linux-gnu": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-x86_64-unknown-linux-musl",
"x86_64-unknown-linux-musl": "https://github.com/mgeisler/rules_rust/releases/download/cargo-bazel-prebuilt/cargo-bazel-x86_64-unknown-linux-musl",
}


CARGO_BAZEL_SHA256S = {
"aarch64-apple-darwin": "5e1617849de558489d2f653644a1ad0c99c4b993fc137f14a7ea890cf06e45d3",
"x86_64-unknown-linux-musl": "db72b6ce92031033ffd50d5147ab78d39f5656eb0980bee276d013bc6e3e2260",
"x86_64-unknown-linux-gnu": "db72b6ce92031033ffd50d5147ab78d39f5656eb0980bee276d013bc6e3e2260",
"aarch64-unknown-linux-musl": "f056c801b59730a2c7cc24fd3606d596d54910068305991b2aa822c32949d4de",
"aarch64-unknown-linux-gnu": "f056c801b59730a2c7cc24fd3606d596d54910068305991b2aa822c32949d4de",
"aarch64-unknown-linux-musl": "f056c801b59730a2c7cc24fd3606d596d54910068305991b2aa822c32949d4de",
"x86_64-unknown-linux-gnu": "db72b6ce92031033ffd50d5147ab78d39f5656eb0980bee276d013bc6e3e2260",
"x86_64-unknown-linux-musl": "db72b6ce92031033ffd50d5147ab78d39f5656eb0980bee276d013bc6e3e2260",
}


CARGO_BAZEL_LABEL = Label("//crate_universe:cargo_bazel_bin")
Loading