-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
swift_feature_allowlist
rule that lets toolchains control w…
…hich packages are allowed to enable/disable specific features. PiperOrigin-RevId: 375484553
- Loading branch information
1 parent
05f18b8
commit 407b1c8
Showing
7 changed files
with
292 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Copyright 2021 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. | ||
|
||
"""Support for restricting access to features based on an allowlist.""" | ||
|
||
load(":providers.bzl", "SwiftAllowlistPackageInfo", "SwiftFeatureAllowlistInfo") | ||
|
||
def _parse_allowlist_package(package_spec): | ||
"""Parses an allowlist package specification from a string. | ||
Args: | ||
package_spec: A string that represents a possibly recursive package | ||
specification, with an optional exclusion marker in front. | ||
Returns: | ||
An instance of `SwiftAllowlistPackageInfo` containing the parsed | ||
information from the package specification. | ||
""" | ||
if package_spec.startswith("-"): | ||
excluded = True | ||
package_spec = package_spec[1:] | ||
else: | ||
excluded = False | ||
|
||
if package_spec.endswith("/..."): | ||
match_subpackages = True | ||
package_spec = package_spec[:-4] | ||
else: | ||
match_subpackages = False | ||
|
||
return SwiftAllowlistPackageInfo( | ||
excluded = excluded, | ||
match_subpackages = match_subpackages, | ||
package = package_spec, | ||
) | ||
|
||
def _swift_feature_allowlist_impl(ctx): | ||
return [SwiftFeatureAllowlistInfo( | ||
allowlist_label = str(ctx.label), | ||
managed_features = ctx.attr.managed_features, | ||
packages = [ | ||
_parse_allowlist_package(package_spec) | ||
for package_spec in ctx.attr.packages | ||
], | ||
)] | ||
|
||
swift_feature_allowlist = rule( | ||
attrs = { | ||
"managed_features": attr.string_list( | ||
allow_empty = True, | ||
doc = """\ | ||
A list of feature strings that are permitted to be specified by the targets in | ||
the packages matched by the `packages` attribute. This list may include both | ||
feature names and/or negations (a name with a leading `-`); a regular feature | ||
name means that the targets in the matching packages may explicitly request that | ||
the feature be enabled, and a negated feature means that the target may | ||
explicitly request that the feature be disabled. | ||
For example, `managed_features = ["foo", "-bar"]` means that targets in the | ||
allowlist's packages may request that feature `"foo"` be enabled and that | ||
feature `"bar"` be disabled. | ||
""", | ||
mandatory = False, | ||
), | ||
"packages": attr.string_list( | ||
allow_empty = True, | ||
doc = """\ | ||
A list of strings representing packages (possibly recursive) whose targets are | ||
allowed to enable/disable the features in `managed_features`. Each package | ||
pattern is written in the syntax used by the `package_group` function: | ||
* `//foo/bar`: Targets in the package `//foo/bar` but not in subpackages. | ||
* `//foo/bar/...`: Targets in the package `//foo/bar` and any of its | ||
subpackages. | ||
* A leading `-` excludes packages that would otherwise have been included by | ||
the patterns in the list. | ||
Exclusions always take priority over inclusions; order in the list is | ||
irrelevant. | ||
""", | ||
mandatory = True, | ||
), | ||
}, | ||
doc = """\ | ||
Limits the ability to request or disable certain features to a set of packages | ||
(and possibly subpackages) in the workspace. | ||
A Swift toolchain target can reference any number (zero or more) of | ||
`swift_feature_allowlist` targets. The features managed by these allowlists may | ||
overlap. For some package _P_, a feature is allowed to be used by targets in | ||
that package if _P_ matches the `packages` patterns in *all* of the allowlists | ||
that manage that feature. | ||
A feature that is not managed by any allowlist is allowed to be used by any | ||
package. | ||
""", | ||
implementation = _swift_feature_allowlist_impl, | ||
) |
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
407b1c8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#627