Skip to content

Commit

Permalink
add extra_exec_rustc_flags build config (bazelbuild#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
krasimirgg committed Jan 31, 2022
1 parent 9c9358d commit 0946fe9
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 7 deletions.
15 changes: 13 additions & 2 deletions 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", "error_format", "extra_rustc_flags")
load("//rust:defs.bzl", "capture_clippy_output", "error_format", "extra_exec_rustc_flags", "extra_rustc_flags")

exports_files(["LICENSE"])

Expand All @@ -25,7 +25,8 @@ error_format(
visibility = ["//visibility:public"],
)

# This setting may be used to pass extra options to rustc from the command line.
# This setting may be used to pass extra options to rustc from the command line
# in non-exec configuration.
# It applies across all targets whereas the rustc_flags option on targets applies only
# to that target. This can be useful for passing build-wide options such as LTO.
extra_rustc_flags(
Expand All @@ -34,6 +35,16 @@ extra_rustc_flags(
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
# to that target. This can be useful for passing build-wide options such as LTO.
extra_exec_rustc_flags(
name = "extra_exec_rustc_flags",
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
2 changes: 1 addition & 1 deletion docs/defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen
extra_rustc_flags(<a href="#extra_rustc_flags-name">name</a>)
</pre>

Add additional rustc_flags from the command line with `--@rules_rust//:extra_rustc_flags`. This flag should only be used for flags that need to be applied across the entire build. For options that apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: These flags are currently excluded from the exec configuration (proc-macros, cargo_build_script, etc).
Add additional rustc_flags from the command line with `--@rules_rust//:extra_rustc_flags`. This flag should only be used for flags that need to be applied across the entire build. For options that apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: These flags not applied to the exec configuration (proc-macros, cargo_build_script, etc); use `--@rules_rust//:extra_exec_rustc_flags` to apply flags to the exec configuration.

**ATTRIBUTES**

Expand Down
2 changes: 1 addition & 1 deletion docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen
extra_rustc_flags(<a href="#extra_rustc_flags-name">name</a>)
</pre>

Add additional rustc_flags from the command line with `--@rules_rust//:extra_rustc_flags`. This flag should only be used for flags that need to be applied across the entire build. For options that apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: These flags are currently excluded from the exec configuration (proc-macros, cargo_build_script, etc).
Add additional rustc_flags from the command line with `--@rules_rust//:extra_rustc_flags`. This flag should only be used for flags that need to be applied across the entire build. For options that apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: These flags not applied to the exec configuration (proc-macros, cargo_build_script, etc); use `--@rules_rust//:extra_exec_rustc_flags` to apply flags to the exec configuration.

**ATTRIBUTES**

Expand Down
4 changes: 4 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ load(
load(
"//rust/private:rustc.bzl",
_error_format = "error_format",
_extra_exec_rustc_flags = "extra_exec_rustc_flags",
_extra_rustc_flags = "extra_rustc_flags",
)
load(
Expand Down Expand Up @@ -101,6 +102,9 @@ error_format = _error_format
extra_rustc_flags = _extra_rustc_flags
# 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.

rust_common = _rust_common
# See @rules_rust//rust/private:common.bzl for a complete description.

Expand Down
1 change: 1 addition & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ _common_attrs = {
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
),
"_error_format": attr.label(default = "//:error_format"),
"_extra_exec_rustc_flags": attr.label(default = "//:extra_exec_rustc_flags"),
"_extra_rustc_flags": attr.label(default = "//:extra_rustc_flags"),
"_process_wrapper": attr.label(
default = Label("//util/process_wrapper"),
Expand Down
28 changes: 25 additions & 3 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ ErrorFormatInfo = provider(
)

ExtraRustcFlagsInfo = provider(
doc = "Pass each value as an additional flag to rustc invocations",
fields = {"extra_rustc_flags": "List[string] Extra flags to pass to rustc"},
doc = "Pass each value as an additional flag to non-exec rustc invocations",
fields = {"extra_rustc_flags": "List[string] Extra flags to pass to rustc in non-exec configuration"},
)

ExtraExecRustcFlagsInfo = provider(
doc = "Pass each value as an additional flag to exec rustc invocations",
fields = {"extra_exec_rustc_flags": "List[string] Extra flags to pass to rustc in exec configuration"},
)

def _get_rustc_env(attr, toolchain, crate_name):
Expand Down Expand Up @@ -681,6 +686,9 @@ 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_exec_rustc_flags") and is_exec_configuration(ctx):
rustc_flags.add_all(ctx.attr._extra_exec_rustc_flags[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 @@ -1254,8 +1262,22 @@ extra_rustc_flags = rule(
"Add additional rustc_flags from the command line with `--@rules_rust//:extra_rustc_flags`. " +
"This flag should only be used for flags that need to be applied across the entire build. For options that " +
"apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: " +
"These flags are currently excluded from the exec configuration (proc-macros, cargo_build_script, etc)."
"These flags not applied to the exec configuration (proc-macros, cargo_build_script, etc); " +
"use `--@rules_rust//:extra_exec_rustc_flags` to apply flags to the exec configuration."
),
implementation = _extra_rustc_flags_impl,
build_setting = config.string_list(flag = True),
)

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

extra_exec_rustc_flags = rule(
doc = (
"Add additional rustc_flags in the exec configuration from the command line with `--@rules_rust//:extra_exec_rustc_flags`. " +
"This flag should only be used for flags that need to be applied across the entire build. " +
"These flags only apply to the exec configuration (proc-macros, cargo_build_script, etc)."
),
implementation = _extra_exec_rustc_flags_impl,
build_setting = config.string_list(flag = True),
)
54 changes: 54 additions & 0 deletions test/extra_exec_rustc_flags/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_rust//rust:defs.bzl", "rust_library")
load("defs.bzl", "with_exec_cfg", "with_extra_exec_rustc_flags_cfg")

package(default_visibility = ["//test:__subpackages__"])

# Checks that extra_exec_rustc_flags are passed in exec configuration.
# lib.rs is a sample source file that requires a `--cfg=bazel_exec` flag to build.
# These targets set up transitions so that building :lib triggers building
# lib.rs in exec configuration with //:extra_exec_rustc_flags=[--cfg=bazel_exec].
# The intermediate targets are tagged "manual" as they are not meant to be built
# on their own.

rust_library(
name = "lib_do_not_build_directly",
srcs = ["lib.rs"],
tags = ["manual"],
)

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

with_exec_cfg(
name = "lib",
srcs = ["lib_with_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,
# so they should build just fine with //:extra_exec_rustc_flags=[--cfg=bazel_exec].
rust_library(
name = "lib_no_exec",
srcs = ["lib_no_exec.rs"],
)

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

build_test(
name = "lib_build",
targets = [
":lib",
":lib_no_exec",
":lib_no_exec_with_extra_build_flags",
],
)
44 changes: 44 additions & 0 deletions test/extra_exec_rustc_flags/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Test transitions to test extra_exec_rustc_flags."""

def _extra_exec_rustc_flags_transition_impl(settings, attr):
return {
"//: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"],
)

def _with_extra_exec_rustc_flags_cfg_impl(ctx):
return [DefaultInfo(files = depset(ctx.files.srcs))]

with_extra_exec_rustc_flags_cfg = rule(
implementation = _with_extra_exec_rustc_flags_cfg_impl,
attrs = {
"extra_exec_rustc_flags": attr.string_list(
mandatory = True,
),
"srcs": attr.label_list(
allow_files = True,
cfg = _extra_exec_rustc_flags_transition,
),
"_allowlist_function_transition": attr.label(
default = Label("//tools/allowlists/function_transition_allowlist"),
),
},
)

def _with_exec_cfg_impl(ctx):
return [DefaultInfo(files = depset(ctx.files.srcs))]

with_exec_cfg = rule(
implementation = _with_exec_cfg_impl,
attrs = {
"srcs": attr.label_list(
allow_files = True,
cfg = "exec",
),
},
)
7 changes: 7 additions & 0 deletions test/extra_exec_rustc_flags/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Sample source that fails to compile unless `--cfg=bazel_exec` is passed to rustc.
#[cfg(bazel_exec)]
fn exec() {}

pub fn f() {
exec();
}
7 changes: 7 additions & 0 deletions test/extra_exec_rustc_flags/lib_no_exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Sample source that fails to compile if `--cfg=bazel_exec` is passed to rustc.
#[cfg(not(bazel_exec))]
fn exec() {}

pub fn f() {
exec();
}

0 comments on commit 0946fe9

Please sign in to comment.