• 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    'external_proto_library': {
93        'destination': anything(),
94        'proto_prefix': anything(),
95        'urls': anything(),
96        'hash': anything(),
97        'strip_prefix': anything(),
98    }
99}
100
101
102def check_attributes(entity, kind, errors):
103    attributes = VALID_ATTRIBUTE_KEYS_MAP[kind]
104    name = entity.get('name', anything())
105    for key, value in list(entity.items()):
106        if key == 'name':
107            continue
108        validator = attributes.get(key)
109        if validator:
110            error = validator(value)
111            if error:
112                errors.append(
113                    "{0}({1}) has an invalid value for '{2}': {3}".format(
114                        name, kind, key, error))
115        else:
116            errors.append("{0}({1}) has an invalid attribute '{2}'".format(
117                name, kind, key))
118
119
120def mako_plugin(dictionary):
121    """The exported plugin code for check_attr.
122
123    This validates that filegroups, libs, and target can have only valid
124    attributes. This is mainly for preventing build.yaml from having
125    unnecessary and misleading attributes accidentally.
126    """
127
128    errors = []
129    for filegroup in dictionary.get('filegroups', {}):
130        check_attributes(filegroup, 'filegroup', errors)
131    for lib in dictionary.get('libs', {}):
132        check_attributes(lib, 'lib', errors)
133    for target in dictionary.get('targets', {}):
134        check_attributes(target, 'target', errors)
135    for target in dictionary.get('external_proto_libraries', {}):
136        check_attributes(target, 'external_proto_library', errors)
137    if errors:
138        raise Exception('\n'.join(errors))
139