Chromium Code Reviews| 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 IMPL_MEMBER_CLEANUP_TEMPLATE = """ %(function_name)s = NULL; | 139 IMPL_MEMBER_CLEANUP_TEMPLATE = """ %(function_name)s = NULL; |
| 140 """ | 140 """ |
| 141 | 141 |
| 142 def main(): | 142 def main(): |
| 143 parser = optparse.OptionParser() | 143 parser = optparse.OptionParser() |
| 144 parser.add_option('--name') | 144 parser.add_option('--name') |
| 145 parser.add_option('--output-cc') | 145 parser.add_option('--output-cc') |
| 146 parser.add_option('--output-h') | 146 parser.add_option('--output-h') |
| 147 parser.add_option('--header') | 147 parser.add_option('--header') |
| 148 | 148 |
| 149 parser.add_option('--bundled-header') | |
| 149 parser.add_option('--use-extern-c', action='store_true', default=False) | 150 parser.add_option('--use-extern-c', action='store_true', default=False) |
| 150 parser.add_option('--link-directly', type=int, default=0) | 151 parser.add_option('--link-directly', type=int, default=0) |
| 151 | 152 |
| 152 options, args = parser.parse_args() | 153 options, args = parser.parse_args() |
| 153 | 154 |
| 154 if not options.name: | 155 if not options.name: |
| 155 parser.error('Missing --name parameter') | 156 parser.error('Missing --name parameter') |
| 156 if not options.output_cc: | 157 if not options.output_cc: |
| 157 parser.error('Missing --output-cc parameter') | 158 parser.error('Missing --output-cc parameter') |
| 158 if not options.output_h: | 159 if not options.output_h: |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 183 }) | 184 }) |
| 184 member_init.append(IMPL_MEMBER_INIT_TEMPLATE % { | 185 member_init.append(IMPL_MEMBER_INIT_TEMPLATE % { |
| 185 'function_name': fn, | 186 'function_name': fn, |
| 186 'unique_prefix': unique_prefix | 187 'unique_prefix': unique_prefix |
| 187 }) | 188 }) |
| 188 member_cleanup.append(IMPL_MEMBER_CLEANUP_TEMPLATE % { | 189 member_cleanup.append(IMPL_MEMBER_CLEANUP_TEMPLATE % { |
| 189 'function_name': fn, | 190 'function_name': fn, |
| 190 'unique_prefix': unique_prefix | 191 'unique_prefix': unique_prefix |
| 191 }) | 192 }) |
| 192 | 193 |
| 193 wrapped_header_include = '#include %s' % options.header | 194 header = options.header |
| 195 if options.link_directly == 0 and options.bundled_header: | |
| 196 header = options.bundled_header | |
|
Ryan Sleevi
2012/11/29 21:51:04
The handling of this option creates some degree of
Paweł Hajdan Jr.
2012/11/29 22:29:00
Yup. Why worry though? If you copy an existing exa
Ryan Sleevi
2012/11/29 23:02:58
Code should be clear and self-documenting. I do no
| |
| 197 wrapped_header_include = '#include %s\n' % header | |
| 194 | 198 |
| 195 # Some libraries (e.g. libpci) have headers that cannot be included | 199 # Some libraries (e.g. libpci) have headers that cannot be included |
| 196 # without extern "C", otherwise they cause the link to fail. | 200 # without extern "C", otherwise they cause the link to fail. |
| 197 # TODO(phajdan.jr): This is a workaround for broken headers. Remove it. | 201 # TODO(phajdan.jr): This is a workaround for broken headers. Remove it. |
| 198 if options.use_extern_c: | 202 if options.use_extern_c: |
| 199 wrapped_header_include = 'extern "C" {\n%s\n}\n' % wrapped_header_include | 203 wrapped_header_include = 'extern "C" {\n%s\n}\n' % wrapped_header_include |
| 200 | 204 |
| 201 # It seems cleaner just to have a single #define here and #ifdefs in bunch | 205 # It seems cleaner just to have a single #define here and #ifdefs in bunch |
| 202 # of places, rather than having a different set of templates, duplicating | 206 # of places, rather than having a different set of templates, duplicating |
| 203 # or complicating more code. | 207 # or complicating more code. |
| 204 if options.link_directly == 0: | 208 if options.link_directly == 0: |
| 205 wrapped_header_include += '#define %s_DLOPEN\n' % unique_prefix | 209 wrapped_header_include += '#define %s_DLOPEN\n' % unique_prefix |
| 206 elif options.link_directly == 1: | 210 elif options.link_directly == 1: |
| 207 wrapped_header_include += '#define %s_DT_NEEDED\n' % unique_prefix | 211 wrapped_header_include += '#define %s_DT_NEEDED\n' % unique_prefix |
|
Ryan Sleevi
2012/11/29 23:02:58
It appears your generator violates the Google C++
Mark Mentovai
2012/11/30 20:22:27
Ryan Sleevi wrote:
| |
| 208 else: | 212 else: |
| 209 parser.error('Invalid value for --link-directly. Should be 0 or 1.') | 213 parser.error('Invalid value for --link-directly. Should be 0 or 1.') |
| 210 | 214 |
| 211 # Make it easier for people to find the code generator just in case. | 215 # 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 | 216 # Doing it this way is more maintainable, because it's going to work |
| 213 # even if file gets moved without updating the contents. | 217 # even if file gets moved without updating the contents. |
| 214 generator_path = os.path.abspath(__file__) | 218 generator_path = os.path.abspath(__file__) |
| 215 | 219 |
| 216 header_contents = HEADER_TEMPLATE % { | 220 header_contents = HEADER_TEMPLATE % { |
| 217 'generator_path': generator_path, | 221 'generator_path': generator_path, |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 239 impl_file = open(options.output_cc, 'w') | 243 impl_file = open(options.output_cc, 'w') |
| 240 try: | 244 try: |
| 241 impl_file.write(impl_contents) | 245 impl_file.write(impl_contents) |
| 242 finally: | 246 finally: |
| 243 impl_file.close() | 247 impl_file.close() |
| 244 | 248 |
| 245 return 0 | 249 return 0 |
| 246 | 250 |
| 247 if __name__ == '__main__': | 251 if __name__ == '__main__': |
| 248 sys.exit(main()) | 252 sys.exit(main()) |
| OLD | NEW |