1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Code generator for Ozone platform list.
This script takes as arguments a list of platform names and generates a C++
source file containing a list of those platforms.
Each platform gets an integer identifier that is used to find objects for that
platform (particularly constructors for platform-specific objects).
Example Output: ./generate_ozone_platform_list.py --default wayland dri wayland
// platform_list.txt
wayland
dri
// platform_list.h
#ifndef UI_OZONE_PLATFORM_LIST_H_
#define UI_OZONE_PLATFORM_LIST_H_
namespace ui {
const int kPlatformWayland = 0;
const int kPlatformDri = 1;
extern const char *kPlatformNames[kPlatformCount];
} // namespace ui
// platform_list.cc
#include "ui/ozone/platform_list.h"
namespace ui {
const char *kPlatformNames[] = {
"wayland", // kPlatformWayland
"dri", // kPlatformDri
};
} // namespace ui
#endif // UI_OZONE_PLATFORM_LIST_H_
"""
try:
from StringIO import StringIO # for Python 2
except ImportError:
from io import StringIO # for Python 3
import optparse
import os
import collections
import re
import sys
def GetConstantName(name):
"""Determine name of static constructor function from platform name.
We just capitalize the platform name and prepend "CreateOzonePlatform".
"""
return 'kPlatform' + name.capitalize()
def GeneratePlatformListText(out, platforms):
"""Generate text file with list of platform names, in platform id order."""
for platform in platforms:
out.write(platform)
out.write('\n')
out.write('\n')
def GeneratePlatformListHeader(out, platforms):
"""Generate ids of ozone platforms & declaration of static names array."""
out.write('// DO NOT MODIFY. GENERATED BY generate_ozone_platform_list.py\n')
out.write('\n')
out.write('#ifndef UI_OZONE_PLATFORM_LIST_H_\n')
out.write('#define UI_OZONE_PLATFORM_LIST_H_\n')
out.write('\n')
out.write('namespace ui {\n')
out.write('\n')
# Prototypes for platform initializers.
for plat_id, plat_name in enumerate(platforms):
out.write('const int %s = %d;\n' % (GetConstantName(plat_name), plat_id))
out.write('\n')
# Platform count.
out.write('const int kPlatformCount = %d;\n' % len(platforms))
out.write('\n')
# Declaration for names list.
out.write('extern const char* kPlatformNames[kPlatformCount];\n')
out.write('\n')
out.write('} // namespace ui\n')
out.write('\n')
out.write('#endif // UI_OZONE_PLATFORM_LIST_H_\n')
out.write('\n')
def GeneratePlatformListSource(out, platforms):
"""Generate static array containing a list of ozone platforms."""
out.write('// DO NOT MODIFY. GENERATED BY generate_ozone_platform_list.py\n')
out.write('\n')
out.write('#include "ui/ozone/platform_list.h"\n')
out.write('\n')
out.write('namespace ui {\n')
out.write('\n')
# Definition of names list.
out.write('const char* kPlatformNames[] = {\n')
# Prototypes for platform initializers.
for plat_name in platforms:
out.write(' "%s", // %s\n' % (plat_name, GetConstantName(plat_name)))
out.write('};\n')
out.write('\n')
out.write('} // namespace ui\n')
out.write('\n')
def main(argv):
parser = optparse.OptionParser()
parser.add_option('--output_cc')
parser.add_option('--output_h')
parser.add_option('--output_txt')
parser.add_option('--default')
options, platforms = parser.parse_args(argv)
# Reorder the platforms when --default is specified.
# The default platform must appear first in the platform list.
if options.default and options.default in platforms:
platforms.remove(options.default)
platforms.insert(0, options.default)
# Write to standard output or file specified by --output_{cc,h}.
out_cc = getattr(sys.stdout, 'buffer', sys.stdout)
out_h = getattr(sys.stdout, 'buffer', sys.stdout)
out_txt = getattr(sys.stdout, 'buffer', sys.stdout)
if options.output_cc:
out_cc = open(options.output_cc, 'wb')
if options.output_h:
out_h = open(options.output_h, 'wb')
if options.output_txt:
out_txt = open(options.output_txt, 'wb')
out_txt_str = StringIO()
out_h_str = StringIO()
out_cc_str = StringIO()
GeneratePlatformListText(out_txt_str, platforms)
out_txt.write(out_txt_str.getvalue().encode('utf-8'))
GeneratePlatformListHeader(out_h_str, platforms)
out_h.write(out_h_str.getvalue().encode('utf-8'))
GeneratePlatformListSource(out_cc_str, platforms)
out_cc.write(out_cc_str.getvalue().encode('utf-8'))
if options.output_cc:
out_cc.close()
if options.output_h:
out_h.close()
if options.output_txt:
out_txt.close()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
|