gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 3 | title: Configurable build attributes |
daroberts | 223aebd | 2021-02-18 17:53:30 -0800 | [diff] [blame] | 4 | category: getting-started |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 5 | --- |
| 6 | |
Googler | 458daaa | 2021-01-08 18:55:00 -0800 | [diff] [blame] | 7 | # Configurable Build Attributes |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 8 | |
laurentlb | 79b9431 | 2018-11-07 05:18:59 -0800 | [diff] [blame] | 9 | **_Configurable attributes_**, commonly known as [`select()`]( |
| 10 | be/functions.html#select), is a Bazel feature that lets users toggle the values |
Googler | 2ebf3bd | 2021-05-21 09:35:36 -0700 | [diff] [blame] | 11 | of build rule attributes at the command line. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 12 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 13 | This can be used, for example, for a multiplatform library that automatically |
| 14 | chooses the appropriate implementation for the architecture, or for a |
| 15 | feature-configurable binary that can be customized at build time. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 16 | |
| 17 | ## Example |
| 18 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 19 | ```python |
| 20 | # myapp/BUILD |
| 21 | |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 22 | cc_binary( |
| 23 | name = "mybinary", |
| 24 | srcs = ["main.cc"], |
| 25 | deps = select({ |
| 26 | ":arm_build": [":arm_lib"], |
| 27 | ":x86_debug_build": [":x86_dev_lib"], |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 28 | "//conditions:default": [":generic_lib"], |
| 29 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | config_setting( |
| 33 | name = "arm_build", |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 34 | values = {"cpu": "arm"}, |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | config_setting( |
| 38 | name = "x86_debug_build", |
| 39 | values = { |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 40 | "cpu": "x86", |
| 41 | "compilation_mode": "dbg", |
| 42 | }, |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 43 | ) |
| 44 | ``` |
| 45 | |
| 46 | This declares a `cc_binary` that "chooses" its deps based on the flags at the |
| 47 | command line. Specficially, `deps` becomes: |
| 48 | |
| 49 | <table> |
| 50 | <tr style="background: #E9E9E9; font-weight: bold"> |
| 51 | <td>Command</td> |
| 52 | <td>deps =</td> |
| 53 | </tr> |
| 54 | <tr> |
| 55 | <td><code>bazel build //myapp:mybinary --cpu=arm</code></td> |
| 56 | <td><code>[":arm_lib"]</code></td> |
| 57 | </tr> |
| 58 | <tr> |
Thi | d1a8a90 | 2018-12-21 15:43:24 -0800 | [diff] [blame] | 59 | <td><code>bazel build //myapp:mybinary -c dbg --cpu=x86</code></td> |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 60 | <td><code>[":x86_dev_lib"]</code></td> |
| 61 | </tr> |
| 62 | <tr> |
| 63 | <td><code>bazel build //myapp:mybinary --cpu=ppc</code></td> |
| 64 | <td><code>[":generic_lib"]</code></td> |
| 65 | </tr> |
| 66 | <tr> |
| 67 | <td><code>bazel build //myapp:mybinary -c dbg --cpu=ppc</code></td> |
| 68 | <td><code>[":generic_lib"]</code></td> |
| 69 | </tr> |
| 70 | </table> |
Greg Estren | e821cfa | 2020-02-20 09:34:56 -0800 | [diff] [blame] | 71 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 72 | `select()` serves as a placeholder for a value that will be chosen based on |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 73 | *configuration conditions*, which are labels referencing [`config_setting`](be/general.html#config_setting) |
| 74 | targets. By using `select()` in a configurable attribute, the attribute |
| 75 | effectively adopts different values when different conditions hold. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 76 | |
| 77 | Matches must be unambiguous: either exactly one condition must match or, if |
| 78 | multiple conditions match, one's `values` must be a strict superset of all |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 79 | others'. For example, `values = {"cpu": "x86", "compilation_mode": "dbg"}` is an |
| 80 | unambiguous specialization of `values = {"cpu": "x86"}`. The built-in condition |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 81 | [`//conditions:default`](#the-default-condition) automatically matches when |
| 82 | nothing else does. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 83 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 84 | While this example uses `deps`, `select()` works just as well on `srcs`, |
| 85 | `resources`, `cmd`, and most other attributes. Only a small number of attributes |
| 86 | are *non-configurable*, and these are clearly annotated. For example, |
| 87 | `config_setting`'s own |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 88 | [`values`](be/general.html#config_setting.values) attribute is non-configurable. |
| 89 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 90 | ## `select()` and dependencies |
| 91 | |
| 92 | Certain attributes change the build parameters for all transitive dependencies |
| 93 | under a target. For example, `genrule`'s `tools` changes `--cpu` to the CPU of |
| 94 | the machine running Bazel (which, thanks to cross-compilation, may be different |
| 95 | than the CPU the target is built for). This is known as a |
aiuto | 4e9a77e | 2021-06-15 08:55:11 -0700 | [diff] [blame] | 96 | [configuration transition](https://docs.bazel.build/versions/main/glossary.html#transition). |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 97 | |
| 98 | Given |
| 99 | |
| 100 | ```python |
| 101 | #myapp/BUILD |
| 102 | |
| 103 | config_setting( |
| 104 | name = "arm_cpu", |
| 105 | values = {"cpu": "arm"}, |
| 106 | ) |
| 107 | |
| 108 | config_setting( |
| 109 | name = "x86_cpu", |
| 110 | values = {"cpu": "x86"}, |
| 111 | ) |
| 112 | |
| 113 | genrule( |
| 114 | name = "my_genrule", |
| 115 | srcs = select({ |
| 116 | ":arm_cpu": ["g_arm.src"], |
| 117 | ":x86_cpu": ["g_x86.src"], |
Googler | d63925b | 2021-02-05 10:57:50 -0800 | [diff] [blame] | 118 | }), |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 119 | tools = select({ |
| 120 | ":arm_cpu": [":tool1"], |
| 121 | ":x86_cpu": [":tool2"], |
| 122 | }), |
| 123 | ) |
| 124 | |
| 125 | cc_binary( |
| 126 | name = "tool1", |
| 127 | srcs = select({ |
| 128 | ":arm_cpu": ["armtool.cc"], |
| 129 | ":x86_cpu": ["x86tool.cc"], |
| 130 | }), |
| 131 | ) |
| 132 | ``` |
| 133 | |
| 134 | running |
| 135 | |
| 136 | ```sh |
| 137 | $ bazel build //myapp:my_genrule --cpu=arm |
| 138 | ``` |
| 139 | |
| 140 | on an `x86` developer machine binds the build to `g_arm.src`, `tool1`, and |
| 141 | `x86tool.cc`. Both of the `select`s attached to `my_genrule` use `my_genrule`'s |
| 142 | build parameters, which include `--cpu=arm`. The `tools` attribute changes |
| 143 | `--cpu` to `x86` for `tool1` and its transitive dependencies. The `select` on |
| 144 | `tool1` uses `tool1`'s build parameters, which include `--cpu=x86`. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 145 | |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 146 | ## Configuration conditions |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 147 | |
| 148 | Each key in a configurable attribute is a label reference to a |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 149 | [`config_setting`](be/general.html#config_setting) or |
| 150 | [`constraint_value`](be/platform.html#constraint_value). |
| 151 | |
| 152 | `config_setting` is just a collection of |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 153 | expected command line flag settings. By encapsulating these in a target, it's |
| 154 | easy to maintain "standard" conditions users can reference from multiple places. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 155 | |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 156 | `constraint_value` provides support for [multi-platform behavior](#platforms). |
| 157 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 158 | |
| 159 | ### Built-in flags |
| 160 | |
| 161 | Flags like `--cpu` are built into Bazel: the build tool natively understands |
| 162 | them for all builds in all projects. These are specified with |
| 163 | [`config_setting`](be/general.html#config_setting)'s |
| 164 | [`values`](be/general.html#config_setting.values) attribute: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 165 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 166 | ```python |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 167 | config_setting( |
| 168 | name = "meaningful_condition_name", |
| 169 | values = { |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 170 | "flag1": "value1", |
| 171 | "flag2": "value2", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 172 | ... |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 173 | }, |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 174 | ) |
| 175 | ``` |
| 176 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 177 | `flagN` is a flag name (without `--`, so `"cpu"` instead of `"--cpu"`). `valueN` |
| 178 | is the expected value for that flag. `:meaningful_condition_name` matches if |
| 179 | *every* entry in `values` matches. Order is irrelevant. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 180 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 181 | `valueN` is parsed as if it was set on the command line. This means: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 182 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 183 | * `values = { "compilation_mode": "opt" }` matches `bazel build -c opt` |
| 184 | * `values = { "force_pic": "true" }` matches `bazel build --force_pic=1` |
| 185 | * `values = { "force_pic": "0" }` matches `bazel build --noforce_pic` |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 186 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 187 | `config_setting` only supports flags that affect target behavior. For example, |
| 188 | [`--show_progress`](user-manual.html#flag--show_progress) isn't allowed because |
| 189 | it only affects how Bazel reports progress to the user. Targets can't use that |
| 190 | flag to construct their results. The exact set of supported flags isn't |
| 191 | documented. In practice, most flags that "make sense" work. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 192 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 193 | ### Custom flags |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 194 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 195 | You can model your own project-specific flags with |
| 196 | [Starlark build |
gregce | cd2209a | 2020-10-13 11:17:01 -0700 | [diff] [blame] | 197 | settings][BuildSettings]. Unlike built-in flags, these are defined as build |
| 198 | targets, so Bazel references them with target labels. |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 199 | |
| 200 | These are triggered with [`config_setting`](be/general.html#config_setting)'s |
| 201 | [`flag_values`](be/general.html#config_setting.flag_values) |
| 202 | attribute: |
| 203 | |
| 204 | ```python |
| 205 | config_setting( |
| 206 | name = "meaningful_condition_name", |
| 207 | flag_values = { |
| 208 | "//myflags:flag1": "value1", |
| 209 | "//myflags:flag2": "value2", |
| 210 | ... |
| 211 | }, |
| 212 | ) |
| 213 | ``` |
| 214 | |
aiuto | 4e9a77e | 2021-06-15 08:55:11 -0700 | [diff] [blame] | 215 | Behavior is the same as for [built-in flags](#built-in-flags). See [here](https://github.com/bazelbuild/examples/tree/HEAD/rules/starlark_configurations/select_on_build_setting) |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 216 | for a working example. |
| 217 | |
| 218 | [`--define`](command-line-reference.html#flag--define) |
| 219 | is an alternative legacy syntax for custom flags (for example |
| 220 | `--define foo=bar`). This can be expressed either in the |
| 221 | [values](be/general.html#config_setting.values) attribute |
| 222 | (`values = {"define": "foo=bar"}`) or the |
| 223 | [define_values](be/general.html#config_setting.define_values) attribute |
| 224 | (`define_values = {"foo": "bar"}`). `--define` is only supported for backwards |
| 225 | compatibility. Prefer Starlark build settings whenever possible. |
| 226 | |
| 227 | `values`, `flag_values`, and `define_values` evaluate independently. The |
| 228 | `config_setting` matches if all values across all of them match. |
| 229 | |
| 230 | ## The default condition |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 231 | |
| 232 | The built-in condition `//conditions:default` matches when no other condition |
| 233 | matches. |
| 234 | |
| 235 | Because of the "exactly one match" rule, a configurable attribute with no match |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 236 | and no default condition emits a `"no matching conditions"` error. This can |
| 237 | protect against silent failures from unexpected settings: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 238 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 239 | ```python |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 240 | # myapp/BUILD |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 241 | |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 242 | config_setting( |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 243 | name = "x86_cpu", |
| 244 | values = {"cpu": "x86"}, |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 245 | ) |
| 246 | |
| 247 | cc_library( |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 248 | name = "x86_only_lib", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 249 | srcs = select({ |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 250 | ":x86_cpu": ["lib.cc"], |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 251 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 252 | ) |
| 253 | ``` |
| 254 | |
| 255 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 256 | $ bazel build //myapp:x86_only_lib --cpu=arm |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 257 | ERROR: Configurable attribute "srcs" doesn't match this configuration (would |
| 258 | a default condition help?). |
| 259 | Conditions checked: |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 260 | //myapp:x86_cpu |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 261 | ``` |
| 262 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 263 | For even clearer errors, you can set custom messages with `select()`'s |
| 264 | [`no_match_error`](#custom-error-messages) attribute. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 265 | |
| 266 | ## Platforms |
| 267 | |
| 268 | While the ability to specify multiple flags on the command line provides |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 269 | flexibility, it can also be burdensome to individually set each one every time |
| 270 | you want to build a target. |
| 271 | [Platforms](platforms.html) |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 272 | let you consolidate these into simple bundles. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 273 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 274 | ```python |
| 275 | # myapp/BUILD |
| 276 | |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 277 | sh_binary( |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 278 | name = "my_rocks", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 279 | srcs = select({ |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 280 | ":basalt": ["pyroxene.sh"], |
| 281 | ":marble": ["calcite.sh"], |
| 282 | "//conditions:default": ["feldspar.sh"], |
| 283 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 284 | ) |
| 285 | |
| 286 | config_setting( |
| 287 | name = "basalt", |
| 288 | constraint_values = [ |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 289 | ":black", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 290 | ":igneous", |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 291 | ], |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 292 | ) |
| 293 | |
| 294 | config_setting( |
| 295 | name = "marble", |
| 296 | constraint_values = [ |
| 297 | ":white", |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 298 | ":metamorphic", |
| 299 | ], |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 300 | ) |
| 301 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 302 | # constraint_setting acts as an enum type, and constraint_value as an enum value. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 303 | constraint_setting(name = "color") |
| 304 | constraint_value(name = "black", constraint_setting = "color") |
| 305 | constraint_value(name = "white", constraint_setting = "color") |
| 306 | constraint_setting(name = "texture") |
| 307 | constraint_value(name = "smooth", constraint_setting = "texture") |
| 308 | constraint_setting(name = "type") |
| 309 | constraint_value(name = "igneous", constraint_setting = "type") |
| 310 | constraint_value(name = "metamorphic", constraint_setting = "type") |
| 311 | |
| 312 | platform( |
| 313 | name = "basalt_platform", |
| 314 | constraint_values = [ |
| 315 | ":black", |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 316 | ":igneous", |
| 317 | ], |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 318 | ) |
| 319 | |
| 320 | platform( |
| 321 | name = "marble_platform", |
| 322 | constraint_values = [ |
| 323 | ":white", |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 324 | ":smooth", |
| 325 | ":metamorphic", |
| 326 | ], |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 327 | ) |
| 328 | ``` |
| 329 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 330 | The platform can be specified on the command line. It activates the |
| 331 | `config_setting`s that contain a subset of the platform's `constraint_values`, |
| 332 | allowing those `config_setting`s to match in `select()` expressions. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 333 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 334 | For example, in order to set the `srcs` attribute of `my_rocks` to `calcite.sh`, |
Googler | a1a29ec | 2021-02-26 12:19:25 -0800 | [diff] [blame] | 335 | you can simply run |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 336 | |
| 337 | ```sh |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 338 | bazel build //my_app:my_rocks --platforms=//myapp:marble_platform |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 339 | ``` |
| 340 | |
| 341 | Without platforms, this might look something like |
| 342 | |
| 343 | ```sh |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 344 | bazel build //my_app:my_rocks --define color=white --define texture=smooth --define type=metamorphic |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 345 | ``` |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 346 | |
Greg Estren | 12b06b2 | 2020-09-11 10:58:43 -0700 | [diff] [blame] | 347 | `select()` can also directly read `constraint_value`s: |
| 348 | |
| 349 | ```python |
| 350 | constraint_setting(name = "type") |
| 351 | constraint_value(name = "igneous", constraint_setting = "type") |
| 352 | constraint_value(name = "metamorphic", constraint_setting = "type") |
| 353 | sh_binary( |
| 354 | name = "my_rocks", |
| 355 | srcs = select({ |
| 356 | ":igneous": ["igneous.sh"], |
| 357 | ":metamorphic" ["metamorphic.sh"], |
| 358 | }), |
| 359 | ) |
| 360 | ``` |
| 361 | |
| 362 | This saves the need for boilerplate `config_setting`s when you only need to |
| 363 | check against single values. |
| 364 | |
| 365 | Platforms are still under development. See the |
| 366 | [documentation](platforms-intro.html) for details. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 367 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 368 | ## Combining `select()`s |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 369 | |
| 370 | `select` can appear multiple times in the same attribute: |
| 371 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 372 | ```python |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 373 | sh_binary( |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 374 | name = "my_target", |
| 375 | srcs = ["always_include.sh"] + |
| 376 | select({ |
| 377 | ":armeabi_mode": ["armeabi_src.sh"], |
| 378 | ":x86_mode": ["x86_src.sh"], |
| 379 | }) + |
| 380 | select({ |
| 381 | ":opt_mode": ["opt_extras.sh"], |
| 382 | ":dbg_mode": ["dbg_extras.sh"], |
| 383 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 384 | ) |
| 385 | ``` |
| 386 | |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 387 | `select` cannot appear inside another `select`. If you need to nest `selects` |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 388 | and your attribute takes other targets as values, use an intermediate target: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 389 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 390 | ```python |
| 391 | sh_binary( |
| 392 | name = "my_target", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 393 | srcs = ["always_include.sh"], |
| 394 | deps = select({ |
| 395 | ":armeabi_mode": [":armeabi_lib"], |
| 396 | ... |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 397 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 398 | ) |
| 399 | |
| 400 | sh_library( |
| 401 | name = "armeabi_lib", |
| 402 | srcs = select({ |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 403 | ":opt_mode": ["armeabi_with_opt.sh"], |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 404 | ... |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 405 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 406 | ) |
| 407 | ``` |
| 408 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 409 | If you need a `select` to match when multiple conditions match, consider [AND |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 410 | chaining](#and-chaining). |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 411 | |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 412 | ## OR chaining |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 413 | |
| 414 | Consider the following: |
| 415 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 416 | ```python |
| 417 | sh_binary( |
| 418 | name = "my_target", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 419 | srcs = ["always_include.sh"], |
| 420 | deps = select({ |
| 421 | ":config1": [":standard_lib"], |
| 422 | ":config2": [":standard_lib"], |
| 423 | ":config3": [":standard_lib"], |
| 424 | ":config4": [":special_lib"], |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 425 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 426 | ) |
| 427 | ``` |
| 428 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 429 | Most conditions evaluate to the same dep. But this syntax is hard to read and |
| 430 | maintain. It would be nice to not have to repeat `[":standard_lib"]` multiple |
| 431 | times. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 432 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 433 | One option is to predefine the value as a BUILD variable: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 434 | |
| 435 | ```python |
| 436 | STANDARD_DEP = [":standard_lib"] |
| 437 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 438 | sh_binary( |
| 439 | name = "my_target", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 440 | srcs = ["always_include.sh"], |
| 441 | deps = select({ |
| 442 | ":config1": STANDARD_DEP, |
| 443 | ":config2": STANDARD_DEP, |
| 444 | ":config3": STANDARD_DEP, |
| 445 | ":config4": [":special_lib"], |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 446 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 447 | ) |
| 448 | ``` |
| 449 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 450 | This makes it easier to manage the dependency. But it still causes unnecessary |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 451 | duplication. |
| 452 | |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 453 | For more direct support, use one of the following: |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 454 | |
| 455 | ### <a name="selects-with-or"></a>`selects.with_or` |
| 456 | |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 457 | The |
arostovtsev | 414f3c8 | 2021-04-16 11:11:39 -0700 | [diff] [blame] | 458 | [with_or](https://github.com/bazelbuild/bazel-skylib/blob/main/docs/selects_doc.md#selectswith_or) |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 459 | macro in [Skylib](https://github.com/bazelbuild/bazel-skylib)'s |
arostovtsev | 414f3c8 | 2021-04-16 11:11:39 -0700 | [diff] [blame] | 460 | [`selects`](https://github.com/bazelbuild/bazel-skylib/blob/main/docs/selects_doc.md) |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 461 | module supports `OR`ing conditions directly inside a `select`: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 462 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 463 | ```python |
cparsons | 7e10a4d | 2019-08-29 16:29:41 -0700 | [diff] [blame] | 464 | load("@bazel_skylib//lib:selects.bzl", "selects") |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 465 | ``` |
| 466 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 467 | ```python |
| 468 | sh_binary( |
| 469 | name = "my_target", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 470 | srcs = ["always_include.sh"], |
| 471 | deps = selects.with_or({ |
| 472 | (":config1", ":config2", ":config3"): [":standard_lib"], |
| 473 | ":config4": [":special_lib"], |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 474 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 475 | ) |
| 476 | ``` |
| 477 | |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 478 | ### <a name="selects-config-setting-or-group"></a>`selects.config_setting_group` |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 479 | |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 480 | |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 481 | The |
arostovtsev | 414f3c8 | 2021-04-16 11:11:39 -0700 | [diff] [blame] | 482 | [config_setting_group](https://github.com/bazelbuild/bazel-skylib/blob/main/docs/selects_doc.md#selectsconfig_setting_group) |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 483 | macro in [Skylib](https://github.com/bazelbuild/bazel-skylib)'s |
arostovtsev | 414f3c8 | 2021-04-16 11:11:39 -0700 | [diff] [blame] | 484 | [`selects`](https://github.com/bazelbuild/bazel-skylib/blob/main/docs/selects_doc.md) |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 485 | module supports `OR`ing multiple `config_setting`s: |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 486 | |
| 487 | ```python |
cparsons | 7e10a4d | 2019-08-29 16:29:41 -0700 | [diff] [blame] | 488 | load("@bazel_skylib//lib:selects.bzl", "selects") |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 489 | ``` |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 490 | |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 491 | |
| 492 | ```python |
| 493 | config_setting( |
| 494 | name = "config1", |
| 495 | values = {"cpu": "arm"}, |
| 496 | ) |
| 497 | config_setting( |
| 498 | name = "config2", |
| 499 | values = {"compilation_mode": "dbg"}, |
| 500 | ) |
| 501 | selects.config_setting_group( |
| 502 | name = "config1_or_2", |
| 503 | match_any = [":config1", ":config2"], |
| 504 | ) |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 505 | sh_binary( |
| 506 | name = "my_target", |
| 507 | srcs = ["always_include.sh"], |
| 508 | deps = select({ |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 509 | ":config1_or_2": [":standard_lib"], |
| 510 | "//conditions:default": [":other_lib"], |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 511 | }), |
| 512 | ) |
| 513 | ``` |
| 514 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 515 | Unlike `selects.with_or`, different targets can share `:config1_or_2` across |
| 516 | different attributes. |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 517 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 518 | It's an error for multiple conditions to match unless one is an unambiguous |
| 519 | "specialization" of the others. See [here](#example) for details. |
Greg Estren | 016e217 | 2019-01-29 13:28:48 -0800 | [diff] [blame] | 520 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 521 | ## AND chaining |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 522 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 523 | If you need a `select` branch to match when multiple conditions match, use the |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 524 | [Skylib](https://github.com/bazelbuild/bazel-skylib) macro |
arostovtsev | 414f3c8 | 2021-04-16 11:11:39 -0700 | [diff] [blame] | 525 | [config_setting_group](https://github.com/bazelbuild/bazel-skylib/blob/main/docs/selects_doc.md#selectsconfig_setting_group): |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 526 | |
| 527 | ```python |
| 528 | config_setting( |
| 529 | name = "config1", |
| 530 | values = {"cpu": "arm"}, |
| 531 | ) |
| 532 | config_setting( |
| 533 | name = "config2", |
| 534 | values = {"compilation_mode": "dbg"}, |
| 535 | ) |
| 536 | selects.config_setting_group( |
| 537 | name = "config1_and_2", |
| 538 | match_all = [":config1", ":config2"], |
| 539 | ) |
| 540 | sh_binary( |
| 541 | name = "my_target", |
| 542 | srcs = ["always_include.sh"], |
| 543 | deps = select({ |
| 544 | ":config1_and_2": [":standard_lib"], |
| 545 | "//conditions:default": [":other_lib"], |
Matt Mackay | 552f0e72 | 2020-11-12 20:01:36 -0800 | [diff] [blame] | 546 | }), |
Greg Estren | ed0f7d0 | 2019-07-17 13:07:25 -0700 | [diff] [blame] | 547 | ) |
| 548 | ``` |
| 549 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 550 | Unlike OR chaining, existing `config_setting`s can't be directly `AND`ed |
| 551 | inside a `select`. You have to explicitly wrap them in a `config_setting_group`. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 552 | |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 553 | ## Custom error messages |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 554 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 555 | By default, when no condition matches, the target the `select()` is attached to |
| 556 | fails with the error: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 557 | |
| 558 | ```sh |
| 559 | ERROR: Configurable attribute "deps" doesn't match this configuration (would |
| 560 | a default condition help?). |
| 561 | Conditions checked: |
| 562 | //tools/cc_target_os:darwin |
| 563 | //tools/cc_target_os:android |
| 564 | ``` |
| 565 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 566 | This can be customized with the [`no_match_error`](be/functions.html#select) |
| 567 | attribute: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 568 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 569 | ```python |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 570 | cc_library( |
| 571 | name = "my_lib", |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 572 | deps = select( |
| 573 | { |
| 574 | "//tools/cc_target_os:android": [":android_deps"], |
| 575 | "//tools/cc_target_os:windows": [":windows_deps"], |
| 576 | }, |
| 577 | no_match_error = "Please build with an Android or Windows toolchain", |
| 578 | ), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 579 | ) |
| 580 | ``` |
| 581 | |
| 582 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 583 | $ bazel build //myapp:my_lib |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 584 | ERROR: Configurable attribute "deps" doesn't match this configuration: Please |
| 585 | build with an Android or Windows toolchain |
| 586 | ``` |
| 587 | |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 588 | ## <a name="rules"></a>Rules compatibility |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 589 | Rule implementations receive the *resolved values* of configurable |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 590 | attributes. For example, given: |
| 591 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 592 | ```python |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 593 | # myapp/BUILD |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 594 | |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 595 | some_rule( |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 596 | name = "my_target", |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 597 | some_attr = select({ |
| 598 | ":foo_mode": [":foo"], |
| 599 | ":bar_mode": [":bar"], |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 600 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 601 | ) |
| 602 | ``` |
| 603 | |
| 604 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 605 | $ bazel build //myapp/my_target --define mode=foo |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 606 | ``` |
| 607 | |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 608 | Rule implementation code sees `ctx.attr.some_attr` as `[":foo"]`. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 609 | |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 610 | Macros can accept `select()` clauses and pass them through to native |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 611 | rules. But *they cannot directly manipulate them*. For example, there's no way |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 612 | for a macro to convert |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 613 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 614 | ```python |
| 615 | select({"foo": "val"}, ...) |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 616 | ``` |
| 617 | |
| 618 | to |
| 619 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 620 | ```python |
| 621 | select({"foo": "val_with_suffix"}, ...) |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 622 | ``` |
| 623 | |
| 624 | This is for two reasons. |
| 625 | |
| 626 | First, macros that need to know which path a `select` will choose *cannot work* |
glukasiknuro | 61d663a | 2020-05-27 11:48:16 -0700 | [diff] [blame] | 627 | because macros are evaluated in Bazel's [loading phase](guide.html#loading-phase), |
laurentlb | 79b9431 | 2018-11-07 05:18:59 -0800 | [diff] [blame] | 628 | which occurs before flag values are known. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 629 | This is a core Bazel design restriction that's unlikely to change any time soon. |
| 630 | |
| 631 | Second, macros that just need to iterate over *all* `select` paths, while |
| 632 | technically feasible, lack a coherent UI. Further design is necessary to change |
| 633 | this. |
| 634 | |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 635 | ## <a name="query"></a>Bazel query and cquery |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 636 | Bazel [`query`](query-how-to.html) operates over Bazel's |
aiuto | 4e9a77e | 2021-06-15 08:55:11 -0700 | [diff] [blame] | 637 | [loading phase](https://docs.bazel.build/versions/main/glossary.html#loading-phase). |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 638 | This means it doesn't know what command line flags a target uses since those |
| 639 | flags aren't evaluated until later in the build (in the |
aiuto | 4e9a77e | 2021-06-15 08:55:11 -0700 | [diff] [blame] | 640 | [analysis phase](https://docs.bazel.build/versions/main/glossary.html#analysis-phase)). |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 641 | So it can't determine which `select()` branches are chosen. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 642 | |
Grzegorz Lukasik | a804818 | 2021-03-17 07:28:58 -0700 | [diff] [blame] | 643 | Bazel [`cquery`](cquery.html) operates after Bazel's analysis phase, so it has |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 644 | all this information and can accurately resolve `select()`s. |
| 645 | |
| 646 | Consider: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 647 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 648 | ```python |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 649 | load("@bazel_skylib//rules:common_settings.bzl", "string_flag") |
| 650 | ``` |
| 651 | ```python |
| 652 | # myapp/BUILD |
| 653 | |
| 654 | string_flag( |
| 655 | name = "dog_type", |
| 656 | build_setting_default = "cat" |
| 657 | ) |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 658 | |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 659 | cc_library( |
| 660 | name = "my_lib", |
| 661 | deps = select({ |
| 662 | ":long": [":foo_dep"], |
| 663 | ":short": [":bar_dep"], |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 664 | }), |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 665 | ) |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 666 | |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 667 | config_setting( |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 668 | name = "long", |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 669 | flag_values = {":dog_type": "dachshund"}, |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 670 | ) |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 671 | |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 672 | config_setting( |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 673 | name = "short", |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 674 | flag_values = {":dog_type": "pug"}, |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 675 | ) |
| 676 | ``` |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 677 | |
Gautam Korlam | 82552dd | 2021-01-05 22:08:27 -0800 | [diff] [blame] | 678 | `query` overapproximates `:my_lib`'s dependencies: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 679 | |
| 680 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 681 | $ bazel query 'deps(//myapp:my_lib)' |
| 682 | //myapp:my_lib |
| 683 | //myapp:foo_dep |
| 684 | //myapp:bar_dep |
| 685 | ``` |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 686 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 687 | while `cquery` shows its exact dependencies: |
| 688 | |
| 689 | ```sh |
| 690 | $ bazel cquery 'deps(//myapp:my_lib)' --//myapp:dog_type=pug |
| 691 | //myapp:my_lib |
| 692 | //myapp:bar_dep |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 693 | ``` |
| 694 | |
| 695 | ## FAQ |
| 696 | |
Greg Estren | e821cfa | 2020-02-20 09:34:56 -0800 | [diff] [blame] | 697 | ### <a name="macros-select"></a>Why doesn't select() work in macros? |
laurentlb | 6b6b84a | 2018-10-05 14:14:29 -0700 | [diff] [blame] | 698 | select() *does* work in rules! See [Rules compatibility](#rules) for |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 699 | details. |
| 700 | |
| 701 | The key issue this question usually means is that select() doesn't work in |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 702 | *macros*. These are different than *rules*. See the |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 703 | documentation on [rules](skylark/rules.html) and [macros](skylark/macros.html) |
| 704 | to understand the difference. |
| 705 | Here's an end-to-end example: |
| 706 | |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 707 | Define a rule and macro: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 708 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 709 | ```python |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 710 | # myapp/defs.bzl |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 711 | |
| 712 | # Rule implementation: when an attribute is read, all select()s have already |
| 713 | # been resolved. So it looks like a plain old attribute just like any other. |
| 714 | def _impl(ctx): |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 715 | name = ctx.attr.name |
| 716 | allcaps = ctx.attr.my_config_string.upper() # This works fine on all values. |
| 717 | print("My name is " + name + " with custom message: " + allcaps) |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 718 | |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 719 | # Rule declaration: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 720 | my_custom_bazel_rule = rule( |
| 721 | implementation = _impl, |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 722 | attrs = {"my_config_string": attr.string()}, |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 723 | ) |
| 724 | |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 725 | # Macro declaration: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 726 | def my_custom_bazel_macro(name, my_config_string): |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 727 | allcaps = my_config_string.upper() # This line won't work with select(s). |
| 728 | print("My name is " + name + " with custom message: " + allcaps) |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 729 | ``` |
| 730 | |
| 731 | Instantiate the rule and macro: |
| 732 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 733 | ```python |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 734 | # myapp/BUILD |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 735 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 736 | load("//myapp:defs.bzl", "my_custom_bazel_rule") |
| 737 | load("//myapp:defs.bzl", "my_custom_bazel_macro") |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 738 | |
| 739 | my_custom_bazel_rule( |
| 740 | name = "happy_rule", |
| 741 | my_config_string = select({ |
| 742 | "//tools/target_cpu:x86": "first string", |
| 743 | "//tools/target_cpu:ppc": "second string", |
| 744 | }), |
| 745 | ) |
| 746 | |
| 747 | my_custom_bazel_macro( |
| 748 | name = "happy_macro", |
| 749 | my_config_string = "fixed string", |
| 750 | ) |
| 751 | |
| 752 | my_custom_bazel_macro( |
| 753 | name = "sad_macro", |
| 754 | my_config_string = select({ |
| 755 | "//tools/target_cpu:x86": "first string", |
| 756 | "//tools/target_cpu:ppc": "other string", |
| 757 | }), |
| 758 | ) |
| 759 | ``` |
| 760 | |
| 761 | Building fails because `sad_macro` can't process the `select()`: |
| 762 | |
| 763 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 764 | $ bazel build //myapp:all |
| 765 | ERROR: /myworkspace/myapp/BUILD:17:1: Traceback |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 766 | (most recent call last): |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 767 | File "/myworkspace/myapp/BUILD", line 17 |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 768 | my_custom_bazel_macro(name = "sad_macro", my_config_stri..."})) |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 769 | File "/myworkspace/myapp/defs.bzl", line 4, in |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 770 | my_custom_bazel_macro |
| 771 | my_config_string.upper() |
| 772 | type 'select' has no method upper(). |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 773 | ERROR: error loading package 'myapp': Package 'myapp' contains errors. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 774 | ``` |
| 775 | |
Googler | a1a29ec | 2021-02-26 12:19:25 -0800 | [diff] [blame] | 776 | Building succeeds when you comment out `sad_macro`: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 777 | |
| 778 | ```sh |
| 779 | # Comment out sad_macro so it doesn't mess up the build. |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 780 | $ bazel build //myapp:all |
| 781 | DEBUG: /myworkspace/myapp/defs.bzl:5:3: My name is happy_macro with custom message: FIXED STRING. |
| 782 | DEBUG: /myworkspace/myapp/hi.bzl:15:3: My name is happy_rule with custom message: FIRST STRING. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 783 | ``` |
| 784 | |
| 785 | This is impossible to change because *by definition* macros are evaluated before |
| 786 | Bazel reads the build's command line flags. That means there isn't enough |
| 787 | information to evaluate select()s. |
| 788 | |
| 789 | Macros can, however, pass `select()`s as opaque blobs to rules: |
| 790 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 791 | ```python |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 792 | # myapp/defs.bzl |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 793 | |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 794 | def my_custom_bazel_macro(name, my_config_string): |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 795 | print("Invoking macro " + name) |
| 796 | my_custom_bazel_rule( |
| 797 | name = name + "_as_target", |
| 798 | my_config_string = my_config_string, |
| 799 | ) |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 800 | ``` |
| 801 | |
| 802 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 803 | $ bazel build //myapp:sad_macro_less_sad |
| 804 | DEBUG: /myworkspace/myapp/defs.bzl:23:3: Invoking macro sad_macro_less_sad. |
| 805 | DEBUG: /myworkspace/myapp/defs.bzl:15:3: My name is sad_macro_less_sad with custom message: FIRST STRING. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 806 | ``` |
| 807 | |
Greg Estren | e821cfa | 2020-02-20 09:34:56 -0800 | [diff] [blame] | 808 | ### <a name="boolean-select"></a>Why does select() always return true? |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 809 | Because *macros* (but not rules) by definition |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 810 | [can't evaluate `select()`s](#macros-select), any attempt to do so |
Ray Loyzaga | ca59146 | 2020-01-02 06:26:58 -0800 | [diff] [blame] | 811 | usually produces an error: |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 812 | |
| 813 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 814 | ERROR: /myworkspace/myapp/BUILD:17:1: Traceback |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 815 | (most recent call last): |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 816 | File "/myworkspace/myapp/BUILD", line 17 |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 817 | my_custom_bazel_macro(name = "sad_macro", my_config_stri..."})) |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 818 | File "/myworkspace/myapp/defs.bzl", line 4, in |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 819 | my_custom_bazel_macro |
| 820 | my_config_string.upper() |
| 821 | type 'select' has no method upper(). |
| 822 | ``` |
| 823 | |
| 824 | Booleans are a special case that fail silently, so you should be particularly |
| 825 | vigilant with them: |
| 826 | |
| 827 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 828 | $ cat myapp/defs.bzl |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 829 | def my_boolean_macro(boolval): |
| 830 | print("TRUE" if boolval else "FALSE") |
| 831 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 832 | $ cat myapp/BUILD |
| 833 | load("//myapp:defs.bzl", "my_boolean_macro") |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 834 | my_boolean_macro( |
| 835 | boolval = select({ |
| 836 | "//tools/target_cpu:x86": True, |
| 837 | "//tools/target_cpu:ppc": False, |
| 838 | }), |
| 839 | ) |
| 840 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 841 | $ bazel build //myapp:all --cpu=x86 |
| 842 | DEBUG: /myworkspace/myapp/defs.bzl:4:3: TRUE. |
| 843 | $ bazel build //mypro:all --cpu=ppc |
| 844 | DEBUG: /myworkspace/myapp/defs.bzl:4:3: TRUE. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 845 | ``` |
| 846 | |
laurentlb | dc702ce | 2018-08-17 06:52:20 -0700 | [diff] [blame] | 847 | This happens because macros don't understand the contents of `select()`. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 848 | So what they're really evaluting is the `select()` object itself. According to |
| 849 | [Pythonic](https://docs.python.org/release/2.5.2/lib/truth.html) design |
| 850 | standards, all objects aside from a very small number of exceptions |
| 851 | automatically return true. |
Greg Estren | e821cfa | 2020-02-20 09:34:56 -0800 | [diff] [blame] | 852 | ### <a name="inspectable-select"></a>Can I read select() like a dict? |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 853 | Macros [can't](#macros-select) evaluate select(s) because macros evaluate before |
| 854 | Bazel knows what the build's command line parameters are. Can they at least read |
| 855 | the `select()`'s dictionary to, for example, add a suffix to each value? |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 856 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 857 | Conceptually this is possible, but it isn't yet a Bazel feature. |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 858 | What you *can* do today is prepare a straight dictionary, then feed it into a |
| 859 | `select()`: |
| 860 | |
| 861 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 862 | $ cat myapp/defs.bzl |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 863 | def selecty_genrule(name, select_cmd): |
| 864 | for key in select_cmd.keys(): |
| 865 | select_cmd[key] += " WITH SUFFIX" |
| 866 | native.genrule( |
| 867 | name = name, |
| 868 | outs = [name + ".out"], |
| 869 | srcs = [], |
| 870 | cmd = "echo " + select(select_cmd + {"//conditions:default": "default"}) |
| 871 | + " > $@" |
| 872 | ) |
| 873 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 874 | $ cat myapp/BUILD |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 875 | selecty_genrule( |
| 876 | name = "selecty", |
| 877 | select_cmd = { |
| 878 | "//tools/target_cpu:x86": "x86 mode", |
| 879 | }, |
| 880 | ) |
| 881 | |
| 882 | $ bazel build //testapp:selecty --cpu=x86 && cat bazel-genfiles/testapp/selecty.out |
| 883 | x86 mode WITH SUFFIX |
| 884 | ``` |
| 885 | |
| 886 | If you'd like to support both `select()` and native types, you can do this: |
| 887 | |
| 888 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 889 | $ cat myapp/defs.bzl |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 890 | def selecty_genrule(name, select_cmd): |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 891 | cmd_suffix = "" |
| 892 | if type(select_cmd) == "string": |
| 893 | cmd_suffix = select_cmd + " WITH SUFFIX" |
| 894 | elif type(select_cmd) == "dict": |
| 895 | for key in select_cmd.keys(): |
| 896 | select_cmd[key] += " WITH SUFFIX" |
| 897 | cmd_suffix = select(select_cmd + {"//conditions:default": "default"}) |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 898 | |
brandjon | eadd5f6 | 2018-10-02 12:05:44 -0700 | [diff] [blame] | 899 | native.genrule( |
| 900 | name = name, |
| 901 | outs = [name + ".out"], |
| 902 | srcs = [], |
| 903 | cmd = "echo " + cmd_suffix + "> $@", |
| 904 | ) |
gregce | c44a87b | 2018-06-08 12:07:03 -0700 | [diff] [blame] | 905 | ``` |
John Cater | 7780b75 | 2019-01-16 12:14:35 -0800 | [diff] [blame] | 906 | |
Greg Estren | e821cfa | 2020-02-20 09:34:56 -0800 | [diff] [blame] | 907 | ### <a name="bind-select"></a>Why doesn't select() work with bind()? |
John Cater | 7780b75 | 2019-01-16 12:14:35 -0800 | [diff] [blame] | 908 | |
| 909 | Because [`bind()`](be/workspace.html#bind) is a WORKSPACE rule, not a BUILD rule. |
| 910 | |
| 911 | Workspace rules do not have a specific configuration, and aren't evaluated in |
| 912 | the same way as BUILD rules. Therefore, a `select()` in a `bind()` can't |
| 913 | actually evaluate to any specific branch. |
| 914 | |
| 915 | Instead, you should use [`alias()`](be/general.html#alias), with a `select()` in |
| 916 | the `actual` attribute, to perform this type of run-time determination. This |
| 917 | works correctly, since `alias()` is a BUILD rule, and is evaluated with a |
| 918 | specific configuration. |
| 919 | |
| 920 | You can even have a `bind()` target point to an `alias()`, if needed. |
| 921 | |
| 922 | ```sh |
| 923 | $ cat WORKSPACE |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 924 | workspace(name = "myapp") |
John Cater | 7780b75 | 2019-01-16 12:14:35 -0800 | [diff] [blame] | 925 | bind(name = "openssl", actual = "//:ssl") |
| 926 | http_archive(name = "alternative", ...) |
| 927 | http_archive(name = "boringssl", ...) |
| 928 | |
| 929 | $ cat BUILD |
| 930 | config_setting( |
| 931 | name = "alt_ssl", |
| 932 | define_values = { |
| 933 | "ssl_library": "alternative", |
| 934 | }, |
| 935 | ) |
| 936 | |
| 937 | alias( |
| 938 | name = "ssl", |
| 939 | actual = select({ |
| 940 | "//:alt_ssl": "@alternative//:ssl", |
| 941 | "//conditions:default": "@boringssl//:ssl", |
| 942 | }), |
| 943 | ) |
| 944 | ``` |
| 945 | |
| 946 | With this setup, you can pass `--define ssl_library=alternative`, and any target |
| 947 | that depends on either `//:ssl` or `//external:ssl` will see the alternative |
| 948 | located at `@alternative//:ssl`. |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 949 | |
| 950 | ### Why doesn't my select() choose what I expect? |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 951 | If `//myapp:foo` has a `select()` that doesn't choose the condition you expect, |
| 952 | use [cquery](cquery.html) and `bazel config` to debug: |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 953 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 954 | If `//myapp:foo` is the top-level target you're building, run: |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 955 | |
| 956 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 957 | $ bazel cquery //myapp:foo <desired build flags> |
| 958 | //myapp:foo (12e23b9a2b534a) |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 959 | ``` |
| 960 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 961 | If you're building some other target `//bar` that depends on |
| 962 | //myapp:foo somewhere in its subgraph, run: |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 963 | |
| 964 | ```sh |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 965 | $ bazel cquery 'somepath(//bar, //myapp:foo)' <desired build flags> |
| 966 | //bar:bar (3ag3193fee94a2) |
| 967 | //bar:intermediate_dep (12e23b9a2b534a) |
| 968 | //myapp:foo (12e23b9a2b534a) |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 969 | ``` |
| 970 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 971 | The `(12e23b9a2b534a)` next to `//myapp:foo` is a *hash* of the |
| 972 | configuration that resolves `//myapp:foo`'s `select()`. You can inspect its |
| 973 | values with `bazel config`: |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 974 | |
| 975 | ```sh |
| 976 | $ bazel config 12e23b9a2b534a |
| 977 | BuildConfiguration 12e23b9a2b534a |
| 978 | Fragment com.google.devtools.build.lib.analysis.config.CoreOptions { |
| 979 | cpu: darwin |
| 980 | compilation_mode: fastbuild |
| 981 | ... |
| 982 | } |
| 983 | Fragment com.google.devtools.build.lib.rules.cpp.CppOptions { |
| 984 | linkopt: [-Dfoo=bar] |
| 985 | ... |
| 986 | } |
| 987 | ... |
| 988 | ``` |
| 989 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 990 | Then compare this output against the settings expected by each `config_setting`. |
gregce | 8d55178 | 2020-04-16 11:18:15 -0700 | [diff] [blame] | 991 | |
gregce | 8e8dcc8 | 2020-08-25 08:30:28 -0700 | [diff] [blame] | 992 | `//myapp:foo` may exist in different configurations in the same build. See the |
| 993 | [cquery docs](cquery.html) for guidance on using `somepath` to get the right |
| 994 | one. |
juliexxia | d3f0e01 | 2020-10-08 14:01:37 -0700 | [diff] [blame] | 995 | |
| 996 | Caution: To prevent restarting the Bazel server, invoke `bazel config` with the |
| 997 | same command line flags as the `bazel cquery`. The `config` command relies on |
| 998 | the configuration nodes from the still-running server of the previous command. |
gregce | cd2209a | 2020-10-13 11:17:01 -0700 | [diff] [blame] | 999 | |
| 1000 | [BuildSettings]: skylark/config.html#user-defined-build-settings |