Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | """Generate coroutine wrappers for block subsystem. |
| 3 | |
| 4 | The program parses one or several concatenated c files from stdin, |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 5 | searches for functions with the 'co_wrapper' specifier |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 6 | and generates corresponding wrappers on stdout. |
| 7 | |
| 8 | Usage: block-coroutine-wrapper.py generated-file.c FILE.[ch]... |
| 9 | |
| 10 | Copyright (c) 2020 Virtuozzo International GmbH. |
| 11 | |
| 12 | This program is free software; you can redistribute it and/or modify |
| 13 | it under the terms of the GNU General Public License as published by |
| 14 | the Free Software Foundation; either version 2 of the License, or |
| 15 | (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | """ |
| 25 | |
| 26 | import sys |
| 27 | import re |
| 28 | from typing import Iterator |
| 29 | |
| 30 | |
| 31 | def gen_header(): |
| 32 | copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL) |
| 33 | copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE) |
| 34 | copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE) |
| 35 | return f"""\ |
| 36 | /* |
| 37 | * File is generated by scripts/block-coroutine-wrapper.py |
| 38 | * |
| 39 | {copyright} |
| 40 | */ |
| 41 | |
| 42 | #include "qemu/osdep.h" |
| 43 | #include "block/coroutines.h" |
| 44 | #include "block/block-gen.h" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 45 | #include "block/block_int.h" |
| 46 | #include "block/dirty-bitmap.h" |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 47 | """ |
| 48 | |
| 49 | |
| 50 | class ParamDecl: |
| 51 | param_re = re.compile(r'(?P<decl>' |
| 52 | r'(?P<type>.*[ *])' |
| 53 | r'(?P<name>[a-z][a-z0-9_]*)' |
| 54 | r')') |
| 55 | |
| 56 | def __init__(self, param_decl: str) -> None: |
| 57 | m = self.param_re.match(param_decl.strip()) |
| 58 | if m is None: |
| 59 | raise ValueError(f'Wrong parameter declaration: "{param_decl}"') |
| 60 | self.decl = m.group('decl') |
| 61 | self.type = m.group('type') |
| 62 | self.name = m.group('name') |
| 63 | |
| 64 | |
| 65 | class FuncDecl: |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 66 | def __init__(self, wrapper_type: str, return_type: str, name: str, |
| 67 | args: str, variant: str) -> None: |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 68 | self.return_type = return_type.strip() |
| 69 | self.name = name.strip() |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 70 | self.struct_name = snake_to_camel(self.name) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 71 | self.args = [ParamDecl(arg.strip()) for arg in args.split(',')] |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 72 | self.create_only_co = 'mixed' not in variant |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 73 | self.graph_rdlock = 'bdrv_rdlock' in variant |
Kevin Wolf | de90329 | 2023-09-11 11:46:05 +0200 | [diff] [blame] | 74 | self.graph_wrlock = 'bdrv_wrlock' in variant |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 75 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 76 | self.wrapper_type = wrapper_type |
| 77 | |
| 78 | if wrapper_type == 'co': |
Kevin Wolf | de90329 | 2023-09-11 11:46:05 +0200 | [diff] [blame] | 79 | if self.graph_wrlock: |
| 80 | raise ValueError(f"co function can't be wrlock: {self.name}") |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 81 | subsystem, subname = self.name.split('_', 1) |
| 82 | self.target_name = f'{subsystem}_co_{subname}' |
| 83 | else: |
| 84 | assert wrapper_type == 'no_co' |
| 85 | subsystem, co_infix, subname = self.name.split('_', 2) |
| 86 | if co_infix != 'co': |
| 87 | raise ValueError(f"Invalid no_co function name: {self.name}") |
| 88 | if not self.create_only_co: |
| 89 | raise ValueError(f"no_co function can't be mixed: {self.name}") |
Kevin Wolf | e84c07b | 2023-09-29 16:51:37 +0200 | [diff] [blame] | 90 | if self.graph_rdlock and self.graph_wrlock: |
| 91 | raise ValueError("function can't be both rdlock and wrlock: " |
| 92 | f"{self.name}") |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 93 | self.target_name = f'{subsystem}_{subname}' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 94 | |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 95 | self.get_result = 's->ret = ' |
| 96 | self.ret = 'return s.ret;' |
| 97 | self.co_ret = 'return ' |
| 98 | self.return_field = self.return_type + " ret;" |
| 99 | if self.return_type == 'void': |
| 100 | self.get_result = '' |
| 101 | self.ret = '' |
| 102 | self.co_ret = '' |
| 103 | self.return_field = '' |
| 104 | |
Kevin Wolf | dea97c1 | 2023-05-25 14:47:02 +0200 | [diff] [blame] | 105 | def gen_ctx(self, prefix: str = '') -> str: |
| 106 | t = self.args[0].type |
Kevin Wolf | d218434 | 2023-09-11 11:46:06 +0200 | [diff] [blame] | 107 | name = self.args[0].name |
Kevin Wolf | dea97c1 | 2023-05-25 14:47:02 +0200 | [diff] [blame] | 108 | if t == 'BlockDriverState *': |
Kevin Wolf | d218434 | 2023-09-11 11:46:06 +0200 | [diff] [blame] | 109 | return f'bdrv_get_aio_context({prefix}{name})' |
Kevin Wolf | dea97c1 | 2023-05-25 14:47:02 +0200 | [diff] [blame] | 110 | elif t == 'BdrvChild *': |
Kevin Wolf | d218434 | 2023-09-11 11:46:06 +0200 | [diff] [blame] | 111 | return f'bdrv_get_aio_context({prefix}{name}->bs)' |
Kevin Wolf | dea97c1 | 2023-05-25 14:47:02 +0200 | [diff] [blame] | 112 | elif t == 'BlockBackend *': |
Kevin Wolf | d218434 | 2023-09-11 11:46:06 +0200 | [diff] [blame] | 113 | return f'blk_get_aio_context({prefix}{name})' |
Kevin Wolf | dea97c1 | 2023-05-25 14:47:02 +0200 | [diff] [blame] | 114 | else: |
| 115 | return 'qemu_get_aio_context()' |
| 116 | |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 117 | def gen_list(self, format: str) -> str: |
| 118 | return ', '.join(format.format_map(arg.__dict__) for arg in self.args) |
| 119 | |
| 120 | def gen_block(self, format: str) -> str: |
| 121 | return '\n'.join(format.format_map(arg.__dict__) for arg in self.args) |
| 122 | |
| 123 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 124 | # Match wrappers declared with a co_wrapper mark |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 125 | func_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)' |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 126 | r'(\s*coroutine_fn)?' |
| 127 | r'\s*(?P<wrapper_type>(no_)?co)_wrapper' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 128 | r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*' |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 129 | r'(?P<wrapper_name>[a-z][a-z0-9_]*)' |
| 130 | r'\((?P<args>[^)]*)\);$', re.MULTILINE) |
| 131 | |
| 132 | |
| 133 | def func_decl_iter(text: str) -> Iterator: |
| 134 | for m in func_decl_re.finditer(text): |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 135 | yield FuncDecl(wrapper_type=m.group('wrapper_type'), |
| 136 | return_type=m.group('return_type'), |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 137 | name=m.group('wrapper_name'), |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 138 | args=m.group('args'), |
| 139 | variant=m.group('variant')) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 140 | |
| 141 | |
| 142 | def snake_to_camel(func_name: str) -> str: |
| 143 | """ |
| 144 | Convert underscore names like 'some_function_name' to camel-case like |
| 145 | 'SomeFunctionName' |
| 146 | """ |
| 147 | words = func_name.split('_') |
| 148 | words = [w[0].upper() + w[1:] for w in words] |
| 149 | return ''.join(words) |
| 150 | |
| 151 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 152 | def create_mixed_wrapper(func: FuncDecl) -> str: |
| 153 | """ |
| 154 | Checks if we are already in coroutine |
| 155 | """ |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 156 | name = func.target_name |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 157 | struct_name = func.struct_name |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 158 | graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else '' |
| 159 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 160 | return f"""\ |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 161 | {func.return_type} {func.name}({ func.gen_list('{decl}') }) |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 162 | {{ |
| 163 | if (qemu_in_coroutine()) {{ |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 164 | {graph_assume_lock} |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 165 | {func.co_ret}{name}({ func.gen_list('{name}') }); |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 166 | }} else {{ |
| 167 | {struct_name} s = {{ |
Stefan Hajnoczi | c12887e | 2023-09-12 19:10:37 -0400 | [diff] [blame] | 168 | .poll_state.ctx = qemu_get_current_aio_context(), |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 169 | .poll_state.in_progress = true, |
| 170 | |
| 171 | { func.gen_block(' .{name} = {name},') } |
| 172 | }}; |
| 173 | |
| 174 | s.poll_state.co = qemu_coroutine_create({name}_entry, &s); |
| 175 | |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 176 | bdrv_poll_co(&s.poll_state); |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 177 | {func.ret} |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 178 | }} |
| 179 | }}""" |
| 180 | |
| 181 | |
| 182 | def create_co_wrapper(func: FuncDecl) -> str: |
| 183 | """ |
| 184 | Assumes we are not in coroutine, and creates one |
| 185 | """ |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 186 | name = func.target_name |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 187 | struct_name = func.struct_name |
| 188 | return f"""\ |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 189 | {func.return_type} {func.name}({ func.gen_list('{decl}') }) |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 190 | {{ |
| 191 | {struct_name} s = {{ |
Stefan Hajnoczi | c12887e | 2023-09-12 19:10:37 -0400 | [diff] [blame] | 192 | .poll_state.ctx = qemu_get_current_aio_context(), |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 193 | .poll_state.in_progress = true, |
| 194 | |
| 195 | { func.gen_block(' .{name} = {name},') } |
| 196 | }}; |
| 197 | assert(!qemu_in_coroutine()); |
| 198 | |
| 199 | s.poll_state.co = qemu_coroutine_create({name}_entry, &s); |
| 200 | |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 201 | bdrv_poll_co(&s.poll_state); |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 202 | {func.ret} |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 203 | }}""" |
| 204 | |
| 205 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 206 | def gen_co_wrapper(func: FuncDecl) -> str: |
Vladimir Sementsov-Ogievskiy | bb43694 | 2021-06-10 13:07:57 +0300 | [diff] [blame] | 207 | assert not '_co_' in func.name |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 208 | assert func.wrapper_type == 'co' |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 209 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 210 | name = func.target_name |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 211 | struct_name = func.struct_name |
Vladimir Sementsov-Ogievskiy | bb43694 | 2021-06-10 13:07:57 +0300 | [diff] [blame] | 212 | |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 213 | graph_lock='' |
| 214 | graph_unlock='' |
| 215 | if func.graph_rdlock: |
| 216 | graph_lock=' bdrv_graph_co_rdlock();' |
| 217 | graph_unlock=' bdrv_graph_co_rdunlock();' |
| 218 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 219 | creation_function = create_mixed_wrapper |
| 220 | if func.create_only_co: |
| 221 | creation_function = create_co_wrapper |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 222 | |
| 223 | return f"""\ |
| 224 | /* |
| 225 | * Wrappers for {name} |
| 226 | */ |
| 227 | |
| 228 | typedef struct {struct_name} {{ |
| 229 | BdrvPollCo poll_state; |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 230 | {func.return_field} |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 231 | { func.gen_block(' {decl};') } |
| 232 | }} {struct_name}; |
| 233 | |
| 234 | static void coroutine_fn {name}_entry(void *opaque) |
| 235 | {{ |
| 236 | {struct_name} *s = opaque; |
| 237 | |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 238 | {graph_lock} |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 239 | {func.get_result}{name}({ func.gen_list('s->{name}') }); |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 240 | {graph_unlock} |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 241 | s->poll_state.in_progress = false; |
| 242 | |
| 243 | aio_wait_kick(); |
| 244 | }} |
| 245 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 246 | {creation_function(func)}""" |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 247 | |
| 248 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 249 | def gen_no_co_wrapper(func: FuncDecl) -> str: |
| 250 | assert '_co_' in func.name |
| 251 | assert func.wrapper_type == 'no_co' |
| 252 | |
| 253 | name = func.target_name |
| 254 | struct_name = func.struct_name |
| 255 | |
Kevin Wolf | de90329 | 2023-09-11 11:46:05 +0200 | [diff] [blame] | 256 | graph_lock='' |
| 257 | graph_unlock='' |
Kevin Wolf | e84c07b | 2023-09-29 16:51:37 +0200 | [diff] [blame] | 258 | if func.graph_rdlock: |
| 259 | graph_lock=' bdrv_graph_rdlock_main_loop();' |
| 260 | graph_unlock=' bdrv_graph_rdunlock_main_loop();' |
| 261 | elif func.graph_wrlock: |
Stefan Hajnoczi | 6bc30f1 | 2023-12-05 13:20:02 -0500 | [diff] [blame] | 262 | graph_lock=' bdrv_graph_wrlock();' |
| 263 | graph_unlock=' bdrv_graph_wrunlock();' |
Kevin Wolf | de90329 | 2023-09-11 11:46:05 +0200 | [diff] [blame] | 264 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 265 | return f"""\ |
| 266 | /* |
| 267 | * Wrappers for {name} |
| 268 | */ |
| 269 | |
| 270 | typedef struct {struct_name} {{ |
| 271 | Coroutine *co; |
| 272 | {func.return_field} |
| 273 | { func.gen_block(' {decl};') } |
| 274 | }} {struct_name}; |
| 275 | |
| 276 | static void {name}_bh(void *opaque) |
| 277 | {{ |
| 278 | {struct_name} *s = opaque; |
| 279 | |
Kevin Wolf | de90329 | 2023-09-11 11:46:05 +0200 | [diff] [blame] | 280 | {graph_lock} |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 281 | {func.get_result}{name}({ func.gen_list('s->{name}') }); |
Kevin Wolf | de90329 | 2023-09-11 11:46:05 +0200 | [diff] [blame] | 282 | {graph_unlock} |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 283 | |
| 284 | aio_co_wake(s->co); |
| 285 | }} |
| 286 | |
| 287 | {func.return_type} coroutine_fn {func.name}({ func.gen_list('{decl}') }) |
| 288 | {{ |
| 289 | {struct_name} s = {{ |
| 290 | .co = qemu_coroutine_self(), |
| 291 | { func.gen_block(' .{name} = {name},') } |
| 292 | }}; |
| 293 | assert(qemu_in_coroutine()); |
| 294 | |
| 295 | aio_bh_schedule_oneshot(qemu_get_aio_context(), {name}_bh, &s); |
| 296 | qemu_coroutine_yield(); |
| 297 | |
| 298 | {func.ret} |
| 299 | }}""" |
| 300 | |
| 301 | |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 302 | def gen_wrappers(input_code: str) -> str: |
| 303 | res = '' |
| 304 | for func in func_decl_iter(input_code): |
| 305 | res += '\n\n\n' |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame] | 306 | if func.wrapper_type == 'co': |
| 307 | res += gen_co_wrapper(func) |
| 308 | else: |
| 309 | res += gen_no_co_wrapper(func) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 310 | |
| 311 | return res |
| 312 | |
| 313 | |
| 314 | if __name__ == '__main__': |
| 315 | if len(sys.argv) < 3: |
| 316 | exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') |
| 317 | |
| 318 | with open(sys.argv[1], 'w', encoding='utf-8') as f_out: |
| 319 | f_out.write(gen_header()) |
| 320 | for fname in sys.argv[2:]: |
| 321 | with open(fname, encoding='utf-8') as f_in: |
| 322 | f_out.write(gen_wrappers(f_in.read())) |
| 323 | f_out.write('\n') |