-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
cargo_build_script
to work without runfiles support.
- Loading branch information
1 parent
30df2ef
commit 0b52f38
Showing
12 changed files
with
218 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
load(":runfiles_enabled.bzl", "runfiles_enabled_build_setting") | ||
|
||
bzl_library( | ||
name = "bzl_lib", | ||
srcs = glob(["**/*.bzl"]), | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
runfiles_enabled_build_setting( | ||
name = "runfiles_enabled", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""A small utility module dedicated to detecting whether or not the `--enable_runfiles` and `--windows_enable_symlinks` flag are enabled | ||
""" | ||
|
||
RunfilesEnabledInfo = provider( | ||
doc = "A singleton provider that contains the raw value of a build setting", | ||
fields = { | ||
"value": "The value of the build setting in the current configuration. " + | ||
"This value may come from the command line or an upstream transition, " + | ||
"or else it will be the build setting's default.", | ||
}, | ||
) | ||
|
||
def _runfiles_enabled_setting_impl(ctx): | ||
return RunfilesEnabledInfo(value = ctx.attr.value) | ||
|
||
runfiles_enabled_setting = rule( | ||
implementation = _runfiles_enabled_setting_impl, | ||
doc = "A bool-typed build setting that cannot be set on the command line", | ||
attrs = { | ||
"value": attr.bool( | ||
doc = "A boolean value", | ||
mandatory = True, | ||
), | ||
}, | ||
) | ||
|
||
_RUNFILES_ENABLED_ATTR_NAME = "_runfiles_enabled" | ||
|
||
def runfiles_enabled_attr(default = Label("//cargo/private:runfiles_enabled")): | ||
return { | ||
_RUNFILES_ENABLED_ATTR_NAME: attr.label( | ||
doc = "A flag representing whether or not runfiles are enabled.", | ||
providers = [RunfilesEnabledInfo], | ||
default = default, | ||
cfg = "exec", | ||
), | ||
} | ||
|
||
def runfiles_enabled_build_setting(name, **kwargs): | ||
native.config_setting( | ||
name = "{}_enable_runfiles".format(name), | ||
values = {"enable_runfiles": "true"}, | ||
) | ||
|
||
native.config_setting( | ||
name = "{}_disable_runfiles".format(name), | ||
values = {"enable_runfiles": "false"}, | ||
) | ||
|
||
runfiles_enabled_setting( | ||
name = name, | ||
value = select({ | ||
# If either of the runfiles are set, use the flag | ||
":{}_enable_runfiles".format(name): True, | ||
":{}_disable_runfiles".format(name): False, | ||
# Otherwise fall back to the system default. | ||
"@platforms//os:windows": False, | ||
"//conditions:default": True, | ||
}), | ||
**kwargs | ||
) | ||
|
||
def is_runfiles_enabled(attr): | ||
"""Determine whether or not runfiles are enabled. | ||
Args: | ||
attr (struct): A rule's struct of attributes (`ctx.attr`) | ||
Returns: | ||
bool: The enable_runfiles value. | ||
""" | ||
|
||
runfiles_enabled = getattr(attr, _RUNFILES_ENABLED_ATTR_NAME, None) | ||
|
||
return runfiles_enabled[RunfilesEnabledInfo].value if runfiles_enabled else True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
load("//rust:defs.bzl", "rust_binary") | ||
|
||
rust_binary( | ||
name = "runfiles_maker", | ||
srcs = ["runfiles_maker.rs"], | ||
edition = "2021", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! A tool for building runfiles directories for Bazel environments that don't | ||
//! support runfiles or have runfiles enabled. | ||
use std::collections::BTreeMap; | ||
use std::path::PathBuf; | ||
|
||
struct Args { | ||
pub output_dir: PathBuf, | ||
pub runfiles: BTreeMap<PathBuf, PathBuf>, | ||
} | ||
|
||
impl Args { | ||
fn parse() -> Self { | ||
let args_file = std::env::args().nth(1).expect("No args file was passed."); | ||
|
||
let content = std::fs::read_to_string( | ||
args_file | ||
.strip_prefix("@") | ||
.expect("Param files should start with @"), | ||
) | ||
.unwrap(); | ||
let mut args = content.lines(); | ||
|
||
let output_dir = PathBuf::from( | ||
args.next() | ||
.unwrap_or_else(|| panic!("Not enough arguments provided.")), | ||
); | ||
let runfiles = args | ||
.map(|s| { | ||
let s = if s.starts_with("'") && s.ends_with("'") { | ||
s.trim_matches('\'') | ||
} else { | ||
s | ||
}; | ||
let (src, dest) = s | ||
.split_once('=') | ||
.unwrap_or_else(|| panic!("Unexpected runfiles argument: {}", s)); | ||
(PathBuf::from(src), PathBuf::from(dest)) | ||
}) | ||
.collect::<BTreeMap<_, _>>(); | ||
|
||
assert!(!runfiles.is_empty(), "No runfiles found"); | ||
|
||
Args { | ||
output_dir, | ||
runfiles, | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let args = Args::parse(); | ||
|
||
for (src, dest) in args.runfiles.iter() { | ||
let out_dest = args.output_dir.join(dest); | ||
std::fs::create_dir_all( | ||
out_dest | ||
.parent() | ||
.expect("The output location should have a valid parent."), | ||
) | ||
.expect("Failed to create output directory"); | ||
std::fs::copy(src, &out_dest).unwrap_or_else(|e| { | ||
panic!( | ||
"Failed to copy file {} -> {}\n{:?}", | ||
src.display(), | ||
out_dest.display(), | ||
e | ||
) | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.