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
3 changes: 3 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ bazel_dep(name = "apple_support", version = "1.24.1", repo_name = "build_bazel_a
internal_deps = use_extension("//rust/private:internal_extensions.bzl", "i")
use_repo(
internal_deps,
"rra",
"rra__miniz_oxide-0.9.1",
"rra__object-0.39.1",
"rrra",
"rrra__anyhow-1.0.102",
"rrra__camino-1.2.2",
Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")

rust_analyzer_dependencies()

load("@rules_rust//tools/auditable:deps.bzl", "auditable_dependencies")

auditable_dependencies()

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
Expand Down
6 changes: 5 additions & 1 deletion cargo/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("//rust:defs.bzl", "rust_binary")

# Loads rust_binary directly from rust/private:rust.bzl (not //rust:defs.bzl)
# to avoid a dependency cycle with the auditable_injector.
# buildifier: disable=bzl-visibility
load("//rust/private:rust.bzl", "rust_binary")

rust_binary(
name = "copy_file",
Expand Down
7 changes: 6 additions & 1 deletion cargo/private/cargo_build_script_runner/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("//rust:defs.bzl", "rust_library", "rust_test")

# Loads rust_binary directly from rust/private:rust.bzl (not //rust:defs.bzl)
# to avoid a dependency cycle with the auditable_injector.
# buildifier: disable=bzl-visibility
load("//rust/private:rust.bzl", "rust_binary")

rust_library(
name = "cargo_build_script_runner",
Expand Down
6 changes: 5 additions & 1 deletion cargo/private/cargo_build_script_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ load(
"name_to_pkg_name",
_build_script_run = "cargo_build_script",
)
load("//rust:defs.bzl", "rust_binary")

# Loads rust_binary directly from rust/private:rust.bzl (not //rust:defs.bzl)
# to avoid a dependency cycle with the auditable_injector.
# buildifier: disable=bzl-visibility
load("//rust/private:rust.bzl", "rust_binary")

def cargo_build_script(
*,
Expand Down
5 changes: 4 additions & 1 deletion cargo/private/cargo_toml_variable_extractor/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
load("//rust:defs.bzl", "rust_binary")
# Loads rust_binary directly from rust/private:rust.bzl (not //rust:defs.bzl)
# to avoid a dependency cycle with the auditable_injector.
# buildifier: disable=bzl-visibility
load("//rust/private:rust.bzl", "rust_binary")

rust_binary(
name = "cargo_toml_variable_extractor",
Expand Down
17 changes: 13 additions & 4 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,23 @@ rust_library = _rust_library
rust_static_library = _rust_static_library
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_shared_library = _rust_shared_library
# See @rules_rust//rust/private:rust.bzl for a complete description.
_AUDITABLE_INJECTOR_SELECT = select({
str(Label("//rust/settings:auditable_enabled")): str(Label("//tools/auditable:auditable_injector")),
"//conditions:default": None,
})

def rust_shared_library(name, **kwargs):
"""Builds a Rust shared library. See @rules_rust//rust/private:rust.bzl for a complete description."""
kwargs.setdefault("auditable_injector", _AUDITABLE_INJECTOR_SELECT)
_rust_shared_library(name = name, **kwargs)

rust_proc_macro = _rust_proc_macro
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_binary = _rust_binary
# See @rules_rust//rust/private:rust.bzl for a complete description.
def rust_binary(name, **kwargs):
"""Builds a Rust binary crate. See @rules_rust//rust/private:rust.bzl for a complete description."""
kwargs.setdefault("auditable_injector", _AUDITABLE_INJECTOR_SELECT)
_rust_binary(name = name, **kwargs)

rust_library_group = _rust_library_group
# See @rules_rust//rust/private:rust.bzl for a complete description.
Expand Down
6 changes: 6 additions & 0 deletions rust/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def _create_crate_info(**kwargs):
kwargs.update({"rustc_env_files": []})
if not "data" in kwargs:
kwargs.update({"data": depset([])})
if not "pkg_name" in kwargs:
kwargs.update({"pkg_name": kwargs.get("name", "")})
if not "version" in kwargs:
kwargs.update({"version": "0.0.0"})
if not "source" in kwargs:
kwargs.update({"source": "Local"})
return CrateInfo(**kwargs)

rust_common = struct(
Expand Down
2 changes: 2 additions & 0 deletions rust/private/internal_extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load("@bazel_features//:features.bzl", "bazel_features")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//rust/private:repository_utils.bzl", "TINYJSON_KWARGS")
load("//tools/auditable:deps.bzl", "auditable_dependencies")
load("//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")

def _internal_deps_impl(module_ctx):
Expand All @@ -14,6 +15,7 @@ def _internal_deps_impl(module_ctx):
direct_deps = [struct(repo = "rules_rust_tinyjson", is_dev_dep = False)]
http_archive(**TINYJSON_KWARGS)

direct_deps.extend(auditable_dependencies())
direct_deps.extend(rust_analyzer_dependencies())

# is_dev_dep is ignored here. It's not relevant for internal_deps, as dev
Expand Down
3 changes: 3 additions & 0 deletions rust/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ CrateInfo = provider(
"name": "str: The name of this crate.",
"output": "File: The output File that will be produced, depends on crate type.",
"owner": "Label: The label of the target that produced this CrateInfo",
"pkg_name": "str: The Cargo package name, which may differ from the Rust crate name (e.g. 'rustls-webpki' vs 'webpki').",
"proc_macro_deps": "depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers.",
"root": "File: The source File entrypoint to this crate, eg. lib.rs",
"rustc_env": "Dict[String, String]: Additional `\"key\": \"value\"` environment variables to set for rustc.",
"rustc_env_files": "[File]: Files containing additional environment variables to set for rustc.",
"rustc_output": "File: The output from rustc from producing the output file. It is optional.",
"rustc_rmeta_output": "File: The rmeta file produced for this crate. It is optional.",
"source": "str: The source of this crate (e.g. 'CratesIo', 'Git', 'Local', 'Registry').",
"srcs": "depset[File]: All source Files that are part of the crate.",
"std_dylib": "File: libstd.so file",
"type": (
"str: The type of this crate " +
"(see [rustc --crate-type](https://doc.rust-lang.org/rustc/command-line-arguments.html#--crate-type-a-list-of-types-of-crates-for-the-compiler-to-emit))."
),
"version": "str: The semver version of this crate.",
"wrapped_crate_type": (
"str, optional: The original crate type for targets generated using a previously defined " +
"crate (typically tests using the `rust_test::crate` attribute)"
Expand Down
42 changes: 40 additions & 2 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ load(

# TODO(marco): Separate each rule into its own file.

def _get_pkg_name(ctx):
"""Extract the Cargo package name from a 'crate-name=...' tag.

crate_universe adds this tag to vendored crates. Returns empty string if
not found, in which case _create_crate_info defaults to the crate name.
"""
for tag in getattr(ctx.attr, "tags", []):
if tag.startswith("crate-name="):
return tag[len("crate-name="):]
return ""

def _assert_no_deprecated_attributes(_ctx):
"""Forces a failure if any deprecated attributes were specified

Expand Down Expand Up @@ -245,6 +256,9 @@ def _rust_library_common(ctx, crate_type):
compile_data = depset(compile_data),
compile_data_targets = depset(ctx.attr.compile_data),
owner = ctx.label,
version = getattr(ctx.attr, "version", "0.0.0"),
source = getattr(ctx.attr, "source", "Local"),
pkg_name = _get_pkg_name(ctx),
cfgs = _collect_cfgs(ctx, toolchain, crate_root, crate_type, crate_is_test = False),
),
)
Expand Down Expand Up @@ -309,6 +323,9 @@ def _rust_binary_impl(ctx):
compile_data = depset(compile_data),
compile_data_targets = depset(ctx.attr.compile_data),
owner = ctx.label,
version = getattr(ctx.attr, "version", "0.0.0"),
source = getattr(ctx.attr, "source", "Local"),
pkg_name = _get_pkg_name(ctx),
cfgs = _collect_cfgs(ctx, toolchain, crate_root, ctx.attr.crate_type, crate_is_test = False),
),
)
Expand Down Expand Up @@ -431,6 +448,9 @@ def _rust_test_impl(ctx):
compile_data_targets = compile_data_targets,
wrapped_crate_type = crate.type,
owner = ctx.label,
version = getattr(ctx.attr, "version", "0.0.0"),
source = getattr(ctx.attr, "source", "Local"),
pkg_name = _get_pkg_name(ctx),
cfgs = _collect_cfgs(ctx, toolchain, crate.root, crate_type, crate_is_test = True),
)
else:
Expand Down Expand Up @@ -487,6 +507,9 @@ def _rust_test_impl(ctx):
compile_data = depset(compile_data),
compile_data_targets = depset(ctx.attr.compile_data),
owner = ctx.label,
version = getattr(ctx.attr, "version", "0.0.0"),
source = getattr(ctx.attr, "source", "Local"),
pkg_name = _get_pkg_name(ctx),
cfgs = _collect_cfgs(ctx, toolchain, crate_root, crate_type, crate_is_test = True),
)

Expand Down Expand Up @@ -605,6 +628,9 @@ RUSTC_ATTRS = {
"_always_enable_metadata_output_groups": attr.label(
default = Label("//rust/settings:always_enable_metadata_output_groups"),
),
"_auditable": attr.label(
default = Label("//rust/settings:auditable"),
),
"_error_format": attr.label(
default = Label("//rust/settings:error_format"),
),
Expand Down Expand Up @@ -791,6 +817,10 @@ _COMMON_ATTRS = {
# "name": attr.string(
# doc = "This name will also be used as the name of the crate built by this rule.",
# `),
"source": attr.string(
doc = "The source of this crate (e.g. 'crates.io', 'git', 'local', 'registry'). Used for cargo-auditable dependency tracking.",
default = "Local",
),
"srcs": attr.label_list(
doc = dedent("""\
List of Rust `.rs` source files used to build the library.
Expand Down Expand Up @@ -1062,9 +1092,17 @@ _rust_shared_library_transition = transition(
],
)

_AUDITABLE_INJECTOR_ATTR = {
"auditable_injector": attr.label(
doc = "The auditable_injector tool for embedding cargo-auditable metadata. " +
"Set to @rules_rust//tools/auditable:auditable_injector to enable.",
cfg = "exec",
),
}

rust_shared_library = rule(
implementation = _rust_shared_library_impl,
attrs = _COMMON_ATTRS | _PLATFORM_ATTRS | _EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS,
attrs = _COMMON_ATTRS | _PLATFORM_ATTRS | _EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS | _AUDITABLE_INJECTOR_ATTR,
fragments = ["cpp"],
cfg = _rust_shared_library_transition,
toolchains = [
Expand Down Expand Up @@ -1187,7 +1225,7 @@ _rust_binary_transition = transition(
rust_binary = rule(
implementation = _rust_binary_impl,
provides = COMMON_PROVIDERS,
attrs = _COMMON_ATTRS | _RUST_BINARY_ATTRS | _PLATFORM_ATTRS,
attrs = _COMMON_ATTRS | _RUST_BINARY_ATTRS | _PLATFORM_ATTRS | _AUDITABLE_INJECTOR_ATTR,
executable = True,
fragments = ["cpp"],
cfg = _rust_binary_transition,
Expand Down
Loading
Loading