master
py 49 lines 1.41 KB
Raw
1 #!/usr/bin/env python3
2 #
3 # Extract QEMU Plugin API symbols from a header file
4 #
5 # Copyright 2024 Linaro Ltd
6 #
7 # Author: Pierrick Bouvier <pierrick.bouvier@linaro.org>
8 #
9 # This work is licensed under the terms of the GNU GPL, version 2 or later.
10 # See the COPYING file in the top-level directory.
11 #
12 # SPDX-License-Identifier: GPL-2.0-or-later
13
14 import argparse
15 import re
16
17 def extract_symbols(plugin_header):
18 with open(plugin_header) as file:
19 content = file.read()
20 # Remove QEMU_PLUGIN_API macro definition.
21 content = content.replace('#define QEMU_PLUGIN_API', '')
22 expected = content.count('QEMU_PLUGIN_API')
23 # Find last word between QEMU_PLUGIN_API and ( to get the function name,
24 # matching on several lines. Discard attributes, if any.
25 # We use *? non-greedy quantifier.
26 syms = re.findall(
27 r'QEMU_PLUGIN_API\s+(?:__attribute__\(\(\S+\)\))?.*?(\w+)\s*\(',
28 content,
29 re.DOTALL,
30 )
31 syms.sort()
32 # Ensure we found as many symbols as API markers.
33 assert len(syms) == expected
34 return syms
35
36 def main() -> None:
37 parser = argparse.ArgumentParser(description='Extract QEMU plugin symbols')
38 parser.add_argument('plugin_header', help='Path to QEMU plugin header.')
39 args = parser.parse_args()
40
41 syms = extract_symbols(args.plugin_header)
42
43 print('{')
44 for s in syms:
45 print(" {};".format(s))
46 print('};')
47
48 if __name__ == '__main__':
49 main()