OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (C) 2012 Google Inc. All rights reserved. | 2 # Copyright (C) 2012 Google Inc. All rights reserved. |
3 # | 3 # |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 return content | 78 return content |
79 | 79 |
80 | 80 |
81 def process_file(file_name): | 81 def process_file(file_name): |
82 variable_name, content = make_variable_name_and_read(file_name) | 82 variable_name, content = make_variable_name_and_read(file_name) |
83 content = strip_whitespace_and_comments(file_name, content) | 83 content = strip_whitespace_and_comments(file_name, content) |
84 size = len(content) | 84 size = len(content) |
85 return variable_name, content | 85 return variable_name, content |
86 | 86 |
87 | 87 |
88 def write_header_file(header_file_name, flag, names_and_contents, namespace='bli
nk'): | 88 def write_header_file(header_file_name, flag, names_and_contents, namespace): |
89 with open(header_file_name, 'w') as header_file: | 89 with open(header_file_name, 'w') as header_file: |
90 if flag: | 90 if flag: |
91 header_file.write('#if ' + flag + '\n') | 91 header_file.write('#if ' + flag + '\n') |
92 header_file.write('namespace %s {\n' % namespace) | 92 header_file.write('namespace %s {\n' % namespace) |
93 for variable_name, content in names_and_contents: | 93 for variable_name, content in names_and_contents: |
94 size = len(content) | 94 size = len(content) |
95 header_file.write('extern const char %s[%d];\n' % (variable_name, si
ze)) | 95 header_file.write('extern const char %s[%d];\n' % (variable_name, si
ze)) |
96 header_file.write('}\n') | 96 header_file.write('}\n') |
97 if flag: | 97 if flag: |
98 header_file.write('#endif\n') | 98 header_file.write('#endif\n') |
99 | 99 |
100 | 100 |
101 def write_cpp_file(cpp_file_name, flag, names_and_contents, header_file_name, na
mespace='blink'): | 101 def write_cpp_file(cpp_file_name, flag, names_and_contents, header_file_name, na
mespace): |
102 with open(cpp_file_name, 'w') as cpp_file: | 102 with open(cpp_file_name, 'w') as cpp_file: |
103 cpp_file.write('#include "config.h"\n') | 103 cpp_file.write('#include "config.h"\n') |
104 cpp_file.write('#include "%s"\n' % os.path.basename(header_file_name)) | 104 cpp_file.write('#include "%s"\n' % os.path.basename(header_file_name)) |
105 if flag: | 105 if flag: |
106 cpp_file.write('#if ' + flag + '\n') | 106 cpp_file.write('#if ' + flag + '\n') |
107 cpp_file.write('namespace %s {\n' % namespace) | 107 cpp_file.write('namespace %s {\n' % namespace) |
108 for variable_name, content in names_and_contents: | 108 for variable_name, content in names_and_contents: |
109 cpp_file.write(cpp_constant_string(variable_name, content)) | 109 cpp_file.write(cpp_constant_string(variable_name, content)) |
110 cpp_file.write('}\n') | 110 cpp_file.write('}\n') |
111 if flag: | 111 if flag: |
(...skipping 15 matching lines...) Expand all Loading... |
127 output.append('\n') | 127 output.append('\n') |
128 output.append('\n') | 128 output.append('\n') |
129 return ''.join(output) | 129 return ''.join(output) |
130 | 130 |
131 | 131 |
132 def main(): | 132 def main(): |
133 parser = OptionParser() | 133 parser = OptionParser() |
134 parser.add_option('--out-h', dest='out_header') | 134 parser.add_option('--out-h', dest='out_header') |
135 parser.add_option('--out-cpp', dest='out_cpp') | 135 parser.add_option('--out-cpp', dest='out_cpp') |
136 parser.add_option('--condition', dest='flag') | 136 parser.add_option('--condition', dest='flag') |
| 137 parser.add_option('--namespace', dest='namespace', default='blink') |
137 (options, args) = parser.parse_args() | 138 (options, args) = parser.parse_args() |
138 if len(args) < 1: | 139 if len(args) < 1: |
139 parser.error('Need one or more input files') | 140 parser.error('Need one or more input files') |
140 if not options.out_header: | 141 if not options.out_header: |
141 parser.error('Need to specify --out-h=filename') | 142 parser.error('Need to specify --out-h=filename') |
142 if not options.out_cpp: | 143 if not options.out_cpp: |
143 parser.error('Need to specify --out-cpp=filename') | 144 parser.error('Need to specify --out-cpp=filename') |
144 | 145 |
145 if options.flag: | 146 if options.flag: |
146 options.flag = options.flag.replace(' AND ', ' && ') | 147 options.flag = options.flag.replace(' AND ', ' && ') |
147 options.flag = options.flag.replace(' OR ', ' || ') | 148 options.flag = options.flag.replace(' OR ', ' || ') |
148 | 149 |
149 names_and_contents = [process_file(file_name) for file_name in args] | 150 names_and_contents = [process_file(file_name) for file_name in args] |
150 | 151 |
151 if options.out_header: | 152 if options.out_header: |
152 write_header_file(options.out_header, options.flag, names_and_contents) | 153 write_header_file(options.out_header, options.flag, names_and_contents,
options.namespace) |
153 write_cpp_file(options.out_cpp, options.flag, names_and_contents, options.ou
t_header) | 154 write_cpp_file(options.out_cpp, options.flag, names_and_contents, options.ou
t_header, options.namespace) |
154 | 155 |
155 | 156 |
156 if __name__ == '__main__': | 157 if __name__ == '__main__': |
157 sys.exit(main()) | 158 sys.exit(main()) |
OLD | NEW |