Skip to content

Commit

Permalink
Update docs and tests to use the @platforms//:incompatible constraint
Browse files Browse the repository at this point in the history
With e03cb63 bazel now distributes
the 0.0.2 release of bazelbuild/platforms. This lets us use the
`@platforms//:incompatible` constraint instead of setting up our own.

This patch updates the docs and the relevant tests to use the commonly
available constraint.

Closes #12690.

PiperOrigin-RevId: 347678913
  • Loading branch information
philsc authored and copybara-github committed Dec 15, 2020
1 parent 7428006 commit 2eb1bf5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
39 changes: 15 additions & 24 deletions site/docs/platforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,16 @@ FAILED: Build did NOT complete successfully

### More expressive constraints

For more flexibility in expressing constraints, create a user-defined
For more flexibility in expressing constraints, use the
`@platforms//:incompatible`
[`constraint_value`](platform.html#constraint_value) that no platform
satisfies. For example, Put the following somewhere in your project and change
`//:not_compatible` in the subsequent examples to match your location.
satisfies.

```python
constraint_setting(name = "not_compatible_setting")

constraint_value(
name = "not_compatible",
constraint_setting = ":not_compatible_setting",
)
```

Use [`select()`](functions.html#select) in combination with `:not_compatible`
to express more complicated restrictions. For example, use it to implement
basic OR logic. The following marks a library compatible with macOS and Linux,
but no other platforms. Note that an empty constraints list is equivalent to
"compatible with everything".
Use [`select()`](functions.html#select) in combination with
`@platforms//:incompatible` to express more complicated restrictions. For
example, use it to implement basic OR logic. The following marks a library
compatible with macOS and Linux, but no other platforms. Note that an empty
constraints list is equivalent to "compatible with everything".

```python
cc_library(
Expand All @@ -196,18 +187,18 @@ cc_library(
target_compatible_with = select({
"@platforms//os:osx": [],
"@platforms//os:linux": [],
"//conditions:default": ["//:not_compatible"],
"//conditions:default": ["@platforms//:incompatible"],
],
)
```

The above can be interpreted as follows:

1. If we are targeting macOS, then this target has no constraints.
2. If we are targeting Linux, then this target has no constraints.
3. Otherwise the target has the `:not_compatible` constraint. Because
`:not_compatible` is not part of any platforms, the target is deemed
incompatible.
1. When targeting macOS, the target has no constraints.
2. When targeting Linux, the target has no constraints.
3. Otherwise, the target has the `@platforms//:incompatible` constraint. Because
`@platforms//:incompatible` is not part of any platform, the target is
deemed incompatible.

To make your constraints more readable, use
[skylib](https://github.com/bazelbuild/bazel-skylib)'s
Expand All @@ -221,7 +212,7 @@ cc_library(
name = "non_arm_lib",
srcs = "non_arm_lib.cc",
target_compatible_with = select({
"@platforms//cpu:arm": ["//:not_compatible"],
"@platforms//cpu:arm": ["@platforms//:incompatible"],
"//conditions:default": [],
],
)
Expand Down
2 changes: 2 additions & 0 deletions src/test/shell/integration/target_compatible_with_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ EOF
# We're not validating visibility here. Let everything access these targets.
package(default_visibility = ["//visibility:public"])
# TODO(philsc): Get rid of this and use @platforms//:incompatible instead.
# Right now it's problematic because Google CI doesn't support @platforms.
constraint_setting(name = "not_compatible_setting")
constraint_value(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ EOF
# We're not validating visibility here. Let everything access these targets.
package(default_visibility = ["//visibility:public"])
constraint_setting(name = "not_compatible_setting")
constraint_value(
name = "not_compatible",
constraint_setting = ":not_compatible_setting",
)
constraint_setting(name = "foo_version")
constraint_value(
Expand Down

0 comments on commit 2eb1bf5

Please sign in to comment.