Skip to content

Commit

Permalink
Add additional docs and warnings about using bind() and select().
Browse files Browse the repository at this point in the history
Fixes #6882.

Closes #7148.

PiperOrigin-RevId: 229602681
  • Loading branch information
katre authored and Copybara-Service committed Jan 16, 2019
1 parent fcf46b2 commit 7780b75
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
46 changes: 45 additions & 1 deletion site/docs/configurable-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ title: Configurable Build Attributes
* [Rules Compatibility](#rules)
* [Bazel Query and Cquery](#query)
* [FAQ](#faq)
* [Why doesn't select() work in macros](#macros-select)
* [Why doesn't select() work in macros?](#macros-select)
* [Why does select() always return true?](#boolean-select)
* [Can I read select() like a dict?](#inspectable-select)
* [Why doesn't select() work with bind()?](#bind-select)

 

Expand Down Expand Up @@ -870,3 +871,46 @@ def selecty_genrule(name, select_cmd):
cmd = "echo " + cmd_suffix + "> $@",
)
```
## <a name="bind-select"></a>Why doesn't select() work with bind()?
Because [`bind()`](be/workspace.html#bind) is a WORKSPACE rule, not a BUILD rule.
Workspace rules do not have a specific configuration, and aren't evaluated in
the same way as BUILD rules. Therefore, a `select()` in a `bind()` can't
actually evaluate to any specific branch.
Instead, you should use [`alias()`](be/general.html#alias), with a `select()` in
the `actual` attribute, to perform this type of run-time determination. This
works correctly, since `alias()` is a BUILD rule, and is evaluated with a
specific configuration.
You can even have a `bind()` target point to an `alias()`, if needed.
```sh
$ cat WORKSPACE
workspace(name = "myproject")
bind(name = "openssl", actual = "//:ssl")
http_archive(name = "alternative", ...)
http_archive(name = "boringssl", ...)
$ cat BUILD
config_setting(
name = "alt_ssl",
define_values = {
"ssl_library": "alternative",
},
)
alias(
name = "ssl",
actual = select({
"//:alt_ssl": "@alternative//:ssl",
"//conditions:default": "@boringssl//:ssl",
}),
)
```
With this setup, you can pass `--define ssl_library=alternative`, and any target
that depends on either `//:ssl` or `//external:ssl` will see the alternative
located at `@alternative//:ssl`.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public Metadata getMetadata() {
<em><p>Warning: use of <code>bind()</code> is not recommended. See "<a
href="https://github.com/bazelbuild/bazel/issues/1952">Consider removing bind</a>" for a long
discussion of its issues and alternatives.</p></em>
<em><p>Warning: <code>select()</code> cannot be used in <code>bind()</code>. See the <a
href="../configurable-attributes.html#bind-select">Configurable Attributes FAQ</a> for
details.</p></em>
<p>Gives a target an alias in the <code>//external</code> package.</p>
Expand Down

0 comments on commit 7780b75

Please sign in to comment.