[glib] gdbus-codegen: Support for separate C header and code generation



commit e4d68c7b3e8b01ab1a4231bf6da21d045cb5a816
Author: Iñigo Martínez <inigomartinez gmail com>
Date:   Thu Jan 4 14:18:07 2018 +0100

    gdbus-codegen: Support for separate C header and code generation
    
    gdbus-codegen's options only allow a simultaneous header and source
    code generation.
    
    A `--header` and `--body` options have been added along with the
    `--output` option which allow separate C header and code
    generation.
    
    These options cannot be used in addition to the old options such
    as `--generate-c-code`, `--generate-docbook` or
    `--output-directory`.
    
    These options have also been added to gdbus-codegen's documentation.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791015

 docs/reference/gio/gdbus-codegen.xml  |   52 ++++++++++++++++++++++++++++++
 gio/gdbus-2.0/codegen/codegen_main.py |   56 ++++++++++++++++++++++++++-------
 2 files changed, 96 insertions(+), 12 deletions(-)
---
diff --git a/docs/reference/gio/gdbus-codegen.xml b/docs/reference/gio/gdbus-codegen.xml
index 9d5f3ce..67b139f 100644
--- a/docs/reference/gio/gdbus-codegen.xml
+++ b/docs/reference/gio/gdbus-codegen.xml
@@ -37,6 +37,9 @@
     <arg><option>--generate-docbook</option> <replaceable>OUTFILES</replaceable></arg>
     <arg><option>--pragma-once</option></arg>
     <arg><option>--xml-files</option> <replaceable>FILE</replaceable></arg>
+    <arg><option>--header</option></arg>
+    <arg><option>--body</option></arg>
+    <arg><option>--output</option> <replaceable>OUTFILE</replaceable></arg>
     <group choice="plain" rep="repeat">
       <arg>
         <option>--annotate</option>
@@ -263,6 +266,55 @@
     </varlistentry>
 
     <varlistentry>
+      <term><option>--header</option></term>
+      <listitem>
+        <para>
+          If this option is passed, it will generate the header code and write it to the disk by
+          using the path and file name provided by <option>--output</option>.
+        </para>
+        <para>
+          Using <option>--generate-c-code</option>, <option>--generate-docbook</option> or
+          <option>--output-directory</option> are not allowed to be used along with
+          <option>--header</option> and <option>--body</option> options, because these options
+          are used to generate only one file.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><option>--body</option></term>
+      <listitem>
+        <para>
+          If this option is passed, it will generate the source code and write it to the disk by
+          using the path and file name provided by <option>--output</option>.
+        </para>
+        <para>
+          Using <option>--generate-c-code</option>, <option>--generate-docbook</option> or
+          <option>--output-directory</option> are not allowed to be used along with
+          <option>--header</option> and <option>--body</option> options, because these options
+          are used to generate only one file.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><option>--output</option> <replaceable>OUTFILE</replaceable></term>
+      <listitem>
+        <para>
+          The full path where the header (<option>--header</option>) or the source code
+          (<option>--body</option>) will be written, using the path and filename provided by
+          <option>--output</option>. The full path could be something like
+          <literal>$($OUTFILE).{c,h}</literal>.
+        </para>
+        <para>
+          Using <option>--generate-c-code</option>, <option>--generate-docbook</option> or
+          <option>--output-directory</option> is not allowed along with
+          <option>--output</option>, because the latter is used to generate only one file.
+        </para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
       <term><option>--annotate</option> <replaceable>ELEMENT</replaceable> <replaceable>KEY</replaceable> 
<replaceable>VALUE</replaceable></term>
       <listitem>
         <para>
diff --git a/gio/gdbus-2.0/codegen/codegen_main.py b/gio/gdbus-2.0/codegen/codegen_main.py
index a650e2e..baf4266 100755
--- a/gio/gdbus-2.0/codegen/codegen_main.py
+++ b/gio/gdbus-2.0/codegen/codegen_main.py
@@ -3,6 +3,7 @@
 # GDBus - GLib D-Bus Library
 #
 # Copyright (C) 2008-2011 Red Hat, Inc.
+# Copyright (C) 2018 Iñigo Martínez <inigomartinez gmail com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -20,8 +21,8 @@
 # Author: David Zeuthen <davidz redhat com>
 
 import argparse
+import os
 import sys
-from os import path
 
 from . import config
 from . import dbustypes
@@ -157,8 +158,6 @@ def codegen_main():
                             help='The namespace to use for generated C code')
     arg_parser.add_argument('--c-generate-object-manager', action='store_true',
                             help='Generate a GDBusObjectManagerClient subclass when generating C code')
-    arg_parser.add_argument('--generate-c-code', metavar='OUTFILES',
-                            help='Generate C code in OUTFILES.[ch]')
     arg_parser.add_argument('--c-generate-autocleanup', choices=['none', 'objects', 'all'], 
default='objects',
                             help='Generate autocleanup support')
     arg_parser.add_argument('--generate-docbook', metavar='OUTFILES',
@@ -167,13 +166,49 @@ def codegen_main():
                             help='Use "pragma once" as the inclusion guard')
     arg_parser.add_argument('--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE',
                             help='Add annotation (may be used several times)')
-    arg_parser.add_argument('--output-directory', metavar='OUTDIR', default='',
-                            help='Location to output generated files')
+
+    group = arg_parser.add_mutually_exclusive_group()
+    group.add_argument('--generate-c-code', metavar='OUTFILES',
+                       help='Generate C code in OUTFILES.[ch]')
+    group.add_argument('--header', action='store_true',
+                       help='Generate C headers')
+    group.add_argument('--body', action='store_true',
+                       help='Generate C code')
+
+    group = arg_parser.add_mutually_exclusive_group()
+    group.add_argument('--output', metavar='FILE',
+                       help='Write output into the specified file')
+    group.add_argument('--output-directory', metavar='OUTDIR', default='',
+                       help='Location to output generated files')
+
     args = arg_parser.parse_args();
 
     if len(args.xml_files) > 0:
         print_warning('The "--xml-files" option is deprecated; use positional arguments instead')
 
+    if ((args.generate_c_code is not None or args.generate_docbook is not None) and
+            args.output is not None):
+        print_error('Using --generate-c-code or --generate-docbook and '
+                    '--output at the same time is not allowed')
+
+    if args.generate_c_code:
+        outdir = args.output_directory
+        header_name = args.generate_c_code + '.h'
+        h_file = os.path.join(outdir, header_name)
+        args.header = True
+        c_file = os.path.join(outdir, args.generate_c_code + '.c')
+        args.body = True
+    else:
+        if args.output is None:
+            print_error('Using --header or --body requires --output')
+
+        if args.header:
+            h_file = args.output
+            header_name = os.path.basename(h_file)
+        elif args.body:
+            c_file = args.output
+            header_name = os.path.splitext(c_file)[0] + '.h'
+
     all_ifaces = []
     for fname in args.files + args.xml_files:
         with open(fname, 'rb') as f:
@@ -187,17 +222,13 @@ def codegen_main():
     for i in all_ifaces:
         i.post_process(args.interface_prefix, args.c_namespace)
 
-    outdir = args.output_directory
-
     docbook = args.generate_docbook
     docbook_gen = codegen_docbook.DocbookCodeGenerator(all_ifaces);
     if docbook:
         ret = docbook_gen.generate(docbook, outdir)
 
-    c_code = args.generate_c_code
-    if c_code:
-        header_name = c_code + '.h'
-        with open(path.join(outdir, header_name), 'w') as outfile:
+    if args.header:
+        with open(h_file, 'w') as outfile:
             gen = codegen.HeaderCodeGenerator(all_ifaces,
                                               args.c_namespace,
                                               args.c_generate_object_manager,
@@ -207,7 +238,8 @@ def codegen_main():
                                               outfile)
             gen.generate()
 
-        with open(path.join(outdir, c_code + '.c'), 'w') as outfile:
+    if args.body:
+        with open(c_file, 'w') as outfile:
             gen = codegen.CodeGenerator(all_ifaces,
                                         args.c_namespace,
                                         args.c_generate_object_manager,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]