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