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