OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Code generator for Ozone platform list. |
| 7 |
| 8 This script takes as arguments a list of platform names and generates a C++ |
| 9 source file containing a list of those platforms. Each list entry contains the |
| 10 name and a function pointer to the initializer for that platform. |
| 11 |
| 12 Example Output: ./generate_ozone_platform_list.py --default wayland dri wayland |
| 13 |
| 14 #include "ui/ozone/ozone_platform_list.h" |
| 15 |
| 16 namespace ui { |
| 17 |
| 18 OzonePlatform* CreateOzonePlatformDri(); |
| 19 OzonePlatform* CreateOzonePlatformWayland(); |
| 20 |
| 21 const OzonePlatformListEntry kOzonePlatforms[] = { |
| 22 { "wayland", &CreateOzonePlatformWayland }, |
| 23 { "dri", &CreateOzonePlatformDri }, |
| 24 }; |
| 25 |
| 26 const int kOzonePlatformCount = 2; |
| 27 |
| 28 } // namespace ui |
| 29 """ |
| 30 |
| 31 import optparse |
| 32 import os |
| 33 import collections |
| 34 import re |
| 35 import sys |
| 36 import string |
| 37 |
| 38 |
| 39 def GetConstructorName(name): |
| 40 """Determine name of static constructor function from platform name. |
| 41 |
| 42 We just capitalize the platform name and prepend "CreateOzonePlatform". |
| 43 """ |
| 44 |
| 45 return 'CreateOzonePlatform' + string.capitalize(name) |
| 46 |
| 47 |
| 48 def GeneratePlatformList(out, platforms): |
| 49 """Generate static array containing a list of ozone platforms.""" |
| 50 |
| 51 out.write('#include "ui/ozone/ozone_platform_list.h"\n') |
| 52 out.write('\n') |
| 53 |
| 54 out.write('namespace ui {\n') |
| 55 out.write('\n') |
| 56 |
| 57 # Prototypes for platform initializers. |
| 58 for platform in platforms: |
| 59 out.write('OzonePlatform* %s();\n' % GetConstructorName(platform)) |
| 60 out.write('\n') |
| 61 |
| 62 # List of platform names and initializers. |
| 63 out.write('const OzonePlatformListEntry kOzonePlatforms[] = {\n') |
| 64 for platform in platforms: |
| 65 out.write(' { "%s", &%s },\n' % (platform, GetConstructorName(platform))) |
| 66 out.write('};\n') |
| 67 out.write('\n') |
| 68 |
| 69 out.write('const int kOzonePlatformCount = %d;\n' % len(platforms)) |
| 70 out.write('\n') |
| 71 |
| 72 out.write('} // namespace ui\n') |
| 73 |
| 74 |
| 75 def main(argv): |
| 76 parser = optparse.OptionParser() |
| 77 parser.add_option('--output_file') |
| 78 parser.add_option('--default') |
| 79 options, platforms = parser.parse_args(argv) |
| 80 |
| 81 # Write to standard output or file specified by --output_file. |
| 82 out = sys.stdout |
| 83 if options.output_file: |
| 84 out = open(options.output_file, 'wb') |
| 85 |
| 86 # Reorder the platforms when --default is specified. |
| 87 # The default platform must appear first in the platform list. |
| 88 if options.default and options.default in platforms: |
| 89 platforms.remove(options.default) |
| 90 platforms.insert(0, options.default) |
| 91 |
| 92 GeneratePlatformList(out, platforms) |
| 93 |
| 94 if options.output_file: |
| 95 out.close() |
| 96 |
| 97 return 0 |
| 98 |
| 99 |
| 100 if __name__ == '__main__': |
| 101 sys.exit(main(sys.argv[1:])) |
OLD | NEW |