3 # Copyright (C) 2009 Google Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
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
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.
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.
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.
35 """Generate aggregate .cpp files that include multiple V8 binding .cpp files.
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.
41 aggregate_generated_bindings.py COMPONENT_DIR IDL_FILES_LIST -- OUTPUT_FILE1 OUTPUT_FILE2 ...
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.
48 Design doc: http://www.chromium.org/developers/design-documents/idl-build
56 from utilities import idl_filename_to_component, idl_filename_to_interface_name, read_idl_files_list_from_file
58 # A regexp for finding Conditional attributes in interface definitions.
59 CONDITIONAL_PATTERN = re.compile(
62 r'Conditional=([\_0-9a-zA-Z]*)'
65 r'((callback|partial)\s+)?'
72 COPYRIGHT_TEMPLATE = """/*
73 * THIS FILE WAS AUTOMATICALLY GENERATED, DO NOT EDIT.
75 * This file was generated by the action_derivedsourcesallinone.py script.
77 * Copyright (C) 2009 Google Inc. All rights reserved.
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
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.
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.
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()
108 match = CONDITIONAL_PATTERN.search(idl_contents)
111 return match.group(1)
114 def extract_meta_data(file_paths):
115 """Extracts conditional and interface name from each IDL file."""
118 for file_path in file_paths:
119 if not file_path.endswith('.idl'):
120 print 'WARNING: non-IDL file passed: "%s"' % file_path
122 if not os.path.exists(file_path):
123 print 'WARNING: file not found: "%s"' % file_path
126 # Extract interface name from file name
127 interface_name = idl_filename_to_interface_name(file_path)
130 'conditional': extract_conditional(file_path),
131 'name': interface_name,
133 meta_data_list.append(meta_data)
135 return meta_data_list
138 def generate_content(component_dir, aggregate_partial_interfaces, files_meta_data_this_partition):
140 output = [COPYRIGHT_TEMPLATE,
141 '#define NO_IMPLICIT_ATOMICSTRING\n\n']
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:
150 output.append('#endif\n')
152 output.append('\n#if ENABLE(%s)\n' % conditional)
153 prev_conditional = conditional
155 if aggregate_partial_interfaces:
156 cpp_filename = 'V8%sPartial.cpp' % meta_data['name']
158 cpp_filename = 'V8%s.cpp' % meta_data['name']
160 output.append('#include "bindings/%s/v8/%s"\n' %
161 (component_dir, cpp_filename))
164 output.append('#endif\n')
166 return ''.join(output)
169 def write_content(content, output_file_name):
170 parent_path, file_name = os.path.split(output_file_name)
171 if not os.path.exists(parent_path):
172 print 'Creating directory: %s' % parent_path
173 os.makedirs(parent_path)
174 with open(output_file_name, 'w') as f:
180 raise Exception('Expected at least 5 arguments.')
181 component_dir = args[1]
182 input_file_name = args[2]
183 in_out_break_index = args.index('--')
184 output_file_names = args[in_out_break_index + 1:]
186 idl_file_names = read_idl_files_list_from_file(input_file_name)
187 components = set([idl_filename_to_component(filename)
188 for filename in idl_file_names])
189 if len(components) != 1:
190 raise Exception('Cannot aggregate generated codes in different components')
191 aggregate_partial_interfaces = component_dir not in components
193 files_meta_data = extract_meta_data(idl_file_names)
194 total_partitions = len(output_file_names)
195 for partition, file_name in enumerate(output_file_names):
196 files_meta_data_this_partition = [
197 meta_data for meta_data in files_meta_data
198 if hash(meta_data['name']) % total_partitions == partition]
199 file_contents = generate_content(component_dir,
200 aggregate_partial_interfaces,
201 files_meta_data_this_partition)
202 write_content(file_contents, file_name)
205 if __name__ == '__main__':
206 sys.exit(main(sys.argv))