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 <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2024-02-06 13:48:45 +00:00
parent 5e8f053d33
commit fc7942f46b
4 changed files with 34 additions and 23 deletions

View File

@ -297,8 +297,16 @@ The following options are supported:
only one file. only one file.
Since GLib 2.80, if *OUTFILE* is the literal string ``-``, the header 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 or source code will be written to standard output.
starting with ``-``, it should be prefixed with ``./``.
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* ``--annotate`` *ELEMENT* *KEY* *VALUE*

View File

@ -1124,15 +1124,13 @@ class InterfaceInfoBodyCodeGenerator:
self.outfile.write("\n") self.outfile.write("\n")
self.outfile.write( self.outfile.write(
"#ifdef HAVE_CONFIG_H\n" "#ifdef HAVE_CONFIG_H\n" '# include "config.h"\n' "#endif\n" "\n"
'# include "config.h"\n'
"#endif\n"
"\n"
'#include "%s"\n'
"\n"
"#include <string.h>\n" % (self.header_name)
) )
self.outfile.write("\n")
if self.header_name:
self.outfile.write('#include "%s"\n\n' % (self.header_name))
self.outfile.write("#include <string.h>\n\n")
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
@ -1472,20 +1470,21 @@ class CodeGenerator:
def generate_body_preamble(self): def generate_body_preamble(self):
basenames = ", ".join(self.input_files_basenames) basenames = ", ".join(self.input_files_basenames)
self.outfile.write(LICENSE_STR.format(config.VERSION, basenames)) self.outfile.write(LICENSE_STR.format(config.VERSION, basenames))
if self.symbol_decoration_define is not None: if self.symbol_decoration_define is not None:
self.outfile.write("\n") self.outfile.write("\n")
self.outfile.write("#define %s\n" % self.symbol_decoration_define) self.outfile.write("#define %s\n" % self.symbol_decoration_define)
self.outfile.write("\n") self.outfile.write("\n")
self.outfile.write( self.outfile.write(
"#ifdef HAVE_CONFIG_H\n" "#ifdef HAVE_CONFIG_H\n" '# include "config.h"\n' "#endif\n" "\n"
'# include "config.h"\n'
"#endif\n"
"\n"
'#include "%s"\n'
"\n"
"#include <string.h>\n" % (self.header_name)
) )
if self.header_name:
self.outfile.write('#include "%s"\n\n' % (self.header_name))
self.outfile.write("#include <string.h>\n")
self.outfile.write( self.outfile.write(
"#ifdef G_OS_UNIX\n" "# include <gio/gunixfdlist.h>\n" "#endif\n" "\n" "#ifdef G_OS_UNIX\n" "# include <gio/gunixfdlist.h>\n" "#endif\n" "\n"
) )

View File

@ -336,7 +336,11 @@ def codegen_main():
print_error("Using --body requires --output") print_error("Using --body requires --output")
c_file = args.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: elif args.interface_info_header:
if args.output is None: if args.output is None:
print_error("Using --interface-info-header requires --output") print_error("Using --interface-info-header requires --output")
@ -358,7 +362,11 @@ def codegen_main():
) )
c_file = args.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"
# Check the minimum GLib version. The minimum --glib-min-required is 2.30, # Check the minimum GLib version. The minimum --glib-min-required is 2.30,
# because thats when gdbus-codegen was introduced. Support 1, 2 or 3 # because thats when gdbus-codegen was introduced. Support 1, 2 or 3

View File

@ -409,8 +409,6 @@ G_END_DECLS
{standard_config_h_include} {standard_config_h_include}
#include "-.h"
{standard_header_includes} {standard_header_includes}
{private_gvalues_getters} {private_gvalues_getters}
@ -432,8 +430,6 @@ G_END_DECLS
{standard_config_h_include} {standard_config_h_include}
#include "-.h"
{interface_info_header_includes}""".format( {interface_info_header_includes}""".format(
**result.subs **result.subs
), ),