From fc7942f46b117027b27ba52d8a563b329174d85d Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 6 Feb 2024 13:48:45 +0000 Subject: [PATCH] gdbus-codegen: If writing body to stdout, don't try to include header If we're writing the body to standard output, we cannot know what the filename of the corresponding header is going to be, but it seems vanishingly unlikely that it will be either `stdout.h` (which we would traditionally have generated) or `-.h` (which we would have generated since !3886). This makes some of the output snippets sufficiently short that black(1) requires that they are folded into a single line. Signed-off-by: Simon McVittie --- docs/reference/gio/gdbus-codegen.rst | 12 +++++++++-- gio/gdbus-2.0/codegen/codegen.py | 29 +++++++++++++-------------- gio/gdbus-2.0/codegen/codegen_main.py | 12 +++++++++-- gio/tests/codegen.py | 4 ---- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/docs/reference/gio/gdbus-codegen.rst b/docs/reference/gio/gdbus-codegen.rst index 8c6eab985..7fd5011b1 100644 --- a/docs/reference/gio/gdbus-codegen.rst +++ b/docs/reference/gio/gdbus-codegen.rst @@ -297,8 +297,16 @@ The following options are supported: only one file. Since GLib 2.80, if *OUTFILE* is the literal string ``-``, the header - or source code will be written to standard output. To write to a file - starting with ``-``, it should be prefixed with ``./``. + or source code will be written to standard output. + + For ``--body`` and ``--interface-info-body``, the generated code will not + automatically ``#include`` a corresponding header file when writing to + standard output, because there is no obvious name for that header file. + This might make it necessary to use ``cc -include foo.h``, or generate a + filename like ``foo-impl.h`` and ``#include`` it into a wrapper ``.c`` file. + + In the rare situation that the intended output filename starts with ``-``, + it should be prefixed with ``./``. ``--annotate`` *ELEMENT* *KEY* *VALUE* diff --git a/gio/gdbus-2.0/codegen/codegen.py b/gio/gdbus-2.0/codegen/codegen.py index b1c5fb929..2e8ef8e2a 100644 --- a/gio/gdbus-2.0/codegen/codegen.py +++ b/gio/gdbus-2.0/codegen/codegen.py @@ -1124,15 +1124,13 @@ class InterfaceInfoBodyCodeGenerator: self.outfile.write("\n") self.outfile.write( - "#ifdef HAVE_CONFIG_H\n" - '# include "config.h"\n' - "#endif\n" - "\n" - '#include "%s"\n' - "\n" - "#include \n" % (self.header_name) + "#ifdef HAVE_CONFIG_H\n" '# include "config.h"\n' "#endif\n" "\n" ) - self.outfile.write("\n") + + if self.header_name: + self.outfile.write('#include "%s"\n\n' % (self.header_name)) + + self.outfile.write("#include \n\n") # ---------------------------------------------------------------------------------------------------- @@ -1472,20 +1470,21 @@ class CodeGenerator: def generate_body_preamble(self): basenames = ", ".join(self.input_files_basenames) self.outfile.write(LICENSE_STR.format(config.VERSION, basenames)) + if self.symbol_decoration_define is not None: self.outfile.write("\n") self.outfile.write("#define %s\n" % self.symbol_decoration_define) + self.outfile.write("\n") self.outfile.write( - "#ifdef HAVE_CONFIG_H\n" - '# include "config.h"\n' - "#endif\n" - "\n" - '#include "%s"\n' - "\n" - "#include \n" % (self.header_name) + "#ifdef HAVE_CONFIG_H\n" '# include "config.h"\n' "#endif\n" "\n" ) + if self.header_name: + self.outfile.write('#include "%s"\n\n' % (self.header_name)) + + self.outfile.write("#include \n") + self.outfile.write( "#ifdef G_OS_UNIX\n" "# include \n" "#endif\n" "\n" ) diff --git a/gio/gdbus-2.0/codegen/codegen_main.py b/gio/gdbus-2.0/codegen/codegen_main.py index 58af5c654..d5353f570 100644 --- a/gio/gdbus-2.0/codegen/codegen_main.py +++ b/gio/gdbus-2.0/codegen/codegen_main.py @@ -336,7 +336,11 @@ def codegen_main(): print_error("Using --body requires --output") c_file = args.output - header_name = os.path.splitext(os.path.basename(c_file))[0] + ".h" + + if c_file == "-": + header_name = "" + else: + header_name = os.path.splitext(os.path.basename(c_file))[0] + ".h" elif args.interface_info_header: if args.output is None: print_error("Using --interface-info-header requires --output") @@ -358,7 +362,11 @@ def codegen_main(): ) c_file = args.output - header_name = os.path.splitext(os.path.basename(c_file))[0] + ".h" + + if c_file == "-": + header_name = "" + else: + header_name = os.path.splitext(os.path.basename(c_file))[0] + ".h" # Check the minimum GLib version. The minimum --glib-min-required is 2.30, # because that’s when gdbus-codegen was introduced. Support 1, 2 or 3 diff --git a/gio/tests/codegen.py b/gio/tests/codegen.py index f66b92c8c..a18dcb1a9 100644 --- a/gio/tests/codegen.py +++ b/gio/tests/codegen.py @@ -409,8 +409,6 @@ G_END_DECLS {standard_config_h_include} -#include "-.h" - {standard_header_includes} {private_gvalues_getters} @@ -432,8 +430,6 @@ G_END_DECLS {standard_config_h_include} -#include "-.h" - {interface_info_header_includes}""".format( **result.subs ),