Skip to content

Instantly share code, notes, and snippets.

@siberex
Last active November 25, 2020 19:00
Show Gist options
  • Save siberex/ce6ce993474e47f3f9c9fd50b5db7ddb to your computer and use it in GitHub Desktop.
Save siberex/ce6ce993474e47f3f9c9fd50b5db7ddb to your computer and use it in GitHub Desktop.
Bazel switch targets by platform
# 3.8+
# https://docs.bazel.build/versions/master/platforms.html#skipping-incompatible-targets
# https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
# Pre 3.8:
#
# Bazel Platforms Cookbook:
# https://docs.google.com/document/d/1UZaVcL08wePB41ATZHcxQV4Pu1YfA1RvvWm8FbZHuW8/edit#
#
# Use this only for simple cases like skipping Windows-only targets on Linux CI.
# Read Description!
# https://github.com/hlopko/bazel_platforms_examples/tree/master/examples/05_select_on_platform
rule_binary(
name = "my_binary_windows",
# ...
)
rule_binary(
name = "my_binary_macos",
# ...
)
rule_binary(
name = "my_binary_linux",
# ...
)
config_setting(
name = "is_windows",
constraint_values = [
"@platforms//os:windows"
],
)
config_setting(
name = "is_macos",
constraint_values = [
"@platforms//os:macos"
],
)
config_setting(
name = "is_linux",
constraint_values = [
"@platforms//os:linux"
],
)
filegroup(
name = "empty"
)
alias(
name = "my_binary",
actual = select({
":is_windows": ":my_binary_windows",
":is_macos": ":my_binary_macos",
":is_linux": ":my_binary_linux",
# Skip entirely for other platforms
"//conditions:default": ":empty",
}),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment