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 "gather_licenses_info_and_write", 20 "write_licenses_info", 21) 22load( 23 "@rules_license//rules_gathering:gathering_providers.bzl", 24 "TransitiveLicensesInfo", 25) 26 27# Forward licenses used until users migrate. Delete at 0.0.7 or 0.1.0. 28load( 29 "@rules_license//sample_reports:licenses_used.bzl", 30 _licenses_used = "licenses_used", 31) 32 33licenses_used = _licenses_used 34 35# This rule is proof of concept, and may not represent the final 36# form of a rule for compliance validation. 37def _check_license_impl(ctx): 38 # Gather all licenses and write information to one place 39 40 licenses_file = ctx.actions.declare_file("_%s_licenses_info.json" % ctx.label.name) 41 write_licenses_info(ctx, ctx.attr.deps, licenses_file) 42 43 license_files = [] 44 if ctx.outputs.license_texts: 45 license_files = get_licenses_mapping(ctx.attr.deps).keys() 46 47 # Now run the checker on it 48 inputs = [licenses_file] 49 outputs = [ctx.outputs.report] 50 args = ctx.actions.args() 51 args.add("--licenses_info", licenses_file.path) 52 args.add("--report", ctx.outputs.report.path) 53 if ctx.attr.check_conditions: 54 args.add("--check_conditions") 55 if ctx.outputs.copyright_notices: 56 args.add("--copyright_notices", ctx.outputs.copyright_notices.path) 57 outputs.append(ctx.outputs.copyright_notices) 58 if ctx.outputs.license_texts: 59 args.add("--license_texts", ctx.outputs.license_texts.path) 60 outputs.append(ctx.outputs.license_texts) 61 inputs.extend(license_files) 62 ctx.actions.run( 63 mnemonic = "CheckLicenses", 64 progress_message = "Checking license compliance for %s" % ctx.label, 65 inputs = inputs, 66 outputs = outputs, 67 executable = ctx.executable._checker, 68 arguments = [args], 69 ) 70 return [ 71 DefaultInfo(files = depset(outputs)), 72 OutputGroupInfo(licenses_file = depset([licenses_file])), 73 ] 74 75_check_license = rule( 76 implementation = _check_license_impl, 77 attrs = { 78 "deps": attr.label_list( 79 aspects = [gather_licenses_info], 80 ), 81 "check_conditions": attr.bool(default = True, mandatory = False), 82 "copyright_notices": attr.output(mandatory = False), 83 "license_texts": attr.output(mandatory = False), 84 "report": attr.output(mandatory = True), 85 "_checker": attr.label( 86 default = Label("@rules_license//tools:checker_demo"), 87 executable = True, 88 allow_files = True, 89 cfg = "exec", 90 ), 91 }, 92) 93 94# TODO(b/152546336): Update the check to take a pointer to a condition list. 95def check_license(**kwargs): 96 _check_license(**kwargs) 97 98def _manifest_impl(ctx): 99 # Gather all licenses and make it available as deps for downstream rules 100 # Additionally write the list of license filenames to a file that can 101 # also be used as an input to downstream rules. 102 licenses_file = ctx.actions.declare_file(ctx.attr.out.name) 103 mappings = get_licenses_mapping(ctx.attr.deps, ctx.attr.warn_on_legacy_licenses) 104 ctx.actions.write( 105 output = licenses_file, 106 content = "\n".join([",".join([f.path, p]) for (f, p) in mappings.items()]), 107 ) 108 return [DefaultInfo(files = depset(mappings.keys()))] 109 110_manifest = rule( 111 implementation = _manifest_impl, 112 doc = """Internal tmplementation method for manifest().""", 113 attrs = { 114 "deps": attr.label_list( 115 doc = """List of targets to collect license files for.""", 116 aspects = [gather_licenses_info], 117 ), 118 "out": attr.output( 119 doc = """Output file.""", 120 mandatory = True, 121 ), 122 "warn_on_legacy_licenses": attr.bool(default = False), 123 }, 124) 125 126def manifest(name, deps, out = None, **kwargs): 127 if not out: 128 out = name + ".manifest" 129 130 _manifest(name = name, deps = deps, out = out, **kwargs) 131 132def get_licenses_mapping(deps, warn = False): 133 """Creates list of entries representing all licenses for the deps. 134 135 Args: 136 137 deps: a list of deps which should have TransitiveLicensesInfo providers. 138 This requires that you have run the gather_licenses_info 139 aspect over them 140 141 warn: boolean, if true, display output about legacy targets that need 142 update 143 144 Returns: 145 {File:package_name} 146 """ 147 tls = [] 148 for dep in deps: 149 lds = dep[TransitiveLicensesInfo].licenses 150 tls.append(lds) 151 152 ds = depset(transitive = tls) 153 154 # Ignore any legacy licenses that may be in the report 155 mappings = {} 156 for lic in ds.to_list(): 157 if type(lic.license_text) == "File": 158 mappings[lic.license_text] = lic.package_name 159 elif warn: 160 print("Legacy license %s not included, rule needs updating" % lic.license_text) 161 162 return mappings 163