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