1#!/usr/bin/python
2#
3# Copyright (C) 2013 Google Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Generate event interfaces .in file (EventInterfaces.in).
32
33The event interfaces .in file contains a list of all Event interfaces, i.e.,
34all interfaces that inherit from Event, including Event itself,
35together with certain extended attributes.
36
37Paths are in POSIX format, and relative to Source/.
38
39This list is used in core/ to generate EventFactory and EventNames.
40The .in format is documented in build/scripts/in_file.py.
41"""
42
43from optparse import OptionParser
44import os
45import posixpath
46import sys
47
48from utilities import get_file_contents, read_file_to_list, write_file, get_interface_extended_attributes_from_idl
49
50EXPORTED_EXTENDED_ATTRIBUTES = (
51    'Conditional',
52    'ImplementedAs',
53    'RuntimeEnabled',
54)
55module_path = os.path.dirname(os.path.realpath(__file__))
56source_dir = os.path.normpath(os.path.join(module_path, os.pardir, os.pardir))
57
58
59def parse_options():
60    parser = OptionParser()
61    parser.add_option('--event-idl-files-list', help='file listing event IDL files')
62    parser.add_option('--event-interfaces-file', help='output file')
63    parser.add_option('--write-file-only-if-changed', type='int', help='if true, do not write an output file if it would be identical to the existing one, which avoids unnecessary rebuilds in ninja')
64    parser.add_option('--suffix', help='specify a suffix to the namespace, i.e., "Modules". Default is None.')
65
66    options, args = parser.parse_args()
67    if options.event_idl_files_list is None:
68        parser.error('Must specify a file listing event IDL files using --event-idl-files-list.')
69    if options.event_interfaces_file is None:
70        parser.error('Must specify an output file using --event-interfaces-file.')
71    if options.write_file_only_if_changed is None:
72        parser.error('Must specify whether file is only written if changed using --write-file-only-if-changed.')
73    options.write_file_only_if_changed = bool(options.write_file_only_if_changed)
74    if args:
75        parser.error('No arguments allowed, but %d given.' % len(args))
76    return options
77
78
79def write_event_interfaces_file(event_idl_files, destination_filename, only_if_changed, suffix):
80    def extended_attribute_string(name, value):
81        if name == 'RuntimeEnabled':
82            value += 'Enabled'
83        return name + '=' + value
84
85    def interface_line(full_path):
86        relative_path_local, _ = os.path.splitext(os.path.relpath(full_path, source_dir))
87        relative_path_posix = relative_path_local.replace(os.sep, posixpath.sep)
88
89        idl_file_contents = get_file_contents(full_path)
90        extended_attributes = get_interface_extended_attributes_from_idl(idl_file_contents)
91        extended_attributes_list = [
92            extended_attribute_string(name, extended_attributes[name])
93            for name in EXPORTED_EXTENDED_ATTRIBUTES
94            if name in extended_attributes]
95
96        return '%s %s\n' % (relative_path_posix,
97                            ', '.join(extended_attributes_list))
98
99    lines = ['namespace="Event"\n']
100    if suffix:
101        lines.append('suffix="' + suffix + '"\n')
102    lines.append('\n')
103    interface_lines = [interface_line(event_idl_file)
104                       for event_idl_file in event_idl_files]
105    interface_lines.sort()
106    lines.extend(interface_lines)
107    write_file(''.join(lines), destination_filename, only_if_changed)
108
109
110################################################################################
111
112def main():
113    options = parse_options()
114    event_idl_files = read_file_to_list(options.event_idl_files_list)
115    write_event_interfaces_file(event_idl_files,
116                                options.event_interfaces_file,
117                                options.write_file_only_if_changed,
118                                options.suffix)
119
120
121if __name__ == '__main__':
122    sys.exit(main())
123