1aaaa20b6SVladimir Sementsov-Ogievskiy#! /usr/bin/env python3 2aaaa20b6SVladimir Sementsov-Ogievskiy"""Generate coroutine wrappers for block subsystem. 3aaaa20b6SVladimir Sementsov-Ogievskiy 4aaaa20b6SVladimir Sementsov-OgievskiyThe program parses one or several concatenated c files from stdin, 576a2f554SEmanuele Giuseppe Espositosearches for functions with the 'co_wrapper' specifier 6aaaa20b6SVladimir Sementsov-Ogievskiyand generates corresponding wrappers on stdout. 7aaaa20b6SVladimir Sementsov-Ogievskiy 8aaaa20b6SVladimir Sementsov-OgievskiyUsage: block-coroutine-wrapper.py generated-file.c FILE.[ch]... 9aaaa20b6SVladimir Sementsov-Ogievskiy 10aaaa20b6SVladimir Sementsov-OgievskiyCopyright (c) 2020 Virtuozzo International GmbH. 11aaaa20b6SVladimir Sementsov-Ogievskiy 12aaaa20b6SVladimir Sementsov-OgievskiyThis program is free software; you can redistribute it and/or modify 13aaaa20b6SVladimir Sementsov-Ogievskiyit under the terms of the GNU General Public License as published by 14aaaa20b6SVladimir Sementsov-Ogievskiythe Free Software Foundation; either version 2 of the License, or 15aaaa20b6SVladimir Sementsov-Ogievskiy(at your option) any later version. 16aaaa20b6SVladimir Sementsov-Ogievskiy 17aaaa20b6SVladimir Sementsov-OgievskiyThis program is distributed in the hope that it will be useful, 18aaaa20b6SVladimir Sementsov-Ogievskiybut WITHOUT ANY WARRANTY; without even the implied warranty of 19aaaa20b6SVladimir Sementsov-OgievskiyMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20aaaa20b6SVladimir Sementsov-OgievskiyGNU General Public License for more details. 21aaaa20b6SVladimir Sementsov-Ogievskiy 22aaaa20b6SVladimir Sementsov-OgievskiyYou should have received a copy of the GNU General Public License 23aaaa20b6SVladimir Sementsov-Ogievskiyalong with this program. If not, see <http://www.gnu.org/licenses/>. 24aaaa20b6SVladimir Sementsov-Ogievskiy""" 25aaaa20b6SVladimir Sementsov-Ogievskiy 26aaaa20b6SVladimir Sementsov-Ogievskiyimport sys 27aaaa20b6SVladimir Sementsov-Ogievskiyimport re 28aaaa20b6SVladimir Sementsov-Ogievskiyfrom typing import Iterator 29aaaa20b6SVladimir Sementsov-Ogievskiy 30aaaa20b6SVladimir Sementsov-Ogievskiy 31aaaa20b6SVladimir Sementsov-Ogievskiydef gen_header(): 32aaaa20b6SVladimir Sementsov-Ogievskiy copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL) 33aaaa20b6SVladimir Sementsov-Ogievskiy copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE) 34aaaa20b6SVladimir Sementsov-Ogievskiy copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE) 35aaaa20b6SVladimir Sementsov-Ogievskiy return f"""\ 36aaaa20b6SVladimir Sementsov-Ogievskiy/* 37aaaa20b6SVladimir Sementsov-Ogievskiy * File is generated by scripts/block-coroutine-wrapper.py 38aaaa20b6SVladimir Sementsov-Ogievskiy * 39aaaa20b6SVladimir Sementsov-Ogievskiy{copyright} 40aaaa20b6SVladimir Sementsov-Ogievskiy */ 41aaaa20b6SVladimir Sementsov-Ogievskiy 42aaaa20b6SVladimir Sementsov-Ogievskiy#include "qemu/osdep.h" 43aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/coroutines.h" 44aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/block-gen.h" 45e2c1c34fSMarkus Armbruster#include "block/block_int.h" 46e2c1c34fSMarkus Armbruster#include "block/dirty-bitmap.h" 47aaaa20b6SVladimir Sementsov-Ogievskiy""" 48aaaa20b6SVladimir Sementsov-Ogievskiy 49aaaa20b6SVladimir Sementsov-Ogievskiy 50aaaa20b6SVladimir Sementsov-Ogievskiyclass ParamDecl: 51aaaa20b6SVladimir Sementsov-Ogievskiy param_re = re.compile(r'(?P<decl>' 52aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<type>.*[ *])' 53aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<name>[a-z][a-z0-9_]*)' 54aaaa20b6SVladimir Sementsov-Ogievskiy r')') 55aaaa20b6SVladimir Sementsov-Ogievskiy 56aaaa20b6SVladimir Sementsov-Ogievskiy def __init__(self, param_decl: str) -> None: 57aaaa20b6SVladimir Sementsov-Ogievskiy m = self.param_re.match(param_decl.strip()) 58aaaa20b6SVladimir Sementsov-Ogievskiy if m is None: 59aaaa20b6SVladimir Sementsov-Ogievskiy raise ValueError(f'Wrong parameter declaration: "{param_decl}"') 60aaaa20b6SVladimir Sementsov-Ogievskiy self.decl = m.group('decl') 61aaaa20b6SVladimir Sementsov-Ogievskiy self.type = m.group('type') 62aaaa20b6SVladimir Sementsov-Ogievskiy self.name = m.group('name') 63aaaa20b6SVladimir Sementsov-Ogievskiy 64aaaa20b6SVladimir Sementsov-Ogievskiy 65aaaa20b6SVladimir Sementsov-Ogievskiyclass FuncDecl: 66d6ee2e32SKevin Wolf def __init__(self, wrapper_type: str, return_type: str, name: str, 67d6ee2e32SKevin Wolf args: str, variant: str) -> None: 68aaaa20b6SVladimir Sementsov-Ogievskiy self.return_type = return_type.strip() 69aaaa20b6SVladimir Sementsov-Ogievskiy self.name = name.strip() 7076a2f554SEmanuele Giuseppe Esposito self.struct_name = snake_to_camel(self.name) 71aaaa20b6SVladimir Sementsov-Ogievskiy self.args = [ParamDecl(arg.strip()) for arg in args.split(',')] 7276a2f554SEmanuele Giuseppe Esposito self.create_only_co = 'mixed' not in variant 73e6d3f7a6SEmanuele Giuseppe Esposito self.graph_rdlock = 'bdrv_rdlock' in variant 74de903298SKevin Wolf self.graph_wrlock = 'bdrv_wrlock' in variant 7576a2f554SEmanuele Giuseppe Esposito 76d6ee2e32SKevin Wolf self.wrapper_type = wrapper_type 77d6ee2e32SKevin Wolf 78d6ee2e32SKevin Wolf if wrapper_type == 'co': 79de903298SKevin Wolf if self.graph_wrlock: 80de903298SKevin Wolf raise ValueError(f"co function can't be wrlock: {self.name}") 8176a2f554SEmanuele Giuseppe Esposito subsystem, subname = self.name.split('_', 1) 82d6ee2e32SKevin Wolf self.target_name = f'{subsystem}_co_{subname}' 83d6ee2e32SKevin Wolf else: 84d6ee2e32SKevin Wolf assert wrapper_type == 'no_co' 85d6ee2e32SKevin Wolf subsystem, co_infix, subname = self.name.split('_', 2) 86d6ee2e32SKevin Wolf if co_infix != 'co': 87d6ee2e32SKevin Wolf raise ValueError(f"Invalid no_co function name: {self.name}") 88d6ee2e32SKevin Wolf if not self.create_only_co: 89d6ee2e32SKevin Wolf raise ValueError(f"no_co function can't be mixed: {self.name}") 90d6ee2e32SKevin Wolf if self.graph_rdlock: 91d6ee2e32SKevin Wolf raise ValueError(f"no_co function can't be rdlock: {self.name}") 92d6ee2e32SKevin Wolf self.target_name = f'{subsystem}_{subname}' 9376a2f554SEmanuele Giuseppe Esposito 94dea97c1fSKevin Wolf self.ctx = self.gen_ctx() 95aaaa20b6SVladimir Sementsov-Ogievskiy 965b317b8dSEmanuele Giuseppe Esposito self.get_result = 's->ret = ' 975b317b8dSEmanuele Giuseppe Esposito self.ret = 'return s.ret;' 985b317b8dSEmanuele Giuseppe Esposito self.co_ret = 'return ' 995b317b8dSEmanuele Giuseppe Esposito self.return_field = self.return_type + " ret;" 1005b317b8dSEmanuele Giuseppe Esposito if self.return_type == 'void': 1015b317b8dSEmanuele Giuseppe Esposito self.get_result = '' 1025b317b8dSEmanuele Giuseppe Esposito self.ret = '' 1035b317b8dSEmanuele Giuseppe Esposito self.co_ret = '' 1045b317b8dSEmanuele Giuseppe Esposito self.return_field = '' 1055b317b8dSEmanuele Giuseppe Esposito 106dea97c1fSKevin Wolf def gen_ctx(self, prefix: str = '') -> str: 107dea97c1fSKevin Wolf t = self.args[0].type 108*d2184349SKevin Wolf name = self.args[0].name 109dea97c1fSKevin Wolf if t == 'BlockDriverState *': 110*d2184349SKevin Wolf return f'bdrv_get_aio_context({prefix}{name})' 111dea97c1fSKevin Wolf elif t == 'BdrvChild *': 112*d2184349SKevin Wolf return f'bdrv_get_aio_context({prefix}{name}->bs)' 113dea97c1fSKevin Wolf elif t == 'BlockBackend *': 114*d2184349SKevin Wolf return f'blk_get_aio_context({prefix}{name})' 115dea97c1fSKevin Wolf else: 116dea97c1fSKevin Wolf return 'qemu_get_aio_context()' 117dea97c1fSKevin Wolf 118aaaa20b6SVladimir Sementsov-Ogievskiy def gen_list(self, format: str) -> str: 119aaaa20b6SVladimir Sementsov-Ogievskiy return ', '.join(format.format_map(arg.__dict__) for arg in self.args) 120aaaa20b6SVladimir Sementsov-Ogievskiy 121aaaa20b6SVladimir Sementsov-Ogievskiy def gen_block(self, format: str) -> str: 122aaaa20b6SVladimir Sementsov-Ogievskiy return '\n'.join(format.format_map(arg.__dict__) for arg in self.args) 123aaaa20b6SVladimir Sementsov-Ogievskiy 124aaaa20b6SVladimir Sementsov-Ogievskiy 12576a2f554SEmanuele Giuseppe Esposito# Match wrappers declared with a co_wrapper mark 1266700dfb1SEmanuele Giuseppe Espositofunc_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)' 127d6ee2e32SKevin Wolf r'(\s*coroutine_fn)?' 128d6ee2e32SKevin Wolf r'\s*(?P<wrapper_type>(no_)?co)_wrapper' 12976a2f554SEmanuele Giuseppe Esposito r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*' 130aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<wrapper_name>[a-z][a-z0-9_]*)' 131aaaa20b6SVladimir Sementsov-Ogievskiy r'\((?P<args>[^)]*)\);$', re.MULTILINE) 132aaaa20b6SVladimir Sementsov-Ogievskiy 133aaaa20b6SVladimir Sementsov-Ogievskiy 134aaaa20b6SVladimir Sementsov-Ogievskiydef func_decl_iter(text: str) -> Iterator: 135aaaa20b6SVladimir Sementsov-Ogievskiy for m in func_decl_re.finditer(text): 136d6ee2e32SKevin Wolf yield FuncDecl(wrapper_type=m.group('wrapper_type'), 137d6ee2e32SKevin Wolf return_type=m.group('return_type'), 138aaaa20b6SVladimir Sementsov-Ogievskiy name=m.group('wrapper_name'), 13976a2f554SEmanuele Giuseppe Esposito args=m.group('args'), 14076a2f554SEmanuele Giuseppe Esposito variant=m.group('variant')) 141aaaa20b6SVladimir Sementsov-Ogievskiy 142aaaa20b6SVladimir Sementsov-Ogievskiy 143aaaa20b6SVladimir Sementsov-Ogievskiydef snake_to_camel(func_name: str) -> str: 144aaaa20b6SVladimir Sementsov-Ogievskiy """ 145aaaa20b6SVladimir Sementsov-Ogievskiy Convert underscore names like 'some_function_name' to camel-case like 146aaaa20b6SVladimir Sementsov-Ogievskiy 'SomeFunctionName' 147aaaa20b6SVladimir Sementsov-Ogievskiy """ 148aaaa20b6SVladimir Sementsov-Ogievskiy words = func_name.split('_') 149aaaa20b6SVladimir Sementsov-Ogievskiy words = [w[0].upper() + w[1:] for w in words] 150aaaa20b6SVladimir Sementsov-Ogievskiy return ''.join(words) 151aaaa20b6SVladimir Sementsov-Ogievskiy 152aaaa20b6SVladimir Sementsov-Ogievskiy 15376a2f554SEmanuele Giuseppe Espositodef create_mixed_wrapper(func: FuncDecl) -> str: 15476a2f554SEmanuele Giuseppe Esposito """ 15576a2f554SEmanuele Giuseppe Esposito Checks if we are already in coroutine 15676a2f554SEmanuele Giuseppe Esposito """ 157d6ee2e32SKevin Wolf name = func.target_name 15876a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 159e6d3f7a6SEmanuele Giuseppe Esposito graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else '' 160e6d3f7a6SEmanuele Giuseppe Esposito 16176a2f554SEmanuele Giuseppe Esposito return f"""\ 1626700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') }) 16376a2f554SEmanuele Giuseppe Esposito{{ 16476a2f554SEmanuele Giuseppe Esposito if (qemu_in_coroutine()) {{ 165e6d3f7a6SEmanuele Giuseppe Esposito {graph_assume_lock} 1665b317b8dSEmanuele Giuseppe Esposito {func.co_ret}{name}({ func.gen_list('{name}') }); 16776a2f554SEmanuele Giuseppe Esposito }} else {{ 16876a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 1690582fb82SEmanuele Giuseppe Esposito .poll_state.ctx = {func.ctx}, 17076a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 17176a2f554SEmanuele Giuseppe Esposito 17276a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 17376a2f554SEmanuele Giuseppe Esposito }}; 17476a2f554SEmanuele Giuseppe Esposito 17576a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 17676a2f554SEmanuele Giuseppe Esposito 1776700dfb1SEmanuele Giuseppe Esposito bdrv_poll_co(&s.poll_state); 1785b317b8dSEmanuele Giuseppe Esposito {func.ret} 17976a2f554SEmanuele Giuseppe Esposito }} 18076a2f554SEmanuele Giuseppe Esposito}}""" 18176a2f554SEmanuele Giuseppe Esposito 18276a2f554SEmanuele Giuseppe Esposito 18376a2f554SEmanuele Giuseppe Espositodef create_co_wrapper(func: FuncDecl) -> str: 18476a2f554SEmanuele Giuseppe Esposito """ 18576a2f554SEmanuele Giuseppe Esposito Assumes we are not in coroutine, and creates one 18676a2f554SEmanuele Giuseppe Esposito """ 187d6ee2e32SKevin Wolf name = func.target_name 18876a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 18976a2f554SEmanuele Giuseppe Esposito return f"""\ 1906700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') }) 19176a2f554SEmanuele Giuseppe Esposito{{ 19276a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 1930582fb82SEmanuele Giuseppe Esposito .poll_state.ctx = {func.ctx}, 19476a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 19576a2f554SEmanuele Giuseppe Esposito 19676a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 19776a2f554SEmanuele Giuseppe Esposito }}; 19876a2f554SEmanuele Giuseppe Esposito assert(!qemu_in_coroutine()); 19976a2f554SEmanuele Giuseppe Esposito 20076a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 20176a2f554SEmanuele Giuseppe Esposito 2026700dfb1SEmanuele Giuseppe Esposito bdrv_poll_co(&s.poll_state); 2035b317b8dSEmanuele Giuseppe Esposito {func.ret} 20476a2f554SEmanuele Giuseppe Esposito}}""" 20576a2f554SEmanuele Giuseppe Esposito 20676a2f554SEmanuele Giuseppe Esposito 207d6ee2e32SKevin Wolfdef gen_co_wrapper(func: FuncDecl) -> str: 208bb436948SVladimir Sementsov-Ogievskiy assert not '_co_' in func.name 209d6ee2e32SKevin Wolf assert func.wrapper_type == 'co' 210aaaa20b6SVladimir Sementsov-Ogievskiy 211d6ee2e32SKevin Wolf name = func.target_name 21276a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 213bb436948SVladimir Sementsov-Ogievskiy 214e6d3f7a6SEmanuele Giuseppe Esposito graph_lock='' 215e6d3f7a6SEmanuele Giuseppe Esposito graph_unlock='' 216e6d3f7a6SEmanuele Giuseppe Esposito if func.graph_rdlock: 217e6d3f7a6SEmanuele Giuseppe Esposito graph_lock=' bdrv_graph_co_rdlock();' 218e6d3f7a6SEmanuele Giuseppe Esposito graph_unlock=' bdrv_graph_co_rdunlock();' 219e6d3f7a6SEmanuele Giuseppe Esposito 22076a2f554SEmanuele Giuseppe Esposito creation_function = create_mixed_wrapper 22176a2f554SEmanuele Giuseppe Esposito if func.create_only_co: 22276a2f554SEmanuele Giuseppe Esposito creation_function = create_co_wrapper 223aaaa20b6SVladimir Sementsov-Ogievskiy 224aaaa20b6SVladimir Sementsov-Ogievskiy return f"""\ 225aaaa20b6SVladimir Sementsov-Ogievskiy/* 226aaaa20b6SVladimir Sementsov-Ogievskiy * Wrappers for {name} 227aaaa20b6SVladimir Sementsov-Ogievskiy */ 228aaaa20b6SVladimir Sementsov-Ogievskiy 229aaaa20b6SVladimir Sementsov-Ogievskiytypedef struct {struct_name} {{ 230aaaa20b6SVladimir Sementsov-Ogievskiy BdrvPollCo poll_state; 2315b317b8dSEmanuele Giuseppe Esposito {func.return_field} 232aaaa20b6SVladimir Sementsov-Ogievskiy{ func.gen_block(' {decl};') } 233aaaa20b6SVladimir Sementsov-Ogievskiy}} {struct_name}; 234aaaa20b6SVladimir Sementsov-Ogievskiy 235aaaa20b6SVladimir Sementsov-Ogievskiystatic void coroutine_fn {name}_entry(void *opaque) 236aaaa20b6SVladimir Sementsov-Ogievskiy{{ 237aaaa20b6SVladimir Sementsov-Ogievskiy {struct_name} *s = opaque; 238aaaa20b6SVladimir Sementsov-Ogievskiy 239e6d3f7a6SEmanuele Giuseppe Esposito{graph_lock} 2405b317b8dSEmanuele Giuseppe Esposito {func.get_result}{name}({ func.gen_list('s->{name}') }); 241e6d3f7a6SEmanuele Giuseppe Esposito{graph_unlock} 242aaaa20b6SVladimir Sementsov-Ogievskiy s->poll_state.in_progress = false; 243aaaa20b6SVladimir Sementsov-Ogievskiy 244aaaa20b6SVladimir Sementsov-Ogievskiy aio_wait_kick(); 245aaaa20b6SVladimir Sementsov-Ogievskiy}} 246aaaa20b6SVladimir Sementsov-Ogievskiy 24776a2f554SEmanuele Giuseppe Esposito{creation_function(func)}""" 248aaaa20b6SVladimir Sementsov-Ogievskiy 249aaaa20b6SVladimir Sementsov-Ogievskiy 250d6ee2e32SKevin Wolfdef gen_no_co_wrapper(func: FuncDecl) -> str: 251d6ee2e32SKevin Wolf assert '_co_' in func.name 252d6ee2e32SKevin Wolf assert func.wrapper_type == 'no_co' 253d6ee2e32SKevin Wolf 254d6ee2e32SKevin Wolf name = func.target_name 255d6ee2e32SKevin Wolf struct_name = func.struct_name 256d6ee2e32SKevin Wolf 257de903298SKevin Wolf graph_lock='' 258de903298SKevin Wolf graph_unlock='' 259de903298SKevin Wolf if func.graph_wrlock: 260de903298SKevin Wolf graph_lock=' bdrv_graph_wrlock(NULL);' 261de903298SKevin Wolf graph_unlock=' bdrv_graph_wrunlock();' 262de903298SKevin Wolf 263d6ee2e32SKevin Wolf return f"""\ 264d6ee2e32SKevin Wolf/* 265d6ee2e32SKevin Wolf * Wrappers for {name} 266d6ee2e32SKevin Wolf */ 267d6ee2e32SKevin Wolf 268d6ee2e32SKevin Wolftypedef struct {struct_name} {{ 269d6ee2e32SKevin Wolf Coroutine *co; 270d6ee2e32SKevin Wolf {func.return_field} 271d6ee2e32SKevin Wolf{ func.gen_block(' {decl};') } 272d6ee2e32SKevin Wolf}} {struct_name}; 273d6ee2e32SKevin Wolf 274d6ee2e32SKevin Wolfstatic void {name}_bh(void *opaque) 275d6ee2e32SKevin Wolf{{ 276d6ee2e32SKevin Wolf {struct_name} *s = opaque; 277dea97c1fSKevin Wolf AioContext *ctx = {func.gen_ctx('s->')}; 278d6ee2e32SKevin Wolf 279de903298SKevin Wolf{graph_lock} 280dea97c1fSKevin Wolf aio_context_acquire(ctx); 281d6ee2e32SKevin Wolf {func.get_result}{name}({ func.gen_list('s->{name}') }); 282dea97c1fSKevin Wolf aio_context_release(ctx); 283de903298SKevin Wolf{graph_unlock} 284d6ee2e32SKevin Wolf 285d6ee2e32SKevin Wolf aio_co_wake(s->co); 286d6ee2e32SKevin Wolf}} 287d6ee2e32SKevin Wolf 288d6ee2e32SKevin Wolf{func.return_type} coroutine_fn {func.name}({ func.gen_list('{decl}') }) 289d6ee2e32SKevin Wolf{{ 290d6ee2e32SKevin Wolf {struct_name} s = {{ 291d6ee2e32SKevin Wolf .co = qemu_coroutine_self(), 292d6ee2e32SKevin Wolf{ func.gen_block(' .{name} = {name},') } 293d6ee2e32SKevin Wolf }}; 294d6ee2e32SKevin Wolf assert(qemu_in_coroutine()); 295d6ee2e32SKevin Wolf 296d6ee2e32SKevin Wolf aio_bh_schedule_oneshot(qemu_get_aio_context(), {name}_bh, &s); 297d6ee2e32SKevin Wolf qemu_coroutine_yield(); 298d6ee2e32SKevin Wolf 299d6ee2e32SKevin Wolf {func.ret} 300d6ee2e32SKevin Wolf}}""" 301d6ee2e32SKevin Wolf 302d6ee2e32SKevin Wolf 303aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrappers(input_code: str) -> str: 304aaaa20b6SVladimir Sementsov-Ogievskiy res = '' 305aaaa20b6SVladimir Sementsov-Ogievskiy for func in func_decl_iter(input_code): 306aaaa20b6SVladimir Sementsov-Ogievskiy res += '\n\n\n' 307d6ee2e32SKevin Wolf if func.wrapper_type == 'co': 308d6ee2e32SKevin Wolf res += gen_co_wrapper(func) 309d6ee2e32SKevin Wolf else: 310d6ee2e32SKevin Wolf res += gen_no_co_wrapper(func) 311aaaa20b6SVladimir Sementsov-Ogievskiy 312aaaa20b6SVladimir Sementsov-Ogievskiy return res 313aaaa20b6SVladimir Sementsov-Ogievskiy 314aaaa20b6SVladimir Sementsov-Ogievskiy 315aaaa20b6SVladimir Sementsov-Ogievskiyif __name__ == '__main__': 316aaaa20b6SVladimir Sementsov-Ogievskiy if len(sys.argv) < 3: 317aaaa20b6SVladimir Sementsov-Ogievskiy exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') 318aaaa20b6SVladimir Sementsov-Ogievskiy 319aaaa20b6SVladimir Sementsov-Ogievskiy with open(sys.argv[1], 'w', encoding='utf-8') as f_out: 320aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_header()) 321aaaa20b6SVladimir Sementsov-Ogievskiy for fname in sys.argv[2:]: 322aaaa20b6SVladimir Sementsov-Ogievskiy with open(fname, encoding='utf-8') as f_in: 323aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_wrappers(f_in.read())) 324aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write('\n') 325