Skip to content

Commit fb3659f

Browse files
authored
[empath-split] Tidy up error checking (#25380)
This puts all parser-related error checking routines in `parse_args` and all other error checking routines, such as checking whether files exist, in `check_errors`. The reason parser-related errors better be in `parse_args` is we can use `parser.error` there, which prints the usage. Also this checks for one more thing: whether `-o` or `--output` is provided. This is forwarded to `wasm-split` and it is a mandatory option for `wasm-split --multi-split`, but because `-o is required` error was printed by not this script but `wasm-split`, it was printed later and the message could be buried in warnings, which was confusing. We check it here directly and error out when one is not provided.
1 parent ab45cf2 commit fb3659f

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

tools/empath-split.py

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,50 @@ def parse_args():
6868
parser.add_argument('--preserve-manifest', action='store_true',
6969
help='Preserve generated manifest file. This sets --verbose too.')
7070
args, forwarded_args = parser.parse_known_args()
71-
if '--manifest' in forwarded_args:
72-
exit_with_error('manifest file will be generated by this script and should not be given')
71+
7372
if args.preserve_manifest:
7473
args.verbose = True
74+
if not args.wasm_split:
75+
args.wasm_split = os.path.join(building.get_binaryen_bin(), shared.exe_suffix('wasm-split'))
76+
77+
if '--manifest' in forwarded_args:
78+
parser.error('manifest file will be generated by this script and should not be given')
79+
if '-o' not in forwarded_args and '--output' not in forwarded_args:
80+
parser.error('-o (--output) is required')
7581
return args, forwarded_args
7682

7783

84+
def check_errors(args):
85+
if args.wasm and not os.path.isfile(args.wasm):
86+
exit_with_error(f"'{args.wasm}' was not found or not a file")
87+
if args.paths_file and not os.path.isfile(args.paths_file):
88+
exit_with_error(f"'{args.paths_file}' was not found or not a file")
89+
90+
if args.sourcemap:
91+
if not os.path.isfile(args.sourcemap):
92+
exit_with_error(f"'{args.sourcemap}' was not found or not a file")
93+
94+
if args.wasm:
95+
with webassembly.Module(args.wasm) as module:
96+
if not args.sourcemap and not emsymbolizer.get_sourceMappingURL_section(module):
97+
exit_with_error('sourceMappingURL section does not exist')
98+
sourcemap = module.get_sourceMappingURL()
99+
if not os.path.isfile(sourcemap):
100+
exit_with_error(f"'{sourcemap}' was not found or not a file")
101+
if not module.has_name_section():
102+
exit_with_error('Name section does not eixst')
103+
104+
if not os.path.isfile(args.wasm_split):
105+
exit_with_error(f"'{args.wasm_split}' was not found or not a file")
106+
107+
108+
def get_sourceMappingURL(wasm, arg_sourcemap):
109+
if arg_sourcemap:
110+
return arg_sourcemap
111+
with webassembly.Module(wasm) as module:
112+
return module.get_sourceMappingURL()
113+
114+
78115
def get_path_to_functions_map(wasm, sourcemap, paths):
79116
def is_synthesized_func(func):
80117
# TODO There can be more
@@ -98,21 +135,13 @@ def is_synthesized_func(func):
98135
# {src file: list of functions} map, and construct {path: list of functions}
99136
# map from it
100137
with webassembly.Module(wasm) as module:
101-
if not module.has_name_section():
102-
exit_with_error('Name section does not eixst')
103-
if not sourcemap:
104-
if not emsymbolizer.get_sourceMappingURL_section(module):
105-
exit_with_error('sourceMappingURL section does not exist')
106-
107138
funcs = module.get_functions()
108139
func_names = module.get_function_names()
109140
assert len(funcs) == len(func_names)
110141

111142
func_to_src = {}
112143
src_to_funcs = {}
113144

114-
if not sourcemap:
115-
sourcemap = module.get_sourceMappingURL()
116145
sm = emsymbolizer.WasmSourceMap()
117146
sm.parse(sourcemap)
118147

@@ -170,20 +199,9 @@ def is_synthesized_func(func):
170199

171200
def main():
172201
args, forwarded_args = parse_args()
173-
if args.wasm_split:
174-
wasm_split = args.wasm_split
175-
else:
176-
wasm_split = os.path.join(building.get_binaryen_bin(), shared.exe_suffix('wasm-split'))
202+
check_errors(args)
177203

178-
if not os.path.isfile(args.wasm):
179-
exit_with_error(f"'{args.wasm}' was not found or not a file")
180-
if not os.path.isfile(args.paths_file):
181-
exit_with_error(f"'{args.paths_file}' was not found or not a file")
182-
if args.sourcemap:
183-
if not os.path.isfile(args.sourcemap):
184-
exit_with_error(f"'{args.sourcemap}' was not found or not a file")
185-
if not os.path.isfile(wasm_split):
186-
exit_with_error(f"'{wasm_split}' was not found or not a file")
204+
sourcemap = get_sourceMappingURL(args.wasm, args.sourcemap)
187205

188206
paths = utils.read_file(args.paths_file).splitlines()
189207
paths = [utils.normalize_path(path.strip()) for path in paths if path.strip()]
@@ -193,7 +211,7 @@ def main():
193211
paths = list(dict.fromkeys(paths))
194212

195213
# Compute {path: list of functions} map
196-
path_to_funcs = get_path_to_functions_map(args.wasm, args.sourcemap, paths)
214+
path_to_funcs = get_path_to_functions_map(args.wasm, sourcemap, paths)
197215

198216
# Write .manifest file
199217
with tempfile.NamedTemporaryFile(suffix=".manifest", mode='w+', delete=args.preserve_manifest) as f:
@@ -213,7 +231,7 @@ def main():
213231
f.write('\n')
214232
f.flush()
215233

216-
cmd = [wasm_split, '--multi-split', args.wasm, '--manifest', manifest]
234+
cmd = [args.wasm_split, '--multi-split', args.wasm, '--manifest', manifest]
217235
if args.verbose:
218236
# This option is used both in this script and wasm-split
219237
cmd.append('-v')

0 commit comments

Comments
 (0)