xref: /aosp_15_r20/external/grpc-grpc/tools/buildgen/plugins/verify_duplicate_sources.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2020 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Buildgen duplicate source validation plugin."""
15
16
17def mako_plugin(dictionary):
18    """The exported plugin code for verify_duplicate_sources.
19
20    This validates that a certain set of libraries don't contain
21    duplicate source files which may cause One Definition Rule (ODR)
22    violation.
23    """
24    errors = []
25    target_groups = (
26        ("gpr", "grpc", "grpc++"),
27        ("gpr", "grpc_unsecure", "grpc++_unsecure"),
28    )
29    lib_map = {lib["name"]: lib for lib in dictionary.get("libs")}
30    for target_group in target_groups:
31        src_map = {}
32        for target in target_group:
33            for src in lib_map[target]["src"]:
34                if src.endswith(".cc"):
35                    if src in src_map:
36                        errors.append(
37                            "Source {0} is used in both {1} and {2}".format(
38                                src, src_map[src], target
39                            )
40                        )
41                    else:
42                        src_map[src] = target
43    if errors:
44        raise Exception("\n".join(errors))
45