Created
March 17, 2021 00:59
-
-
Save jin/417ca4eca67636f38e4a9c248cad686a to your computer and use it in GitHub Desktop.
Configurable attributes/selects on C++ toolchain/crosstool feature flags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/toolchain_utils.bzl;l=23 | |
load("@rules_cc///tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") | |
def _crosstool_feature_flag_impl(ctx): | |
"""Rule that allows select-ing based on if a crosstool feature is enabled.""" | |
toolchain = find_cpp_toolchain(ctx) | |
feature_configuration = cc_common.configure_features( | |
ctx = ctx, | |
cc_toolchain = toolchain, | |
requested_features = ctx.features, | |
unsupported_features = ctx.disabled_features, | |
) | |
feature_enabled = cc_common.is_enabled( | |
feature_configuration = feature_configuration, | |
feature_name = ctx.attr.feature_name, | |
) | |
return [config_common.FeatureFlagInfo( | |
value = ctx.attr.enabled if feature_enabled else ctx.attr.disabled, | |
)] | |
crosstool_feature_flag = rule( | |
implementation = _crosstool_feature_flag_impl, | |
attrs = { | |
"_cc_toolchain": attr.label(default = Label("//tools/cpp:current_cc_toolchain")), | |
"feature_name": attr.string(), | |
"enabled": attr.string(), | |
"disabled": attr.string(), | |
}, | |
fragments = ["cpp"], | |
) | |
# BUILD | |
crosstool_feature_flag( | |
name = "feature_foo", | |
disabled = "no_foo", | |
enabled = "use_foo", | |
feature_name = "foo", | |
) | |
# Translate feature flag to config setting | |
config_setting( | |
name = "foo_on", | |
flag_values = { | |
":feature_foo": "use_foo", | |
# add more flag values to match | |
}, | |
) | |
config_setting( | |
name = "foo_off", | |
flag_values = { | |
":feature_foo": ":no_foo", | |
}, | |
) | |
cc_library( | |
name = "my_library", | |
linkopts = select({ | |
":foo_on": [ | |
# stuff for foo | |
], | |
"//conditions:default": [], | |
}), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment