From 784ba9f8e9bf049d9389b9ce377c8f078a30f3de Mon Sep 17 00:00:00 2001 From: Philipp Schrader Date: Tue, 15 Dec 2020 13:18:21 -0800 Subject: [PATCH] Update docs and tests to use the @platforms//:incompatible constraint With e03cb63e059420847d6578d7cbfe93f05615c95e 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 --- site/docs/platforms.md | 39 +++++++------------ .../target_compatible_with_test.sh | 2 + ...rget_compatible_with_test_external_repo.sh | 7 ---- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/site/docs/platforms.md b/site/docs/platforms.md index 9b307085e41cba..4df491864c7b4a 100644 --- a/site/docs/platforms.md +++ b/site/docs/platforms.md @@ -168,25 +168,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( @@ -195,18 +186,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 @@ -220,7 +211,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": [], ], ) diff --git a/src/test/shell/integration/target_compatible_with_test.sh b/src/test/shell/integration/target_compatible_with_test.sh index fd14f2ac9452bd..b79f1ba5a869d9 100755 --- a/src/test/shell/integration/target_compatible_with_test.sh +++ b/src/test/shell/integration/target_compatible_with_test.sh @@ -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( diff --git a/src/test/shell/integration/target_compatible_with_test_external_repo.sh b/src/test/shell/integration/target_compatible_with_test_external_repo.sh index 60d6b64648c877..94464946518245 100755 --- a/src/test/shell/integration/target_compatible_with_test_external_repo.sh +++ b/src/test/shell/integration/target_compatible_with_test_external_repo.sh @@ -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(