summaryrefslogtreecommitdiff
path: root/build-aux/qmi-codegen/qmi-codegen
blob: 1fea7c1dd11d074fa4b0f03e8ec3bba3a4c34cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2012 Lanedo GmbH
# Copyright (C) 2012-2017 Aleksander Morgado <aleksander@aleksander.es>
#

import os
import sys
import optparse
import json

from Client      import Client
from MessageList import MessageList
import utils

def codegen_main():
    # Input arguments
    arg_parser = optparse.OptionParser('%prog [options]')
    arg_parser.add_option('', '--input', metavar='JSONFILE',
                          help='Input JSON-formatted database')
    arg_parser.add_option('', '--output', metavar='OUTFILES',
                          help='Generate C code in OUTFILES.[ch]')
    arg_parser.add_option('', '--include', metavar='JSONFILE', action='append',
                          help='Additional common types in a JSON-formatted database')
    arg_parser.add_option('', '--collection', metavar='[JSONFILE]',
                          help='Collection of messages to be included in the build')
    (opts, args) = arg_parser.parse_args();

    if opts.input == None:
        raise RuntimeError('Input JSON file is mandatory')
    if opts.output == None:
        raise RuntimeError('Output file pattern is mandatory')
    if opts.include == None:
        opts.include = []

    # Prepare output file names
    output_file_c = open(opts.output + ".c", 'w')
    output_file_h = open(opts.output + ".h", 'w')
    output_file_sections = open(opts.output + ".sections", 'w')

    # If a collection given, load it
    collection_list_json = None
    if opts.collection != None:
        collection_contents = utils.read_json_file(opts.collection)
        collection_list_json = json.loads(collection_contents)

    # Load all common types
    common_object_list_json = []
    opts.include.append(opts.input)
    for include in opts.include:
        include_contents = utils.read_json_file(include)
        include_list = json.loads(include_contents)
        for obj in include_list:
            if 'common-ref' in obj:
                common_object_list_json.append(obj)

    # Load database file contents
    database_file_contents = utils.read_json_file(opts.input)

    # Build message list
    object_list_json = json.loads(database_file_contents)
    message_list = MessageList(collection_list_json, object_list_json, common_object_list_json)

    # Validate that all items in the collection are in the message list
    if collection_list_json is not None:
        for message_id_enum_name in collection_list_json:
            if message_id_enum_name.startswith(message_list.request_prefix) or \
               message_id_enum_name.startswith(message_list.indication_prefix):
                if message_list.contains(message_id_enum_name) == False:
                    raise ValueError('Message ' + message_id_enum_name + ' in collection does not exist')

    # Add common stuff to the output files
    utils.add_copyright(output_file_c);
    utils.add_copyright(output_file_h);
    utils.add_header_start(output_file_h, os.path.basename(opts.output), message_list.service)
    utils.add_source_start(output_file_c, os.path.basename(opts.output))

    # Emit the message creation/parsing code
    message_list.emit(output_file_h, output_file_c)

    # Build our own client
    client = Client(object_list_json)
    client.emit(output_file_h, output_file_c, message_list)

    # Emit sections
    client.emit_sections(output_file_sections, message_list)
    message_list.emit_sections(output_file_sections)

    utils.add_header_stop(output_file_h, os.path.basename(opts.output))

    output_file_c.close()
    output_file_h.close()
    output_file_sections.close()

    sys.exit(0)


if __name__ == "__main__":
    codegen_main()