xref: /aosp_15_r20/build/bazel/rules/license/license.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2021 The Android Open Source Project
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#     http://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
15# Rules for declaring Android licenses used by a package.
16# See: go/license-checking-v2
17
18load("@rules_license//rules:license.bzl", "license")
19
20_special_licenses = {
21    "legacy_by_exception_only": 0,
22    "legacy_not_a_contribution": 0,
23    "legacy_not_allowed": 0,
24    "legacy_notice": 0,
25    "legacy_permissive": 0,
26    "legacy_proprietary": 0,
27    "legacy_reciprocal": 0,
28    "legacy_restricted": 0,
29    "legacy_unencumbered": 0,
30    "legacy_unknown": 0,
31}
32_spdx_license_prefix = "SPDX-license-identifier-"
33_spdx_package = "//build/soong/licenses:"
34
35def _remap_license_kind(license_kind):
36    # In bazel license_kind is a label.
37    # First, map legacy license kinds.
38    if license_kind in _special_licenses:
39        return _spdx_package + license_kind
40
41    # Map SPDX licenses to the ones defined in build/soong/licenses.
42    if license_kind.startswith(_spdx_license_prefix):
43        return _spdx_package + license_kind
44
45    # Last resort.
46    return license_kind
47
48# buildifier: disable=function-docstring-args
49def android_license(
50        name,
51        license_text = "__NO_LICENSE__",  # needed as `license` expects it
52        visibility = ["//visibility:public"],
53        license_kinds = [],
54        copyright_notice = None,
55        package_name = None,
56        tags = []):
57    """Wrapper for license rule.
58
59    Args:
60      name: str target name.
61      license_text: str Filename of the license file
62      visibility: list(label) visibility spec
63      license_kinds: list(text) list of license_kind targets.
64      copyright_notice: str Copyright notice associated with this package.
65      package_name : str A human readable name identifying this package. This
66                     may be used to produce an index of OSS packages used by
67                     an application.
68      tags: list(str) tags applied to the rule
69    """
70
71    license(
72        name = name,
73        license_kinds = [_remap_license_kind(x) for x in license_kinds],
74        license_text = license_text,
75        copyright_notice = copyright_notice,
76        package_name = package_name,
77        visibility = visibility,
78        tags = tags,
79    )
80