@@ -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+
78115def 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
171200def 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