1# Copyright 2022 Google LLC. All rights reserved. 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"""kt_traverse_exports visitor for identifying forbidden deps of Kotlin rules. 16 17Currently this system recognizes: 18 - nano protos 19 - targets in forbidden packages 20 - targets exporting other forbidden targets 21""" 22 23load("//:visibility.bzl", "RULES_KOTLIN") 24load("//bazel:stubs.bzl", "is_exempt_dep", "is_forbidden_dep") 25 26visibility(RULES_KOTLIN) 27 28def _error(target, msg): 29 return (str(target.label), msg) 30 31def _check_forbidden(target, ctx_rule): 32 if is_exempt_dep(target): 33 return [] 34 35 if is_forbidden_dep(target): 36 return [_error(target, "Forbidden package")] 37 38 # Identify nano protos using tag (b/122083175) 39 for tag in ctx_rule.attr.tags: 40 if "nano_proto_library" == tag: 41 return [_error(target, "nano_proto_library")] 42 43 return [] 44 45def _if_not_checked(target): 46 return [] if is_exempt_dep(target) else [_error(target, "Not checked")] 47 48def _validate_deps(error_set): 49 if not error_set: 50 return 51 52 error_lines = [ 53 " " + name + " : " + msg 54 for (name, msg) in error_set.to_list() 55 ] 56 fail("Forbidden deps, see go/kotlin/build-rules#restrictions:\n" + "\n".join(error_lines)) 57 58kt_forbidden_deps_visitor = struct( 59 name = "forbidden_deps", 60 visit_target = _check_forbidden, 61 filter_edge = None, 62 process_unvisited_target = _if_not_checked, 63 finish_expansion = _validate_deps, 64) 65