Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ jobs:
package: "tools"
runner: "ubuntu-latest"

- name: "Bazel 8.x: @tests (Linux x64)"
bazel_version: "8.x"
package: "tests"
runner: "ubuntu-latest"

runs-on:
- "${{ matrix.runner }}"

Expand Down
10 changes: 10 additions & 0 deletions docs/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ bazel_dep(
# Always overridden to use local path.
version = "HEAD",
)
bazel_dep(
name = "package_metadata_extensions",
# Always overridden to use local path.
version = "HEAD",
)

#
# Overrides from modules from this repository.
Expand All @@ -31,6 +36,11 @@ local_path_override(
path = "../metadata",
)

local_path_override(
module_name = "package_metadata_extensions",
path = "../metadata-extensions",
)

bazel_dep(
name = "supply_chain_tools",
# Always overridden to use local path.
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
## Modules

- [@package_metadata](./metadata)
- [@package_metadata_extensions](./metadata-extensions)
- [@supply_chain_tools](./tools)
17 changes: 17 additions & 0 deletions docs/metadata-extensions/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@stardoc//stardoc:stardoc.bzl", "stardoc")

stardoc(
name = "defs",
out = "defs.generated.md",
input = "@package_metadata_extensions//:defs.bzl",
deps = [
"@package_metadata_extensions//:srcs",
],
)

diff_test(
name = "defs_test",
file1 = ":defs",
file2 = "defs.md",
)
3 changes: 3 additions & 0 deletions docs/metadata-extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Module `@package_metadata_extensions`

Extensions to [@package_metadata](../metadata) that require newer Bazel versions.
27 changes: 27 additions & 0 deletions docs/metadata-extensions/defs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->

Public API of `@package_metadata_extensions`.

<a id="package_metadata_override"></a>

## package_metadata_override

<pre>
load("@package_metadata_extensions//:defs.bzl", "package_metadata_override")

package_metadata_override(*, <a href="#package_metadata_override-name">name</a>, <a href="#package_metadata_override-metadata">metadata</a>, <a href="#package_metadata_override-targets">targets</a>, <a href="#package_metadata_override-visibility">visibility</a>)
</pre>



**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="package_metadata_override-name"></a>name | A unique name for this macro instance. Normally, this is also the name for the macro's main or only target. The names of any other targets that this macro might create will be this name with a string suffix. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="package_metadata_override-metadata"></a>metadata | A `package_metadata` target to use for the provided packages.<br><br>This overrides any `package_metadata` directly declared by the packages. | <a href="https://bazel.build/concepts/labels">Label</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | |
| <a id="package_metadata_override-targets"></a>targets | A list of targets the override applies to.<br><br>This allows passing targets of the form<br><br> - `//foo/...` matches all targets under `//foo` in the current repository. - `//foo` matches all targets in `//foo` (but not in subpackages). - `//foo:bar` matches exactly target `//foo:bar`.<br><br>This also supports referencing packages and targets in external repositories.<br><br> - `@other_repo//foo/...` matches all targets under `//foo` in `other_repo`. - `@other_repo//foo` matches all targets in `//foo` in `other_repo` (but not in subpackages). - `@other_repo//foo:bar` matches exactly target `//foo:bar` in `other_repo`. | List of strings; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | |
| <a id="package_metadata_override-visibility"></a>visibility | The visibility to be passed to this macro's exported targets. It always implicitly includes the location where this macro is instantiated, so this attribute only needs to be explicitly set if you want the macro's targets to be additionally visible somewhere else. | <a href="https://bazel.build/concepts/labels">List of labels</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | optional | |


23 changes: 23 additions & 0 deletions docs/metadata-extensions/rules/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@stardoc//stardoc:stardoc.bzl", "stardoc")

[
[
stardoc(
name = file,
out = "{}.generated.md".format(file),
input = "@package_metadata_extensions//rules:{}.bzl".format(file),
deps = [
"@package_metadata_extensions//rules:srcs",
],
),
diff_test(
name = "{}_test".format(file),
file1 = ":{}".format(file),
file2 = "{}.md".format(file),
),
]
for file in [
"package_metadata_override",
]
]
27 changes: 27 additions & 0 deletions docs/metadata-extensions/rules/package_metadata_override.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->

Declares rule `package_metadata_override`.

<a id="package_metadata_override"></a>

## package_metadata_override

<pre>
load("@package_metadata_extensions//rules:package_metadata_override.bzl", "package_metadata_override")

package_metadata_override(*, <a href="#package_metadata_override-name">name</a>, <a href="#package_metadata_override-metadata">metadata</a>, <a href="#package_metadata_override-targets">targets</a>, <a href="#package_metadata_override-visibility">visibility</a>)
</pre>



**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="package_metadata_override-name"></a>name | A unique name for this macro instance. Normally, this is also the name for the macro's main or only target. The names of any other targets that this macro might create will be this name with a string suffix. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="package_metadata_override-metadata"></a>metadata | A `package_metadata` target to use for the provided packages.<br><br>This overrides any `package_metadata` directly declared by the packages. | <a href="https://bazel.build/concepts/labels">Label</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | |
| <a id="package_metadata_override-targets"></a>targets | A list of targets the override applies to.<br><br>This allows passing targets of the form<br><br> - `//foo/...` matches all targets under `//foo` in the current repository. - `//foo` matches all targets in `//foo` (but not in subpackages). - `//foo:bar` matches exactly target `//foo:bar`.<br><br>This also supports referencing packages and targets in external repositories.<br><br> - `@other_repo//foo/...` matches all targets under `//foo` in `other_repo`. - `@other_repo//foo` matches all targets in `//foo` in `other_repo` (but not in subpackages). - `@other_repo//foo:bar` matches exactly target `//foo:bar` in `other_repo`. | List of strings; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | |
| <a id="package_metadata_override-visibility"></a>visibility | The visibility to be passed to this macro's exported targets. It always implicitly includes the location where this macro is instantiated, so this attribute only needs to be explicitly set if you want the macro's targets to be additionally visible somewhere else. | <a href="https://bazel.build/concepts/labels">List of labels</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | optional | |


18 changes: 18 additions & 0 deletions metadata-extensions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ load("@package_metadata//licenses/rules:license.bzl", "license")
load("@package_metadata//purl:purl.bzl", "purl")
load("@package_metadata//rules:package_metadata.bzl", "package_metadata")

exports_files(
[
"defs.bzl",
],
visibility = ["//visibility:public"],
)

filegroup(
name = "srcs",
srcs = [
"defs.bzl",
] + [
"//providers:srcs",
"//rules:srcs",
],
visibility = ["//visibility:public"],
)

package_metadata(
name = "package_metadata",
attributes = [
Expand Down
8 changes: 8 additions & 0 deletions metadata-extensions/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Public API of `@package_metadata_extensions`."""

load("//rules:package_metadata_override.bzl", _package_metadata_override = "package_metadata_override")

visibility("public")

# Rules
package_metadata_override = _package_metadata_override
14 changes: 14 additions & 0 deletions metadata-extensions/providers/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports_files(
[
"package_specification_info.bzl",
],
visibility = ["//visibility:public"],
)

filegroup(
name = "srcs",
srcs = [
"package_specification_info.bzl",
],
visibility = ["//visibility:public"],
)
54 changes: 54 additions & 0 deletions metadata-extensions/providers/package_specification_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Declares provider `PackageSpecificationInfo`."""

visibility([
"//...",
])

def _contains_impl(configs, label):
config = configs.get(label.repo_name, None)
if not config:
return False

for cfg in config:
if cfg.recursive:
if label.package.startswith(cfg.package):
return True
continue

if label.package != cfg.package:
continue

if cfg.target != None:
if label.name == cfg.target:
return True
else:
return True

return False

def _contains(negative, positive, label):
if _contains_impl(negative, label):
return False

return _contains_impl(positive, label)

def _init(negative, positive):
return {
"contains": lambda target: _contains(negative, positive, target),
}

PackageSpecificationInfo, _create = provider(
doc = """
Information about transitive package specifications used in package groups.

This is a Starlark implementation of [PackageSpecificationInfo](https://bazel.build/rules/lib/providers/PackageSpecificationInfo).
""".strip(),
fields = {
"contains": """
Checks if a target exists in a package group.

Parameter **MUST** be a label.
""".strip(),
},
init = _init,
)
18 changes: 18 additions & 0 deletions metadata-extensions/rules/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
exports_files(
[
"package_metadata_override.bzl",
],
visibility = ["//visibility:public"],
)

filegroup(
name = "srcs",
srcs = [
"package_group.bzl",
"package_metadata_override.bzl",
] + [
"//providers:srcs",
"@package_metadata//providers:srcs",
],
visibility = ["//visibility:public"],
)
92 changes: 92 additions & 0 deletions metadata-extensions/rules/package_group.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Declares rule `package_group`."""

load("//providers:package_specification_info.bzl", "PackageSpecificationInfo")

visibility([
"//...",
])

def _package_group_rule_impl(ctx):
positive = {}
negative = {}
for pattern in [json.decode(pattern) for pattern in ctx.attr.patterns]:
if pattern["negative"]:
config = negative
else:
config = positive

repo = pattern["repo"]
config.setdefault(repo, []).append(struct(
package = pattern["package"],
target = pattern["target"],
recursive = pattern["recursive"],
))

return [
PackageSpecificationInfo(
negative = negative,
positive = positive,
),
]

_package_group_rule = rule(
implementation = _package_group_rule_impl,
attrs = {
"patterns": attr.string_list(
mandatory = True,
),
},
provides = [
PackageSpecificationInfo,
],
)

def _package_group_impl(name, packages, visibility):
patterns = []
for label, configs in packages.items():
for config in json.decode(configs):
patterns.append({
"negative": config["negative"],
"package": config["package"],
"recursive": config["recursive"],
"repo": label.repo_name,
"target": config["target"],
})

_package_group_rule(
name = name,
visibility = visibility,
patterns = [json.encode(pattern) for pattern in patterns],
)

package_group = macro(
implementation = _package_group_impl,
attrs = {
"packages": attr.label_keyed_string_dict(
mandatory = True,
configurable = False,
doc = """
A dict from a dummy label to the parsed configuration.

Key: a label in the repository of the target pattern. Only `Label.repo_name` is
used. Does not need to extist.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: typo

Value: a `json.encode()`'d list of dicts defining the configuration of the
target pattern.

```starlark
{
"negative": True | False
"pattern": "path/to/package/...",
}
```
""".strip(),
),
},
doc = """
A starlark implementation of [package_group](https://bazel.build/reference/be/functions#package_group).

Unlike the native implementation in Bazel, this allows patterns referencing other repositories.

This has a very subtle API. Not for direct use.
""".strip(),
)
Loading