-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement cc_feature_set and cc_feature_constraint. END_PUBLIC PiperOrigin-RevId: 610713183 Change-Id: Ia009ac536b71cd9aa44578f823f13361c1580e37
- Loading branch information
1 parent
2b6cdcf
commit db08757
Showing
6 changed files
with
216 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Implementation of the cc_feature_constraint rule.""" | ||
|
||
load( | ||
"//cc/toolchains/impl:collect.bzl", | ||
"collect_features", | ||
"collect_provider", | ||
) | ||
load("//cc/toolchains/impl:features_attr.bzl", "disallow_features_attr") | ||
load( | ||
":cc_toolchain_info.bzl", | ||
"FeatureConstraintInfo", | ||
"FeatureSetInfo", | ||
) | ||
|
||
def _cc_feature_constraint_impl(ctx): | ||
all_of = collect_provider(ctx.attr.all_of, FeatureConstraintInfo) | ||
none_of = [collect_features(ctx.attr.none_of)] | ||
none_of.extend([fc.none_of for fc in all_of]) | ||
return [FeatureConstraintInfo( | ||
label = ctx.label, | ||
all_of = depset(transitive = [fc.all_of for fc in all_of]), | ||
none_of = depset(transitive = none_of), | ||
)] | ||
|
||
_cc_feature_constraint = rule( | ||
implementation = _cc_feature_constraint_impl, | ||
attrs = { | ||
"all_of": attr.label_list( | ||
providers = [FeatureConstraintInfo], | ||
), | ||
"none_of": attr.label_list( | ||
providers = [FeatureSetInfo], | ||
), | ||
}, | ||
provides = [FeatureConstraintInfo], | ||
doc = """Defines a constraint on features. | ||
Can be used with require_any_of to specify that something is only enabled when | ||
a constraint is met.""", | ||
) | ||
cc_feature_constraint = disallow_features_attr(_cc_feature_constraint) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Implementation of the cc_feature_set rule.""" | ||
|
||
load("//cc/toolchains/impl:collect.bzl", "collect_features") | ||
load("//cc/toolchains/impl:features_attr.bzl", "require_features_attr") | ||
load( | ||
":cc_toolchain_info.bzl", | ||
"FeatureConstraintInfo", | ||
"FeatureSetInfo", | ||
) | ||
|
||
def _cc_feature_set_impl(ctx): | ||
features = collect_features(ctx.attr.features_) | ||
return [ | ||
FeatureSetInfo(label = ctx.label, features = features), | ||
FeatureConstraintInfo( | ||
label = ctx.label, | ||
all_of = features, | ||
none_of = depset([]), | ||
), | ||
] | ||
|
||
_cc_feature_set = rule( | ||
implementation = _cc_feature_set_impl, | ||
attrs = { | ||
"features_": attr.label_list( | ||
providers = [FeatureSetInfo], | ||
doc = "A set of features", | ||
), | ||
}, | ||
provides = [FeatureSetInfo], | ||
doc = """Defines a set of features. | ||
Example: | ||
cc_feature_set( | ||
name = "thin_lto_requirements", | ||
all_of = [ | ||
":thin_lto", | ||
":opt", | ||
], | ||
) | ||
""", | ||
) | ||
cc_feature_set = require_features_attr(_cc_feature_set) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Helpers for dealing with the fact that features is a reserved attribute.""" | ||
|
||
# buildifier: disable=unnamed-macro | ||
def disallow_features_attr(rule): | ||
def rule_wrapper(*, name, **kwargs): | ||
if "features" in kwargs: | ||
fail("Cannot use features in %s" % native.package_relative_label(name)) | ||
rule(name = name, **kwargs) | ||
|
||
return rule_wrapper | ||
|
||
def require_features_attr(rule): | ||
def rule_wrapper(*, name, features, **kwargs): | ||
rule(name = name, features_ = features, **kwargs) | ||
|
||
return rule_wrapper |
This file contains 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
This file contains 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
This file contains 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