Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 1 | # Copyright 2015 gRPC authors. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 2 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 6 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 14 | """Buildgen expand filegroups plugin. |
| 15 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 16 | This takes the list of libs from our yaml dictionary, |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 17 | and expands any and all filegroup. |
| 18 | |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | def excluded(filename, exclude_res): |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 23 | for r in exclude_res: |
| 24 | if r.search(filename): |
| 25 | return True |
| 26 | return False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 27 | |
| 28 | |
Craig Tiller | 03915e5 | 2016-04-07 09:15:10 -0700 | [diff] [blame] | 29 | def uniquify(lst): |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 30 | out = [] |
| 31 | for el in lst: |
| 32 | if el not in out: |
| 33 | out.append(el) |
| 34 | return out |
Craig Tiller | 03915e5 | 2016-04-07 09:15:10 -0700 | [diff] [blame] | 35 | |
| 36 | |
Craig Tiller | d1697d9 | 2016-04-05 16:05:46 -0700 | [diff] [blame] | 37 | FILEGROUP_LISTS = ['src', 'headers', 'public_headers', 'deps'] |
| 38 | |
Craig Tiller | d1697d9 | 2016-04-05 16:05:46 -0700 | [diff] [blame] | 39 | FILEGROUP_DEFAULTS = { |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 40 | 'language': 'c', |
| 41 | 'boringssl': False, |
| 42 | 'zlib': False, |
| 43 | 'ares': False, |
Craig Tiller | d1697d9 | 2016-04-05 16:05:46 -0700 | [diff] [blame] | 44 | } |
Craig Tiller | 44cc10b | 2016-03-28 10:45:29 -0700 | [diff] [blame] | 45 | |
| 46 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 47 | def mako_plugin(dictionary): |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 48 | """The exported plugin code for expand_filegroups. |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 49 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 50 | The list of libs in the build.yaml file can contain "filegroups" tags. |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 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 | """ |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 55 | libs = dictionary.get('libs') |
| 56 | targets = dictionary.get('targets') |
| 57 | filegroups_list = dictionary.get('filegroups') |
| 58 | filegroups = {} |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 59 | |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 60 | for fg in filegroups_list: |
Craig Tiller | 44cc10b | 2016-03-28 10:45:29 -0700 | [diff] [blame] | 61 | for lst in FILEGROUP_LISTS: |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 62 | fg[lst] = fg.get(lst, []) |
| 63 | fg['own_%s' % lst] = list(fg[lst]) |
| 64 | for attr, val in FILEGROUP_DEFAULTS.iteritems(): |
| 65 | if attr not in fg: |
| 66 | fg[attr] = val |
Craig Tiller | 44cc10b | 2016-03-28 10:45:29 -0700 | [diff] [blame] | 67 | |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 68 | todo = list(filegroups_list) |
| 69 | skips = 0 |
| 70 | |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 71 | while todo: |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 72 | assert skips != len( |
| 73 | todo), "infinite loop in filegroup uses clauses: %r" % [ |
| 74 | t['name'] for t in todo |
| 75 | ] |
| 76 | # take the first element of the todo list |
| 77 | cur = todo[0] |
| 78 | todo = todo[1:] |
| 79 | # check all uses filegroups are present (if no, skip and come back later) |
| 80 | skip = False |
| 81 | for uses in cur.get('uses', []): |
| 82 | if uses not in filegroups: |
| 83 | skip = True |
| 84 | if skip: |
| 85 | skips += 1 |
| 86 | todo.append(cur) |
| 87 | else: |
| 88 | skips = 0 |
| 89 | assert 'plugins' not in cur |
| 90 | plugins = [] |
| 91 | for uses in cur.get('uses', []): |
| 92 | for plugin in filegroups[uses]['plugins']: |
| 93 | if plugin not in plugins: |
| 94 | plugins.append(plugin) |
| 95 | for lst in FILEGROUP_LISTS: |
| 96 | vals = cur.get(lst, []) |
| 97 | vals.extend(filegroups[uses].get(lst, [])) |
| 98 | cur[lst] = vals |
| 99 | cur_plugin_name = cur.get('plugin') |
| 100 | if cur_plugin_name: |
| 101 | plugins.append(cur_plugin_name) |
| 102 | cur['plugins'] = plugins |
| 103 | filegroups[cur['name']] = cur |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 104 | |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 105 | # build reverse dependency map |
| 106 | things = {} |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 107 | for thing in dictionary['libs'] + dictionary['targets'] + dictionary['filegroups']: |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 108 | things[thing['name']] = thing |
| 109 | thing['used_by'] = [] |
| 110 | thing_deps = lambda t: t.get('uses', []) + t.get('filegroups', []) + t.get('deps', []) |
| 111 | for thing in things.itervalues(): |
| 112 | done = set() |
| 113 | todo = thing_deps(thing) |
| 114 | while todo: |
| 115 | cur = todo[0] |
| 116 | todo = todo[1:] |
| 117 | if cur in done: continue |
| 118 | things[cur]['used_by'].append(thing['name']) |
| 119 | todo.extend(thing_deps(things[cur])) |
| 120 | done.add(cur) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 121 | |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 122 | # the above expansion can introduce duplicate filenames: contract them here |
| 123 | for fg in filegroups.itervalues(): |
| 124 | for lst in FILEGROUP_LISTS: |
| 125 | fg[lst] = uniquify(fg.get(lst, [])) |
Craig Tiller | d1697d9 | 2016-04-05 16:05:46 -0700 | [diff] [blame] | 126 | |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 127 | for tgt in dictionary['targets']: |
| 128 | for lst in FILEGROUP_LISTS: |
| 129 | tgt[lst] = tgt.get(lst, []) |
| 130 | tgt['own_%s' % lst] = list(tgt[lst]) |
| 131 | |
| 132 | for lib in libs + targets: |
| 133 | assert 'plugins' not in lib |
| 134 | plugins = [] |
| 135 | for lst in FILEGROUP_LISTS: |
| 136 | vals = lib.get(lst, []) |
| 137 | lib[lst] = list(vals) |
| 138 | lib['own_%s' % lst] = list(vals) |
| 139 | for fg_name in lib.get('filegroups', []): |
| 140 | fg = filegroups[fg_name] |
| 141 | for plugin in fg['plugins']: |
| 142 | if plugin not in plugins: |
| 143 | plugins.append(plugin) |
| 144 | for lst in FILEGROUP_LISTS: |
| 145 | vals = lib.get(lst, []) |
| 146 | vals.extend(fg.get(lst, [])) |
| 147 | lib[lst] = vals |
| 148 | lib['plugins'] = plugins |
| 149 | if lib.get('generate_plugin_registry', False): |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 150 | lib['src'].append( |
| 151 | 'src/core/plugin_registry/%s_plugin_registry.cc' % lib['name']) |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 152 | for lst in FILEGROUP_LISTS: |
| 153 | lib[lst] = uniquify(lib.get(lst, [])) |