xref: /aosp_15_r20/external/bazelbuild-rules_license/rules/license_impl.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 licenses used by a package.
15
16"""
17
18load(
19    "@rules_license//rules:providers.bzl",
20    "LicenseInfo",
21    "LicenseKindInfo",
22)
23
24# Debugging verbosity
25_VERBOSITY = 0
26
27def _debug(loglevel, msg):
28    if _VERBOSITY > loglevel:
29        print(msg)  # buildifier: disable=print
30
31#
32# license()
33#
34
35def license_rule_impl(ctx):
36    provider = LicenseInfo(
37        license_kinds = tuple([k[LicenseKindInfo] for k in ctx.attr.license_kinds]),
38        copyright_notice = ctx.attr.copyright_notice,
39        package_name = ctx.attr.package_name or ctx.build_file_path.rstrip("/BUILD"),
40        package_url = ctx.attr.package_url,
41        package_version = ctx.attr.package_version,
42        license_text = ctx.file.license_text,
43        label = ctx.label,
44        namespace = ctx.attr.namespace,
45    )
46    _debug(0, provider)
47    return [provider]
48