-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into auth-for-downloads
- Loading branch information
Showing
6 changed files
with
202 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
load(":force_all_deps_direct_test.bzl", "force_all_deps_direct_test_suite") | ||
|
||
############################ UNIT TESTS ############################# | ||
force_all_deps_direct_test_suite(name = "force_all_deps_direct_test_suite") |
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,5 @@ | ||
use transitive::transitive_fn; | ||
|
||
pub fn direct_fn() { | ||
transitive_fn(); | ||
} |
61 changes: 61 additions & 0 deletions
61
test/unit/force_all_deps_direct/force_all_deps_direct_test.bzl
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,61 @@ | ||
"""Unittest to verify that we can treat all dependencies as direct dependencies""" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "analysistest") | ||
load("//rust:defs.bzl", "rust_library") | ||
load("//test/unit:common.bzl", "assert_action_mnemonic", "assert_argv_contains_prefix") | ||
load("//test/unit/force_all_deps_direct:generator.bzl", "generator") | ||
|
||
def _force_all_deps_direct_rustc_flags_test(ctx): | ||
env = analysistest.begin(ctx) | ||
tut = analysistest.target_under_test(env) | ||
action = tut.actions[1] | ||
argv = action.argv | ||
assert_action_mnemonic(env, action, "Rustc") | ||
assert_argv_contains_prefix( | ||
env, | ||
action, | ||
"--extern=transitive", | ||
) | ||
return analysistest.end(env) | ||
|
||
force_all_deps_direct_test = analysistest.make(_force_all_deps_direct_rustc_flags_test) | ||
|
||
def _force_all_deps_direct_test(): | ||
rust_library( | ||
name = "direct", | ||
srcs = ["direct.rs"], | ||
edition = "2018", | ||
deps = [":transitive"], | ||
) | ||
|
||
rust_library( | ||
name = "transitive", | ||
srcs = ["transitive.rs"], | ||
edition = "2018", | ||
) | ||
|
||
generator( | ||
name = "generate", | ||
deps = [":direct"], | ||
tags = ["noclippy"], | ||
) | ||
|
||
force_all_deps_direct_test( | ||
name = "force_all_deps_direct_rustc_flags_test", | ||
target_under_test = ":generate", | ||
) | ||
|
||
def force_all_deps_direct_test_suite(name): | ||
"""Entry-point macro called from the BUILD file. | ||
Args: | ||
name: Name of the macro. | ||
""" | ||
_force_all_deps_direct_test() | ||
|
||
native.test_suite( | ||
name = name, | ||
tests = [ | ||
":force_all_deps_direct_rustc_flags_test", | ||
], | ||
) |
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,92 @@ | ||
"""A custom rule that threats all its dependencies as direct dependencies.""" | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:common.bzl", "rust_common") | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:providers.bzl", "BuildInfo", "CrateInfo", "DepInfo", "DepVariantInfo") | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:rustc.bzl", "rustc_compile_action") | ||
|
||
def _generator_impl(ctx): | ||
rs_file = ctx.actions.declare_file(ctx.label.name + "_generated.rs") | ||
ctx.actions.run_shell( | ||
outputs = [rs_file], | ||
command = """cat <<EOF > {} | ||
use direct::direct_fn; | ||
use transitive::transitive_fn; | ||
pub fn call_both() {} | ||
direct_fn(); | ||
transitive_fn(); | ||
{} | ||
EOF | ||
""".format(rs_file.path, "{", "}"), | ||
mnemonic = "WriteRsFile", | ||
) | ||
|
||
toolchain = ctx.toolchains[Label("//rust:toolchain")] | ||
|
||
# Determine unique hash for this rlib | ||
output_hash = repr(hash(rs_file.path)) | ||
crate_name = ctx.label.name | ||
crate_type = "rlib" | ||
|
||
rust_lib_name = "{prefix}{name}-{lib_hash}{extension}".format( | ||
prefix = "lib", | ||
name = crate_name, | ||
lib_hash = output_hash, | ||
extension = ".rlib", | ||
) | ||
|
||
deps = [DepVariantInfo( | ||
crate_info = dep[CrateInfo] if CrateInfo in dep else None, | ||
dep_info = dep[DepInfo] if DepInfo in dep else None, | ||
build_info = dep[BuildInfo] if BuildInfo in dep else None, | ||
cc_info = dep[CcInfo] if CcInfo in dep else None, | ||
) for dep in ctx.attr.deps] | ||
|
||
rust_lib = ctx.actions.declare_file(rust_lib_name) | ||
return rustc_compile_action( | ||
ctx = ctx, | ||
attr = ctx.attr, | ||
toolchain = toolchain, | ||
crate_info = rust_common.create_crate_info( | ||
name = crate_name, | ||
type = crate_type, | ||
root = rs_file, | ||
srcs = depset([rs_file]), | ||
deps = depset(deps), | ||
proc_macro_deps = depset([]), | ||
aliases = {}, | ||
output = rust_lib, | ||
owner = ctx.label, | ||
edition = "2018", | ||
compile_data = depset([]), | ||
rustc_env = {}, | ||
is_test = False, | ||
), | ||
output_hash = output_hash, | ||
force_all_deps_direct = True, | ||
) | ||
|
||
generator = rule( | ||
implementation = _generator_impl, | ||
attrs = { | ||
"deps": attr.label_list(), | ||
"_cc_toolchain": attr.label( | ||
default = "@bazel_tools//tools/cpp:current_cc_toolchain", | ||
), | ||
"_error_format": attr.label(default = "@rules_rust//:error_format"), | ||
"_process_wrapper": attr.label( | ||
default = Label("@rules_rust//util/process_wrapper"), | ||
executable = True, | ||
allow_single_file = True, | ||
cfg = "exec", | ||
), | ||
}, | ||
toolchains = ["@rules_rust//rust:toolchain", "@bazel_tools//tools/cpp:toolchain_type"], | ||
incompatible_use_toolchain_transition = True, | ||
fragments = ["cpp"], | ||
) |
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 @@ | ||
pub fn transitive_fn() {} |