sbus_codegen revision fcd8093c58638dc7c4f9cddfc97f273b94ce2ead
59528addd704f8d5757b54e540520f74e588a7c7Automatic Updater# Stef Walter <stefw@redhat.com>
75b70a68aefaa17ac4e768d5ed85d2f50d471490Automatic Updater# Copyright (C) 2014 Red Hat
ea854b585041ad19f70f7af15e08144ef2c2bd1bMark Andrews# This program is free software; you can redistribute it and/or modify
78cb74fab4665da2e2641ba909c6f59f74cc4193Automatic Updater# it under the terms of the GNU General Public License as published by
c89d02f2fb4c06168236d600e86831cff324f763Mark Andrews# the Free Software Foundation; either version 3 of the License, or
90ff38a0d8deaf5f9c2aa5916d99b2e572d28738Automatic Updater# (at your option) any later version.
ac4e70ff8955669341f435bc0a734a17c01af124Mark Andrews# This program is distributed in the hope that it will be useful,
6c6a121295b30772cbf3dd75a51fb9d883051a0eAutomatic Updater# but WITHOUT ANY WARRANTY; without even the implied warranty of
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9cd5eb6fe0f26d65724b99216cb31dcdd12e4afdAutomatic Updater# GNU General Public License for more details.
56874aef380a64a2c183b7c282c3e7a361d67fa1Automatic Updater# You should have received a copy of the GNU General Public License
c651f15b30f1dae5cc2f00878fb5da5b3a35a468Mark Andrews# along with this program. If not, see <http://www.gnu.org/licenses/>.
c651f15b30f1dae5cc2f00878fb5da5b3a35a468Mark Andrews# Some parser code from GLib
80faf1588895fd26490f82f95a7a1b771df1c324Automatic Updater# Copyright (C) 2008-2011 Red Hat, Inc.
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater# This library is free software; you can redistribute it and/or
efb0e886f18894a1d2489f1ad74ad14b579e11c7Mark Andrews# modify it under the terms of the GNU Lesser General Public
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater# License as published by the Free Software Foundation; either
91216cff91b34c9ff6e846dc23f248219cafe660Andreas Gustafsson# version 2 of the License, or (at your option) any later version.
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater# This library is distributed in the hope that it will be useful,
91216cff91b34c9ff6e846dc23f248219cafe660Andreas Gustafsson# but WITHOUT ANY WARRANTY; without even the implied warranty of
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# Lesser General Public License for more details.
aa9c561961e9d877946ebaa8795fa2be054ab7bfEvan Hunt# You should have received a copy of the GNU Lesser General
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# Public License along with this library; if not, write to the
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
aa9c561961e9d877946ebaa8795fa2be054ab7bfEvan Hunt# Boston, MA 02111-1307, USA.
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# Portions by: David Zeuthen <davidz@redhat.com>
2d2dc37599979c83495510f8af8d1756753aa2c5Automatic Updater# DBus interfaces are defined here:
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# The introspection data format has become the standard way to represent a
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# DBus interface. For many examples see /usr/share/dbus-1/interfaces/ on a
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# typical linux machine.
cdfc81e048bd34c1d628380247bda6b80a89e20eAutomatic Updater# A word about annotations. These are extra flags or values that can be
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# assigned to anything. So far, the codegen supports this annotation:
9513a2a6670951f5cf5477fcfec9f933fcaff628Automatic Updater# - An annotation specified in the specification that tells us what C symbol
eabc9c3c07cd956d3c436bd7614cb162dabdda76Mark Andrews# to generate for a given interface or method. By default the codegen will
eabc9c3c07cd956d3c436bd7614cb162dabdda76Mark Andrews# build up a symbol name from the DBus name.
db5b7e2cdf150c46e8242d3e2e3ad3f5c7300258Automatic Updater# -----------------------------------------------------------------------------
c651f15b30f1dae5cc2f00878fb5da5b3a35a468Mark Andrews # Lets us print problems like a compiler would
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater return "%s:%d: %s" % (self.file, self.line, message)
dd65eb1efb40b1c47d57963192bfc54873b219beAutomatic Updater raise DBusXmlException('No name on element')
9174e44c14b1cb91a651fa1dc29470438c246ab9Automatic Updater return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol", self.name)
133e6d43fa82e80d3798be4de00f4540f485ec6cAutomatic Updater def __init__(self, method, name, signature):
133e6d43fa82e80d3798be4de00f4540f485ec6cAutomatic Updater return "%s_%s" % (self.iface.c_name(), self.c_name())
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater return "%s_%s" % (self.iface.c_name(), self.c_name())
fe80a4909bf62b602feaf246866e9d29f7654194Automatic Updater def __init__(self, iface, name, signature, access):
133e6d43fa82e80d3798be4de00f4540f485ec6cAutomatic Updater raise DBusXmlException('Invalid access type %s'%self.access)
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater return "%s_%s" % (self.iface.c_name(), self.c_name())
2bb3422dc683c013db7042f5736240de6b86f182Automatic Updater return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol",
3b6e4c84a525b0b3fc9e8affd8bb9fa5c000345fAutomatic Updater# -----------------------------------------------------------------------------
19b3dc94bce93fa76bd7e066f9298630dbc9dcb4Automatic Updater# Code Generation
bc0a53583d92309bebcf93c408e2f3247ebd3d3cAutomatic Updater out("/* arguments for %s.%s */", parent.iface.name, parent.name)
ce9cad6bb04869c5e94d9dc721032b25117f9210Automatic Updater out("const struct sbus_arg_meta %s%s[] = {", parent.fq_c_name(), suffix)
59528addd704f8d5757b54e540520f74e588a7c7Automatic Updater out(" { \"%s\", \"%s\" },", arg.name, arg.signature)
ce9cad6bb04869c5e94d9dc721032b25117f9210Automatic Updater out("const struct sbus_method_meta %s__methods[] = {", iface.c_name())
9cd5eb6fe0f26d65724b99216cb31dcdd12e4afdAutomatic Updater out(" offsetof(struct %s, %s),", iface.c_name(), meth.c_name())
f7c88d61cc1ad2435b0b7cfaedfc9d5248c0be25Automatic Updater out("const struct sbus_signal_meta %s__signals[] = {", iface.c_name())
ce9cad6bb04869c5e94d9dc721032b25117f9210Automatic Updater out("/* property info for %s */", iface.name)
cf7e98f59148b559946a7f1ca728471374f1eef3Automatic Updater out("const struct sbus_property_meta %s__properties[] = {", iface.c_name())
361bec4bdec45042897fb479b7071cd05bbd56b9Automatic Updater out(" \"%s\", /* signature */", prop.signature)
71bd43eebd9d6e42dbcae62b730f5b6508d5acd8Automatic Updater out(" SBUS_PROPERTY_READABLE | SBUS_PROPERTY_WRITABLE,")
7d12a6b412fe47e6d6582923fd6954ab8cd0baebAutomatic Updater out("extern const struct sbus_interface_meta %s_meta;", iface.c_name())
ca35524ce2b57e6f1b261d23565d1288a355d12fAutomatic Updater out("/* interface info for %s */", iface.name)
7d12a6b412fe47e6d6582923fd6954ab8cd0baebAutomatic Updater out("const struct sbus_interface_meta %s_meta = {", iface.c_name())
7d12a6b412fe47e6d6582923fd6954ab8cd0baebAutomatic Updaterdef generate_source(ifaces, filename, include_header=None):
7d12a6b412fe47e6d6582923fd6954ab8cd0baebAutomatic Updater out("/* The following definitions are auto-generated from %s */", basename)
e8fc8c884b44371784805e1e0d3100da403dd3f1Automatic Updater out("#include \"%s\"", os.path.basename(include_header))
59528addd704f8d5757b54e540520f74e588a7c7Automatic Updater source_properties(iface, iface.properties)
f2770f6b39a9b2a98afb7a11ed105f73f1570c1eAutomatic Updater out(" struct sbus_vtable vtable; /* derive from sbus_vtable */")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington # TODO: Property getters and setters will go here
9cd5eb6fe0f26d65724b99216cb31dcdd12e4afdAutomatic Updater guard = "__%s__" % re.sub(r'([^_A-Z0-9])', "_", basename.upper())
9cd5eb6fe0f26d65724b99216cb31dcdd12e4afdAutomatic Updater out("/* The following declarations are auto-generated from %s */", basename)
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out("/* ------------------------------------------------------------------------")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * These structures are filled in by implementors of the different")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * dbus interfaces to handle method calls.")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * Handler functions of type sbus_msg_handler_fn accept raw messages,")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * other handlers will be typed appropriately. If a handler that is")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * set to NULL is invoked it will result in a")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * org.freedesktop.DBus.Error.NotSupported error for the caller.")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out("/* ------------------------------------------------------------------------")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * These structure definitions are filled in with the information about")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * the interfaces, methods, properties and so on.")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington out(" * The actual definitions are found in the accompanying C file next")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington# -----------------------------------------------------------------------------
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington# XML Interface Parsing
9cd5eb6fe0f26d65724b99216cb31dcdd12e4afdAutomatic Updater raise DBusXmlException("Missing attribute '%s'" % name)
ae7e54b14c946e0984c191554db9abb4893f9349Automatic Updater raise DBusXmlException("Empty attribute '%s'" % name)
ae7e54b14c946e0984c191554db9abb4893f9349Automatic Updater parser.CommentHandler = self.handle_comment
ae7e54b14c946e0984c191554db9abb4893f9349Automatic Updater parser.CharacterDataHandler = self.handle_char_data
ae7e54b14c946e0984c191554db9abb4893f9349Automatic Updater parser.StartElementHandler = self.handle_start_element
ae7e54b14c946e0984c191554db9abb4893f9349Automatic Updater parser.EndElementHandler = self.handle_end_element
ce9cad6bb04869c5e94d9dc721032b25117f9210Automatic Updater def handle_start_element(self, name, attrs):
ce9cad6bb04869c5e94d9dc721032b25117f9210Automatic Updater elif self.cur_object and name == STATE_ANNOTATION:
ce9cad6bb04869c5e94d9dc721032b25117f9210Automatic Updater self.cur_object.annotations[expect_attr(attrs, 'name')] = val
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington iface = Interface(expect_attr(attrs, 'name'))
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington method = Method(self.cur_object, expect_attr(attrs, 'name'))
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington signal = Signal(self.cur_object, expect_attr(attrs, 'name'))
7adcb4de92bf4383a4c5624c4ed256736d02bc6dMark Andrews raise DBusXmlException('Invalid direction "%s"' % direction)
ce9cad6bb04869c5e94d9dc721032b25117f9210Automatic Updater assert False, 'Unhandled state "%s" while entering element with name "%s"' % (self.state, name)
9cd5eb6fe0f26d65724b99216cb31dcdd12e4afdAutomatic Updater self.cur_object_stack.append(old_cur_object)
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington self.cur_object = self.cur_object_stack.pop()
7adcb4de92bf4383a4c5624c4ed256736d02bc6dMark Andrews parser = optparse.OptionParser("usage: %prog [options] introspect.xml ...")
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington parser.set_description("sbus_codegen generates sbus interface structures \
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington help="Set output file name (default: stdout)",
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington print >> sys.stderr, "sbus_codegen: no input file specified"
a26b22914b7bf25f065afb8cdef983766dcd672bAutomatic Updater if options.mode not in ["header", "source"]:
9cd5eb6fe0f26d65724b99216cb31dcdd12e4afdAutomatic Updater print >> sys.stderr, "sbus_codegen: specify --mode=header or --mode=source"
9e3a7b0faf417a10f5f689edf288807b2d5eedc5Brian Wellington generate_header(parser.parsed_interfaces, filename)
068a66979695c77359e7a9181bb3f831c965b21cMark Andrews generate_source(parser.parsed_interfaces, filename, options.include)
532d27b39244fadfcf8d8b4593f4c65434c9c664Automatic Updater # Write output at end to be nice to 'make'