Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra_rustc_flag and extra_exec_rustc_flag #1413

Merged
merged 11 commits into from
Jul 12, 2022
14 changes: 13 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust:defs.bzl", "capture_clippy_output", "clippy_flags", "error_format", "extra_exec_rustc_flags", "extra_rustc_flags")
load("//rust:defs.bzl", "capture_clippy_output", "clippy_flags", "error_format", "extra_exec_rustc_flag", "extra_exec_rustc_flags", "extra_rustc_flag", "extra_rustc_flags")

exports_files(["LICENSE"])

Expand Down Expand Up @@ -55,6 +55,12 @@ extra_rustc_flags(
visibility = ["//visibility:public"],
)

extra_rustc_flag(
name = "extra_rustc_flag",
build_setting_default = "",
visibility = ["//visibility:public"],
)

# This setting may be used to pass extra options to rustc from the command line
# in exec configuration.
# It applies across all targets whereas the rustc_flags option on targets applies only
Expand All @@ -65,6 +71,12 @@ extra_exec_rustc_flags(
visibility = ["//visibility:public"],
)

extra_exec_rustc_flag(
name = "extra_exec_rustc_flag",
build_setting_default = "",
visibility = ["//visibility:public"],
)

# This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html
label_flag(
name = "clippy.toml",
Expand Down
8 changes: 8 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ load(
load(
"//rust/private:rustc.bzl",
_error_format = "error_format",
_extra_exec_rustc_flag = "extra_exec_rustc_flag",
_extra_exec_rustc_flags = "extra_exec_rustc_flags",
_extra_rustc_flag = "extra_rustc_flag",
_extra_rustc_flags = "extra_rustc_flags",
)
load(
Expand Down Expand Up @@ -102,9 +104,15 @@ capture_clippy_output = _capture_clippy_output
error_format = _error_format
# See @rules_rust//rust/private:rustc.bzl for a complete description.

extra_rustc_flag = _extra_rustc_flag
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can yo make sure the new flags are added to docs? You should only need to update https://github.com/bazelbuild/rules_rust/blob/0.6.0/docs/symbols.bzl and https://github.com/bazelbuild/rules_rust/blob/0.6.0/docs/BUILD.bazel#L46-L60. Then CI will tell you to run ./docs/update_docs.sh from the root of the repo.

# See @rules_rust//rust/private:rustc.bzl for a complete description.

extra_rustc_flags = _extra_rustc_flags
# See @rules_rust//rust/private:rustc.bzl for a complete description.

extra_exec_rustc_flag = _extra_exec_rustc_flag
# See @rules_rust//rust/private:rustc.bzl for a complete description.

extra_exec_rustc_flags = _extra_exec_rustc_flags
# See @rules_rust//rust/private:rustc.bzl for a complete description.

Expand Down
1 change: 1 addition & 0 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ rust_clippy_aspect = aspect(
doc = "The desired `--error-format` flags for clippy",
default = "//:error_format",
),
"_extra_rustc_flag": attr.label(default = "//:extra_rustc_flag"),
"_extra_rustc_flags": attr.label(default = "//:extra_rustc_flags"),
"_process_wrapper": attr.label(
doc = "A process wrapper for running clippy on all platforms",
Expand Down
12 changes: 9 additions & 3 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -666,13 +666,19 @@ _common_attrs = {
cfg = "exec",
),
"_error_format": attr.label(
default = Label("//:error_format"),
default = "//:error_format",
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
),
"_extra_exec_rustc_flag": attr.label(
default = "//:extra_exec_rustc_flag",
),
"_extra_exec_rustc_flags": attr.label(
default = Label("//:extra_exec_rustc_flags"),
default = "//:extra_exec_rustc_flags",
),
"_extra_rustc_flag": attr.label(
default = "//:extra_rustc_flag",
),
"_extra_rustc_flags": attr.label(
default = Label("//:extra_rustc_flags"),
default = "//:extra_rustc_flags",
),
"_import_macro_dep": attr.label(
default = Label("//util/import"),
Expand Down
30 changes: 30 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,15 @@ def construct_arguments(
if hasattr(ctx.attr, "_extra_rustc_flags") and not is_exec_configuration(ctx):
rustc_flags.add_all(ctx.attr._extra_rustc_flags[ExtraRustcFlagsInfo].extra_rustc_flags)

if hasattr(ctx.attr, "_extra_rustc_flag") and not is_exec_configuration(ctx):
rustc_flags.add_all(ctx.attr._extra_rustc_flag[ExtraRustcFlagsInfo].extra_rustc_flags)

if hasattr(ctx.attr, "_extra_exec_rustc_flags") and is_exec_configuration(ctx):
rustc_flags.add_all(ctx.attr._extra_exec_rustc_flags[ExtraExecRustcFlagsInfo].extra_exec_rustc_flags)

if hasattr(ctx.attr, "_extra_exec_rustc_flag") and is_exec_configuration(ctx):
rustc_flags.add_all(ctx.attr._extra_exec_rustc_flag[ExtraExecRustcFlagsInfo].extra_exec_rustc_flags)

# Create a struct which keeps the arguments separate so each may be tuned or
# replaced where necessary
args = struct(
Expand Down Expand Up @@ -1520,6 +1526,18 @@ extra_rustc_flags = rule(
build_setting = config.string_list(flag = True),
)

def _extra_rustc_flag_impl(ctx):
return ExtraRustcFlagsInfo(extra_rustc_flags = [f for f in ctx.build_setting_value if f != ""])

extra_rustc_flag = rule(
doc = (
"Add additional rustc_flag from the command line with `--@rules_rust//:extra_rustc_flag`. " +
"Multiple uses are accumulated and appended after the extra_rustc_flags."
),
implementation = _extra_rustc_flag_impl,
build_setting = config.string(flag = True, allow_multiple = True),
)

def _extra_exec_rustc_flags_impl(ctx):
return ExtraExecRustcFlagsInfo(extra_exec_rustc_flags = ctx.build_setting_value)

Expand All @@ -1532,3 +1550,15 @@ extra_exec_rustc_flags = rule(
implementation = _extra_exec_rustc_flags_impl,
build_setting = config.string_list(flag = True),
)

def _extra_exec_rustc_flag_impl(ctx):
return ExtraExecRustcFlagsInfo(extra_exec_rustc_flags = [f for f in ctx.build_setting_value if f != ""])

extra_exec_rustc_flag = rule(
doc = (
"Add additional rustc_flags in the exec configuration from the command line with `--@rules_rust//:extra_exec_rustc_flag`. " +
"Multiple uses are accumulated and appended after the extra_exec_rustc_flags."
),
implementation = _extra_exec_rustc_flag_impl,
build_setting = config.string(flag = True, allow_multiple = True),
)
24 changes: 24 additions & 0 deletions test/extra_exec_rustc_flags/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@ rust_library(
with_extra_exec_rustc_flags_cfg(
name = "lib_with_exec_flags_do_not_build_directly",
srcs = ["lib_do_not_build_directly"],
extra_exec_rustc_flag = [],
extra_exec_rustc_flags = ["--cfg=bazel_exec"],
tags = ["manual"],
)

with_extra_exec_rustc_flags_cfg(
name = "lib_with_singular_exec_flags_do_not_build_directly",
srcs = ["lib_do_not_build_directly"],
extra_exec_rustc_flag = ["--cfg=bazel_exec"],
extra_exec_rustc_flags = [],
tags = ["manual"],
)

with_exec_cfg(
name = "lib",
srcs = ["lib_with_exec_flags_do_not_build_directly"],
)

with_exec_cfg(
name = "lib_singular",
srcs = ["lib_with_singular_exec_flags_do_not_build_directly"],
)

# Checks that extra_exec_rustc_flags are not passed in non-exec configurations.
# lib_no_exec.rs is a sample source file that fails to build if
# `--cfg=bazel_exec` is present. The targets below are built in non-exec configurations,
Expand All @@ -43,14 +57,24 @@ rust_library(
with_extra_exec_rustc_flags_cfg(
name = "lib_no_exec_with_extra_build_flags",
srcs = ["lib_no_exec"],
extra_exec_rustc_flag = [],
extra_exec_rustc_flags = ["--cfg=bazel_exec"],
)

with_extra_exec_rustc_flags_cfg(
name = "lib_no_exec_with_singular_extra_build_flags",
srcs = ["lib_no_exec"],
extra_exec_rustc_flag = ["--cfg=bazel_exec"],
extra_exec_rustc_flags = [],
)

build_test(
name = "lib_build",
targets = [
":lib",
":lib_singular",
":lib_no_exec",
":lib_no_exec_with_extra_build_flags",
":lib_no_exec_with_singular_extra_build_flags",
],
)
6 changes: 5 additions & 1 deletion test/extra_exec_rustc_flags/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

def _extra_exec_rustc_flags_transition_impl(_settings, attr):
return {
"//:extra_exec_rustc_flag": attr.extra_exec_rustc_flag,
"//:extra_exec_rustc_flags": attr.extra_exec_rustc_flags,
}

_extra_exec_rustc_flags_transition = transition(
implementation = _extra_exec_rustc_flags_transition_impl,
inputs = [],
outputs = ["//:extra_exec_rustc_flags"],
outputs = ["//:extra_exec_rustc_flags", "//:extra_exec_rustc_flag"],
)

def _with_extra_exec_rustc_flags_cfg_impl(ctx):
Expand All @@ -17,6 +18,9 @@ def _with_extra_exec_rustc_flags_cfg_impl(ctx):
with_extra_exec_rustc_flags_cfg = rule(
implementation = _with_extra_exec_rustc_flags_cfg_impl,
attrs = {
"extra_exec_rustc_flag": attr.string_list(
mandatory = True,
),
"extra_exec_rustc_flags": attr.string_list(
mandatory = True,
),
Expand Down
20 changes: 20 additions & 0 deletions test/unit/extra_rustc_flags/extra_rustc_flags_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ extra_rustc_flags_present_test = analysistest.make(
},
)

extra_rustc_flag_present_test = analysistest.make(
mypmc marked this conversation as resolved.
Show resolved Hide resolved
_extra_rustc_flags_present_test,
attrs = {
"lib_exec": attr.label(
mandatory = True,
cfg = "exec",
),
},
config_settings = {
"@//:extra_rustc_flag": [EXTRA_FLAG],
"@//:extra_rustc_flags": [],
},
)

def _define_test_targets():
rust_library(
name = "lib",
Expand Down Expand Up @@ -85,6 +99,12 @@ def extra_rustc_flags_test_suite(name):
lib_exec = ":lib",
)

extra_rustc_flag_present_test(
name = "extra_rustc_flag_present_test",
target_under_test = ":lib",
lib_exec = ":lib",
)

native.test_suite(
name = name,
tests = [
Expand Down