Flutter Engine
The Flutter Engine
create_snapshot_file.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
4# for details. All rights reserved. Use of this source code is governed by a
5# BSD-style license that can be found in the LICENSE file.
6
7# Script to convert snapshot files to a C++ file which can be compiled and
8# linked together with VM binary.
9
10import getopt
11import optparse
12import string
13import subprocess
14import sys
15import utils
16
17HOST_OS = utils.GuessOS()
18HOST_CPUS = utils.GuessCpus()
19
20
22 result = optparse.OptionParser()
23 result.add_option(
24 "--vm_input_bin",
25 action="store",
26 type="string",
27 help="input file name of the vm isolate snapshot in binary form")
28 result.add_option(
29 "--input_bin",
30 action="store",
31 type="string",
32 help="input file name of the isolate snapshot in binary form")
33 result.add_option(
34 "--input_cc",
35 action="store",
36 type="string",
37 help="input file name which contains the C buffer template")
38 result.add_option(
39 "--output",
40 action="store",
41 type="string",
42 help="output file name into which snapshot in C buffer form is generated"
43 )
44 result.add_option(
45 "-v",
46 "--verbose",
47 help='Verbose output.',
48 default=False,
49 action="store_true")
50 return result
51
52
53def ProcessOptions(options):
54 if not options.vm_input_bin:
55 sys.stderr.write('--vm_input_bin not specified\n')
56 return False
57 if not options.input_bin:
58 sys.stderr.write('--input_bin not specified\n')
59 return False
60 if not options.input_cc:
61 sys.stderr.write('--input_cc not specified\n')
62 return False
63 if not options.output:
64 sys.stderr.write('--output not specified\n')
65 return False
66 return True
67
68
69def WriteBytesAsText(out, input_file):
70 """Writes byte contents of the input_file into out file as text.
71
72 Output is formatted as a list of comma separated integer values - one value
73 for each byte.
74 """
75 with open(input_file, 'rb') as input:
76 lineCounter = 0
77 line = ' '
78 for byte in input.read():
79 line += ' %d,' % ord(byte)
80 lineCounter += 1
81 if lineCounter == 10:
82 out.write(line + '\n')
83 line = ' '
84 lineCounter = 0
85 if lineCounter != 0:
86 out.write(line + '\n')
87
88
89def GenerateFileFromTemplate(output_file, input_cc_file, vm_isolate_input_file,
90 isolate_input_file):
91 """Generates C++ file based on a input_cc_file template and two binary files
92
93 Template is expected to have two %s placeholders which would be filled
94 with binary contents of the given files each formatted as a comma separated
95 list of integers.
96 """
97 snapshot_cc_text = open(input_cc_file).read()
98 chunks = snapshot_cc_text.split("%s")
99 if len(chunks) != 3:
100 raise Exception("Template %s should contain exactly two %%s occurrences"
101 % input_cc_file)
102
103 with open(output_file, 'w') as out:
104 out.write(chunks[0])
105 WriteBytesAsText(out, vm_isolate_input_file)
106 out.write(chunks[1])
107 WriteBytesAsText(out, isolate_input_file)
108 out.write(chunks[2])
109
110
111def Main():
112 # Parse options.
113 parser = BuildOptions()
114 (options, args) = parser.parse_args()
115 if not ProcessOptions(options):
116 parser.print_help()
117 return 1
118
119 # If there are additional arguments, report error and exit.
120 if args:
121 parser.print_help()
122 return 1
123
124 GenerateFileFromTemplate(options.output, options.input_cc,
125 options.vm_input_bin, options.input_bin)
126
127 return 0
128
129
130if __name__ == '__main__':
131 sys.exit(Main())
static bool read(SkStream *stream, void *buffer, size_t amount)
def WriteBytesAsText(out, input_file)
def GenerateFileFromTemplate(output_file, input_cc_file, vm_isolate_input_file, isolate_input_file)
def GuessOS()
Definition: utils.py:21
def GuessCpus()
Definition: utils.py:55