check_bazel_workspace.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. 'com_google_libprotobuf_mutator', 'com_github_cncf_udpa'
  48. ]
  49. _GRPC_BAZEL_ONLY_DEPS = [
  50. 'upb', # third_party/upb is checked in locally
  51. 'rules_cc',
  52. 'com_google_absl',
  53. 'io_opencensus_cpp',
  54. _BAZEL_SKYLIB_DEP_NAME,
  55. _BAZEL_TOOLCHAINS_DEP_NAME,
  56. _BAZEL_COMPDB_DEP_NAME,
  57. _TWISTED_TWISTED_DEP_NAME,
  58. _YAML_PYYAML_DEP_NAME,
  59. _TWISTED_INCREMENTAL_DEP_NAME,
  60. _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME,
  61. _TWISTED_CONSTANTLY_DEP_NAME,
  62. 'io_bazel_rules_go',
  63. 'build_bazel_rules_apple',
  64. 'build_bazel_apple_support',
  65. 'com_googlesource_code_re2',
  66. 'bazel_gazelle',
  67. 'opencensus_proto',
  68. 'com_envoyproxy_protoc_gen_validate',
  69. 'com_google_googleapis',
  70. 'com_google_libprotobuf_mutator'
  71. ]
  72. class BazelEvalState(object):
  73. def __init__(self, names_and_urls, overridden_name=None):
  74. self.names_and_urls = names_and_urls
  75. self.overridden_name = overridden_name
  76. def http_archive(self, **args):
  77. self.archive(**args)
  78. def new_http_archive(self, **args):
  79. self.archive(**args)
  80. def bind(self, **args):
  81. pass
  82. def existing_rules(self):
  83. if self.overridden_name:
  84. return [self.overridden_name]
  85. return []
  86. def archive(self, **args):
  87. assert self.names_and_urls.get(args['name']) is None
  88. if args['name'] in _GRPC_BAZEL_ONLY_DEPS:
  89. self.names_and_urls[args['name']] = 'dont care'
  90. return
  91. url = args.get('url', None)
  92. if not url:
  93. # we will only be looking for git commit hashes, so concatenating
  94. # the urls is fine.
  95. url = ' '.join(args['urls'])
  96. self.names_and_urls[args['name']] = url
  97. def git_repository(self, **args):
  98. assert self.names_and_urls.get(args['name']) is None
  99. if args['name'] in _GRPC_BAZEL_ONLY_DEPS:
  100. self.names_and_urls[args['name']] = 'dont care'
  101. return
  102. self.names_and_urls[args['name']] = args['remote']
  103. def grpc_python_deps(self):
  104. pass
  105. # Parse git hashes from bazel/grpc_deps.bzl {new_}http_archive rules
  106. with open(os.path.join('bazel', 'grpc_deps.bzl'), 'r') as f:
  107. names_and_urls = {}
  108. eval_state = BazelEvalState(names_and_urls)
  109. bazel_file = f.read()
  110. # grpc_deps.bzl only defines 'grpc_deps' and 'grpc_test_only_deps', add these
  111. # lines to call them.
  112. bazel_file += '\ngrpc_deps()\n'
  113. bazel_file += '\ngrpc_test_only_deps()\n'
  114. build_rules = {
  115. 'native': eval_state,
  116. 'http_archive': lambda **args: eval_state.http_archive(**args),
  117. 'load': lambda a, b: None,
  118. 'git_repository': lambda **args: eval_state.git_repository(**args),
  119. 'grpc_python_deps': lambda: None,
  120. }
  121. exec((bazel_file), build_rules)
  122. for name in _GRPC_DEP_NAMES:
  123. assert name in list(names_and_urls.keys())
  124. assert len(_GRPC_DEP_NAMES) == len(list(names_and_urls.keys()))
  125. # There are some "bazel-only" deps that are exceptions to this sanity check,
  126. # we don't require that there is a corresponding git module for these.
  127. names_without_bazel_only_deps = list(names_and_urls.keys())
  128. for dep_name in _GRPC_BAZEL_ONLY_DEPS:
  129. names_without_bazel_only_deps.remove(dep_name)
  130. archive_urls = [names_and_urls[name] for name in names_without_bazel_only_deps]
  131. workspace_git_hashes = {
  132. re.search(git_hash_pattern, url).group() for url in archive_urls
  133. }
  134. if len(workspace_git_hashes) == 0:
  135. print("(Likely) parse error, did not find any bazel git dependencies.")
  136. sys.exit(1)
  137. # Validate the equivalence of the git submodules and Bazel git dependencies. The
  138. # condition we impose is that there is a git submodule for every dependency in
  139. # the workspace, but not necessarily conversely. E.g. Bloaty is a dependency
  140. # not used by any of the targets built by Bazel.
  141. if len(workspace_git_hashes - git_submodule_hashes) > 0:
  142. print(
  143. "Found discrepancies between git submodules and Bazel WORKSPACE dependencies"
  144. )
  145. print(("workspace_git_hashes: %s" % workspace_git_hashes))
  146. print(("git_submodule_hashes: %s" % git_submodule_hashes))
  147. print(("workspace_git_hashes - git_submodule_hashes: %s" %
  148. (workspace_git_hashes - git_submodule_hashes)))
  149. sys.exit(1)
  150. # Also check that we can override each dependency
  151. for name in _GRPC_DEP_NAMES:
  152. names_and_urls_with_overridden_name = {}
  153. state = BazelEvalState(names_and_urls_with_overridden_name,
  154. overridden_name=name)
  155. rules = {
  156. 'native': state,
  157. 'http_archive': lambda **args: state.http_archive(**args),
  158. 'load': lambda a, b: None,
  159. 'git_repository': lambda **args: state.git_repository(**args),
  160. 'grpc_python_deps': lambda *args, **kwargs: None,
  161. }
  162. exec((bazel_file), rules)
  163. assert name not in list(names_and_urls_with_overridden_name.keys())
  164. sys.exit(0)