Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / scripts / aggregate_generated_bindings.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2009 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 # Copyright (c) 2009 The Chromium Authors. All rights reserved.
32 # Use of this source code is governed by a BSD-style license that can be
33 # found in the LICENSE file.
34
35 """Generate aggregate .cpp files that include multiple V8 binding .cpp files.
36
37 This can be a single output file, to preserve symbol space; or multiple output
38 files, to reduce maximum compilation unit size and allow parallel compilation.
39
40 Usage:
41 aggregate_generated_bindings.py COMPONENT_DIR IDL_FILES_LIST -- OUTPUT_FILE1 OUTPUT_FILE2 ...
42
43 COMPONENT_DIR is the relative directory of a component, e.g., 'core', 'modules'.
44 IDL_FILES_LIST is a text file containing the IDL file paths, so the command
45 line doesn't exceed OS length limits.
46 OUTPUT_FILE1 etc. are filenames of output files.
47
48 Design doc: http://www.chromium.org/developers/design-documents/idl-build
49 """
50
51 import errno
52 import os
53 import re
54 import sys
55
56 from utilities import idl_filename_to_interface_name, read_idl_files_list_from_file
57
58 # A regexp for finding Conditional attributes in interface definitions.
59 CONDITIONAL_PATTERN = re.compile(
60     r'\['
61     r'[^\]]*'
62     r'Conditional=([\_0-9a-zA-Z]*)'
63     r'[^\]]*'
64     r'\]\s*'
65     r'((callback|partial)\s+)?'
66     r'interface\s+'
67     r'\w+\s*'
68     r'(:\s*\w+\s*)?'
69     r'{',
70     re.MULTILINE)
71
72 COPYRIGHT_TEMPLATE = """/*
73  * THIS FILE WAS AUTOMATICALLY GENERATED, DO NOT EDIT.
74  *
75  * This file was generated by the action_derivedsourcesallinone.py script.
76  *
77  * Copyright (C) 2009 Google Inc.  All rights reserved.
78  *
79  * Redistribution and use in source and binary forms, with or without
80  * modification, are permitted provided that the following conditions
81  * are met:
82  * 1. Redistributions of source code must retain the above copyright
83  *    notice, this list of conditions and the following disclaimer.
84  * 2. Redistributions in binary form must reproduce the above copyright
85  *    notice, this list of conditions and the following disclaimer in the
86  *    documentation and/or other materials provided with the distribution.
87  *
88  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
89  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
91  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
92  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
93  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
94  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
95  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
96  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
97  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
98  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99  */
100 """
101
102
103 def extract_conditional(idl_file_path):
104     """Find [Conditional] interface extended attribute."""
105     with open(idl_file_path) as idl_file:
106         idl_contents = idl_file.read()
107
108     match = CONDITIONAL_PATTERN.search(idl_contents)
109     if not match:
110         return None
111     return match.group(1)
112
113
114 def extract_meta_data(file_paths):
115     """Extracts conditional and interface name from each IDL file."""
116     meta_data_list = []
117
118     for file_path in file_paths:
119         if not file_path.endswith('.idl'):
120             print 'WARNING: non-IDL file passed: "%s"' % file_path
121             continue
122         if not os.path.exists(file_path):
123             print 'WARNING: file not found: "%s"' % file_path
124             continue
125
126         # Extract interface name from file name
127         interface_name = idl_filename_to_interface_name(file_path)
128
129         meta_data = {
130             'conditional': extract_conditional(file_path),
131             'name': interface_name,
132         }
133         meta_data_list.append(meta_data)
134
135     return meta_data_list
136
137
138 def generate_content(component_dir, files_meta_data_this_partition):
139     # Add fixed content.
140     output = [COPYRIGHT_TEMPLATE,
141               '#define NO_IMPLICIT_ATOMICSTRING\n\n']
142
143     # List all includes segmented by if and endif.
144     prev_conditional = None
145     files_meta_data_this_partition.sort(key=lambda e: e['conditional'])
146     for meta_data in files_meta_data_this_partition:
147         conditional = meta_data['conditional']
148         if prev_conditional != conditional:
149             if prev_conditional:
150                 output.append('#endif\n')
151             if conditional:
152                 output.append('\n#if ENABLE(%s)\n' % conditional)
153         prev_conditional = conditional
154
155         output.append('#include "bindings/%s/v8/V8%s.cpp"\n' %
156                       (component_dir, meta_data['name']))
157
158     if prev_conditional:
159         output.append('#endif\n')
160
161     return ''.join(output)
162
163
164 def write_content(content, output_file_name):
165     parent_path, file_name = os.path.split(output_file_name)
166     if not os.path.exists(parent_path):
167         print 'Creating directory: %s' % parent_path
168         os.makedirs(parent_path)
169     with open(output_file_name, 'w') as f:
170         f.write(content)
171
172
173 def main(args):
174     if len(args) <= 4:
175         raise Exception('Expected at least 5 arguments.')
176     component_dir = args[1]
177     input_file_name = args[2]
178     in_out_break_index = args.index('--')
179     output_file_names = args[in_out_break_index + 1:]
180
181     idl_file_names = read_idl_files_list_from_file(input_file_name)
182     files_meta_data = extract_meta_data(idl_file_names)
183     total_partitions = len(output_file_names)
184     for partition, file_name in enumerate(output_file_names):
185         files_meta_data_this_partition = [
186                 meta_data for meta_data in files_meta_data
187                 if hash(meta_data['name']) % total_partitions == partition]
188         file_contents = generate_content(component_dir,
189                                          files_meta_data_this_partition)
190         write_content(file_contents, file_name)
191
192
193 if __name__ == '__main__':
194     sys.exit(main(sys.argv))