• 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"""License compliance checking."""
15
16load(
17    "@rules_license//rules:gather_licenses_info.bzl",
18    "gather_licenses_info",
19    "write_licenses_info",
20)
21
22def _licenses_used_impl(ctx):
23    # Gather all licenses and make it available as JSON
24    write_licenses_info(ctx, ctx.attr.deps, ctx.outputs.out)
25    return [DefaultInfo(files = depset([ctx.outputs.out]))]
26
27_licenses_used = rule(
28    implementation = _licenses_used_impl,
29    doc = """Internal tmplementation method for licenses_used().""",
30    attrs = {
31        "deps": attr.label_list(
32            doc = """List of targets to collect LicenseInfo for.""",
33            aspects = [gather_licenses_info],
34        ),
35        "out": attr.output(
36            doc = """Output file.""",
37            mandatory = True,
38        ),
39    },
40)
41
42def licenses_used(name, deps, out = None, **kwargs):
43    """Collects LicensedInfo providers for a set of targets and writes as JSON.
44
45    The output is a single JSON array, with an entry for each license used.
46    See gather_licenses_info.bzl:write_licenses_info() for a description of the schema.
47
48    Args:
49      name: The target.
50      deps: A list of targets to get LicenseInfo for. The output is the union of
51            the result, not a list of information for each dependency.
52      out: The output file name. Default: <name>.json.
53      **kwargs: Other args
54
55    Usage:
56
57      licenses_used(
58          name = "license_info",
59          deps = [":my_app"],
60          out = "license_info.json",
61      )
62    """
63    if not out:
64        out = name + ".json"
65    _licenses_used(name = name, deps = deps, out = out, **kwargs)
66