1#!/usr/bin/env python3 2 3# Copyright 2016 gRPC authors. 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 17import ast 18import os 19import re 20import subprocess 21import sys 22 23os.chdir(os.path.join(os.path.dirname(sys.argv[0]), "../../..")) 24 25git_hash_pattern = re.compile("[0-9a-f]{40}") 26 27# Parse git hashes from submodules 28git_submodules = ( 29 subprocess.check_output("git submodule", shell=True) 30 .decode() 31 .strip() 32 .split("\n") 33) 34git_submodule_hashes = { 35 re.search(git_hash_pattern, s).group() for s in git_submodules 36} 37 38_BAZEL_SKYLIB_DEP_NAME = "bazel_skylib" 39_BAZEL_TOOLCHAINS_DEP_NAME = "bazel_toolchains" 40_BAZEL_COMPDB_DEP_NAME = "bazel_compdb" 41_TWISTED_TWISTED_DEP_NAME = "com_github_twisted_twisted" 42_YAML_PYYAML_DEP_NAME = "com_github_yaml_pyyaml" 43_TWISTED_INCREMENTAL_DEP_NAME = "com_github_twisted_incremental" 44_ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME = ( 45 "com_github_zopefoundation_zope_interface" 46) 47_TWISTED_CONSTANTLY_DEP_NAME = "com_github_twisted_constantly" 48 49_GRPC_DEP_NAMES = [ 50 "platforms", 51 "boringssl", 52 "zlib", 53 "com_google_protobuf", 54 "com_google_googletest", 55 "rules_cc", 56 "com_github_google_benchmark", 57 "com_github_cares_cares", 58 "com_google_absl", 59 "com_google_fuzztest", 60 "io_opencensus_cpp", 61 "io_opentelemetry_cpp", 62 # TODO(stanleycheung): remove when prometheus-cpp has new release 63 "com_github_jupp0r_prometheus_cpp", 64 "envoy_api", 65 _BAZEL_SKYLIB_DEP_NAME, 66 _BAZEL_TOOLCHAINS_DEP_NAME, 67 _BAZEL_COMPDB_DEP_NAME, 68 _TWISTED_TWISTED_DEP_NAME, 69 _YAML_PYYAML_DEP_NAME, 70 _TWISTED_INCREMENTAL_DEP_NAME, 71 _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME, 72 _TWISTED_CONSTANTLY_DEP_NAME, 73 "io_bazel_rules_go", 74 "build_bazel_rules_apple", 75 "build_bazel_apple_support", 76 "com_googlesource_code_re2", 77 "bazel_gazelle", 78 "opencensus_proto", 79 "com_envoyproxy_protoc_gen_validate", 80 "com_google_googleapis", 81 "com_google_libprotobuf_mutator", 82 "com_github_cncf_xds", 83 "google_cloud_cpp", 84] 85 86_GRPC_BAZEL_ONLY_DEPS = [ 87 "platforms", 88 "rules_cc", 89 "com_google_absl", 90 "com_google_fuzztest", 91 "io_opencensus_cpp", 92 # TODO(stanleycheung): remove when prometheus-cpp has new release 93 "com_github_jupp0r_prometheus_cpp", 94 _BAZEL_SKYLIB_DEP_NAME, 95 _BAZEL_TOOLCHAINS_DEP_NAME, 96 _BAZEL_COMPDB_DEP_NAME, 97 _TWISTED_TWISTED_DEP_NAME, 98 _YAML_PYYAML_DEP_NAME, 99 _TWISTED_INCREMENTAL_DEP_NAME, 100 _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME, 101 _TWISTED_CONSTANTLY_DEP_NAME, 102 "io_bazel_rules_go", 103 "build_bazel_rules_apple", 104 "build_bazel_apple_support", 105 "com_googlesource_code_re2", 106 "bazel_gazelle", 107 "opencensus_proto", 108 "com_envoyproxy_protoc_gen_validate", 109 "com_google_googleapis", 110 "com_google_libprotobuf_mutator", 111 "google_cloud_cpp", 112] 113 114 115class BazelEvalState(object): 116 def __init__(self, names_and_urls, overridden_name=None): 117 self.names_and_urls = names_and_urls 118 self.overridden_name = overridden_name 119 120 def http_archive(self, **args): 121 self.archive(**args) 122 123 def new_http_archive(self, **args): 124 self.archive(**args) 125 126 def bind(self, **args): 127 pass 128 129 def existing_rules(self): 130 if self.overridden_name: 131 return [self.overridden_name] 132 return [] 133 134 def archive(self, **args): 135 assert self.names_and_urls.get(args["name"]) is None 136 if args["name"] in _GRPC_BAZEL_ONLY_DEPS: 137 self.names_and_urls[args["name"]] = "dont care" 138 return 139 url = args.get("url", None) 140 if not url: 141 # we will only be looking for git commit hashes, so concatenating 142 # the urls is fine. 143 url = " ".join(args["urls"]) 144 self.names_and_urls[args["name"]] = url 145 146 def git_repository(self, **args): 147 assert self.names_and_urls.get(args["name"]) is None 148 if args["name"] in _GRPC_BAZEL_ONLY_DEPS: 149 self.names_and_urls[args["name"]] = "dont care" 150 return 151 self.names_and_urls[args["name"]] = args["remote"] 152 153 def grpc_python_deps(self): 154 pass 155 156 157# Parse git hashes from bazel/grpc_deps.bzl {new_}http_archive rules 158with open(os.path.join("bazel", "grpc_deps.bzl"), "r") as f: 159 names_and_urls = {} 160 eval_state = BazelEvalState(names_and_urls) 161 bazel_file = f.read() 162 163# grpc_deps.bzl only defines 'grpc_deps' and 'grpc_test_only_deps', add these 164# lines to call them. 165bazel_file += "\ngrpc_deps()\n" 166bazel_file += "\ngrpc_test_only_deps()\n" 167build_rules = { 168 "native": eval_state, 169 "http_archive": lambda **args: eval_state.http_archive(**args), 170 "load": lambda a, b: None, 171 "git_repository": lambda **args: eval_state.git_repository(**args), 172 "grpc_python_deps": lambda: None, 173 "Label": lambda a: None, 174} 175exec((bazel_file), build_rules) 176grpc_dep_names_set = set(_GRPC_DEP_NAMES) 177names_set = set(names_and_urls.keys()) 178if grpc_dep_names_set != names_set: 179 print("Differences detected between GRPC_DEP_NAMES and grpc_deps.bzl") 180 print("- GRPC_DEP_NAMES only:", grpc_dep_names_set - names_set) 181 print("- grpc_deps.bzl only:", names_set - grpc_dep_names_set) 182 sys.exit(1) 183 184# There are some "bazel-only" deps that are exceptions to this sanity check, 185# we don't require that there is a corresponding git module for these. 186names_without_bazel_only_deps = list(names_and_urls.keys()) 187for dep_name in _GRPC_BAZEL_ONLY_DEPS: 188 names_without_bazel_only_deps.remove(dep_name) 189archive_urls = [names_and_urls[name] for name in names_without_bazel_only_deps] 190workspace_git_hashes = { 191 re.search(git_hash_pattern, url).group() for url in archive_urls 192} 193if len(workspace_git_hashes) == 0: 194 print("(Likely) parse error, did not find any bazel git dependencies.") 195 sys.exit(1) 196 197# Validate the equivalence of the git submodules and Bazel git dependencies. The 198# condition we impose is that there is a git submodule for every dependency in 199# the workspace, but not necessarily conversely. E.g. Bloaty is a dependency 200# not used by any of the targets built by Bazel. 201if len(workspace_git_hashes - git_submodule_hashes) > 0: 202 print( 203 "Found discrepancies between git submodules and Bazel WORKSPACE" 204 " dependencies" 205 ) 206 print(("workspace_git_hashes: %s" % workspace_git_hashes)) 207 print(("git_submodule_hashes: %s" % git_submodule_hashes)) 208 print( 209 "workspace_git_hashes - git_submodule_hashes: %s" 210 % (workspace_git_hashes - git_submodule_hashes) 211 ) 212 sys.exit(1) 213 214# Also check that we can override each dependency 215for name in _GRPC_DEP_NAMES: 216 names_and_urls_with_overridden_name = {} 217 state = BazelEvalState( 218 names_and_urls_with_overridden_name, overridden_name=name 219 ) 220 rules = { 221 "native": state, 222 "http_archive": lambda **args: state.http_archive(**args), 223 "load": lambda a, b: None, 224 "git_repository": lambda **args: state.git_repository(**args), 225 "grpc_python_deps": lambda *args, **kwargs: None, 226 "Label": lambda a: None, 227 } 228 exec((bazel_file), rules) 229 assert name not in list(names_and_urls_with_overridden_name.keys()) 230 231sys.exit(0) 232