• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    },
73)
74
75# buildifier: disable=function-docstring-args
76def license(
77        name,
78        license_text = "LICENSE",
79        license_kind = None,
80        license_kinds = None,
81        copyright_notice = None,
82        package_name = None,
83        package_url = None,
84        package_version = None,
85        namespace = None,
86        tags = [],
87        visibility = ["//visibility:public"]):
88    """Wrapper for license rule.
89
90    @wraps(_license)
91
92    Args:
93      name: str target name.
94      license_text: str Filename of the license file
95      license_kind: label a single license_kind. Only one of license_kind or license_kinds may
96                    be specified
97      license_kinds: list(label) list of license_kind targets.
98      copyright_notice: str Copyright notice associated with this package.
99      package_name: str A human readable name identifying this package. This
100                    may be used to produce an index of OSS packages used by
101                    an application.
102      package_url: str The canonical URL this package was downloaded from.
103      package_version: str The version corresponding the the URL.
104      tags: list(str) tags applied to the rule
105      visibility: list(label) visibility spec.
106    """
107    if license_kind:
108        if license_kinds:
109            fail("Can not use both license_kind and license_kinds")
110        license_kinds = [license_kind]
111
112    if _require_license_text_is_a_file:
113        # Make sure the file exists as named in the rule. A glob expression that
114        # expands to the name of the file is not acceptable.
115        srcs = native.glob([license_text])
116        if len(srcs) != 1 or srcs[0] != license_text:
117            fail("Specified license file doesn't exist: %s" % license_text)
118
119    # TODO(0.0.6 release): Remove this warning and fail hard instead.
120    if namespace:
121        # buildifier: disable=print
122        print("license(namespace=<str>) is deprecated.")
123
124    _license(
125        name = name,
126        license_kinds = license_kinds,
127        license_text = license_text,
128        copyright_notice = copyright_notice,
129        package_name = package_name,
130        package_url = package_url,
131        package_version = package_version,
132        applicable_licenses = [],
133        visibility = visibility,
134        tags = tags,
135        testonly = 0,
136    )
137