Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / tools / buildgen / plugins / expand_filegroups.py
1 # Copyright 2015 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 expand filegroups plugin.
15
16 This takes the list of libs from our yaml dictionary,
17 and expands any and all filegroup.
18
19 """
20
21
22 def excluded(filename, exclude_res):
23     for r in exclude_res:
24         if r.search(filename):
25             return True
26     return False
27
28
29 def uniquify(lst):
30     out = []
31     for el in lst:
32         if el not in out:
33             out.append(el)
34     return out
35
36
37 FILEGROUP_LISTS = ['src', 'headers', 'public_headers', 'deps']
38
39 FILEGROUP_DEFAULTS = {
40     'language': 'c',
41     'boringssl': False,
42     'zlib': False,
43     'ares': False,
44 }
45
46
47 def mako_plugin(dictionary):
48     """The exported plugin code for expand_filegroups.
49
50   The list of libs in the build.yaml file can contain "filegroups" tags.
51   These refer to the filegroups in the root object. We will expand and
52   merge filegroups on the src, headers and public_headers properties.
53
54   """
55     libs = dictionary.get('libs')
56     targets = dictionary.get('targets')
57     filegroups_list = dictionary.get('filegroups')
58     filegroups_set = set(fg['name'] for fg in filegroups_list)
59     filegroups = {}
60
61     for fg in filegroups_list:
62         for lst in FILEGROUP_LISTS:
63             fg[lst] = fg.get(lst, [])
64             fg['own_%s' % lst] = list(fg[lst])
65         for attr, val in FILEGROUP_DEFAULTS.items():
66             if attr not in fg:
67                 fg[attr] = val
68
69     todo = list(filegroups_list)
70     skips = 0
71
72     while todo:
73         assert skips != len(
74             todo), "infinite loop in filegroup uses clauses: %r" % [
75                 t['name'] for t in todo
76             ]
77         # take the first element of the todo list
78         cur = todo[0]
79         todo = todo[1:]
80         # check all uses filegroups are present (if no, skip and come back later)
81         skip = False
82         for use in cur.get('uses', []):
83             assert use in filegroups_set, (
84                 "filegroup(%s) uses non-existent %s" % (cur['name'], use))
85             if use not in filegroups:
86                 skip = True
87         if skip:
88             skips += 1
89             todo.append(cur)
90         else:
91             skips = 0
92             assert 'plugins' not in cur
93             plugins = []
94             for uses in cur.get('uses', []):
95                 for plugin in filegroups[uses]['plugins']:
96                     if plugin not in plugins:
97                         plugins.append(plugin)
98                 for lst in FILEGROUP_LISTS:
99                     vals = cur.get(lst, [])
100                     vals.extend(filegroups[uses].get(lst, []))
101                     cur[lst] = vals
102             cur_plugin_name = cur.get('plugin')
103             if cur_plugin_name:
104                 plugins.append(cur_plugin_name)
105             cur['plugins'] = plugins
106             filegroups[cur['name']] = cur
107
108     # build reverse dependency map
109     things = {}
110     for thing in dictionary['libs'] + dictionary['targets'] + dictionary[
111             'filegroups']:
112         things[thing['name']] = thing
113         thing['used_by'] = []
114     thing_deps = lambda t: t.get('uses', []) + t.get('filegroups', []) + t.get(
115         'deps', [])
116     for thing in things.values():
117         done = set()
118         todo = thing_deps(thing)
119         while todo:
120             cur = todo[0]
121             todo = todo[1:]
122             if cur in done:
123                 continue
124             things[cur]['used_by'].append(thing['name'])
125             todo.extend(thing_deps(things[cur]))
126             done.add(cur)
127
128     # the above expansion can introduce duplicate filenames: contract them here
129     for fg in filegroups.values():
130         for lst in FILEGROUP_LISTS:
131             fg[lst] = uniquify(fg.get(lst, []))
132
133     for tgt in dictionary['targets']:
134         for lst in FILEGROUP_LISTS:
135             tgt[lst] = tgt.get(lst, [])
136             tgt['own_%s' % lst] = list(tgt[lst])
137
138     for lib in libs + targets:
139         assert 'plugins' not in lib
140         plugins = []
141         for lst in FILEGROUP_LISTS:
142             vals = lib.get(lst, [])
143             lib[lst] = list(vals)
144             lib['own_%s' % lst] = list(vals)
145         for fg_name in lib.get('filegroups', []):
146             fg = filegroups[fg_name]
147             for plugin in fg['plugins']:
148                 if plugin not in plugins:
149                     plugins.append(plugin)
150             for lst in FILEGROUP_LISTS:
151                 vals = lib.get(lst, [])
152                 vals.extend(fg.get(lst, []))
153                 lib[lst] = vals
154             lib['plugins'] = plugins
155         if lib.get('generate_plugin_registry', False):
156             lib['src'].append('src/core/plugin_registry/%s_plugin_registry.cc' %
157                               lib['name'])
158         for lst in FILEGROUP_LISTS:
159             lib[lst] = uniquify(lib.get(lst, []))