Skip to content

Commit

Permalink
Merge branch 'main' into data
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jul 1, 2021
2 parents 0d197ef + 4bf51b3 commit fe7355f
Show file tree
Hide file tree
Showing 20 changed files with 309 additions and 65 deletions.
4 changes: 2 additions & 2 deletions cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("//rust:rust.bzl", "rust_binary")
load("//rust/private:rustc.bzl", "BuildInfo", "get_compilation_mode_opts", "get_linker_and_args")

# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "expand_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")
load("//rust/private:utils.bzl", "expand_dict_value_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")

def get_cc_compile_env(cc_toolchain, feature_configuration):
"""Gather cc environment variables from the given `cc_toolchain`
Expand Down Expand Up @@ -117,7 +117,7 @@ def _build_script_impl(ctx):
for f in ctx.attr.crate_features:
env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1"

env.update(expand_locations(
env.update(expand_dict_value_locations(
ctx,
ctx.attr.build_script_env,
getattr(ctx.attr, "data", []) +
Expand Down
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ PAGES = dict([
"rust_toolchain",
"rust_toolchain_repository",
"rust_toolchain_repository_proxy",
"rust_stdlib_filegroup",
],
),
page(
Expand Down
14 changes: 7 additions & 7 deletions docs/defs.md

Large diffs are not rendered by default.

34 changes: 27 additions & 7 deletions docs/flatten.md

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions docs/rust_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@
* [rust_toolchain](#rust_toolchain)
* [rust_toolchain_repository](#rust_toolchain_repository)
* [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy)
* [rust_stdlib_filegroup](#rust_stdlib_filegroup)

<a id="#rust_stdlib_filegroup"></a>

## rust_stdlib_filegroup

<pre>
rust_stdlib_filegroup(<a href="#rust_stdlib_filegroup-name">name</a>, <a href="#rust_stdlib_filegroup-srcs">srcs</a>)
</pre>

A dedicated filegroup-like rule for Rust stdlib artifacts.

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_stdlib_filegroup-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="rust_stdlib_filegroup-srcs"></a>srcs | The list of targets/files that are components of the rust-stdlib file group | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | required | |


<a id="#rust_toolchain"></a>

Expand Down
2 changes: 2 additions & 0 deletions docs/symbols.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ load(
)
load(
"@rules_rust//rust:toolchain.bzl",
_rust_stdlib_filegroup = "rust_stdlib_filegroup",
_rust_toolchain = "rust_toolchain",
)
load(
Expand Down Expand Up @@ -96,6 +97,7 @@ rust_bindgen_repositories = _rust_bindgen_repositories
rust_toolchain = _rust_toolchain
rust_proto_toolchain = _rust_proto_toolchain
rust_proto_repositories = _rust_proto_repositories
rust_stdlib_filegroup = _rust_stdlib_filegroup

cargo_build_script = _cargo_build_script

Expand Down
23 changes: 23 additions & 0 deletions examples/flag_locations/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load(
"@rules_rust//rust:rust.bzl",
"rust_test",
)

# generate a file containing cfg flags
genrule(
name = "flag_generator",
outs = ["generated_flag.data"],
cmd = "echo --cfg=test_flag > $@",
)

rust_test(
name = "test",
srcs = [
"main.rs",
],
data = [":flag_generator"],
edition = "2018",
rustc_flags = [
"@$(location :flag_generator)",
],
)
9 changes: 9 additions & 0 deletions examples/flag_locations/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn test() {
// we should be able to read rustc args from a generated file
if cfg!(test_flag) {
return;
}

unreachable!();
}
7 changes: 7 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

"""Public entry point to all Rust rules and supported APIs."""

load(
"//rust:toolchain.bzl",
_rust_stdlib_filegroup = "rust_stdlib_filegroup",
)
load(
"//rust/private:clippy.bzl",
_rust_clippy = "rust_clippy",
Expand Down Expand Up @@ -111,3 +115,6 @@ rustfmt_aspect = _rustfmt_aspect

rustfmt_test = _rustfmt_test
# See @rules_rust//rust/private:rustfmt.bzl for a complete description.

rust_stdlib_filegroup = _rust_stdlib_filegroup
# See @rules_rust//rust:toolchain.bzl for a complete description.
3 changes: 2 additions & 1 deletion rust/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ which exports the `rust_common` struct.
In the Bazel lingo, `rust_common` gives the access to the Rust Sandwich API.
"""

load(":providers.bzl", "CrateInfo", "DepInfo")
load(":providers.bzl", "CrateInfo", "DepInfo", "StdLibInfo")

def _create_crate_info(**kwargs):
"""A constructor for a `CrateInfo` provider
Expand All @@ -43,4 +43,5 @@ rust_common = struct(
create_crate_info = _create_crate_info,
crate_info = CrateInfo,
dep_info = DepInfo,
stdlib_info = StdLibInfo,
)
17 changes: 17 additions & 0 deletions rust/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ DepInfo = provider(
"transitive_noncrates": "depset[LinkerInput]: All transitive dependencies that aren't crates.",
},
)

StdLibInfo = provider(
doc = (
"A collection of files either found within the `rust-stdlib` artifact or " +
"generated based on existing files."
),
fields = {
"alloc_files": "List[File]: `.a` files related to the `alloc` module.",
"between_alloc_and_core_files": "List[File]: `.a` files related to the `compiler_builtins` module.",
"between_core_and_std_files": "List[File]: `.a` files related to all modules except `adler`, `alloc`, `compiler_builtins`, `core`, and `std`.",
"core_files": "List[File]: `.a` files related to the `core` and `adler` modules",
"dot_a_files": "Depset[File]: Generated `.a` files",
"srcs": "List[Target]: The original `src` attribute.",
"std_files": "Depset[File]: `.a` files associated with the `std` module.",
"std_rlibs": "List[File]: All `.rlib` files",
},
)
4 changes: 3 additions & 1 deletion rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def BUILD_for_clippy(target_triple):
return _build_file_for_clippy_template.format(binary_ext = system_to_binary_ext(system))

_build_file_for_stdlib_template = """\
filegroup(
load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
rust_stdlib_filegroup(
name = "rust_lib-{target_triple}",
srcs = glob(
[
Expand Down
13 changes: 10 additions & 3 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load(
"crate_name_from_attr",
"dedent",
"determine_output_hash",
"expand_locations",
"expand_dict_value_locations",
"find_toolchain",
)

Expand Down Expand Up @@ -342,7 +342,7 @@ def _create_test_launcher(ctx, toolchain, output, providers):

# Expand the environment variables and write them to a file
environ_file = ctx.actions.declare_file(launcher_filename + ".launchfiles/env")
environ = expand_locations(
environ = expand_dict_value_locations(
ctx,
getattr(ctx.attr, "env", {}),
data,
Expand Down Expand Up @@ -624,7 +624,14 @@ _common_attrs = {
"""),
),
"rustc_flags": attr.string_list(
doc = "List of compiler flags passed to `rustc`.",
doc = dedent("""\
List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined
source/output path variables like `$location`, `$execpath`, and `$rootpath`.
This expansion is useful if you wish to pass a generated file of
arguments to rustc: `@$(location //package:target)`.
"""),
),
# TODO(stardoc): How do we provide additional documentation to an inherited attribute?
# "name": attr.string(
Expand Down
19 changes: 13 additions & 6 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ load("//rust/private:common.bzl", "rust_common")
load(
"//rust/private:utils.bzl",
"crate_name_from_attr",
"expand_locations",
"expand_dict_value_locations",
"expand_list_element_locations",
"find_cc_toolchain",
"get_lib_name",
"get_preferred_artifact",
Expand Down Expand Up @@ -438,9 +439,16 @@ def construct_arguments(

# Tell Rustc where to find the standard library
args.add_all(rust_lib_paths, before_each = "-L", format_each = "%s")

args.add_all(rust_flags)
args.add_all(getattr(attr, "rustc_flags", []))

data_paths = getattr(attr, "data", []) + getattr(attr, "compile_data", [])
args.add_all(
expand_list_element_locations(
ctx,
getattr(attr, "rustc_flags", []),
data_paths,
),
)
add_edition_flags(args, crate_info)

# Link!
Expand Down Expand Up @@ -471,11 +479,10 @@ def construct_arguments(
env["CARGO_BIN_EXE_" + dep_crate_info.output.basename] = dep_crate_info.output.short_path

# Update environment with user provided variables.
env.update(expand_locations(
env.update(expand_dict_value_locations(
ctx,
crate_info.rustc_env,
getattr(attr, "data", []) +
getattr(attr, "compile_data", []),
data_paths,
))

# This empty value satisfies Clippy, which otherwise complains about the
Expand Down
25 changes: 24 additions & 1 deletion rust/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _expand_location(ctx, env, data):
env = env.replace(directive, "${pwd}/" + directive)
return ctx.expand_location(env, data)

def expand_locations(ctx, env, data):
def expand_dict_value_locations(ctx, env, data):
"""Performs location-macro expansion on string values.
$(execroot ...) and $(location ...) are prefixed with ${pwd},
Expand All @@ -167,6 +167,8 @@ def expand_locations(ctx, env, data):
as compilation happens in a separate sandbox folder, so when it comes time
to read the file at runtime, the path is no longer valid.
See [`expand_location`](https://docs.bazel.build/versions/main/skylark/lib/ctx.html#expand_location) for detailed documentation.
Args:
ctx (ctx): The rule's context object
env (dict): A dict whose values we iterate over
Expand All @@ -179,6 +181,27 @@ def expand_locations(ctx, env, data):
"""
return dict([(k, _expand_location(ctx, v, data)) for (k, v) in env.items()])

def expand_list_element_locations(ctx, args, data):
"""Performs location-macro expansion on a list of string values.
$(execroot ...) and $(location ...) are prefixed with ${pwd},
which process_wrapper and build_script_runner will expand at run time
to the absolute path.
See [`expand_location`](https://docs.bazel.build/versions/main/skylark/lib/ctx.html#expand_location) for detailed documentation.
Args:
ctx (ctx): The rule's context object
args (list): A list we iterate over
data (sequence of Targets): The targets which may be referenced by
location macros. This is expected to be the `data` attribute of
the target, though may have other targets or attributes mixed in.
Returns:
list: A list of arguments with expanded location macros
"""
return [_expand_location(ctx, arg, data) for arg in args]

def name_to_crate_name(name):
"""Converts a build target's name into the name of its associated crate.
Expand Down
Loading

0 comments on commit fe7355f

Please sign in to comment.