check_bazel_workspace.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python3
  2. # Copyright 2016 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import ast
  16. import os
  17. import re
  18. import subprocess
  19. import sys
  20. os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
  21. git_hash_pattern = re.compile('[0-9a-f]{40}')
  22. # Parse git hashes from submodules
  23. git_submodules = subprocess.check_output(
  24. 'git submodule', shell=True).decode().strip().split('\n')
  25. git_submodule_hashes = {
  26. re.search(git_hash_pattern, s).group() for s in git_submodules
  27. }
  28. _BAZEL_SKYLIB_DEP_NAME = 'bazel_skylib'
  29. _BAZEL_TOOLCHAINS_DEP_NAME = 'bazel_toolchains'
  30. _BAZEL_COMPDB_DEP_NAME = 'bazel_compdb'
  31. _TWISTED_TWISTED_DEP_NAME = 'com_github_twisted_twisted'
  32. _YAML_PYYAML_DEP_NAME = 'com_github_yaml_pyyaml'
  33. _TWISTED_INCREMENTAL_DEP_NAME = 'com_github_twisted_incremental'
  34. _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME = 'com_github_zopefoundation_zope_interface'
  35. _TWISTED_CONSTANTLY_DEP_NAME = 'com_github_twisted_constantly'
  36. _GRPC_DEP_NAMES = [
  37. 'upb', 'boringssl', 'zlib', 'com_google_protobuf', 'com_google_googletest',
  38. 'rules_cc', 'com_github_google_benchmark', 'com_github_cares_cares',
  39. 'com_google_absl', 'io_opencensus_cpp', 'envoy_api', _BAZEL_SKYLIB_DEP_NAME,
  40. _BAZEL_TOOLCHAINS_DEP_NAME, _BAZEL_COMPDB_DEP_NAME,
  41. _TWISTED_TWISTED_DEP_NAME, _YAML_PYYAML_DEP_NAME,
  42. _TWISTED_INCREMENTAL_DEP_NAME, _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME,
  43. _TWISTED_CONSTANTLY_DEP_NAME, 'io_bazel_rules_go',
  44. 'build_bazel_rules_apple', 'build_bazel_apple_support', 'libuv',
  45. 'com_googlesource_code_re2', 'bazel_gazelle', 'opencensus_proto',
  46. 'com_envoyproxy_protoc_gen_validate', 'com_google_googleapis'
  47. ]
  48. _GRPC_BAZEL_ONLY_DEPS = [
  49. 'upb', # third_party/upb is checked in locally
  50. 'rules_cc',
  51. 'com_google_absl',
  52. 'io_opencensus_cpp',
  53. _BAZEL_SKYLIB_DEP_NAME,
  54. _BAZEL_TOOLCHAINS_DEP_NAME,
  55. _BAZEL_COMPDB_DEP_NAME,
  56. _TWISTED_TWISTED_DEP_NAME,
  57. _YAML_PYYAML_DEP_NAME,
  58. _TWISTED_INCREMENTAL_DEP_NAME,
  59. _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME,
  60. _TWISTED_CONSTANTLY_DEP_NAME,
  61. 'io_bazel_rules_go',
  62. 'build_bazel_rules_apple',
  63. 'build_bazel_apple_support',
  64. 'com_googlesource_code_re2',
  65. 'bazel_gazelle',
  66. 'opencensus_proto',
  67. 'com_envoyproxy_protoc_gen_validate',
  68. 'com_google_googleapis',
  69. ]
  70. class BazelEvalState(object):
  71. def __init__(self, names_and_urls, overridden_name=None):
  72. self.names_and_urls = names_and_urls
  73. self.overridden_name = overridden_name
  74. def http_archive(self, **args):
  75. self.archive(**args)
  76. def new_http_archive(self, **args):
  77. self.archive(**args)
  78. def bind(self, **args):
  79. pass
  80. def existing_rules(self):
  81. if self.overridden_name:
  82. return [self.overridden_name]
  83. return []
  84. def archive(self, **args):
  85. assert self.names_and_urls.get(args['name']) is None
  86. if args['name'] in _GRPC_BAZEL_ONLY_DEPS:
  87. self.names_and_urls[args['name']] = 'dont care'
  88. return
  89. url = args.get('url', None)
  90. if not url:
  91. # we will only be looking for git commit hashes, so concatenating
  92. # the urls is fine.
  93. url = ' '.join(args['urls'])
  94. self.names_and_urls[args['name']] = url
  95. def git_repository(self, **args):
  96. assert self.names_and_urls.get(args['name']) is None
  97. if args['name'] in _GRPC_BAZEL_ONLY_DEPS:
  98. self.names_and_urls[args['name']] = 'dont care'
  99. return
  100. self.names_and_urls[args['name']] = args['remote']
  101. def grpc_python_deps(self):
  102. pass
  103. # Parse git hashes from bazel/grpc_deps.bzl {new_}http_archive rules
  104. with open(os.path.join('bazel', 'grpc_deps.bzl'), 'r') as f:
  105. names_and_urls = {}
  106. eval_state = BazelEvalState(names_and_urls)
  107. bazel_file = f.read()
  108. # grpc_deps.bzl only defines 'grpc_deps' and 'grpc_test_only_deps', add these
  109. # lines to call them.
  110. bazel_file += '\ngrpc_deps()\n'
  111. bazel_file += '\ngrpc_test_only_deps()\n'
  112. build_rules = {
  113. 'native': eval_state,
  114. 'http_archive': lambda **args: eval_state.http_archive(**args),
  115. 'load': lambda a, b: None,
  116. 'git_repository': lambda **args: eval_state.git_repository(**args),
  117. 'grpc_python_deps': lambda: None,
  118. }
  119. exec((bazel_file), build_rules)
  120. for name in _GRPC_DEP_NAMES:
  121. assert name in list(names_and_urls.keys())
  122. assert len(_GRPC_DEP_NAMES) == len(list(names_and_urls.keys()))
  123. # There are some "bazel-only" deps that are exceptions to this sanity check,
  124. # we don't require that there is a corresponding git module for these.
  125. names_without_bazel_only_deps = list(names_and_urls.keys())
  126. for dep_name in _GRPC_BAZEL_ONLY_DEPS:
  127. names_without_bazel_only_deps.remove(dep_name)
  128. archive_urls = [names_and_urls[name] for name in names_without_bazel_only_deps]
  129. workspace_git_hashes = {
  130. re.search(git_hash_pattern, url).group() for url in archive_urls
  131. }
  132. if len(workspace_git_hashes) == 0:
  133. print("(Likely) parse error, did not find any bazel git dependencies.")
  134. sys.exit(1)
  135. # Validate the equivalence of the git submodules and Bazel git dependencies. The
  136. # condition we impose is that there is a git submodule for every dependency in
  137. # the workspace, but not necessarily conversely. E.g. Bloaty is a dependency
  138. # not used by any of the targets built by Bazel.
  139. if len(workspace_git_hashes - git_submodule_hashes) > 0:
  140. print(
  141. "Found discrepancies between git submodules and Bazel WORKSPACE dependencies"
  142. )
  143. print("workspace_git_hashes: %s" % workspace_git_hashes)
  144. print("git_submodule_hashes: %s" % git_submodule_hashes)
  145. print("workspace_git_hashes - git_submodule_hashes: %s" %
  146. (workspace_git_hashes - git_submodule_hashes))
  147. sys.exit(1)
  148. # Also check that we can override each dependency
  149. for name in _GRPC_DEP_NAMES:
  150. names_and_urls_with_overridden_name = {}
  151. state = BazelEvalState(names_and_urls_with_overridden_name,
  152. overridden_name=name)
  153. rules = {
  154. 'native': state,
  155. 'http_archive': lambda **args: state.http_archive(**args),
  156. 'load': lambda a, b: None,
  157. 'git_repository': lambda **args: state.git_repository(**args),
  158. 'grpc_python_deps': lambda *args, **kwargs: None,
  159. }
  160. exec((bazel_file), rules)
  161. assert name not in list(names_and_urls_with_overridden_name.keys())
  162. sys.exit(0)