verify_duplicate_sources.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. def mako_plugin(dictionary):
  16. """The exported plugin code for verify_duplicate_sources.
  17. This validates that a certain set of libraries don't contain
  18. duplicate source files which may cause One Definition Rule (ODR)
  19. violation.
  20. """
  21. errors = []
  22. target_groups = (
  23. ('gpr', 'grpc', 'grpc++'),
  24. ('gpr', 'grpc_unsecure', 'grpc++_unsecure'),
  25. )
  26. lib_map = {lib['name']: lib for lib in dictionary.get('libs')}
  27. for target_group in target_groups:
  28. src_map = {}
  29. for target in target_group:
  30. for src in lib_map[target]['src']:
  31. if src.endswith('.cc'):
  32. if src in src_map:
  33. errors.append(
  34. 'Source {0} is used in both {1} and {2}'.format(
  35. src, src_map[src], target))
  36. else:
  37. src_map[src] = target
  38. if errors:
  39. raise Exception('\n'.join(errors))