]>
git.proxmox.com Git - rustc.git/blob - src/binaryen/auto_update_tests.py
3 import os
, sys
, subprocess
, difflib
5 from scripts
.test
.support
import run_command
, split_wast
6 from scripts
.test
.shared
import (
7 ASM2WASM
, MOZJS
, S2WASM
, WASM_SHELL
, WASM_OPT
, WASM_AS
, WASM_DIS
,
8 WASM_CTOR_EVAL
, WASM_MERGE
, WASM_REDUCE
, WASM2ASM
,
9 BINARYEN_INSTALL_DIR
, has_shell_timeout
)
10 from scripts
.test
.wasm2asm
import tests
, spec_tests
, extra_tests
, assert_tests
13 print '[ processing and updating testcases... ]\n'
15 for asm
in sorted(os
.listdir('test')):
16 if asm
.endswith('.asm.js'):
17 for precise
in [0, 1, 2]:
19 cmd
= ASM2WASM
+ [os
.path
.join('test', asm
), '--enable-threads']
20 wasm
= asm
.replace('.asm.js', '.fromasm')
22 cmd
+= ['--trap-mode=allow', '--ignore-implicit-traps']
25 cmd
+= ['--trap-mode=clamp']
30 cmd
+= ['-O0'] # test that -O0 does nothing
33 if 'debugInfo' in asm
:
36 cmd
+= ['--no-legalize-javascript-ffi']
38 # test mem init importing
39 open('a.mem', 'wb').write(asm
)
40 cmd
+= ['--mem-init=a.mem']
42 cmd
+= ['--mem-base=1024']
43 if 'i64' in asm
or 'wasm-only' in asm
or 'noffi' in asm
:
44 cmd
+= ['--wasm-only']
46 actual
= run_command(cmd
)
47 with
open(os
.path
.join('test', wasm
), 'w') as o
: o
.write(actual
)
48 if 'debugInfo' in asm
:
49 cmd
+= ['--source-map', os
.path
.join('test', wasm
+ '.map'), '-o', 'a.wasm']
54 '.clamp.wast': ['--trap-mode=clamp'],
55 '.js.wast': ['--trap-mode=js'],
57 for dot_s_dir
in ['dot_s', 'llvm_autogenerated']:
58 for s
in sorted(os
.listdir(os
.path
.join('test', dot_s_dir
))):
59 if not s
.endswith('.s'): continue
61 for ext
, ext_args
in extension_arg_map
.iteritems():
62 wasm
= s
.replace('.s', ext
)
63 expected_file
= os
.path
.join('test', dot_s_dir
, wasm
)
64 if ext
!= '.wast' and not os
.path
.exists(expected_file
):
67 full
= os
.path
.join('test', dot_s_dir
, s
)
68 stack_alloc
= ['--allocate-stack=1024'] if dot_s_dir
== 'llvm_autogenerated' else []
69 cmd
= S2WASM
+ [full
, '--emscripten-glue'] + stack_alloc
+ ext_args
70 if s
.startswith('start_'):
72 actual
= run_command(cmd
, stderr
=subprocess
.PIPE
, expected_err
='')
74 with
open(expected_file
, 'w') as o
: o
.write(actual
)
76 for t
in sorted(os
.listdir(os
.path
.join('test', 'print'))):
77 if t
.endswith('.wast'):
79 wasm
= os
.path
.basename(t
).replace('.wast', '')
80 cmd
= WASM_OPT
+ [os
.path
.join('test', 'print', t
), '--print']
81 print ' ', ' '.join(cmd
)
82 actual
= subprocess
.check_output(cmd
)
84 with
open(os
.path
.join('test', 'print', wasm
+ '.txt'), 'w') as o
: o
.write(actual
)
85 cmd
= WASM_OPT
+ [os
.path
.join('test', 'print', t
), '--print-minified']
86 print ' ', ' '.join(cmd
)
87 actual
= subprocess
.check_output(cmd
)
88 with
open(os
.path
.join('test', 'print', wasm
+ '.minified.txt'), 'w') as o
: o
.write(actual
)
90 for t
in sorted(os
.listdir(os
.path
.join('test', 'passes'))):
91 if t
.endswith(('.wast', '.wasm')):
94 passname
= os
.path
.basename(t
).replace('.wast', '').replace('.wasm', '')
95 opts
= [('--' + p
if not p
.startswith('O') else '-' + p
) for p
in passname
.split('_')]
96 t
= os
.path
.join('test', 'passes', t
)
98 for module
, asserts
in split_wast(t
):
99 assert len(asserts
) == 0
100 with
open('split.wast', 'w') as o
: o
.write(module
)
101 cmd
= WASM_OPT
+ opts
+ ['split.wast', '--print']
102 actual
+= run_command(cmd
)
103 with
open(os
.path
.join('test', 'passes', passname
+ ('.bin' if binary
else '') + '.txt'), 'w') as o
: o
.write(actual
)
104 if 'emit-js-wrapper' in t
:
105 with
open('a.js') as i
:
106 with
open(t
+ '.js', 'w') as o
:
108 if 'emit-spec-wrapper' in t
:
109 with
open('a.wat') as i
:
110 with
open(t
+ '.wat', 'w') as o
:
113 print '\n[ checking wasm-opt -o notation... ]\n'
115 wast
= os
.path
.join('test', 'hello_world.wast')
116 cmd
= WASM_OPT
+ [wast
, '-o', 'a.wast', '-S']
118 open(wast
, 'w').write(open('a.wast').read())
120 print '\n[ checking binary format testcases... ]\n'
122 for wast
in sorted(os
.listdir('test')):
123 if wast
.endswith('.wast') and not wast
in []: # blacklist some known failures
124 for debug_info
in [0, 1]:
125 cmd
= WASM_AS
+ [os
.path
.join('test', wast
), '-o', 'a.wasm']
126 if debug_info
: cmd
+= ['-g']
128 if os
.path
.exists('a.wasm'): os
.unlink('a.wasm')
129 subprocess
.check_call(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
130 assert os
.path
.exists('a.wasm')
132 cmd
= WASM_DIS
+ ['a.wasm', '-o', 'a.wast']
134 if os
.path
.exists('a.wast'): os
.unlink('a.wast')
135 subprocess
.check_call(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
136 assert os
.path
.exists('a.wast')
137 actual
= open('a.wast').read()
138 binary_name
= wast
+ '.fromBinary'
139 if not debug_info
: binary_name
+= '.noDebugInfo'
140 with
open(os
.path
.join('test', binary_name
), 'w') as o
: o
.write(actual
)
142 print '\n[ checking example testcases... ]\n'
144 for t
in sorted(os
.listdir(os
.path
.join('test', 'example'))):
145 output_file
= os
.path
.join('bin', 'example')
146 libdir
= os
.path
.join(BINARYEN_INSTALL_DIR
, 'lib')
147 cmd
= ['-Isrc', '-g', '-lasmjs', '-lsupport', '-L' + libdir
, '-pthread', '-o', output_file
]
148 if t
.endswith('.txt'):
149 # check if there is a trace in the file, if so, we should build it
150 out
= subprocess
.Popen([os
.path
.join('scripts', 'clean_c_api_trace.py'), os
.path
.join('test', 'example', t
)], stdout
=subprocess
.PIPE
).communicate()[0]
152 print ' (no trace in ', t
, ')'
154 print ' (will check trace in ', t
, ')'
156 with
open(src
, 'w') as o
: o
.write(out
)
157 expected
= os
.path
.join('test', 'example', t
+ '.txt')
159 src
= os
.path
.join('test', 'example', t
)
160 expected
= os
.path
.join('test', 'example', '.'.join(t
.split('.')[:-1]) + '.txt')
161 if not src
.endswith(('.c', '.cpp')):
163 # build the C file separately
164 extra
= [os
.environ
.get('CC') or 'gcc',
165 src
, '-c', '-o', 'example.o',
166 '-Isrc', '-g', '-L' + libdir
, '-pthread']
167 print 'build: ', ' '.join(extra
)
169 subprocess
.check_call(extra
)
170 # Link against the binaryen C library DSO, using rpath
171 cmd
= ['example.o', '-lbinaryen', '-Wl,-rpath=' + os
.path
.abspath(libdir
)] + cmd
172 print ' ', t
, src
, expected
173 if os
.environ
.get('COMPILER_FLAGS'):
174 for f
in os
.environ
.get('COMPILER_FLAGS').split(' '):
176 cmd
= [os
.environ
.get('CXX') or 'g++', '-std=c++11'] + cmd
178 print 'link: ', ' '.join(cmd
)
179 subprocess
.check_call(cmd
)
180 print 'run...', output_file
181 proc
= subprocess
.Popen([output_file
], stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
182 actual
, err
= proc
.communicate()
183 assert proc
.returncode
== 0, [proc
.returncode
, actual
, err
]
184 with
open(expected
, 'w') as o
: o
.write(actual
)
186 os
.remove(output_file
)
187 if sys
.platform
== 'darwin':
188 # Also removes debug directory produced on Mac OS
189 shutil
.rmtree(output_file
+ '.dSYM')
191 print '\n[ checking wasm-opt testcases... ]\n'
193 for t
in os
.listdir('test'):
194 if t
.endswith('.wast') and not t
.startswith('spec'):
196 t
= os
.path
.join('test', t
)
198 cmd
= WASM_OPT
+ [t
, '--print']
199 actual
= run_command(cmd
)
200 actual
= actual
.replace('printing before:\n', '')
201 open(f
, 'w').write(actual
)
203 print '\n[ checking wasm-dis on provided binaries... ]\n'
205 for t
in os
.listdir('test'):
206 if t
.endswith('.wasm') and not t
.startswith('spec'):
208 t
= os
.path
.join('test', t
)
210 if os
.path
.isfile(t
+ '.map'): cmd
+= ['--source-map', t
+ '.map']
211 actual
= run_command(cmd
)
213 open(t
+ '.fromBinary', 'w').write(actual
)
215 print '\n[ checking wasm-merge... ]\n'
217 for t
in os
.listdir(os
.path
.join('test', 'merge')):
218 if t
.endswith(('.wast', '.wasm')):
220 t
= os
.path
.join('test', 'merge', t
)
222 for finalize
in [0, 1]:
224 cmd
= WASM_MERGE
+ [t
, u
, '-o', 'a.wast', '-S', '--verbose']
225 if finalize
: cmd
+= ['--finalize-memory-base=1024', '--finalize-table-base=8']
226 if opt
: cmd
+= ['-O']
227 stdout
= run_command(cmd
)
228 actual
= open('a.wast').read()
229 out
= t
+ '.combined'
230 if finalize
: out
+= '.finalized'
231 if opt
: out
+= '.opt'
232 with
open(out
, 'w') as o
: o
.write(actual
)
233 with
open(out
+ '.stdout', 'w') as o
: o
.write(stdout
)
236 print '\n[ checking binaryen.js testcases... ]\n'
238 for s
in sorted(os
.listdir(os
.path
.join('test', 'binaryen.js'))):
239 if not s
.endswith('.js'): continue
241 f
= open('a.js', 'w')
242 f
.write(open(os
.path
.join('bin', 'binaryen.js')).read())
243 f
.write(open(os
.path
.join('test', 'binaryen.js', s
)).read())
245 cmd
= [MOZJS
, 'a.js']
246 out
= run_command(cmd
, stderr
=subprocess
.STDOUT
)
247 with
open(os
.path
.join('test', 'binaryen.js', s
+ '.txt'), 'w') as o
: o
.write(out
)
249 print '\n[ checking wasm-ctor-eval... ]\n'
251 for t
in os
.listdir(os
.path
.join('test', 'ctor-eval')):
252 if t
.endswith(('.wast', '.wasm')):
254 t
= os
.path
.join('test', 'ctor-eval', t
)
255 ctors
= open(t
+ '.ctors').read().strip()
256 cmd
= WASM_CTOR_EVAL
+ [t
, '-o', 'a.wast', '-S', '--ctors', ctors
]
257 stdout
= run_command(cmd
)
258 actual
= open('a.wast').read()
260 with
open(out
, 'w') as o
: o
.write(actual
)
262 print '\n[ checking wasm2asm ]\n'
264 for wasm
in tests
+ spec_tests
+ extra_tests
:
265 if not wasm
.endswith('.wast'):
268 asm
= os
.path
.basename(wasm
).replace('.wast', '.2asm.js')
269 expected_file
= os
.path
.join('test', asm
)
271 if not os
.path
.exists(expected_file
):
276 cmd
= WASM2ASM
+ [os
.path
.join('test', wasm
)]
277 out
= run_command(cmd
)
278 with
open(expected_file
, 'w') as o
: o
.write(out
)
280 for wasm
in assert_tests
:
283 asserts
= os
.path
.basename(wasm
).replace('.wast.asserts', '.asserts.js')
284 traps
= os
.path
.basename(wasm
).replace('.wast.asserts', '.traps.js')
285 asserts_expected_file
= os
.path
.join('test', asserts
)
286 traps_expected_file
= os
.path
.join('test', traps
)
288 cmd
= WASM2ASM
+ [os
.path
.join('test', wasm
), '--allow-asserts']
289 out
= run_command(cmd
)
290 with
open(asserts_expected_file
, 'w') as o
: o
.write(out
)
292 cmd
+= ['--pedantic']
293 out
= run_command(cmd
)
294 with
open(traps_expected_file
, 'w') as o
: o
.write(out
)
296 if has_shell_timeout():
297 print '\n[ checking wasm-reduce ]\n'
299 for t
in os
.listdir(os
.path
.join('test', 'reduce')):
300 if t
.endswith('.wast'):
302 t
= os
.path
.join('test', 'reduce', t
)
304 run_command(WASM_AS
+ [t
, '-o', 'a.wasm'])
305 print run_command(WASM_REDUCE
+ ['a.wasm', '--command=bin/wasm-opt b.wasm --fuzz-exec', '-t', 'b.wasm', '-w', 'c.wasm'])
306 expected
= t
+ '.txt'
307 run_command(WASM_DIS
+ ['c.wasm', '-o', expected
])
309 print '\n[ success! ]'