• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    "envoy_api",
63    _BAZEL_SKYLIB_DEP_NAME,
64    _BAZEL_TOOLCHAINS_DEP_NAME,
65    _BAZEL_COMPDB_DEP_NAME,
66    _TWISTED_TWISTED_DEP_NAME,
67    _YAML_PYYAML_DEP_NAME,
68    _TWISTED_INCREMENTAL_DEP_NAME,
69    _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME,
70    _TWISTED_CONSTANTLY_DEP_NAME,
71    "bazel_features",
72    "rules_proto",
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    _BAZEL_SKYLIB_DEP_NAME,
93    _BAZEL_TOOLCHAINS_DEP_NAME,
94    _BAZEL_COMPDB_DEP_NAME,
95    _TWISTED_TWISTED_DEP_NAME,
96    _YAML_PYYAML_DEP_NAME,
97    _TWISTED_INCREMENTAL_DEP_NAME,
98    _ZOPEFOUNDATION_ZOPE_INTERFACE_DEP_NAME,
99    _TWISTED_CONSTANTLY_DEP_NAME,
100    "bazel_features",
101    "rules_proto",
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# Remove bzlmod specific functions
164bazel_file = re.sub(
165    r"^grpc_repo_deps_ext.*$", "", bazel_file, flags=re.MULTILINE
166)
167
168# grpc_deps.bzl only defines 'grpc_deps' and 'grpc_test_only_deps', add these
169# lines to call them.
170bazel_file += "\ngrpc_deps()\n"
171bazel_file += "\ngrpc_test_only_deps()\n"
172build_rules = {
173    "native": eval_state,
174    "http_archive": lambda **args: eval_state.http_archive(**args),
175    "load": lambda a, b: None,
176    "git_repository": lambda **args: eval_state.git_repository(**args),
177    "grpc_python_deps": lambda: None,
178    "Label": lambda a: None,
179}
180exec((bazel_file), build_rules)
181grpc_dep_names_set = set(_GRPC_DEP_NAMES)
182names_set = set(names_and_urls.keys())
183if grpc_dep_names_set != names_set:
184    print("Differences detected between GRPC_DEP_NAMES and grpc_deps.bzl")
185    print("- GRPC_DEP_NAMES only:", grpc_dep_names_set - names_set)
186    print("- grpc_deps.bzl only:", names_set - grpc_dep_names_set)
187    sys.exit(1)
188
189# There are some "bazel-only" deps that are exceptions to this sanity check,
190# we don't require that there is a corresponding git module for these.
191names_without_bazel_only_deps = list(names_and_urls.keys())
192for dep_name in _GRPC_BAZEL_ONLY_DEPS:
193    names_without_bazel_only_deps.remove(dep_name)
194archive_urls = [names_and_urls[name] for name in names_without_bazel_only_deps]
195for url in archive_urls:
196    if re.search(git_hash_pattern, url) is None:
197        print("Cannot find the hash value from url", url)
198        sys.exit(1)
199workspace_git_hashes = {
200    re.search(git_hash_pattern, url).group() for url in archive_urls
201}
202if len(workspace_git_hashes) == 0:
203    print("(Likely) parse error, did not find any bazel git dependencies.")
204    sys.exit(1)
205
206# Validate the equivalence of the git submodules and Bazel git dependencies. The
207# condition we impose is that there is a git submodule for every dependency in
208# the workspace, but not necessarily conversely. E.g. Bloaty is a dependency
209# not used by any of the targets built by Bazel.
210if len(workspace_git_hashes - git_submodule_hashes) > 0:
211    print(
212        "Found discrepancies between git submodules and Bazel WORKSPACE"
213        " dependencies"
214    )
215    print(("workspace_git_hashes: %s" % workspace_git_hashes))
216    print(("git_submodule_hashes: %s" % git_submodule_hashes))
217    print(
218        "workspace_git_hashes - git_submodule_hashes: %s"
219        % (workspace_git_hashes - git_submodule_hashes)
220    )
221    sys.exit(1)
222
223# Also check that we can override each dependency
224for name in _GRPC_DEP_NAMES:
225    names_and_urls_with_overridden_name = {}
226    state = BazelEvalState(
227        names_and_urls_with_overridden_name, overridden_name=name
228    )
229    rules = {
230        "native": state,
231        "http_archive": lambda **args: state.http_archive(**args),
232        "load": lambda a, b: None,
233        "git_repository": lambda **args: state.git_repository(**args),
234        "grpc_python_deps": lambda *args, **kwargs: None,
235        "Label": lambda a: None,
236    }
237    exec((bazel_file), rules)
238    assert name not in list(names_and_urls_with_overridden_name.keys())
239
240sys.exit(0)
241