Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/emsize.py
4091 views
1
#!/usr/bin/env python3
2
# Copyright 2019 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
"""Size helper script
8
9
This script acts as a frontend replacement for `size` that supports combining
10
JS and wasm output from emscripten.
11
The traditional size utility reports the size of each section in a binary
12
and the total. This replacement adds another pseudo-section, "JS" which
13
shows the size of the JavaScript loader file.
14
15
Currently there are many limitations; basically this tool is enough to
16
be used by the LLVM testsuite runner code to analyze size output.
17
18
Currently this tool only supports sysv output format (it accepts but ignores
19
any '-format' argument). It does not accept any other arguments aside from the
20
input file, which is expected to be a JS file. The wasm file is expected to be
21
in the same directory, and have the same basename with a '.wasm' extension.
22
"""
23
24
import argparse
25
import os
26
import subprocess
27
import sys
28
29
from tools import shared
30
31
LLVM_SIZE = shared.llvm_tool_path('llvm-size')
32
33
34
def error(text):
35
print(text, file=sys.stderr, flush=True)
36
return 1
37
38
39
def parse_args(argv):
40
parser = argparse.ArgumentParser(description=__doc__)
41
parser.add_argument('-format', '--format')
42
parser.add_argument('file')
43
args = parser.parse_args(argv)
44
return args.file
45
46
47
def print_sizes(js_file):
48
if not os.path.isfile(js_file):
49
return error('Input JS file %s not foune' % js_file)
50
if not js_file.endswith('.js'):
51
return error('Input file %s does not have a JS extension' % js_file)
52
53
basename = js_file[:-3]
54
55
# Find the JS file size
56
st = os.stat(js_file)
57
js_size = st.st_size
58
59
# Find the rest of the sizes
60
wasm_file = basename + '.wasm'
61
if not os.path.isfile(wasm_file):
62
return error('Wasm file %s not found' % wasm_file)
63
64
sizes = shared.check_call([LLVM_SIZE, '--format=sysv', wasm_file],
65
stdout=subprocess.PIPE).stdout
66
# llvm-size may emit some number of blank lines (after the total), ignore them
67
lines = [line for line in sizes.splitlines() if line]
68
69
# Last line is the total. Add the JS size.
70
total = int(lines[-1].split()[-1])
71
total += js_size
72
73
for line in lines[:-1]:
74
print(line)
75
76
print('JS\t\t%s\t0' % js_size)
77
print('Total\t\t%s' % total)
78
79
80
if __name__ == '__main__':
81
sys.exit(print_sizes(parse_args(sys.argv[1:])))
82
83