xref: /aosp_15_r20/external/bazelbuild-rules_license/rules/license.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 the compliance licenses used by a package.
15
16"""
17
18load(
19    "@rules_license//rules:providers.bzl",
20    "LicenseKindInfo",
21)
22load(
23    "@rules_license//rules:license_impl.bzl",
24    "license_rule_impl",
25)
26
27# Enable this if your organization requires the license text to be a file
28# checked into source control instead of, possibly, another rule.
29_require_license_text_is_a_file = False
30
31# This rule must be named "_license" for backwards compatability with older
32# or Bazel that checked that name explicitly. See
33# https://github.com/bazelbuild/bazel/commit/bbc221f60bc8c9177470529d85c3e47a5d9aaf21
34# TODO(after bazel 7.0 release): Feel free to rename the rule and move.
35_license = rule(
36    implementation = license_rule_impl,
37    attrs = {
38        "license_kinds": attr.label_list(
39            mandatory = False,
40            doc = "License kind(s) of this license. If multiple license kinds are" +
41                  " listed in the LICENSE file, and they all apply, then all" +
42                  " should be listed here. If the user can choose a single one" +
43                  " of many, then only list one here.",
44            providers = [LicenseKindInfo],
45            # This should be the null configuration, not the exec.
46            cfg = "exec",
47        ),
48        "copyright_notice": attr.string(
49            doc = "Copyright notice.",
50        ),
51        "license_text": attr.label(
52            allow_single_file = True,
53            default = "LICENSE",
54            doc = "The license file.",
55        ),
56        "package_name": attr.string(
57            doc = "A human readable name identifying this package." +
58                  " This may be used to produce an index of OSS packages used by" +
59                  " an applicatation.",
60        ),
61        "package_url": attr.string(
62            doc = "The URL this instance of the package was download from." +
63                  " This may be used to produce an index of OSS packages used by" +
64                  " an applicatation.",
65        ),
66        "package_version": attr.string(
67            doc = "A human readable version string identifying this package." +
68                  " This may be used to produce an index of OSS packages used" +
69                  " by an applicatation.  It should be a value that" +
70                  " increases over time, rather than a commit hash."
71        ),
72        "namespace": attr.string(
73            doc = "A human readable name used to organize licenses into categories." +
74                  " This is used in google3 to differentiate third party licenses used" +
75                  " for compliance versus internal licenses used by SLAsan for internal" +
76                  " teams' SLAs.",
77        ),
78    },
79)
80
81# buildifier: disable=function-docstring-args
82def license(
83        name,
84        license_text = "LICENSE",
85        license_kind = None,
86        license_kinds = None,
87        copyright_notice = None,
88        package_name = None,
89        package_url = None,
90        package_version = None,
91        namespace = "compliance",
92        tags = [],
93        visibility = ["//visibility:public"]):
94    """Wrapper for license rule.
95
96    @wraps(_license)
97
98    Args:
99      name: str target name.
100      license_text: str Filename of the license file
101      license_kind: label a single license_kind. Only one of license_kind or license_kinds may
102                    be specified
103      license_kinds: list(label) list of license_kind targets.
104      copyright_notice: str Copyright notice associated with this package.
105      package_name: str A human readable name identifying this package. This
106                    may be used to produce an index of OSS packages used by
107                    an application.
108      package_url: str The canonical URL this package was downloaded from.
109      package_version: str The version corresponding the the URL.
110      namespace: str Undocumened. Internal.
111      tags: list(str) tags applied to the rule
112      visibility: list(label) visibility spec.
113    """
114    if license_kind:
115        if license_kinds:
116            fail("Can not use both license_kind and license_kinds")
117        license_kinds = [license_kind]
118
119    if _require_license_text_is_a_file:
120        # Make sure the file exists as named in the rule. A glob expression that
121        # expands to the name of the file is not acceptable.
122        srcs = native.glob([license_text])
123        if len(srcs) != 1 or srcs[0] != license_text:
124            fail("Specified license file doesn't exist: %s" % license_text)
125
126    _license(
127        name = name,
128        license_kinds = license_kinds,
129        license_text = license_text,
130        copyright_notice = copyright_notice,
131        package_name = package_name,
132        package_url = package_url,
133        package_version = package_version,
134        namespace = namespace,
135        applicable_licenses = [],
136        visibility = visibility,
137        tags = tags,
138        testonly = 0,
139    )
140