3 # Copyright 2015 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Fingerprints the V8 snapshot blob files.
9 Constructs a SHA256 fingerprint of the V8 natives and snapshot blob files and
10 creates a .cc file which includes these fingerprint as variables.
18 _HEADER
= """// Copyright 2015 The Chromium Authors. All rights reserved.
19 // Use of this source code is governed by a BSD-style license that can be
20 // found in the LICENSE file.
22 // This file was generated by fingerprint_v8_snapshot.py.
32 def FingerprintFile(file_path
):
33 input_file
= open(file_path
, 'rb')
34 sha256
= hashlib
.sha256()
36 block
= input_file
.read(sha256
.block_size
)
40 return sha256
.digest()
43 def WriteFingerprint(output_file
, variable_name
, fingerprint
):
44 output_file
.write('\nextern const unsigned char %s[] = { ' % variable_name
)
45 for byte
in fingerprint
:
46 output_file
.write(str(ord(byte
)) + ', ')
47 output_file
.write('};\n')
50 def WriteOutputFile(natives_fingerprint
,
53 output_dir_path
= os
.path
.dirname(output_file_path
)
54 if not os
.path
.exists(output_dir_path
):
55 os
.makedirs(output_dir_path
)
56 output_file
= open(output_file_path
, 'w')
58 output_file
.write(_HEADER
)
59 WriteFingerprint(output_file
, 'g_natives_fingerprint', natives_fingerprint
)
60 output_file
.write('\n')
61 WriteFingerprint(output_file
, 'g_snapshot_fingerprint', snapshot_fingerprint
)
62 output_file
.write(_FOOTER
)
66 parser
= optparse
.OptionParser()
68 parser
.add_option('--snapshot_file',
69 help='The input V8 snapshot blob file path.')
70 parser
.add_option('--natives_file',
71 help='The input V8 natives blob file path.')
72 parser
.add_option('--output_file',
73 help='The path for the output cc file which will be write.')
75 options
, _
= parser
.parse_args()
77 natives_fingerprint
= FingerprintFile(options
.natives_file
)
78 snapshot_fingerprint
= FingerprintFile(options
.snapshot_file
)
80 natives_fingerprint
, snapshot_fingerprint
, options
.output_file
)
85 if __name__
== '__main__':