From 92d9db8eceaffc93288813aed2a66021c01590ec Mon Sep 17 00:00:00 2001 From: David Marcin Date: Thu, 7 Jan 2021 00:43:55 +0000 Subject: [PATCH 01/21] Support passing arbitrary extra codegen args to rustc --- BUILD.bazel | 10 +++++++++- rust/private/clippy.bzl | 1 + rust/private/rust.bzl | 1 + rust/private/rustc.bzl | 15 +++++++++++++++ rust/rust.bzl | 7 +++++++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/BUILD.bazel b/BUILD.bazel index b395c853cd..45c9df4259 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,5 +1,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load("//rust:rust.bzl", "error_format") +load("//rust:rust.bzl", "error_format", "extra_codegen") exports_files(["LICENSE"]) @@ -18,6 +18,14 @@ error_format( visibility = ["//visibility:public"], ) +# This setting may be used to pass codegen options to rustc from the command line. +# See https://doc.rust-lang.org/rustc/codegen-options/index.html for values. +extra_codegen( + name = "extra_codegen", + build_setting_default = [], + visibility = ["//visibility:public"], +) + # This setting is used by the rustfmt rules. See https://bazelbuild.github.io/rules_rust/rust_fmt.html label_flag( name = "rustfmt.toml", diff --git a/rust/private/clippy.bzl b/rust/private/clippy.bzl index b69ebde0e6..52399768e1 100644 --- a/rust/private/clippy.bzl +++ b/rust/private/clippy.bzl @@ -125,6 +125,7 @@ rust_clippy_aspect = aspect( doc = "The desired `--error-format` flags for clippy", default = "//:error_format", ), + "_extra_codegen": attr.label(default = "//:extra_codegen"), "_process_wrapper": attr.label( doc = "A process wrapper for running clippy on all platforms", default = Label("//util/process_wrapper"), diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index c1fb0fa967..e1a81b5305 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -677,6 +677,7 @@ _common_attrs = { default = "@bazel_tools//tools/cpp:current_cc_toolchain", ), "_error_format": attr.label(default = "//:error_format"), + "_extra_codegen": attr.label(default = "//:extra_codegen"), "_process_wrapper": attr.label( default = Label("//util/process_wrapper"), executable = True, diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index f8502208bc..7f4b6dd467 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -54,6 +54,11 @@ ErrorFormatInfo = provider( fields = {"error_format": "(string) [" + ", ".join(_error_format_values) + "]"}, ) +ExtraCodegenInfo = provider( + doc = "Pass additional --codegen arguments for each value", + fields = {"extra_codegen": "List[string] Extra codegen arguments to pass"}, +) + def _get_rustc_env(attr, toolchain): """Gathers rustc environment variables @@ -422,6 +427,8 @@ def construct_arguments( compilation_mode = get_compilation_mode_opts(ctx, toolchain) args.add("--codegen=opt-level=" + compilation_mode.opt_level) args.add("--codegen=debuginfo=" + compilation_mode.debug_info) + if hasattr(ctx.attr, "_extra_codegen") and crate_info.type != 'proc-macro': + args.add_all(['--codegen=' + x for x in ctx.attr._extra_codegen[ExtraCodegenInfo].extra_codegen]) # For determinism to help with build distribution and such args.add("--remap-path-prefix=${pwd}=.") @@ -943,3 +950,11 @@ error_format = rule( implementation = _error_format_impl, build_setting = config.string(flag = True), ) + +def _extra_codegen_impl(ctx): + return ExtraCodegenInfo(extra_codegen = ctx.build_setting_value) + +extra_codegen = rule( + implementation = _extra_codegen_impl, + build_setting = config.string_list(flag = True), +) diff --git a/rust/rust.bzl b/rust/rust.bzl index 47952515a7..75d8f5d406 100644 --- a/rust/rust.bzl +++ b/rust/rust.bzl @@ -33,6 +33,10 @@ load( _rust_test = "rust_test", _rust_test_binary = "rust_test_binary", ) +load( + "//rust/private:rustc.bzl", + _extra_codegen = "extra_codegen", +) def rust_library(**args): """Deprecated. Use the version from "@rules_rust//rust:defs.bzl" instead. @@ -91,5 +95,8 @@ rust_analyzer = _rust_analyzer error_format = _error_format # See @rules_rust//rust/private:rustc.bzl for a complete description. +extra_codegen = _extra_codegen +# 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. From 6b619afa328cd15688d155e4fb707063e16f5927 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 25 May 2021 17:28:37 -0700 Subject: [PATCH 02/21] Update defs.bzl --- rust/defs.bzl | 4 ++++ rust/rust.bzl | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/rust/defs.bzl b/rust/defs.bzl index 112086d157..786c715759 100644 --- a/rust/defs.bzl +++ b/rust/defs.bzl @@ -40,6 +40,7 @@ load( load( "//rust/private:rustc.bzl", _error_format = "error_format", + _extra_codegen = "extra_codegen", ) load( "//rust/private:rustdoc.bzl", @@ -97,6 +98,9 @@ rust_clippy = _rust_clippy error_format = _error_format # See @rules_rust//rust/private:rustc.bzl for a complete description. +extra_codegen = _extra_codegen +# 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. diff --git a/rust/rust.bzl b/rust/rust.bzl index 75d8f5d406..198528d3c3 100644 --- a/rust/rust.bzl +++ b/rust/rust.bzl @@ -17,6 +17,7 @@ load( "//rust:defs.bzl", _error_format = "error_format", + _extra_codegen = "extra_codegen", _rust_analyzer = "rust_analyzer", _rust_analyzer_aspect = "rust_analyzer_aspect", _rust_benchmark = "rust_benchmark", @@ -33,10 +34,6 @@ load( _rust_test = "rust_test", _rust_test_binary = "rust_test_binary", ) -load( - "//rust/private:rustc.bzl", - _extra_codegen = "extra_codegen", -) def rust_library(**args): """Deprecated. Use the version from "@rules_rust//rust:defs.bzl" instead. From 00271533f4ea95c37db06627262503595072376c Mon Sep 17 00:00:00 2001 From: David Marcin Date: Sat, 19 Jun 2021 02:37:55 -0700 Subject: [PATCH 03/21] buildifier fixes --- rust/private/rustc.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 7f4b6dd467..f376537c06 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -427,8 +427,8 @@ def construct_arguments( compilation_mode = get_compilation_mode_opts(ctx, toolchain) args.add("--codegen=opt-level=" + compilation_mode.opt_level) args.add("--codegen=debuginfo=" + compilation_mode.debug_info) - if hasattr(ctx.attr, "_extra_codegen") and crate_info.type != 'proc-macro': - args.add_all(['--codegen=' + x for x in ctx.attr._extra_codegen[ExtraCodegenInfo].extra_codegen]) + if hasattr(ctx.attr, "_extra_codegen") and crate_info.type != "proc-macro": + args.add_all(["--codegen=" + x for x in ctx.attr._extra_codegen[ExtraCodegenInfo].extra_codegen]) # For determinism to help with build distribution and such args.add("--remap-path-prefix=${pwd}=.") From d18c8a72057b90f1653911c9912429645e0ccc1b Mon Sep 17 00:00:00 2001 From: David Marcin Date: Mon, 28 Jun 2021 12:41:59 -0700 Subject: [PATCH 04/21] Add doc string --- docs/BUILD.bazel | 2 ++ docs/symbols.bzl | 5 +++++ rust/private/rustc.bzl | 11 ++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index acb3adedfb..a8d9d33121 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -54,6 +54,8 @@ PAGES = dict([ "rust_benchmark", "rust_test", "rust_test_suite", + "error_format", + "extra_codegen", ], ), page( diff --git a/docs/symbols.bzl b/docs/symbols.bzl index fc9a7a2174..61fbd73232 100644 --- a/docs/symbols.bzl +++ b/docs/symbols.bzl @@ -52,6 +52,8 @@ load( _rust_test_suite = "rust_test_suite", _rustfmt_aspect = "rustfmt_aspect", _rustfmt_test = "rustfmt_test", + _error_format = "error_format", + _extra_codegen = "extra_codegen", ) load( "@rules_rust//rust:repositories.bzl", @@ -118,3 +120,6 @@ crate = _crate rustfmt_aspect = _rustfmt_aspect rustfmt_test = _rustfmt_test + +error_format = _error_format +extra_codegen = _extra_codegen \ No newline at end of file diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index f376537c06..4d83a06622 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -943,9 +943,8 @@ def _error_format_impl(ctx): error_format = rule( doc = ( - "A helper rule for controlling the rustc " + - "[--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) " + - "flag." + "Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) " + + "flag from the command line with --@rules_rust//:error_format. See rustc documentation for valid values." ), implementation = _error_format_impl, build_setting = config.string(flag = True), @@ -955,6 +954,12 @@ def _extra_codegen_impl(ctx): return ExtraCodegenInfo(extra_codegen = ctx.build_setting_value) extra_codegen = rule( + doc = ( + "Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) " + + "options from the command line with --@rules_rust//:extra_codegen. See rustc documentation for valid values. " + + "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." + ), implementation = _extra_codegen_impl, build_setting = config.string_list(flag = True), ) From 4ccc5b13af232ef7e4b8e90aae5107a151e2b15d Mon Sep 17 00:00:00 2001 From: David Marcin Date: Mon, 28 Jun 2021 13:00:44 -0700 Subject: [PATCH 05/21] Regenerate documentation --- docs/defs.md | 38 ++++++++++++++++++++++++++++++++++++++ docs/flatten.md | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/docs/defs.md b/docs/defs.md index 15f0435165..6203b4887f 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -9,6 +9,44 @@ * [rust_benchmark](#rust_benchmark) * [rust_test](#rust_test) * [rust_test_suite](#rust_test_suite) +* [error_format](#error_format) +* [extra_codegen](#extra_codegen) + + + +## error_format + +
+error_format(name)
+
+ +Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with --@rules_rust//:error_format. See rustc documentation for valid values. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | + + + + +## extra_codegen + +
+extra_codegen(name)
+
+ +Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with --@rules_rust//:extra_codegen. See rustc documentation for valid values. 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. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | + diff --git a/docs/flatten.md b/docs/flatten.md index 9708ccd1ee..09191ae5df 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -3,6 +3,8 @@ * [cargo_build_script](#cargo_build_script) * [crate](#crate) * [crate_universe](#crate_universe) +* [error_format](#error_format) +* [extra_codegen](#extra_codegen) * [rust_analyzer](#rust_analyzer) * [rust_analyzer_aspect](#rust_analyzer_aspect) * [rust_benchmark](#rust_benchmark) @@ -75,6 +77,42 @@ Environment Variables: | supported_targets | A list of supported [platform triples](https://doc.rust-lang.org/nightly/rustc/platform-support.html) to consider when resoliving dependencies. | List of strings | optional | ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"] | + + +## error_format + +
+error_format(name)
+
+ +Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with --@rules_rust//:error_format. See rustc documentation for valid values. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | + + + + +## extra_codegen + +
+extra_codegen(name)
+
+ +Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with --@rules_rust//:extra_codegen. See rustc documentation for valid values. 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. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | + + ## rust_analyzer From af55b2f7124fe514eb8cfab77a8546f813d01a17 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Mon, 28 Jun 2021 14:00:34 -0700 Subject: [PATCH 06/21] fix lint --- docs/symbols.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/symbols.bzl b/docs/symbols.bzl index 61fbd73232..b4910918b9 100644 --- a/docs/symbols.bzl +++ b/docs/symbols.bzl @@ -36,6 +36,8 @@ load( ) load( "@rules_rust//rust:defs.bzl", + _error_format = "error_format", + _extra_codegen = "extra_codegen", _rust_analyzer = "rust_analyzer", _rust_analyzer_aspect = "rust_analyzer_aspect", _rust_benchmark = "rust_benchmark", @@ -52,8 +54,6 @@ load( _rust_test_suite = "rust_test_suite", _rustfmt_aspect = "rustfmt_aspect", _rustfmt_test = "rustfmt_test", - _error_format = "error_format", - _extra_codegen = "extra_codegen", ) load( "@rules_rust//rust:repositories.bzl", @@ -122,4 +122,4 @@ rustfmt_aspect = _rustfmt_aspect rustfmt_test = _rustfmt_test error_format = _error_format -extra_codegen = _extra_codegen \ No newline at end of file +extra_codegen = _extra_codegen From ea41964502b1afe5e21d2a39458714553e70a72d Mon Sep 17 00:00:00 2001 From: David Marcin Date: Mon, 28 Jun 2021 14:49:03 -0700 Subject: [PATCH 07/21] PR feedback --- rust/private/rustc.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 4422234df3..9932400ef9 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -960,7 +960,7 @@ def _error_format_impl(ctx): error_format = rule( doc = ( "Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) " + - "flag from the command line with --@rules_rust//:error_format. See rustc documentation for valid values." + "flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values." ), implementation = _error_format_impl, build_setting = config.string(flag = True), @@ -972,7 +972,7 @@ def _extra_codegen_impl(ctx): extra_codegen = rule( doc = ( "Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) " + - "options from the command line with --@rules_rust//:extra_codegen. See rustc documentation for valid values. " + + "options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. " + "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." ), From 5795fec691a49f738b9689e6c8b804044fbb536b Mon Sep 17 00:00:00 2001 From: David Marcin Date: Mon, 28 Jun 2021 14:49:13 -0700 Subject: [PATCH 08/21] Regenerate documentation --- docs/defs.md | 4 ++-- docs/flatten.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index 8d2d446d2e..a270e391bd 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -20,7 +20,7 @@ error_format(name) -Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with --@rules_rust//:error_format. See rustc documentation for valid values. +Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values. **ATTRIBUTES** @@ -38,7 +38,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_codegen(name) -Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with --@rules_rust//:extra_codegen. See rustc documentation for valid values. 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. +Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. **ATTRIBUTES** diff --git a/docs/flatten.md b/docs/flatten.md index 236001788c..c4485b0b0c 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -85,7 +85,7 @@ Environment Variables: error_format(name) -Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with --@rules_rust//:error_format. See rustc documentation for valid values. +Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values. **ATTRIBUTES** @@ -103,7 +103,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_codegen(name) -Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with --@rules_rust//:extra_codegen. See rustc documentation for valid values. 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. +Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. **ATTRIBUTES** From 999ff166e7eca1a00ed8fede02b07b66ad5995a3 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Mon, 28 Jun 2021 14:51:14 -0700 Subject: [PATCH 09/21] Fix link for codegen --- rust/private/rustc.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 9932400ef9..852f6bfc69 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -971,7 +971,7 @@ def _extra_codegen_impl(ctx): extra_codegen = rule( doc = ( - "Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) " + + "Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) " + "options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. " + "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." From 9afa863065172b88e5aa9fb59867e9652d871cbf Mon Sep 17 00:00:00 2001 From: David Marcin Date: Mon, 28 Jun 2021 14:51:21 -0700 Subject: [PATCH 10/21] Regenerate documentation --- docs/defs.md | 2 +- docs/flatten.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index a270e391bd..2ddf83facf 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -38,7 +38,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_codegen(name) -Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. +Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. **ATTRIBUTES** diff --git a/docs/flatten.md b/docs/flatten.md index c4485b0b0c..c74c091129 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -103,7 +103,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_codegen(name) -Add additional [--codegen](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. +Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. **ATTRIBUTES** From 3ceb3d7cf1e58f74be80bfa1252cbbcb717f2d4d Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 20 Jul 2021 13:00:11 -0700 Subject: [PATCH 11/21] Rename to extra_rustc_flags --- BUILD.bazel | 11 ++++++----- docs/BUILD.bazel | 2 +- docs/defs.md | 12 ++++++------ docs/flatten.md | 12 ++++++------ docs/symbols.bzl | 4 ++-- rust/defs.bzl | 4 ++-- rust/private/clippy.bzl | 2 +- rust/private/rust.bzl | 2 +- rust/private/rustc.bzl | 24 +++++++++++++++--------- rust/rust.bzl | 4 ++-- 10 files changed, 42 insertions(+), 35 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index c1c3a39a3f..fa25a3006c 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,5 +1,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load("//rust:rust.bzl", "error_format", "extra_codegen") +load("//rust:rust.bzl", "error_format", "extra_rustc_flags") exports_files(["LICENSE"]) @@ -18,10 +18,11 @@ error_format( visibility = ["//visibility:public"], ) -# This setting may be used to pass codegen options to rustc from the command line. -# See https://doc.rust-lang.org/rustc/codegen-options/index.html for values. -extra_codegen( - name = "extra_codegen", +# This setting may be used to pass extra options to rustc from the command line. +# 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( + name = "extra_rustc_flags", build_setting_default = [], visibility = ["//visibility:public"], ) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index ad417ad5b1..cee409f310 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -57,7 +57,7 @@ PAGES = dict([ "rust_test", "rust_test_suite", "error_format", - "extra_codegen", + "extra_rustc_flags", ], ), page( diff --git a/docs/defs.md b/docs/defs.md index 661ca16d98..1c4360f8fe 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -10,7 +10,7 @@ * [rust_test](#rust_test) * [rust_test_suite](#rust_test_suite) * [error_format](#error_format) -* [extra_codegen](#extra_codegen) +* [extra_rustc_flags](#extra_rustc_flags) @@ -30,22 +30,22 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen | name | A unique name for this target. | Name | required | | - + -## extra_codegen +## extra_rustc_flags
-extra_codegen(name)
+extra_rustc_flags(name)
 
-Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. +Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_rustc_flags`. See rustc documentation for valid values. 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. **ATTRIBUTES** | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | +| name | A unique name for this target. | Name | required | | diff --git a/docs/flatten.md b/docs/flatten.md index 4cfec62d6c..3150d268e7 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -4,7 +4,7 @@ * [crate](#crate) * [crate_universe](#crate_universe) * [error_format](#error_format) -* [extra_codegen](#extra_codegen) +* [extra_rustc_flags](#extra_rustc_flags) * [rust_analyzer](#rust_analyzer) * [rust_analyzer_aspect](#rust_analyzer_aspect) * [rust_benchmark](#rust_benchmark) @@ -101,22 +101,22 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen | name | A unique name for this target. | Name | required | | - + -## extra_codegen +## extra_rustc_flags
-extra_codegen(name)
+extra_rustc_flags(name)
 
-Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. 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. +Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_rustc_flags`. See rustc documentation for valid values. 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. **ATTRIBUTES** | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | +| name | A unique name for this target. | Name | required | | diff --git a/docs/symbols.bzl b/docs/symbols.bzl index 464a5b2f22..1ae587f05e 100644 --- a/docs/symbols.bzl +++ b/docs/symbols.bzl @@ -37,7 +37,7 @@ load( load( "@rules_rust//rust:defs.bzl", _error_format = "error_format", - _extra_codegen = "extra_codegen", + _extra_rustc_flags = "extra_rustc_flags", _rust_analyzer = "rust_analyzer", _rust_analyzer_aspect = "rust_analyzer_aspect", _rust_benchmark = "rust_benchmark", @@ -124,4 +124,4 @@ rustfmt_aspect = _rustfmt_aspect rustfmt_test = _rustfmt_test error_format = _error_format -extra_codegen = _extra_codegen +extra_rustc_flags = _extra_rustc_flags diff --git a/rust/defs.bzl b/rust/defs.bzl index 1c6dbea16e..936bca10f2 100644 --- a/rust/defs.bzl +++ b/rust/defs.bzl @@ -44,7 +44,7 @@ load( load( "//rust/private:rustc.bzl", _error_format = "error_format", - _extra_codegen = "extra_codegen", + _extra_rustc_flags = "extra_rustc_flags", ) load( "//rust/private:rustdoc.bzl", @@ -102,7 +102,7 @@ rust_clippy = _rust_clippy error_format = _error_format # See @rules_rust//rust/private:rustc.bzl for a complete description. -extra_codegen = _extra_codegen +extra_rustc_flags = _extra_rustc_flags # See @rules_rust//rust/private:rustc.bzl for a complete description. rust_common = _rust_common diff --git a/rust/private/clippy.bzl b/rust/private/clippy.bzl index ae1f53c10c..89711bc082 100644 --- a/rust/private/clippy.bzl +++ b/rust/private/clippy.bzl @@ -162,7 +162,7 @@ rust_clippy_aspect = aspect( doc = "The desired `--error-format` flags for clippy", default = "//:error_format", ), - "_extra_codegen": attr.label(default = "//:extra_codegen"), + "_extra_rustc_flags": attr.label(default = "//:extra_rustc_flags"), "_process_wrapper": attr.label( doc = "A process wrapper for running clippy on all platforms", default = Label("//util/process_wrapper"), diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 65fa276f99..b7ee3e5ff2 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -652,7 +652,7 @@ _common_attrs = { default = "@bazel_tools//tools/cpp:current_cc_toolchain", ), "_error_format": attr.label(default = "//:error_format"), - "_extra_codegen": attr.label(default = "//:extra_codegen"), + "_extra_rustc_flags": attr.label(default = "//:extra_rustc_flags"), "_process_wrapper": attr.label( default = Label("//util/process_wrapper"), executable = True, diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index bb98305ac5..db4ac329b2 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -57,9 +57,9 @@ ErrorFormatInfo = provider( fields = {"error_format": "(string) [" + ", ".join(_error_format_values) + "]"}, ) -ExtraCodegenInfo = provider( +ExtraRustcFlagsInfo = provider( doc = "Pass additional --codegen arguments for each value", - fields = {"extra_codegen": "List[string] Extra codegen arguments to pass"}, + fields = {"extra_rustc_flags": "List[string] Extra flags to pass to rustc"}, ) def _get_rustc_env(attr, toolchain): @@ -429,8 +429,6 @@ def construct_arguments( compilation_mode = get_compilation_mode_opts(ctx, toolchain) args.add("--codegen=opt-level=" + compilation_mode.opt_level) args.add("--codegen=debuginfo=" + compilation_mode.debug_info) - if hasattr(ctx.attr, "_extra_codegen") and crate_info.type != "proc-macro": - args.add_all(["--codegen=" + x for x in ctx.attr._extra_codegen[ExtraCodegenInfo].extra_codegen]) # For determinism to help with build distribution and such args.add("--remap-path-prefix=${pwd}=.") @@ -499,6 +497,14 @@ def construct_arguments( # Set the SYSROOT to the directory of the rust_lib files passed to the toolchain env["SYSROOT"] = paths.dirname(toolchain.rust_lib.files.to_list()[0].short_path) + # extra_rustc_flags are meant to apply across all code being compiled in the target + # configuration, not the exec configuration. Checking crate_info.type != "proc-macro" + # isn't perfect because it doesn't catch the libraries that proc-macros use, but I + # don't know how to determine that we are using the exec configuration. + # TODO(djmarcin): Determine how to know if we are running in the exec configuration. + if hasattr(ctx.attr, "_extra_rustc_flags") and crate_info.type != "proc-macro": + args.add_all(ctx.attr._extra_rustc_flags[ExtraRustcFlagsInfo].extra_rustc_flags) + return args, env def rustc_compile_action( @@ -980,16 +986,16 @@ error_format = rule( build_setting = config.string(flag = True), ) -def _extra_codegen_impl(ctx): - return ExtraCodegenInfo(extra_codegen = ctx.build_setting_value) +def _extra_rustc_flags_impl(ctx): + return ExtraRustcFlagsInfo(extra_rustc_flags = ctx.build_setting_value) -extra_codegen = rule( +extra_rustc_flags = rule( doc = ( "Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) " + - "options from the command line with `--@rules_rust//:extra_codegen`. See rustc documentation for valid values. " + + "options from the command line with `--@rules_rust//:extra_rustc_flags`. See rustc documentation for valid values. " + "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." ), - implementation = _extra_codegen_impl, + implementation = _extra_rustc_flags_impl, build_setting = config.string_list(flag = True), ) diff --git a/rust/rust.bzl b/rust/rust.bzl index 198528d3c3..c4d89a99bb 100644 --- a/rust/rust.bzl +++ b/rust/rust.bzl @@ -17,7 +17,7 @@ load( "//rust:defs.bzl", _error_format = "error_format", - _extra_codegen = "extra_codegen", + _extra_rustc_flags = "extra_rustc_flags", _rust_analyzer = "rust_analyzer", _rust_analyzer_aspect = "rust_analyzer_aspect", _rust_benchmark = "rust_benchmark", @@ -92,7 +92,7 @@ rust_analyzer = _rust_analyzer error_format = _error_format # See @rules_rust//rust/private:rustc.bzl for a complete description. -extra_codegen = _extra_codegen +extra_rustc_flags = _extra_rustc_flags # See @rules_rust//rust/private:rustc.bzl for a complete description. rust_common = _rust_common From 4563108a7f097b767e473d78980268aa77d2ab6e Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 20 Jul 2021 13:08:14 -0700 Subject: [PATCH 12/21] Regenerate documentation --- docs/defs.md | 2 +- docs/flatten.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index 1c4360f8fe..b8c9e5625f 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -38,7 +38,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_rustc_flags(name) -Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_rustc_flags`. See rustc documentation for valid values. 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. +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 not intended to apply to things built in the exec configuration such as proc macros. **ATTRIBUTES** diff --git a/docs/flatten.md b/docs/flatten.md index 3150d268e7..c94b870b64 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -109,7 +109,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_rustc_flags(name) -Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) options from the command line with `--@rules_rust//:extra_rustc_flags`. See rustc documentation for valid values. 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. +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 not intended to apply to things built in the exec configuration such as proc macros. **ATTRIBUTES** From da3941a96e9d81f21bd20b26d4a460cab85db7e7 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 20 Jul 2021 13:09:38 -0700 Subject: [PATCH 13/21] buildifier --- rust/private/rustc.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index db4ac329b2..7959b29778 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -991,10 +991,10 @@ def _extra_rustc_flags_impl(ctx): extra_rustc_flags = rule( doc = ( - "Add additional [--codegen](https://doc.rust-lang.org/rustc/codegen-options/index.html) " + - "options from the command line with `--@rules_rust//:extra_rustc_flags`. See rustc documentation for valid values. " + + "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." + "apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: " + + "These flags are not intended to apply to things built in the exec configuration such as proc macros." ), implementation = _extra_rustc_flags_impl, build_setting = config.string_list(flag = True), From 4d9f6c93024c93dc281fabe7a090a315c7f90739 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 20 Jul 2021 14:22:13 -0700 Subject: [PATCH 14/21] Better method to exclude from the exec configuration --- rust/private/rustc.bzl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 9065b55de1..36238c2d62 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -58,7 +58,7 @@ ErrorFormatInfo = provider( ) ExtraRustcFlagsInfo = provider( - doc = "Pass additional --codegen arguments for each value", + doc = "Pass each value as an additional flag to rustc invocations", fields = {"extra_rustc_flags": "List[string] Extra flags to pass to rustc"}, ) @@ -498,11 +498,10 @@ def construct_arguments( env["SYSROOT"] = paths.dirname(toolchain.rust_lib.files.to_list()[0].short_path) # extra_rustc_flags are meant to apply across all code being compiled in the target - # configuration, not the exec configuration. Checking crate_info.type != "proc-macro" - # isn't perfect because it doesn't catch the libraries that proc-macros use, but I - # don't know how to determine that we are using the exec configuration. - # TODO(djmarcin): Determine how to know if we are running in the exec configuration. - if hasattr(ctx.attr, "_extra_rustc_flags") and crate_info.type != "proc-macro": + # configuration, not the exec configuration. + # TODO(djmarcin): Should these flags apply to cfg=exec targets as well? Or via a + # different flag. Also, is there a better way to determine cfg=exec? + if hasattr(ctx.attr, "_extra_rustc_flags") and ctx.genfiles_dir.path.find("-exec-") == -1: args.add_all(ctx.attr._extra_rustc_flags[ExtraRustcFlagsInfo].extra_rustc_flags) return args, env @@ -994,7 +993,7 @@ 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 not intended to apply to things built in the exec configuration such as proc macros." + "These flags are currently excluded from the exec configuration (proc-macros, cargo_build_script, etc)." ), implementation = _extra_rustc_flags_impl, build_setting = config.string_list(flag = True), From 123c00fec2ca5e76ce72e02898d606db89efa156 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 20 Jul 2021 14:22:21 -0700 Subject: [PATCH 15/21] Regenerate documentation --- docs/defs.md | 2 +- docs/flatten.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index b8c9e5625f..f591704f82 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -38,7 +38,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_rustc_flags(name) -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 not intended to apply to things built in the exec configuration such as proc macros. +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). **ATTRIBUTES** diff --git a/docs/flatten.md b/docs/flatten.md index c94b870b64..515bfc3b4a 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -109,7 +109,7 @@ Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-argumen extra_rustc_flags(name) -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 not intended to apply to things built in the exec configuration such as proc macros. +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). **ATTRIBUTES** From 4ccf858135272dc6efd6ca4c823b1eaa61535349 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Sun, 8 Aug 2021 21:27:54 -0700 Subject: [PATCH 16/21] Refactor is_exec_configuration utility function --- rust/private/rustc.bzl | 8 +++----- rust/private/utils.bzl | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 36238c2d62..b5bda2a34d 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -27,6 +27,7 @@ load( "find_cc_toolchain", "get_lib_name", "get_preferred_artifact", + "is_exec_configuration", "make_static_lib_symlink", "relativize", ) @@ -497,11 +498,8 @@ def construct_arguments( # Set the SYSROOT to the directory of the rust_lib files passed to the toolchain env["SYSROOT"] = paths.dirname(toolchain.rust_lib.files.to_list()[0].short_path) - # extra_rustc_flags are meant to apply across all code being compiled in the target - # configuration, not the exec configuration. - # TODO(djmarcin): Should these flags apply to cfg=exec targets as well? Or via a - # different flag. Also, is there a better way to determine cfg=exec? - if hasattr(ctx.attr, "_extra_rustc_flags") and ctx.genfiles_dir.path.find("-exec-") == -1: + # extra_rustc_flags apply to the target configuration, not the exec configuration. + if hasattr(ctx.attr, "_extra_rustc_flags") and is_exec_configuration(ctx): args.add_all(ctx.attr._extra_rustc_flags[ExtraRustcFlagsInfo].extra_rustc_flags) return args, env diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl index e0591d3af1..a54c9be5d9 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl @@ -323,3 +323,18 @@ def make_static_lib_symlink(actions, rlib_file): dot_a = actions.declare_file(basename + ".a", sibling = rlib_file) actions.symlink(output = dot_a, target_file = rlib_file) return dot_a + +def is_exec_configuration(ctx): + """Determine if a context is building for the exec configuration. + + This is helpful when processing command line flags that should apply + to the target configuration but not the exec configuration. + + Args: + ctx (ctx): The ctx object for the current target. + + Returns: + True if the exec configuration is detected, False otherwise. + """ + # TODO(djmarcin): Is there any better way to determine cfg=exec? + return ctx.genfiles_dir.path.find("-exec-") == -1 \ No newline at end of file From 1d806d60b63fb7db465285451ec57fbd32f1f72f Mon Sep 17 00:00:00 2001 From: David Marcin Date: Sun, 8 Aug 2021 21:34:13 -0700 Subject: [PATCH 17/21] buildifier --- rust/private/utils.bzl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl index a54c9be5d9..d615c43665 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl @@ -336,5 +336,6 @@ def is_exec_configuration(ctx): Returns: True if the exec configuration is detected, False otherwise. """ + # TODO(djmarcin): Is there any better way to determine cfg=exec? - return ctx.genfiles_dir.path.find("-exec-") == -1 \ No newline at end of file + return ctx.genfiles_dir.path.find("-exec-") == -1 From 4d4fffc3d0a918afefc870323b197c29034978bf Mon Sep 17 00:00:00 2001 From: David Marcin Date: Sun, 8 Aug 2021 21:37:21 -0700 Subject: [PATCH 18/21] Regenerate documentation --- docs/flatten.md | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/flatten.md b/docs/flatten.md index 40d19789f4..5e41c59e1e 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -85,78 +85,78 @@ Environment Variables: | version | The version of cargo the resolver should use | String | optional | "1.54.0" | - + -## fail_when_enabled +## error_format
-fail_when_enabled(name, flag)
+error_format(name)
 
-A rule that will fail analysis when a flag is enabled. +Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values. **ATTRIBUTES** | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| flag | The incompatible flag to check | String | required | | +| name | A unique name for this target. | Name | required | | - + -## incompatible_flag +## extra_rustc_flags
-incompatible_flag(name, issue)
+extra_rustc_flags(name)
 
-A rule defining an incompatible flag. +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). **ATTRIBUTES** | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | -| issue | The link to the github issue associated with this flag | String | required | | +| name | A unique name for this target. | Name | required | | - + -## error_format +## fail_when_enabled
-error_format(name)
+fail_when_enabled(name, flag)
 
-Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values. +A rule that will fail analysis when a flag is enabled. **ATTRIBUTES** | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | +| name | A unique name for this target. | Name | required | | +| flag | The incompatible flag to check | String | required | | - + -## extra_rustc_flags +## incompatible_flag
-extra_rustc_flags(name)
+incompatible_flag(name, issue)
 
-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). +A rule defining an incompatible flag. **ATTRIBUTES** | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | +| name | A unique name for this target. | Name | required | | +| issue | The link to the github issue associated with this flag | String | required | | From 85eb0e578d99f57d2b1fe41fad0ebdfb8913b2df Mon Sep 17 00:00:00 2001 From: David Marcin Date: Tue, 31 Aug 2021 01:01:36 -0700 Subject: [PATCH 19/21] Add test --- test/unit/extra_rustc_flags/BUILD.bazel | 5 + .../extra_rustc_flags_test.bzl | 94 +++++++++++++++++++ test/unit/extra_rustc_flags/lib.rs | 1 + 3 files changed, 100 insertions(+) create mode 100644 test/unit/extra_rustc_flags/BUILD.bazel create mode 100644 test/unit/extra_rustc_flags/extra_rustc_flags_test.bzl create mode 100644 test/unit/extra_rustc_flags/lib.rs diff --git a/test/unit/extra_rustc_flags/BUILD.bazel b/test/unit/extra_rustc_flags/BUILD.bazel new file mode 100644 index 0000000000..52be64d1a4 --- /dev/null +++ b/test/unit/extra_rustc_flags/BUILD.bazel @@ -0,0 +1,5 @@ +load(":extra_rustc_flags_test.bzl", "extra_rustc_flags_test_suite") + +extra_rustc_flags_test_suite( + name = "extra_rustc_flags_test_suite", +) diff --git a/test/unit/extra_rustc_flags/extra_rustc_flags_test.bzl b/test/unit/extra_rustc_flags/extra_rustc_flags_test.bzl new file mode 100644 index 0000000000..8a0bc9fdd9 --- /dev/null +++ b/test/unit/extra_rustc_flags/extra_rustc_flags_test.bzl @@ -0,0 +1,94 @@ +"""Unittest to verify compile_data (attribute) propagation""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("//rust:defs.bzl", "rust_library") +load("//test/unit:common.bzl", "assert_argv_contains", "assert_argv_contains_not") + +EXTRA_FLAG = "--codegen=linker-plugin-lto" + +def target_action_contains_not_flag(env, target): + action = target.actions[0] + asserts.equals(env, "Rustc", action.mnemonic) + + assert_argv_contains_not( + env = env, + action = action, + flag = EXTRA_FLAG, + ) + +def target_action_contains_flag(env, target): + action = target.actions[0] + asserts.equals(env, "Rustc", action.mnemonic) + + assert_argv_contains( + env = env, + action = action, + flag = EXTRA_FLAG, + ) + +def _extra_rustc_flags_not_present_test(ctx): + env = analysistest.begin(ctx) + target = analysistest.target_under_test(env) + target_action_contains_not_flag(env, target) + + return analysistest.end(env) + +def _extra_rustc_flags_present_test(ctx): + env = analysistest.begin(ctx) + target = analysistest.target_under_test(env) + target_action_contains_flag(env, target) + + # Check the exec configuration target does NOT contain. + target = ctx.attr.lib_exec + target_action_contains_not_flag(env, target) + + return analysistest.end(env) + +extra_rustc_flags_not_present_test = analysistest.make(_extra_rustc_flags_not_present_test) +extra_rustc_flags_present_test = analysistest.make( + _extra_rustc_flags_present_test, + attrs = { + "lib_exec": attr.label( + mandatory = True, + cfg = "exec", + ), + }, + config_settings = { + "//:extra_rustc_flags": [EXTRA_FLAG], + }, +) + +def _define_test_targets(): + rust_library( + name = "lib", + srcs = ["lib.rs"], + edition = "2018", + ) + +def extra_rustc_flags_test_suite(name): + """Entry-point macro called from the BUILD file. + + Args: + name (str): Name of the macro. + """ + + _define_test_targets() + + extra_rustc_flags_not_present_test( + name = "extra_rustc_flags_not_present_test", + target_under_test = ":lib", + ) + + extra_rustc_flags_present_test( + name = "extra_rustc_flags_present_test", + target_under_test = ":lib", + lib_exec = ":lib", + ) + + native.test_suite( + name = name, + tests = [ + ":extra_rustc_flags_not_present_test", + ":extra_rustc_flags_present_test", + ], + ) diff --git a/test/unit/extra_rustc_flags/lib.rs b/test/unit/extra_rustc_flags/lib.rs new file mode 100644 index 0000000000..a38192ad84 --- /dev/null +++ b/test/unit/extra_rustc_flags/lib.rs @@ -0,0 +1 @@ +pub fn call() {} From 51b5a7db51d89c2956bcb907de398e7bc20264ca Mon Sep 17 00:00:00 2001 From: David Marcin Date: Wed, 1 Sep 2021 17:05:58 -0700 Subject: [PATCH 20/21] Revert extraneous changes --- docs/BUILD.bazel | 1 - docs/symbols.bzl | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 5f4dd8d7d4..2c73545797 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -59,7 +59,6 @@ PAGES = dict([ "rust_benchmark", "rust_test", "rust_test_suite", - "error_format", "extra_rustc_flags", ], ), diff --git a/docs/symbols.bzl b/docs/symbols.bzl index 9b243b8155..f174b27eea 100644 --- a/docs/symbols.bzl +++ b/docs/symbols.bzl @@ -41,7 +41,6 @@ load( ) load( "@rules_rust//rust:defs.bzl", - _error_format = "error_format", _extra_rustc_flags = "extra_rustc_flags", _rust_analyzer = "rust_analyzer", _rust_analyzer_aspect = "rust_analyzer_aspect", @@ -143,12 +142,10 @@ crate = _crate rustfmt_aspect = _rustfmt_aspect rustfmt_test = _rustfmt_test -error_format = _error_format extra_rustc_flags = _extra_rustc_flags -fail_when_enabled = _fail_when_enabled incompatible_flag = _incompatible_flag +fail_when_enabled = _fail_when_enabled CrateInfo = _CrateInfo DepInfo = _DepInfo StdLibInfo = _StdLibInfo - From 9457966258ba3992e76f771a421b07548d83af63 Mon Sep 17 00:00:00 2001 From: David Marcin Date: Wed, 1 Sep 2021 17:14:38 -0700 Subject: [PATCH 21/21] Regenerate documentation --- docs/defs.md | 19 ------------------- docs/flatten.md | 19 ------------------- 2 files changed, 38 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index f591704f82..2941a29240 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -9,27 +9,8 @@ * [rust_benchmark](#rust_benchmark) * [rust_test](#rust_test) * [rust_test_suite](#rust_test_suite) -* [error_format](#error_format) * [extra_rustc_flags](#extra_rustc_flags) - - -## error_format - -
-error_format(name)
-
- -Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - ## extra_rustc_flags diff --git a/docs/flatten.md b/docs/flatten.md index 24a69892b9..30f3bd7821 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -7,7 +7,6 @@ * [cargo_build_script](#cargo_build_script) * [crate](#crate) * [crate_universe](#crate_universe) -* [error_format](#error_format) * [extra_rustc_flags](#extra_rustc_flags) * [fail_when_enabled](#fail_when_enabled) * [incompatible_flag](#incompatible_flag) @@ -119,24 +118,6 @@ Environment Variables: | version | The version of cargo the resolver should use | String | optional | "1.54.0" | - - -## error_format - -
-error_format(name)
-
- -Change the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values. - -**ATTRIBUTES** - - -| Name | Description | Type | Mandatory | Default | -| :------------- | :------------- | :------------- | :------------- | :------------- | -| name | A unique name for this target. | Name | required | | - - ## extra_rustc_flags