|
19 | 19 | import sys |
20 | 20 |
|
21 | 21 |
|
22 | | -def main(argv): |
23 | | - parser = argparse.ArgumentParser(description=("Generate dbus bindings.")) |
24 | | - |
25 | | - parser.add_argument("--input_file", |
26 | | - required=True, |
27 | | - help="The dbus service definition XML file.") |
28 | | - |
29 | | - parser.add_argument( |
30 | | - "--output_c", |
31 | | - help="The source file to generate containing the GDBus proxy implementation" |
32 | | - ) |
33 | | - |
34 | | - parser.add_argument( |
35 | | - "--output_h", |
36 | | - help="The header file to generate containing the GDBus proxy interface" |
37 | | - ) |
38 | | - |
39 | | - parser.add_argument("--c-namespace", |
40 | | - default=None, |
41 | | - help="Prefix APIs with C namespace") |
42 | | - |
43 | | - parser.add_argument("--interface-prefix", |
44 | | - default=None, |
45 | | - help="Add interface prefix") |
46 | | - |
47 | | - parser.add_argument("--c-generate-object-manager", |
48 | | - default=False, action='store_true', |
49 | | - help="Generate object manager") |
50 | | - |
51 | | - options = parser.parse_args(argv) |
52 | | - |
53 | | - extra_args = [] |
54 | | - if options.c_namespace: |
55 | | - extra_args += ["--c-namespace", options.c_namespace] |
56 | | - |
57 | | - if options.interface_prefix: |
58 | | - extra_args += ["--interface-prefix", options.interface_prefix] |
59 | | - |
60 | | - if options.c_generate_object_manager: |
61 | | - extra_args += ["--c-generate-object-manager"] |
| 22 | +def main(): |
| 23 | + parser = argparse.ArgumentParser(description="Generate D-Bus bindings") |
| 24 | + parser.add_argument("--output-c", |
| 25 | + help="The source file to generate containing the GDBus proxy implementation") |
| 26 | + parser.add_argument("--output-h", |
| 27 | + help="The header file to generate containing the GDBus proxy interface") |
| 28 | + # Parse our options and forward anything else. |
| 29 | + options, extra_args = parser.parse_known_args() |
62 | 30 |
|
63 | 31 | if options.output_c: |
64 | | - gdbus_args = ["gdbus-codegen", "--body", "--output", options.output_c |
65 | | - ] + extra_args + [options.input_file] |
66 | | - subprocess.check_call(gdbus_args) |
67 | | - sed_args = ["sed", "-i", |
68 | | - r"s/config\.h/BuildConfig.h/g", options.output_c] |
69 | | - if sys.platform == "darwin": |
70 | | - sed_args = ["sed", "-i", "", |
71 | | - r"s/config\.h/BuildConfig.h/g", options.output_c] |
72 | | - subprocess.check_call(sed_args) |
| 32 | + subprocess.check_call(["gdbus-codegen", "--body", "--output", options.output_c] + extra_args) |
| 33 | + with open(options.output_c) as f: |
| 34 | + content = f.read() |
| 35 | + # Replace configuration header file generated by gdbus-codegen. |
| 36 | + content = content.replace('include "config.h"', 'include "BuildConfig.h"') |
| 37 | + with open(options.output_c, "w") as f: |
| 38 | + f.write(content) |
73 | 39 |
|
74 | 40 | if options.output_h: |
75 | | - gdbus_args = [ |
76 | | - "gdbus-codegen", "--header", "--output", options.output_h |
77 | | - ] + extra_args + [options.input_file] |
78 | | - subprocess.check_call(gdbus_args) |
| 41 | + subprocess.check_call(["gdbus-codegen", "--header", "--output", options.output_h] + extra_args) |
| 42 | + with open(options.output_h) as f: |
| 43 | + content = f.read() |
| 44 | + # Code generated with new gdbus-codegen adds checks for `GLIB_VERSION_2_84` which might |
| 45 | + # not yet be defined when compiling with older glib versions (e.g. cross-compilation). |
| 46 | + # This manually provided definition solves the problem with undefined identifiers. |
| 47 | + content = content.replace("G_BEGIN_DECLS", """ |
| 48 | +#ifndef GLIB_VERSION_2_84 |
| 49 | +#define GLIB_VERSION_2_84 G_ENCODE_VERSION(2, 84) |
| 50 | +#endif |
| 51 | +G_BEGIN_DECLS |
| 52 | +""") |
| 53 | + with open(options.output_h, "w") as f: |
| 54 | + f.write(content) |
79 | 55 |
|
80 | 56 | return 0 |
81 | 57 |
|
82 | 58 |
|
83 | 59 | if __name__ == '__main__': |
84 | | - sys.exit(main(sys.argv[1:])) |
| 60 | + sys.exit(main()) |
0 commit comments