Chromium Code Reviews| Index: remoting/tools/build/shader_to_header.py |
| diff --git a/remoting/tools/build/shader_to_header.py b/remoting/tools/build/shader_to_header.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..05e48553be55c1bf1d5c8f7aa20f0a736e8886b8 |
| --- /dev/null |
| +++ b/remoting/tools/build/shader_to_header.py |
| @@ -0,0 +1,106 @@ |
| +#!/usr/bin/python |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Helper script to inline files as const char[] into a C header file. |
| + |
| +Example: |
| + |
| +Input File in_a.vert: |
|
Jamie
2016/06/06 20:03:48
s/File/file/
Yuwei
2016/06/06 20:35:39
Done.
|
| +1" |
| +2 |
| +3\ |
| + |
| +Output File in_b.frag: |
|
Jamie
2016/06/06 20:03:47
s/File/file/
Yuwei
2016/06/06 20:35:39
Done.
|
| +4 |
| +5 |
| +6 |
| + |
| +shader_to_header.py "output.h" "in_a.vert" "in_b.frag" will generate |
| +this: |
| + |
| +#ifndef OUTPUT_H_CHxvtO77kdA8xy26 |
| +#define OUTPUT_H_CHxvtO77kdA8xy26 |
| + |
| +const char kInAVert[] = "1\"\n" |
| +"2\n" |
| +"3\\"; |
| + |
| +const char kInBFrag[] = "4\n" |
| +"5\n" |
| +"6"; |
| + |
| +#endif // OUTPUT_H_CHxvtO77kdA8xy26 |
| + |
| +""" |
| + |
| +import os.path |
| +import random |
| +import string |
| +import sys |
| + |
| +RANDOM_STRING_LENGTH = 16 |
| +STRING_CHARACTERS = string.ascii_uppercase + \ |
|
Jamie
2016/06/06 20:03:47
Use parentheses instead of backslashes for line co
Yuwei
2016/06/06 20:35:39
Done.
|
| + string.ascii_lowercase + \ |
| + string.digits |
| + |
| +def random_str(): |
| + return ''.join(random.choice(STRING_CHARACTERS) |
| + for _ in range(RANDOM_STRING_LENGTH)) |
| + |
| + |
| +def escape_line(line): |
| + # encode('string-escape') doesn't escape double quote so you need to manually |
| + # escape it. |
| + return line.encode('string-escape').replace('"', '\\"') |
| + |
| + |
| +def main(): |
| + if len(sys.argv) < 3: |
| + print 'Usage: shader_to_header.py <output-file> <input-files...>' |
| + return 1 |
| + |
| + output_path = sys.argv[1] |
| + include_guard = os.path.basename(output_path).upper().replace('.', '_') + \ |
| + '_' + random_str() |
| + |
| + with open(output_path, 'w') as output_file: |
| + output_file.write('#ifndef ' + include_guard + '\n' + |
| + '#define ' + include_guard + '\n\n') |
| + |
| + existing_names = set() |
| + argc = len(sys.argv) |
| + for i in xrange(2, argc): |
| + input_path = sys.argv[i] |
| + |
| + with open(input_path, 'r') as input_file: |
| + # hello_world.vert -> kHelloWorldVert |
| + const_name = 'k' + os.path.basename(input_path).title() \ |
| + .replace('_', '').replace('.', '') |
| + if const_name in existing_names: |
| + print >> sys.stderr, 'Error: Constant name ' + const_name + \ |
| + ' is already used by a previous file. Files with the same' + \ |
| + ' name can\'t be inlined into the same header.' |
| + return 1 |
| + |
| + existing_names.add(const_name) |
| + text = input_file.read() |
| + lines = text.splitlines() |
| + |
| + # 1. Append double quote to the front of each line. |
| + # 2. Use '\\n"\n' to connect each line. |
| + # 3. Add the double quote to the end to finish the quotation of last |
| + # line. |
|
Jamie
2016/06/06 20:03:47
As discussed, there's no need for this file to be
Yuwei
2016/06/06 20:35:39
Done.
|
| + inlined = '\\n"\n'.join(map(lambda x: '"' + escape_line(x), lines)) + \ |
| + '"' |
| + output_file.write('const char ' + const_name + '[]' + ' = ' + inlined + |
| + ';\n\n') |
| + |
| + output_file.write('#endif // ' + include_guard + '\n') |
| + |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |