plugin-constructors: Add script to generate constructor registration
[strongswan.git] / src / libstrongswan / plugins / plugin_constructors.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2017 Tobias Brunner
4 # HSR Hochschule fuer Technik Rapperswil
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2 of the License, or (at your
9 # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # for more details.
15
16 import sys
17 from argparse import ArgumentParser
18
19 def generate_output(plugins):
20 """Generate a source file containing plugin constructor registrations"""
21 print("/**")
22 print(" * Register plugin constructors for static libraries")
23 print(" * Created by {0}".format(__file__))
24 print(" */")
25 print("")
26 print("#include <plugins/plugin.h>")
27 print("#include <plugins/plugin_loader.h>")
28 print("")
29
30 for plugin in plugins:
31 print("plugin_t *{0}_plugin_create();".format(plugin.replace('-', '_')))
32
33 print("")
34 print("static void register_plugins() __attribute__ ((constructor));")
35 print("static void register_plugins()")
36 print("{")
37
38 for plugin in plugins:
39 print(' plugin_constructor_register("{0}", {1}_plugin_create);'.format(plugin, plugin.replace('-', '_')))
40
41 print("}")
42
43 print("")
44 print("static void unregister_plugins() __attribute__ ((destructor));")
45 print("static void unregister_plugins()")
46 print("{")
47
48 for plugin in plugins:
49 print(' plugin_constructor_register("{0}", NULL);'.format(plugin))
50
51 print("}")
52 print("")
53
54 parser = ArgumentParser(description = "Generate constructor registration for a list of plugins")
55 parser.add_argument('plugins', metavar="plugin", nargs="*",
56 help = "name of a plugin for which to generate constructor registration")
57
58
59 args = parser.parse_args()
60 generate_output(args.plugins);