|
| 1 | +# allow setting rustc env via build setting |
| 2 | +# https://github.com/bazelbuild/rules_rust/pull/3510 |
| 3 | +diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl |
| 4 | +index 74d458eef..bb51c9cf4 100644 |
| 5 | +--- a/rust/private/rust.bzl |
| 6 | ++++ b/rust/private/rust.bzl |
| 7 | +@@ -531,12 +531,18 @@ RUSTC_ATTRS = { |
| 8 | + "_error_format": attr.label( |
| 9 | + default = Label("//rust/settings:error_format"), |
| 10 | + ), |
| 11 | ++ "_extra_exec_rustc_env": attr.label( |
| 12 | ++ default = Label("//rust/settings:extra_exec_rustc_env"), |
| 13 | ++ ), |
| 14 | + "_extra_exec_rustc_flag": attr.label( |
| 15 | + default = Label("//rust/settings:extra_exec_rustc_flag"), |
| 16 | + ), |
| 17 | + "_extra_exec_rustc_flags": attr.label( |
| 18 | + default = Label("//rust/settings:extra_exec_rustc_flags"), |
| 19 | + ), |
| 20 | ++ "_extra_rustc_env": attr.label( |
| 21 | ++ default = Label("//rust/settings:extra_rustc_env"), |
| 22 | ++ ), |
| 23 | + "_extra_rustc_flag": attr.label( |
| 24 | + default = Label("//rust/settings:extra_rustc_flag"), |
| 25 | + ), |
| 26 | +diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl |
| 27 | +index d78c28902..1e638fdf9 100644 |
| 28 | +--- a/rust/private/rustc.bzl |
| 29 | ++++ b/rust/private/rustc.bzl |
| 30 | +@@ -67,6 +67,16 @@ ErrorFormatInfo = provider( |
| 31 | + fields = {"error_format": "(string) [" + ", ".join(_error_format_values) + "]"}, |
| 32 | + ) |
| 33 | + |
| 34 | ++ExtraRustcEnvInfo = provider( |
| 35 | ++ doc = "Pass each value as an environment variable to non-exec rustc invocations", |
| 36 | ++ fields = {"extra_rustc_env": "List[string] Extra env to pass to rustc in non-exec configuration"}, |
| 37 | ++) |
| 38 | ++ |
| 39 | ++ExtraExecRustcEnvInfo = provider( |
| 40 | ++ doc = "Pass each value as an environment variable to exec rustc invocations", |
| 41 | ++ fields = {"extra_exec_rustc_env": "List[string] Extra env to pass to rustc in exec configuration"}, |
| 42 | ++) |
| 43 | ++ |
| 44 | + ExtraRustcFlagsInfo = provider( |
| 45 | + doc = "Pass each value as an additional flag to non-exec rustc invocations", |
| 46 | + fields = {"extra_rustc_flags": "List[string] Extra flags to pass to rustc in non-exec configuration"}, |
| 47 | +@@ -1102,6 +1112,10 @@ def construct_arguments( |
| 48 | + else: |
| 49 | + rustc_flags.add_all(toolchain.extra_rustc_flags, map_each = map_flag) |
| 50 | + |
| 51 | ++ # extra_rustc_env applies to the target configuration, not the exec configuration. |
| 52 | ++ if hasattr(ctx.attr, "_extra_rustc_env") and not is_exec_configuration(ctx): |
| 53 | ++ env.update(ctx.attr._extra_rustc_env[ExtraRustcEnvInfo].extra_rustc_env) |
| 54 | ++ |
| 55 | + # extra_rustc_flags apply to the target configuration, not the exec configuration. |
| 56 | + if hasattr(ctx.attr, "_extra_rustc_flags") and not is_exec_configuration(ctx): |
| 57 | + rustc_flags.add_all(ctx.attr._extra_rustc_flags[ExtraRustcFlagsInfo].extra_rustc_flags, map_each = map_flag) |
| 58 | +@@ -1113,6 +1127,9 @@ def construct_arguments( |
| 59 | + per_crate_rustc_flags = ctx.attr._per_crate_rustc_flag[PerCrateRustcFlagsInfo].per_crate_rustc_flags |
| 60 | + _add_per_crate_rustc_flags(ctx, rustc_flags, map_flag, crate_info, per_crate_rustc_flags) |
| 61 | + |
| 62 | ++ if hasattr(ctx.attr, "_extra_exec_rustc_env") and is_exec_configuration(ctx): |
| 63 | ++ env.update(ctx.attr._extra_exec_rustc_env[ExtraExecRustcEnvInfo].extra_exec_rustc_env) |
| 64 | ++ |
| 65 | + if hasattr(ctx.attr, "_extra_exec_rustc_flags") and is_exec_configuration(ctx): |
| 66 | + rustc_flags.add_all(ctx.attr._extra_exec_rustc_flags[ExtraExecRustcFlagsInfo].extra_exec_rustc_flags, map_each = map_flag) |
| 67 | + |
| 68 | +@@ -2279,6 +2296,24 @@ rustc_output_diagnostics = rule( |
| 69 | + build_setting = config.bool(flag = True), |
| 70 | + ) |
| 71 | + |
| 72 | ++def _extra_rustc_env_impl(ctx): |
| 73 | ++ env_vars = {} |
| 74 | ++ for entry in ctx.build_setting_value: |
| 75 | ++ if "=" not in entry: |
| 76 | ++ fail("Invalid format for rustc env var: '{}'. Expected 'KEY=value'".format(entry)) |
| 77 | ++ key, val = entry.split("=", 1) |
| 78 | ++ env_vars[key] = val |
| 79 | ++ return ExtraRustcEnvInfo(extra_rustc_env = env_vars) |
| 80 | ++ |
| 81 | ++extra_rustc_env = rule( |
| 82 | ++ doc = ( |
| 83 | ++ "Add additional environment variables to rustc in non-exec configuration using " + |
| 84 | ++ "`--@rules_rust//rust/settings:extra_rustc_env=FOO=bar`. Multiple values may be specified." |
| 85 | ++ ), |
| 86 | ++ implementation = _extra_rustc_env_impl, |
| 87 | ++ build_setting = config.string_list(flag = True), |
| 88 | ++) |
| 89 | ++ |
| 90 | + def _extra_rustc_flags_impl(ctx): |
| 91 | + return ExtraRustcFlagsInfo(extra_rustc_flags = ctx.build_setting_value) |
| 92 | + |
| 93 | +@@ -2319,6 +2354,24 @@ extra_exec_rustc_flags = rule( |
| 94 | + build_setting = config.string_list(flag = True), |
| 95 | + ) |
| 96 | + |
| 97 | ++def _extra_exec_rustc_env_impl(ctx): |
| 98 | ++ env_vars = {} |
| 99 | ++ for entry in ctx.build_setting_value: |
| 100 | ++ if "=" not in entry: |
| 101 | ++ fail("Invalid format for rustc env var: '{}'. Expected 'KEY=value'".format(entry)) |
| 102 | ++ key, val = entry.split("=", 1) |
| 103 | ++ env_vars[key] = val |
| 104 | ++ return ExtraExecRustcEnvInfo(extra_exec_rustc_env = env_vars) |
| 105 | ++ |
| 106 | ++extra_exec_rustc_env = rule( |
| 107 | ++ doc = ( |
| 108 | ++ "Add additional environment variables to rustc in non-exec configuration using " + |
| 109 | ++ "`--@rules_rust//rust/settings:extra_exec_rustc_env=FOO=bar`. Multiple values may be specified." |
| 110 | ++ ), |
| 111 | ++ implementation = _extra_exec_rustc_env_impl, |
| 112 | ++ build_setting = config.string_list(flag = True), |
| 113 | ++) |
| 114 | ++ |
| 115 | + def _extra_exec_rustc_flag_impl(ctx): |
| 116 | + return ExtraExecRustcFlagsInfo(extra_exec_rustc_flags = [f for f in ctx.build_setting_value if f != ""]) |
| 117 | + |
| 118 | +diff --git a/rust/settings/BUILD.bazel b/rust/settings/BUILD.bazel |
| 119 | +index 8fb46461d..3f6bf3073 100644 |
| 120 | +--- a/rust/settings/BUILD.bazel |
| 121 | ++++ b/rust/settings/BUILD.bazel |
| 122 | +@@ -13,8 +13,10 @@ load( |
| 123 | + "experimental_use_coverage_metadata_files", |
| 124 | + "experimental_use_global_allocator", |
| 125 | + "experimental_use_sh_toolchain_for_bootstrap_process_wrapper", |
| 126 | ++ "extra_exec_rustc_env", |
| 127 | + "extra_exec_rustc_flag", |
| 128 | + "extra_exec_rustc_flags", |
| 129 | ++ "extra_rustc_env", |
| 130 | + "extra_rustc_flag", |
| 131 | + "extra_rustc_flags", |
| 132 | + "incompatible_change_rust_test_compilation_output_directory", |
| 133 | +@@ -73,10 +75,14 @@ experimental_use_global_allocator() |
| 134 | + |
| 135 | + experimental_use_sh_toolchain_for_bootstrap_process_wrapper() |
| 136 | + |
| 137 | ++extra_exec_rustc_env() |
| 138 | ++ |
| 139 | + extra_exec_rustc_flag() |
| 140 | + |
| 141 | + extra_exec_rustc_flags() |
| 142 | + |
| 143 | ++extra_rustc_env() |
| 144 | ++ |
| 145 | + extra_rustc_flag() |
| 146 | + |
| 147 | + extra_rustc_flags() |
| 148 | +diff --git a/rust/settings/settings.bzl b/rust/settings/settings.bzl |
| 149 | +index 1444600d2..c795597d0 100644 |
| 150 | +--- a/rust/settings/settings.bzl |
| 151 | ++++ b/rust/settings/settings.bzl |
| 152 | +@@ -19,8 +19,10 @@ load("//rust/private:lto.bzl", "rust_lto_flag") |
| 153 | + load( |
| 154 | + "//rust/private:rustc.bzl", |
| 155 | + _error_format = "error_format", |
| 156 | ++ _extra_exec_rustc_env = "extra_exec_rustc_env", |
| 157 | + _extra_exec_rustc_flag = "extra_exec_rustc_flag", |
| 158 | + _extra_exec_rustc_flags = "extra_exec_rustc_flags", |
| 159 | ++ _extra_rustc_env = "extra_rustc_env", |
| 160 | + _extra_rustc_flag = "extra_rustc_flag", |
| 161 | + _extra_rustc_flags = "extra_rustc_flags", |
| 162 | + _no_std = "no_std", |
| 163 | +@@ -258,6 +260,18 @@ def clippy_flags(): |
| 164 | + build_setting_default = [], |
| 165 | + ) |
| 166 | + |
| 167 | ++# buildifier: disable=unnamed-macro |
| 168 | ++def extra_rustc_env(): |
| 169 | ++ """This setting may be used to pass extra environment variables to rustc from the command line in non-exec configuration. |
| 170 | ++ |
| 171 | ++ It applies across all targets whereas environment variables set in a specific rule apply only to that target. |
| 172 | ++ This can be useful for setting build-wide env flags such as `RUSTC_BOOTSTRAP=1`. |
| 173 | ++ """ |
| 174 | ++ _extra_rustc_env( |
| 175 | ++ name = "extra_rustc_env", |
| 176 | ++ build_setting_default = [], |
| 177 | ++ ) |
| 178 | ++ |
| 179 | + # buildifier: disable=unnamed-macro |
| 180 | + def clippy_flag(): |
| 181 | + """Add a custom clippy flag from the command line with `--@rules_rust//rust/settings:clippy_flag`. |
| 182 | +@@ -292,6 +306,18 @@ def extra_rustc_flag(): |
| 183 | + build_setting_default = [], |
| 184 | + ) |
| 185 | + |
| 186 | ++# buildifier: disable=unnamed-macro |
| 187 | ++def extra_exec_rustc_env(): |
| 188 | ++ """This setting may be used to pass extra environment variables to rustc from the command line in exec configuration. |
| 189 | ++ |
| 190 | ++ It applies to tools built and run during the build process, such as proc-macros and build scripts. |
| 191 | ++ This can be useful for enabling features that are needed during tool compilation. |
| 192 | ++ """ |
| 193 | ++ _extra_exec_rustc_env( |
| 194 | ++ name = "extra_exec_rustc_env", |
| 195 | ++ build_setting_default = [], |
| 196 | ++ ) |
| 197 | ++ |
| 198 | + # buildifier: disable=unnamed-macro |
| 199 | + def extra_exec_rustc_flags(): |
| 200 | + """This setting may be used to pass extra options to rustc from the command line in exec configuration. |
0 commit comments