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