xref: /aosp_15_r20/external/bazelbuild-rules_license/rules/package_info.bzl (revision f578df4fd057ffe2023728444759535685631548)
1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Rules for declaring metadata about a package."""
15
16load(
17    "@rules_license//rules:providers.bzl",
18    "ExperimentalMetadataInfo",
19    "PackageInfo",
20)
21
22#
23# package_info()
24#
25
26def _package_info_impl(ctx):
27    provider = PackageInfo(
28        # Metadata providers must include a type discriminator. We don't need it
29        # to collect the providers, but we do need it to write the JSON. We
30        # key on the type field to look up the correct block of code to pull
31        # data out and format it. We can't to the lookup on the provider class.
32        type = "package_info",
33        label = ctx.label,
34        package_name = ctx.attr.package_name or ctx.build_file_path.rstrip("/BUILD"),
35        package_url = ctx.attr.package_url,
36        package_version = ctx.attr.package_version,
37    )
38    # Experimental alternate design, using a generic 'data' back to hold things
39    generic_provider = ExperimentalMetadataInfo(
40        type = "package_info_alt",
41        label = ctx.label,
42        data = {
43            "package_name": ctx.attr.package_name or ctx.build_file_path.rstrip("/BUILD"),
44            "package_url": ctx.attr.package_url,
45            "package_version": ctx.attr.package_version
46        }
47    )
48    return [provider, generic_provider]
49
50_package_info = rule(
51    implementation = _package_info_impl,
52    attrs = {
53        "package_name": attr.string(
54            doc = "A human readable name identifying this package." +
55                  " This may be used to produce an index of OSS packages used by" +
56                  " an applicatation.",
57        ),
58        "package_url": attr.string(
59            doc = "The URL this instance of the package was download from." +
60                  " This may be used to produce an index of OSS packages used by" +
61                  " an applicatation.",
62        ),
63        "package_version": attr.string(
64            doc = "A human readable version string identifying this package." +
65                  " This may be used to produce an index of OSS packages used" +
66                  " by an applicatation.  It should be a value that" +
67                  " increases over time, rather than a commit hash."
68        ),
69    },
70)
71
72# buildifier: disable=function-docstring-args
73def package_info(
74        name,
75        package_name = None,
76        package_url = None,
77        package_version = None,
78        **kwargs):
79    """Wrapper for package_info rule.
80
81    @wraps(_package_info)
82
83    Args:
84      name: str target name.
85      package_name: str A human readable name identifying this package. This
86                    may be used to produce an index of OSS packages used by
87                    an application.
88      package_url: str The canoncial URL this package distribution was retrieved from.
89                       Note that, because of local mirroring, that might not be the
90                       physical URL it was retrieved from.
91      package_version: str A human readable name identifying version of this package.
92      kwargs: other args. Most are ignored.
93    """
94    visibility = kwargs.get("visibility") or ["//visibility:public"]
95    _package_info(
96        name = name,
97        package_name = package_name,
98        package_url = package_url,
99        package_version = package_version,
100        applicable_licenses = [],
101        visibility = visibility,
102        tags = [],
103        testonly = 0,
104    )
105