OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # Copyright 2016 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 """Helper script to inline files as const char[] into a C header file. | |
7 | |
8 Example: | |
9 | |
10 Input File in_a.vert: | |
Jamie
2016/06/06 20:03:48
s/File/file/
Yuwei
2016/06/06 20:35:39
Done.
| |
11 1" | |
12 2 | |
13 3\ | |
14 | |
15 Output File in_b.frag: | |
Jamie
2016/06/06 20:03:47
s/File/file/
Yuwei
2016/06/06 20:35:39
Done.
| |
16 4 | |
17 5 | |
18 6 | |
19 | |
20 shader_to_header.py "output.h" "in_a.vert" "in_b.frag" will generate | |
21 this: | |
22 | |
23 #ifndef OUTPUT_H_CHxvtO77kdA8xy26 | |
24 #define OUTPUT_H_CHxvtO77kdA8xy26 | |
25 | |
26 const char kInAVert[] = "1\"\n" | |
27 "2\n" | |
28 "3\\"; | |
29 | |
30 const char kInBFrag[] = "4\n" | |
31 "5\n" | |
32 "6"; | |
33 | |
34 #endif // OUTPUT_H_CHxvtO77kdA8xy26 | |
35 | |
36 """ | |
37 | |
38 import os.path | |
39 import random | |
40 import string | |
41 import sys | |
42 | |
43 RANDOM_STRING_LENGTH = 16 | |
44 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.
| |
45 string.ascii_lowercase + \ | |
46 string.digits | |
47 | |
48 def random_str(): | |
49 return ''.join(random.choice(STRING_CHARACTERS) | |
50 for _ in range(RANDOM_STRING_LENGTH)) | |
51 | |
52 | |
53 def escape_line(line): | |
54 # encode('string-escape') doesn't escape double quote so you need to manually | |
55 # escape it. | |
56 return line.encode('string-escape').replace('"', '\\"') | |
57 | |
58 | |
59 def main(): | |
60 if len(sys.argv) < 3: | |
61 print 'Usage: shader_to_header.py <output-file> <input-files...>' | |
62 return 1 | |
63 | |
64 output_path = sys.argv[1] | |
65 include_guard = os.path.basename(output_path).upper().replace('.', '_') + \ | |
66 '_' + random_str() | |
67 | |
68 with open(output_path, 'w') as output_file: | |
69 output_file.write('#ifndef ' + include_guard + '\n' + | |
70 '#define ' + include_guard + '\n\n') | |
71 | |
72 existing_names = set() | |
73 argc = len(sys.argv) | |
74 for i in xrange(2, argc): | |
75 input_path = sys.argv[i] | |
76 | |
77 with open(input_path, 'r') as input_file: | |
78 # hello_world.vert -> kHelloWorldVert | |
79 const_name = 'k' + os.path.basename(input_path).title() \ | |
80 .replace('_', '').replace('.', '') | |
81 if const_name in existing_names: | |
82 print >> sys.stderr, 'Error: Constant name ' + const_name + \ | |
83 ' is already used by a previous file. Files with the same' + \ | |
84 ' name can\'t be inlined into the same header.' | |
85 return 1 | |
86 | |
87 existing_names.add(const_name) | |
88 text = input_file.read() | |
89 lines = text.splitlines() | |
90 | |
91 # 1. Append double quote to the front of each line. | |
92 # 2. Use '\\n"\n' to connect each line. | |
93 # 3. Add the double quote to the end to finish the quotation of last | |
94 # 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.
| |
95 inlined = '\\n"\n'.join(map(lambda x: '"' + escape_line(x), lines)) + \ | |
96 '"' | |
97 output_file.write('const char ' + const_name + '[]' + ' = ' + inlined + | |
98 ';\n\n') | |
99 | |
100 output_file.write('#endif // ' + include_guard + '\n') | |
101 | |
102 return 0 | |
103 | |
104 | |
105 if __name__ == '__main__': | |
106 sys.exit(main()) | |
OLD | NEW |