kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/proto/testdata/proto_verifier_test.bzl (about) 1 """Rules for verifying proto indexer output""" 2 3 # Copyright 2019 The Kythe Authors. All rights reserved. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 load( 18 "@io_kythe//tools/build_rules/verifier_test:verifier_test.bzl", 19 "KytheVerifierSources", 20 "extract", 21 "index_compilation", 22 "verifier_test", 23 ) 24 load("@rules_proto//proto:defs.bzl", "ProtoInfo") 25 26 def _invoke(rulefn, name, **kwargs): 27 """Invoke rulefn with name and kwargs, returning the label of the rule.""" 28 rulefn(name = name, **kwargs) 29 return "//{}:{}".format(native.package_name(), name) 30 31 def get_proto_files_and_proto_paths(protolibs): 32 """Given a list of proto_library targets, returns: 33 * a list of top-level .proto files 34 * a depset of all transitively-included .proto files 35 * a depset of --proto_path locations 36 """ 37 toplevel_srcs = [] 38 for lib in protolibs: 39 info = lib[ProtoInfo] 40 for src in info.direct_sources: 41 toplevel_srcs.append(src) 42 all_srcs = depset([], transitive = [lib[ProtoInfo].transitive_sources for lib in protolibs]) 43 proto_paths = depset( 44 transitive = [lib[ProtoInfo].transitive_proto_path for lib in protolibs] + 45 # Workaround for https://github.com/bazelbuild/bazel/issues/7964. 46 # Since we can't rely on ProtoInfo to provide accurate roots, generate them here. 47 [depset([ 48 src.root.path 49 for src in depset(toplevel_srcs, transitive = [all_srcs], order = "postorder").to_list() 50 if src.root.path 51 ])], 52 order = "postorder", 53 ) 54 return toplevel_srcs, all_srcs, proto_paths 55 56 def _proto_extract_kzip_impl(ctx): 57 toplevel_srcs, all_srcs, pathopt = get_proto_files_and_proto_paths(ctx.attr.srcs) 58 59 args = ctx.actions.args() 60 args.add("--") 61 args.add_all(ctx.attr.opts) 62 args.add_all(pathopt, before_each = "--proto_path") 63 64 extract( 65 srcs = toplevel_srcs, 66 ctx = ctx, 67 extractor = ctx.executable.extractor, 68 kzip = ctx.outputs.kzip, 69 mnemonic = "ProtoExtractKZip", 70 opts = args, 71 vnames_config = ctx.file.vnames_config, 72 deps = all_srcs, 73 ) 74 return [KytheVerifierSources(files = depset(toplevel_srcs))] 75 76 proto_extract_kzip = rule( 77 attrs = { 78 "srcs": attr.label_list( 79 mandatory = True, 80 allow_empty = False, 81 allow_files = True, 82 providers = [ProtoInfo], 83 ), 84 "extractor": attr.label( 85 default = Label("//kythe/cxx/extractor/proto:proto_extractor"), 86 executable = True, 87 cfg = "exec", 88 ), 89 "opts": attr.string_list(), 90 "vnames_config": attr.label( 91 default = Label("//external:vnames_config"), 92 allow_single_file = True, 93 ), 94 }, 95 outputs = {"kzip": "%{name}.kzip"}, 96 implementation = _proto_extract_kzip_impl, 97 ) 98 99 def proto_verifier_test( 100 name, 101 srcs, 102 deps = [], 103 size = "small", 104 tags = [], 105 extractor = None, 106 extractor_opts = [], 107 indexer_opts = [], 108 verifier_opts = [], 109 convert_marked_source = False, 110 resolve_code_facts = False, 111 vnames_config = None, 112 visibility = None): 113 """Extract, analyze, and verify a proto compilation. 114 115 Args: 116 name: Name of the test 117 srcs: The compilation's source file inputs; each file's verifier goals will be checked 118 deps: Optional list of proto_verifier_test targets to be used as proto compilation dependencies 119 size: Test size 120 tags: Test tags 121 extractor: Executable extractor tool to invoke (defaults to protoc_extractor) 122 extractor_opts: List of options passed to the extractor tool 123 indexer_opts: List of options passed to the indexer tool 124 verifier_opts: List of options passed to the verifier tool 125 convert_marked_source: Whether the verifier should convert marked source. 126 vnames_config: Optional path to a VName configuration file 127 visibility: Visibility of underlying build targets 128 Returns: 129 Name of the test rule 130 """ 131 kzip = _invoke( 132 proto_extract_kzip, 133 name = name + "_kzip", 134 testonly = True, 135 srcs = srcs, 136 extractor = extractor, 137 opts = extractor_opts, 138 tags = tags, 139 visibility = visibility, 140 vnames_config = vnames_config, 141 ) 142 entries = _invoke( 143 index_compilation, 144 name = name + "_entries", 145 testonly = True, 146 indexer = "//kythe/cxx/indexer/proto:indexer", 147 target_indexer = "//kythe/cxx/indexer/proto:indexer", 148 opts = indexer_opts + ["--index_file"], 149 tags = tags + ["manual"], 150 visibility = visibility, 151 deps = [kzip], 152 ) 153 vopts = ["--ignore_dups"] + verifier_opts 154 if convert_marked_source: 155 vopts.append("--convert_marked_source") 156 return _invoke( 157 verifier_test, 158 name = name, 159 size = size, 160 srcs = [entries], 161 opts = vopts, 162 tags = tags, 163 resolve_code_facts = resolve_code_facts, 164 visibility = visibility, 165 deps = [entries], 166 )