Flutter Engine
The Flutter Engine
gen_library_src_paths.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5#
6# This python script creates a source path mapping in a C++ source file from
7# a C++ source template and list of dart library files.
8
9import os
10import sys
11import utils
12
13from os.path import join
14from optparse import OptionParser
15
16HOST_OS = utils.GuessOS()
17
18
19def makeString(input_file, var_name):
20 result = 'static const char ' + var_name + '[] = {\n '
21 fileHandle = open(input_file, 'rb')
22 lineCounter = 0
23 for byte in fileHandle.read():
24 result += '\'\\x%02x' % ord(byte) + '\', '
25 lineCounter += 1
26 if lineCounter == 19:
27 result += '\n '
28 lineCounter = 0
29 result += '0};\n'
30 return result
31
32
33def makeSourceArrays(in_files):
34 result = ''
35 file_count = 0
36 for string_file in in_files:
37 if string_file.endswith('.dart'):
38 file_count += 1
39 file_string = makeString(string_file,
40 "source_array_" + str(file_count))
41 result += file_string
42 return result
43
44
45def makeFile(output_file, input_cc_file, include, var_name, lib_name, in_files):
46 part_index = []
47 bootstrap_cc_text = open(input_cc_file).read()
48 bootstrap_cc_text = bootstrap_cc_text.replace("{{SOURCE_ARRAYS}}",
49 makeSourceArrays(in_files))
50 bootstrap_cc_text = bootstrap_cc_text.replace("{{INCLUDE}}", include)
51 bootstrap_cc_text = bootstrap_cc_text.replace("{{VAR_NAME}}", var_name)
52 main_file_found = False
53 file_count = 0
54 for string_file in in_files:
55 if string_file.endswith('.dart'):
56 file_count += 1
57 if (not main_file_found):
58 inpt = open(string_file, 'r')
59 for line in inpt:
60 # File with library tag is the main file.
61 if line.startswith('library '):
62 main_file_found = True
63 bootstrap_cc_text = bootstrap_cc_text.replace(
64 "{{LIBRARY_SOURCE_MAP}}", ' "' + lib_name + '",\n' +
65 ' source_array_' + str(file_count) + ',\n')
66 inpt.close()
67 if (main_file_found):
68 continue
69 part_index.append(' "' + lib_name + "/" + os.path.basename(
70 string_file).replace('\\', '\\\\') + '",\n')
71 part_index.append(' source_array_' + str(file_count) + ',\n\n')
72 bootstrap_cc_text = bootstrap_cc_text.replace("{{LIBRARY_SOURCE_MAP}}", '')
73 bootstrap_cc_text = bootstrap_cc_text.replace("{{PART_SOURCE_MAP}}",
74 ''.join(part_index))
75 open(output_file, 'w').write(bootstrap_cc_text)
76 return True
77
78
79def main(args):
80 try:
81 # Parse input.
82 parser = OptionParser()
83 parser.add_option(
84 "--output", action="store", type="string", help="output file name")
85 parser.add_option(
86 "--input_cc",
87 action="store",
88 type="string",
89 help="input template file")
90 parser.add_option(
91 "--include", action="store", type="string", help="variable name")
92 parser.add_option(
93 "--library_name",
94 action="store",
95 type="string",
96 help="library name")
97 parser.add_option(
98 "--var_name", action="store", type="string", help="variable name")
99
100 (options, args) = parser.parse_args()
101 if not options.output:
102 sys.stderr.write('--output not specified\n')
103 return -1
104 if not len(options.input_cc):
105 sys.stderr.write('--input_cc not specified\n')
106 return -1
107 if not len(options.include):
108 sys.stderr.write('--include not specified\n')
109 return -1
110 if not len(options.var_name):
111 sys.stderr.write('--var_name not specified\n')
112 return -1
113 if not len(options.library_name):
114 sys.stderr.write('--library_name not specified\n')
115 return -1
116 if len(args) == 0:
117 sys.stderr.write('No input files specified\n')
118 return -1
119
120 files = []
121 for arg in args:
122 files.append(arg)
123
124 if not makeFile(options.output, options.input_cc, options.include,
125 options.var_name, options.library_name, files):
126 return -1
127
128 return 0
129 except Exception as inst:
130 sys.stderr.write('gen_library_src_paths.py exception\n')
131 sys.stderr.write(str(inst))
132 sys.stderr.write('\n')
133 return -1
134
135
136if __name__ == '__main__':
137 sys.exit(main(sys.argv))
static bool read(SkStream *stream, void *buffer, size_t amount)
def makeString(input_file, var_name)
def makeFile(output_file, input_cc_file, include, var_name, lib_name, in_files)
Definition: main.py:1
def GuessOS()
Definition: utils.py:21
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741
void write(SkWStream *wStream, const T &text)
Definition: skqp.cpp:188