check_attrs.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright 2019 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 attribute validation plugin."""
  15. def anything():
  16. return lambda v: None
  17. def one_of(values):
  18. return lambda v: ('{0} is not in [{1}]'.format(v, values)
  19. if v not in values else None)
  20. def subset_of(values):
  21. return lambda v: ('{0} is not subset of [{1}]'.format(v, values)
  22. if not all(e in values for e in v) else None)
  23. VALID_ATTRIBUTE_KEYS_MAP = {
  24. 'filegroup': {
  25. 'deps': anything(),
  26. 'headers': anything(),
  27. 'plugin': anything(),
  28. 'public_headers': anything(),
  29. 'src': anything(),
  30. 'uses': anything(),
  31. },
  32. 'lib': {
  33. 'asm_src': anything(),
  34. 'baselib': anything(),
  35. 'boringssl': one_of((True,)),
  36. 'build_system': anything(),
  37. 'build': anything(),
  38. 'cmake_target': anything(),
  39. 'defaults': anything(),
  40. 'deps_linkage': one_of(('static',)),
  41. 'deps': anything(),
  42. 'dll': one_of((True, 'only')),
  43. 'filegroups': anything(),
  44. 'generate_plugin_registry': anything(),
  45. 'headers': anything(),
  46. 'language': one_of(('c', 'c++', 'csharp')),
  47. 'LDFLAGS': anything(),
  48. 'platforms': subset_of(('linux', 'mac', 'posix', 'windows')),
  49. 'public_headers': anything(),
  50. 'secure': one_of(('check', True, False)),
  51. 'src': anything(),
  52. 'vs_proj_dir': anything(),
  53. 'zlib': one_of((True,)),
  54. },
  55. 'target': {
  56. 'args': anything(),
  57. 'benchmark': anything(),
  58. 'boringssl': one_of((True,)),
  59. 'build': anything(),
  60. 'ci_platforms': anything(),
  61. 'corpus_dirs': anything(),
  62. 'cpu_cost': anything(),
  63. 'defaults': anything(),
  64. 'deps': anything(),
  65. 'dict': anything(),
  66. 'exclude_configs': anything(),
  67. 'exclude_iomgrs': anything(),
  68. 'excluded_poll_engines': anything(),
  69. 'filegroups': anything(),
  70. 'flaky': one_of((True, False)),
  71. 'gtest': one_of((True, False)),
  72. 'headers': anything(),
  73. 'language': one_of(('c', 'c89', 'c++', 'csharp')),
  74. 'maxlen': anything(),
  75. 'platforms': subset_of(('linux', 'mac', 'posix', 'windows')),
  76. 'run': one_of((True, False)),
  77. 'secure': one_of(('check', True, False)),
  78. 'src': anything(),
  79. 'timeout_seconds': anything(),
  80. 'uses_polling': anything(),
  81. 'vs_proj_dir': anything(),
  82. 'zlib': one_of((True,)),
  83. },
  84. }
  85. def check_attributes(entity, kind, errors):
  86. attributes = VALID_ATTRIBUTE_KEYS_MAP[kind]
  87. name = entity.get('name', anything())
  88. for key, value in list(entity.items()):
  89. if key == 'name':
  90. continue
  91. validator = attributes.get(key)
  92. if validator:
  93. error = validator(value)
  94. if error:
  95. errors.append(
  96. "{0}({1}) has an invalid value for '{2}': {3}".format(
  97. name, kind, key, error))
  98. else:
  99. errors.append("{0}({1}) has an invalid attribute '{2}'".format(
  100. name, kind, key))
  101. def mako_plugin(dictionary):
  102. """The exported plugin code for check_attr.
  103. This validates that filegroups, libs, and target can have only valid
  104. attributes. This is mainly for preventing build.yaml from having
  105. unnecessary and misleading attributes accidentally.
  106. """
  107. errors = []
  108. for filegroup in dictionary.get('filegroups', {}):
  109. check_attributes(filegroup, 'filegroup', errors)
  110. for lib in dictionary.get('libs', {}):
  111. check_attributes(lib, 'lib', errors)
  112. for target in dictionary.get('targets', {}):
  113. check_attributes(target, 'target', errors)
  114. if errors:
  115. raise Exception('\n'.join(errors))