Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(373)

Side by Side Diff: remoting/tools/build/remoting_c_text_inliner.py

Issue 2036023003: Script to inline shader into C header file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 a.vert:
11 1"
12 2
13 3\
14
15 b.frag:
16 4
17 5
18 6
19
20 remoting_c_text_inliner.py "OUTPUT_H_" "output.h" "a.vert" "b.frag":
21 #ifndef OUTPUT_H
22 #define OUTPUT_H
23
24 const char kAVert[] = "1\"\n"
25 + "2\n"
26 + "3\\";
27
28 const char kBFrag[] = "4\n"
29 + "5\n"
30 + "6";
31
32 #endif //OUTPUT_H
33
34 """
35
36 import os.path
37 import sys
38
39 def escape_line(line):
40 return line.encode('string-escape').replace('"', '\\"')
Yuwei 2016/06/03 23:33:00 Forgot to leave comment for this. encode() escapes
41
42 def main():
43 if len(sys.argv) < 4:
44 print 'Usage: remoting_c_text_inliner.py <include-guard> ' + \
45 '<output-file> <input-files...>'
46 return 1
47
48 include_guard = sys.argv[1]
49
50 output_path = sys.argv[2]
51
52 output_file = open(output_path, 'w')
53
54 output_file.write('#ifndef ' + include_guard + '\n' +
55 '#define ' + include_guard + '\n\n')
56
57 argc = len(sys.argv)
58
59 for i in xrange(3, argc):
60 input_path = sys.argv[i]
61 input_file = open(input_path, 'r')
62
63 # hello_world.vert -> kHelloWorldVert
64 const_name = 'k' + os.path.basename(input_path).title().replace('_', '') \
65 .replace('.', '')
66 text = input_file.read()
67 lines = text.splitlines()
68
69 # a, b -> "a, "b -> "a\n"
Yuwei 2016/06/03 23:33:00 oops... Should be "a, "b -> "a\n", "b -> "a\n", "b
70 inlined = '\\n"\n + '.join(map(lambda x: '"' + escape_line(x), lines)) + '"'
71 output_file.write('const char ' + const_name + '[]' + ' = ' + inlined +
72 ';\n\n')
73 input_file.close()
74
75 output_file.write('#endif //' + include_guard + '\n')
76 output_file.close()
77
78 return 0
79
80
81 if __name__ == '__main__':
82 sys.exit(main())
OLDNEW
« remoting/client/opengl/BUILD.gn ('K') | « remoting/client/opengl/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698