From: Fam Zheng <famz@redhat.com>
To: Amos Kong <akong@redhat.com>
Cc: mdroth@linux.vnet.ibm.com, qiaonuohan@cn.fujitsu.com,
qemu-devel@nongnu.org, xiawenc@linux.vnet.ibm.com,
lcapitulino@redhat.com
Subject: Re: [Qemu-devel] [PATCH v4 2/5] qapi: add qapi-introspect.py code generator
Date: Fri, 24 Jan 2014 17:12:12 +0800 [thread overview]
Message-ID: <20140124091212.GB12401@T430.nay.redhat.com> (raw)
In-Reply-To: <1390488396-16538-3-git-send-email-akong@redhat.com>
On Thu, 01/23 22:46, Amos Kong wrote:
> This is a code generator for qapi introspection. It will parse
> qapi-schema.json, extend schema definitions and generate a schema
> table with metadata, it references to the new structs which we used
> to describe dynamic data structs. The metadata will help C code to
> allocate right structs and provide useful information to management
> to checking suported feature and QMP commandline detail. The schema
> table will be saved to qapi-introspect.h.
>
> The $(prefix) is used to as a namespace to keep the generated code
s/used to as/used as/
> from one schema/code-generation separated from others so code and
> be generated from multiple schemas with clobbering previously
s/with/without/
> created code.
>
> Signed-off-by: Amos Kong <akong@redhat.com>
> ---
> .gitignore | 1 +
> Makefile | 5 +-
> docs/qmp-full-introspection.txt | 17 ++++
> scripts/qapi-introspect.py | 172 ++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 194 insertions(+), 1 deletion(-)
> create mode 100644 scripts/qapi-introspect.py
>
> diff --git a/.gitignore b/.gitignore
> index 1c9d63d..de3cb80 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -22,6 +22,7 @@ linux-headers/asm
> qapi-generated
> qapi-types.[ch]
> qapi-visit.[ch]
> +qapi-introspect.h
> qmp-commands.h
> qmp-marshal.c
> qemu-doc.html
> diff --git a/Makefile b/Makefile
> index bdff4e4..1dac5e7 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -45,7 +45,7 @@ endif
> endif
>
> GENERATED_HEADERS = config-host.h qemu-options.def
> -GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h
> +GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h qapi-introspect.h
> GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c
>
> GENERATED_HEADERS += trace/generated-events.h
> @@ -229,6 +229,9 @@ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
> qmp-commands.h qmp-marshal.c :\
> $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
> $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -m -o "." < $<, " GEN $@")
> +qapi-introspect.h:\
> +$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
> + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py $(gen-out-type) -o "." < $<, " GEN $@")
>
> QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
> $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
> diff --git a/docs/qmp-full-introspection.txt b/docs/qmp-full-introspection.txt
> index d2cf7b3..8ecbc0c 100644
> --- a/docs/qmp-full-introspection.txt
> +++ b/docs/qmp-full-introspection.txt
> @@ -42,3 +42,20 @@ types.
>
> 'anonymous-struct' will be used to describe arbitrary structs
> (dictionary, list or string).
> +
> +== Avoid dead loop in recursive extending ==
> +
> +We have four types (ImageInfo, BlockStats, PciDeviceInfo, ObjectData)
> +that uses themself in their own define data directly or indirectly,
s/themself/themselves/
s/define data/definition/
> +we will not repeatedly extend them to avoid dead loop.
> +
> +We use a 'parents List' to record the visit path, type name of each
> +extended node will be saved to the List.
> +
> +Append type name to the list before extending, and remove type name
> +from the list after extending.
> +
> +If the type name is already extended in parents List, we won't extend
> +it repeatedly for avoiding dead loop.
This "parents" list detail is not reflected in the generated information,
right? I think it's good enough to describe that "type will not be extented
more than once in a schema, when there's direct or indirect recursive
type composition".
> +
> +'recursive' indicates if the type is extended or not.
> diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py
> new file mode 100644
> index 0000000..03179fa
> --- /dev/null
> +++ b/scripts/qapi-introspect.py
> @@ -0,0 +1,172 @@
> +#
> +# QAPI introspection info generator
> +#
> +# Copyright (C) 2014 Red Hat, Inc.
> +#
> +# Authors:
> +# Amos Kong <akong@redhat.com>
> +#
> +# This work is licensed under the terms of the GNU GPLv2.
> +# See the COPYING.LIB file in the top-level directory.
> +
> +from ordereddict import OrderedDict
> +from qapi import *
> +import sys
> +import os
> +import getopt
> +import errno
> +
> +
> +try:
> + opts, args = getopt.gnu_getopt(sys.argv[1:], "hp:o:",
> + ["header", "prefix=", "output-dir="])
> +except getopt.GetoptError, err:
> + print str(err)
> + sys.exit(1)
> +
> +output_dir = ""
> +prefix = ""
> +h_file = 'qapi-introspect.h'
> +
> +do_h = False
> +
> +for o, a in opts:
> + if o in ("-p", "--prefix"):
> + prefix = a
Is this option used in your series?
Thanks,
Fam
> + elif o in ("-o", "--output-dir"):
> + output_dir = a + "/"
> + elif o in ("-h", "--header"):
> + do_h = True
> +
> +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()
> +
> +fdecl = maybe_open(do_h, h_file, 'w')
> +
> +fdecl.write(mcgen('''
> +/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
> +
> +/*
> + * Head file to store parsed information of QAPI schema
> + *
> + * Copyright (C) 2014 Red Hat, Inc.
> + *
> + * Authors:
> + * Amos Kong <akong@redhat.com>
> + *
> + * 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
> +
> +''',
> + guard=guardname(h_file)))
> +
> +def extend_schema(expr, parents=[], member=True):
> + ret = {}
> + recu = 'False'
> + name = ""
> +
> + if type(expr) is OrderedDict:
> + if not member:
> + e = expr.popitem(last=False)
> + typ = e[0]
> + name = e[1]
> + else:
> + typ = "anonymous-struct"
> +
> + if typ == 'enum':
> + for key in expr.keys():
> + ret[key] = expr[key]
> + else:
> + ret = {}
> + for key in expr.keys():
> + ret[key], parents = extend_schema(expr[key], parents)
> +
> + elif type(expr) is list:
> + typ = 'anonymous-struct'
> + ret = []
> + for i in expr:
> + tmp, parents = extend_schema(i, parents)
> + ret.append(tmp)
> + elif type(expr) is str:
> + name = expr
> + if schema_dict.has_key(expr) and expr not in parents:
> + parents.append(expr)
> + typ = schema_dict[expr][1]
> + recu = 'True'
> + ret, parents = extend_schema(schema_dict[expr][0].copy(),
> + parents, False)
> + parents.remove(expr)
> + ret['_obj_recursive'] = 'True'
> + return ret, parents
> + else:
> + return expr, parents
> +
> + return {'_obj_member': "%s" % member, '_obj_type': typ,
> + '_obj_name': name, '_obj_recursive': recu,
> + '_obj_data': ret}, parents
> +
> +
> +exprs = parse_schema(sys.stdin)
> +schema_dict = {}
> +
> +for expr in exprs:
> + if expr.has_key('type') or expr.has_key('enum') or expr.has_key('union'):
> + e = expr.copy()
> +
> + first = e.popitem(last=False)
> + schema_dict[first[1]] = [expr.copy(), first[0]]
> +
> +fdecl.write('''const char *const qmp_schema_table[] = {
> +''')
> +
> +def convert(odict):
> + d = {}
> + for k, v in odict.items():
> + if type(v) is OrderedDict:
> + d[k] = convert(v)
> + elif type(v) is list:
> + l = []
> + for j in v:
> + if type(j) is OrderedDict:
> + l.append(convert(j))
> + else:
> + l.append(j)
> + d[k] = l
> + else:
> + d[k] = v
> + return d
> +
> +count = 0
> +for expr in exprs:
> + fdecl.write(''' /* %s */
> +''' % expr)
> +
> + expr, parents = extend_schema(expr, [], False)
> + fdecl.write(''' "%s",
> +
> +''' % convert(expr))
> +
> +fdecl.write(''' NULL };
> +
> +#endif
> +''')
> +
> +fdecl.flush()
> +fdecl.close()
> --
> 1.8.4.2
>
>
next prev parent reply other threads:[~2014-01-24 9:12 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-23 14:46 [Qemu-devel] [PATCH v4 0/5] QMP full introspection Amos Kong
2014-01-23 14:46 ` [Qemu-devel] [PATCH v4 1/5] qapi: introduce DataObject to describe dynamic structs Amos Kong
2014-02-03 19:56 ` Eric Blake
2014-01-23 14:46 ` [Qemu-devel] [PATCH v4 2/5] qapi: add qapi-introspect.py code generator Amos Kong
2014-01-24 9:12 ` Fam Zheng [this message]
2014-01-24 9:34 ` Amos Kong
2014-01-26 4:51 ` Amos Kong
2014-02-04 0:15 ` Eric Blake
2014-02-11 0:35 ` Eric Blake
2014-01-23 14:46 ` [Qemu-devel] [PATCH v4 3/5] qobject: introduce qobject_get_str() Amos Kong
2014-02-04 0:20 ` Eric Blake
2014-01-23 14:46 ` [Qemu-devel] [PATCH v4 4/5] qmp: full introspection support for QMP Amos Kong
2014-01-24 10:48 ` Fam Zheng
2014-01-27 8:17 ` Amos Kong
2014-01-27 8:50 ` Amos Kong
2014-01-27 9:38 ` Paolo Bonzini
2014-01-27 10:07 ` Amos Kong
2014-01-27 10:15 ` Paolo Bonzini
2014-01-27 10:46 ` Fam Zheng
2014-01-28 10:45 ` Amos Kong
2014-01-28 11:14 ` Paolo Bonzini
2014-01-28 13:58 ` Eric Blake
2014-01-29 8:12 ` Fam Zheng
2014-02-04 0:33 ` Eric Blake
2014-01-23 14:46 ` [Qemu-devel] [PATCH v4 5/5] update docs/qmp-full-introspection.txt Amos Kong
2014-01-24 11:43 ` Paolo Bonzini
2014-01-24 13:07 ` Eric Blake
2014-01-24 8:42 ` [Qemu-devel] [PATCH v4 0/5] QMP full introspection Amos Kong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140124091212.GB12401@T430.nay.redhat.com \
--to=famz@redhat.com \
--cc=akong@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=qiaonuohan@cn.fujitsu.com \
--cc=xiawenc@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).