OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 Creates a library loader (a header and implementation file), | 7 Creates a library loader (a header and implementation file), |
8 which is a wrapper for dlopen or direct linking with given library. | 8 which is a wrapper for dlopen or direct linking with given library. |
9 | 9 |
10 The loader makes it possible to have the same client code for both cases, | 10 The loader makes it possible to have the same client code for both cases, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 parser.error('Missing --name parameter') | 155 parser.error('Missing --name parameter') |
156 if not options.output_cc: | 156 if not options.output_cc: |
157 parser.error('Missing --output-cc parameter') | 157 parser.error('Missing --output-cc parameter') |
158 if not options.output_h: | 158 if not options.output_h: |
159 parser.error('Missing --output-h parameter') | 159 parser.error('Missing --output-h parameter') |
160 if not options.header: | 160 if not options.header: |
161 parser.error('Missing --header paramater') | 161 parser.error('Missing --header paramater') |
162 if not args: | 162 if not args: |
163 parser.error('No function names specified') | 163 parser.error('No function names specified') |
164 | 164 |
165 # Make sure we are always dealing with an absolute path | 165 # Make sure we are always dealing with paths relative to source tree root |
166 # to avoid issues caused by different relative path roots. | 166 # to avoid issues caused by different relative path roots. |
167 options.output_cc = os.path.abspath(options.output_cc) | 167 source_tree_root = os.path.abspath( |
168 options.output_h = os.path.abspath(options.output_h) | 168 os.path.join(os.path.dirname(__file__), '..', '..')) |
| 169 options.output_cc = os.path.relpath(options.output_cc, source_tree_root) |
| 170 options.output_h = os.path.relpath(options.output_h, source_tree_root) |
169 | 171 |
170 # Create a unique prefix, e.g. for header guards. | 172 # Create a unique prefix, e.g. for header guards. |
171 # Stick a known string at the beginning to ensure this doesn't begin | 173 # Stick a known string at the beginning to ensure this doesn't begin |
172 # with an underscore, which is reserved for the C++ implementation. | 174 # with an underscore, which is reserved for the C++ implementation. |
173 unique_prefix = ('LIBRARY_LOADER_' + | 175 unique_prefix = ('LIBRARY_LOADER_' + |
174 re.sub(r'[\W]', '_', options.output_h).upper()) | 176 re.sub(r'[\W]', '_', options.output_h).upper()) |
175 | 177 |
176 member_decls = [] | 178 member_decls = [] |
177 member_init = [] | 179 member_init = [] |
178 member_cleanup = [] | 180 member_cleanup = [] |
(...skipping 25 matching lines...) Expand all Loading... |
204 if options.link_directly == 0: | 206 if options.link_directly == 0: |
205 wrapped_header_include += '#define %s_DLOPEN\n' % unique_prefix | 207 wrapped_header_include += '#define %s_DLOPEN\n' % unique_prefix |
206 elif options.link_directly == 1: | 208 elif options.link_directly == 1: |
207 wrapped_header_include += '#define %s_DT_NEEDED\n' % unique_prefix | 209 wrapped_header_include += '#define %s_DT_NEEDED\n' % unique_prefix |
208 else: | 210 else: |
209 parser.error('Invalid value for --link-directly. Should be 0 or 1.') | 211 parser.error('Invalid value for --link-directly. Should be 0 or 1.') |
210 | 212 |
211 # Make it easier for people to find the code generator just in case. | 213 # Make it easier for people to find the code generator just in case. |
212 # Doing it this way is more maintainable, because it's going to work | 214 # Doing it this way is more maintainable, because it's going to work |
213 # even if file gets moved without updating the contents. | 215 # even if file gets moved without updating the contents. |
214 generator_path = os.path.abspath(__file__) | 216 generator_path = os.path.relpath(__file__, source_tree_root) |
215 | 217 |
216 header_contents = HEADER_TEMPLATE % { | 218 header_contents = HEADER_TEMPLATE % { |
217 'generator_path': generator_path, | 219 'generator_path': generator_path, |
218 'unique_prefix': unique_prefix, | 220 'unique_prefix': unique_prefix, |
219 'wrapped_header_include': wrapped_header_include, | 221 'wrapped_header_include': wrapped_header_include, |
220 'class_name': options.name, | 222 'class_name': options.name, |
221 'member_decls': ''.join(member_decls), | 223 'member_decls': ''.join(member_decls), |
222 } | 224 } |
223 | 225 |
224 impl_contents = IMPL_TEMPLATE % { | 226 impl_contents = IMPL_TEMPLATE % { |
225 'generator_path': generator_path, | 227 'generator_path': generator_path, |
226 'unique_prefix': unique_prefix, | 228 'unique_prefix': unique_prefix, |
227 'generated_header_name': options.output_h, | 229 'generated_header_name': options.output_h, |
228 'class_name': options.name, | 230 'class_name': options.name, |
229 'member_init': ''.join(member_init), | 231 'member_init': ''.join(member_init), |
230 'member_cleanup': ''.join(member_cleanup), | 232 'member_cleanup': ''.join(member_cleanup), |
231 } | 233 } |
232 | 234 |
233 header_file = open(options.output_h, 'w') | 235 header_file = open(os.path.join(source_tree_root, options.output_h), 'w') |
234 try: | 236 try: |
235 header_file.write(header_contents) | 237 header_file.write(header_contents) |
236 finally: | 238 finally: |
237 header_file.close() | 239 header_file.close() |
238 | 240 |
239 impl_file = open(options.output_cc, 'w') | 241 impl_file = open(os.path.join(source_tree_root, options.output_cc), 'w') |
240 try: | 242 try: |
241 impl_file.write(impl_contents) | 243 impl_file.write(impl_contents) |
242 finally: | 244 finally: |
243 impl_file.close() | 245 impl_file.close() |
244 | 246 |
245 return 0 | 247 return 0 |
246 | 248 |
247 if __name__ == '__main__': | 249 if __name__ == '__main__': |
248 sys.exit(main()) | 250 sys.exit(main()) |
OLD | NEW |