From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44172) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yri58-0007hI-QZ for qemu-devel@nongnu.org; Mon, 11 May 2015 03:24:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yri56-0003ci-6f for qemu-devel@nongnu.org; Mon, 11 May 2015 03:24:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42812) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yri55-0003cS-Sy for qemu-devel@nongnu.org; Mon, 11 May 2015 03:24:52 -0400 From: Markus Armbruster Date: Mon, 11 May 2015 09:24:41 +0200 Message-Id: <1431329083-32543-8-git-send-email-armbru@redhat.com> In-Reply-To: <1431329083-32543-1-git-send-email-armbru@redhat.com> References: <1431329083-32543-1-git-send-email-armbru@redhat.com> Subject: [Qemu-devel] [PATCH 7/9] qapi: Factor open_output(), close_output() out of generators List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: mdroth@linux.vnet.ibm.com Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi-commands.py | 101 +++++++++++++++++------------------------------ scripts/qapi-event.py | 85 ++++++++++++--------------------------- scripts/qapi-types.py | 81 ++++++++++++------------------------- scripts/qapi-visit.py | 101 ++++++++++++++++------------------------------- scripts/qapi.py | 50 +++++++++++++++++++++++ 5 files changed, 172 insertions(+), 246 deletions(-) diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 2889877..c3e420e 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -15,8 +15,6 @@ from ordereddict import OrderedDict from qapi import * import re -import os -import errno def generate_command_decl(name, args, ret_type): arglist="" @@ -311,51 +309,18 @@ qapi_init(qmp_init_marshal); registry=registry.rstrip()) return ret -def gen_command_decl_prologue(header, guard, prefix=""): +def gen_command_decl_prologue(prefix=""): ret = mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QAPI function prototypes - * - * Copyright IBM, Corp. 2011 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - -#ifndef %(guard)s -#define %(guard)s - #include "%(prefix)sqapi-types.h" #include "qapi/qmp/qdict.h" #include "qapi/error.h" ''', - header=basename(header), guard=guardname(header), prefix=prefix) + prefix=prefix) return ret def gen_command_def_prologue(prefix="", proxy=False): ret = mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QMP->QAPI command dispatch - * - * Copyright IBM, Corp. 2011 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - #include "qemu-common.h" #include "qemu/module.h" #include "qapi/qmp/qerror.h" @@ -374,8 +339,6 @@ def gen_command_def_prologue(prefix="", proxy=False): ret += '#include "%sqmp-commands.h"' % prefix return ret + "\n\n" -c_file = 'qmp-marshal.c' -h_file = 'qmp-commands.h' middle_mode = False (input_file, output_dir, do_c, do_h, prefix, opts) = \ @@ -385,29 +348,44 @@ for o, a in opts: if o in ("-m", "--middle"): middle_mode = True -c_file = output_dir + prefix + c_file -h_file = output_dir + prefix + h_file - -def maybe_open(really, name, opt): - if really: - return open(name, opt) - else: - import StringIO - return StringIO.StringIO() - -try: - os.makedirs(output_dir) -except os.error, e: - if e.errno != errno.EEXIST: - raise - exprs = parse_schema(input_file) commands = filter(lambda expr: expr.has_key('command'), exprs) commands = filter(lambda expr: not expr.has_key('gen'), commands) -fdecl = maybe_open(do_h, h_file, 'w') -fdef = maybe_open(do_c, c_file, 'w') -ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix) +c_comment = ''' +/* + * schema-defined QMP->QAPI command dispatch + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' +h_comment = ''' +/* + * schema-defined QAPI function prototypes + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' + +(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, + 'qmp-marshal.c', 'qmp-commands.h', + c_comment, h_comment) + +ret = gen_command_decl_prologue(prefix=prefix) fdecl.write(ret) ret = gen_command_def_prologue(prefix=prefix) fdef.write(ret) @@ -431,13 +409,8 @@ for cmd in commands: ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n" fdef.write(ret) -fdecl.write("\n#endif\n"); - if not middle_mode: ret = gen_registry(commands) fdef.write(ret) -fdef.flush() -fdef.close() -fdecl.flush() -fdecl.close() +close_output(fdef, fdecl) diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index bc5ca4a..56bc602 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -11,8 +11,6 @@ from ordereddict import OrderedDict from qapi import * -import os -import errno def _generate_event_api_name(event_name, params): api_name = "void qapi_event_send_%s(" % c_name(event_name).lower(); @@ -214,36 +212,9 @@ const char *%(event_enum_name)s_lookup[] = { ''') return ret - -# Start the real job - -c_file = 'qapi-event.c' -h_file = 'qapi-event.h' - (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line() -c_file = output_dir + prefix + c_file -h_file = output_dir + prefix + h_file - -try: - os.makedirs(output_dir) -except os.error, e: - if e.errno != errno.EEXIST: - raise - -def maybe_open(really, name, opt): - if really: - return open(name, opt) - else: - import StringIO - return StringIO.StringIO() - -fdef = maybe_open(do_c, c_file, 'w') -fdecl = maybe_open(do_h, h_file, 'w') - -fdef.write(mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - +c_comment = ''' /* * schema-defined QAPI event functions * @@ -256,41 +227,43 @@ fdef.write(mcgen(''' * See the COPYING.LIB file in the top-level directory. * */ +''' +h_comment = ''' +/* + * schema-defined QAPI event functions + * + * Copyright (c) 2014 Wenchao Xia + * + * Authors: + * Wenchao Xia + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' +(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, + 'qapi-event.c', 'qapi-event.h', + c_comment, h_comment) + +fdef.write(mcgen(''' #include "qemu-common.h" -#include "%(header)s" +#include "%(prefix)sqapi-event.h" #include "%(prefix)sqapi-visit.h" #include "qapi/qmp-output-visitor.h" #include "qapi/qmp-event.h" ''', - prefix=prefix, header=basename(h_file))) + prefix=prefix)) fdecl.write(mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QAPI event functions - * - * Copyright (c) 2014 Wenchao Xia - * - * Authors: - * Wenchao Xia - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - -#ifndef %(guard)s -#define %(guard)s - #include "qapi/error.h" #include "qapi/qmp/qdict.h" #include "%(prefix)sqapi-types.h" ''', - prefix=prefix, guard=guardname(h_file))) + prefix=prefix)) exprs = parse_schema(input_file) @@ -323,12 +296,4 @@ fdecl.write(ret) ret = generate_event_enum_lookup(event_enum_name, event_enum_strings) fdef.write(ret) -fdecl.write(''' -#endif -''') - -fdecl.flush() -fdecl.close() - -fdef.flush() -fdef.close() +close_output(fdef, fdecl) diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 62044c1..6bd0b13 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -11,8 +11,6 @@ from ordereddict import OrderedDict from qapi import * -import os -import errno def generate_fwd_struct(name, members, builtin_type=False): if builtin_type: @@ -273,8 +271,6 @@ void qapi_free_%(name)s(%(c_type)s obj) c_type=c_type(name), name=c_name(name)) return ret -c_file = 'qapi-types.c' -h_file = 'qapi-types.h' do_builtins = False (input_file, output_dir, do_c, do_h, prefix, opts) = \ @@ -284,28 +280,7 @@ for o, a in opts: if o in ("-b", "--builtins"): do_builtins = True -c_file = output_dir + prefix + c_file -h_file = output_dir + prefix + h_file - -try: - os.makedirs(output_dir) -except os.error, e: - if e.errno != errno.EEXIST: - raise - -def maybe_open(really, name, opt): - if really: - return open(name, opt) - else: - import StringIO - return StringIO.StringIO() - -fdef = maybe_open(do_c, c_file, 'w') -fdecl = maybe_open(do_h, h_file, 'w') - -fdef.write(mcgen(''' -/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ - +c_comment = ''' /* * deallocation functions for schema-defined QAPI types * @@ -319,37 +294,39 @@ fdef.write(mcgen(''' * See the COPYING.LIB file in the top-level directory. * */ +''' +h_comment = ''' +/* + * schema-defined QAPI types + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' +(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, + 'qapi-types.c', 'qapi-types.h', + c_comment, h_comment) + +fdef.write(mcgen(''' #include "qapi/dealloc-visitor.h" #include "%(prefix)sqapi-types.h" #include "%(prefix)sqapi-visit.h" -''', prefix=prefix)) +''', + prefix=prefix)) fdecl.write(mcgen(''' -/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QAPI types - * - * Copyright IBM, Corp. 2011 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - -#ifndef %(guard)s -#define %(guard)s - #include #include -''', - guard=guardname(h_file))) +''')) exprs = parse_schema(input_file) exprs = filter(lambda expr: not expr.has_key('gen'), exprs) @@ -427,12 +404,4 @@ for expr in exprs: continue fdecl.write(ret) -fdecl.write(''' -#endif -''') - -fdecl.flush() -fdecl.close() - -fdef.flush() -fdef.close() +close_output(fdef, fdecl) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index f3e31cc..ba4be8d 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -15,8 +15,6 @@ from ordereddict import OrderedDict from qapi import * import re -import os -import errno implicit_structs = [] @@ -375,8 +373,6 @@ void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **er ''', name=c_name(name)) -c_file = 'qapi-visit.c' -h_file = 'qapi-visit.h' do_builtins = False (input_file, output_dir, do_c, do_h, prefix, opts) = \ @@ -386,70 +382,51 @@ for o, a in opts: if o in ("-b", "--builtins"): do_builtins = True -c_file = output_dir + prefix + c_file -h_file = output_dir + prefix + h_file +c_comment = ''' +/* + * schema-defined QAPI visitor functions + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' +h_comment = ''' +/* + * schema-defined QAPI visitor functions + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ +''' -try: - os.makedirs(output_dir) -except os.error, e: - if e.errno != errno.EEXIST: - raise - -def maybe_open(really, name, opt): - if really: - return open(name, opt) - else: - import StringIO - return StringIO.StringIO() - -fdef = maybe_open(do_c, c_file, 'w') -fdecl = maybe_open(do_h, h_file, 'w') +(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, + 'qapi-visit.c', 'qapi-visit.h', + c_comment, h_comment) fdef.write(mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QAPI visitor functions - * - * Copyright IBM, Corp. 2011 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - #include "qemu-common.h" -#include "%(header)s" +#include "%(prefix)sqapi-visit.h" ''', - header=basename(h_file))) + prefix = prefix)) fdecl.write(mcgen(''' -/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ - -/* - * schema-defined QAPI visitor functions - * - * Copyright IBM, Corp. 2011 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. - * See the COPYING.LIB file in the top-level directory. - * - */ - -#ifndef %(guard)s -#define %(guard)s - #include "qapi/visitor.h" #include "%(prefix)sqapi-types.h" ''', - prefix=prefix, guard=guardname(h_file))) + prefix=prefix)) exprs = parse_schema(input_file) @@ -505,12 +482,4 @@ for expr in exprs: ret += generate_enum_declaration(expr['enum'], expr['data']) fdecl.write(ret) -fdecl.write(''' -#endif -''') - -fdecl.flush() -fdecl.close() - -fdef.flush() -fdef.close() +close_output(fdef, fdecl) diff --git a/scripts/qapi.py b/scripts/qapi.py index 79d1e52..1f68fa9 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -13,6 +13,7 @@ import re from ordereddict import OrderedDict +import errno import getopt import os import sys @@ -1002,3 +1003,52 @@ def parse_command_line(extra_options = "", extra_long_options = []): input_file = args[0] return (input_file, output_dir, do_c, do_h, prefix, extra_opts) + +def open_output(output_dir, do_c, do_h, prefix, c_file, h_file, + c_comment, h_comment): + c_file = output_dir + prefix + c_file + h_file = output_dir + prefix + h_file + + try: + os.makedirs(output_dir) + except os.error, e: + if e.errno != errno.EEXIST: + raise + + def maybe_open(really, name, opt): + if really: + return open(name, opt) + else: + import StringIO + return StringIO.StringIO() + + fdef = maybe_open(do_c, c_file, 'w') + fdecl = maybe_open(do_h, h_file, 'w') + + fdef.write(mcgen(''' +/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ +%(comment)s +''', + comment = c_comment)) + + fdecl.write(mcgen(''' +/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ +%(comment)s +#ifndef %(guard)s +#define %(guard)s + +''', + comment = h_comment, guard = guardname(h_file))) + + return (fdef, fdecl) + +def close_output(fdef, fdecl): + fdecl.write(''' +#endif +''') + + fdecl.flush() + fdecl.close() + + fdef.flush() + fdef.close() -- 1.9.3