Skip to content

Commit 88f44af

Browse files
authored
Remove a lot of the direct use of Popen in favor of run_process (#6760)
1 parent f962c9d commit 88f44af

File tree

6 files changed

+99
-101
lines changed

6 files changed

+99
-101
lines changed

emcc.py

+21-23
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
2828
import sys
2929
import shutil
3030
import tempfile
31-
import subprocess
3231
import shlex
3332
import time
3433
import re
3534
import logging
3635
from subprocess import PIPE
3736

3837
from tools import shared, jsrun, system_libs, client_mods
39-
from tools.shared import execute, suffix, unsuffixed, unsuffixed_basename, WINDOWS, safe_copy, safe_move, run_process, asbytes
38+
from tools.shared import suffix, unsuffixed, unsuffixed_basename, WINDOWS, safe_copy, safe_move, run_process, asbytes
4039
from tools.response_file import substitute_response_files
4140
import tools.line_endings
4241
from tools.toolchain_profiler import ToolchainProfiler
@@ -384,7 +383,7 @@ def run():
384383
here = os.getcwd()
385384
os.chdir(shared.path_from_root())
386385
try:
387-
revision = execute(['git', 'show'], stdout=PIPE, stderr=PIPE)[0].split('\n')[0]
386+
revision = run_process(['git', 'show'], stdout=PIPE, stderr=PIPE).stdout.split('\n')[0]
388387
except:
389388
pass
390389
finally:
@@ -399,7 +398,7 @@ def run():
399398
elif len(sys.argv) == 2 and sys.argv[1] == '-v': # -v with no inputs
400399
# autoconf likes to see 'GNU' in the output to enable shared object support
401400
print('emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) %s' % shared.EMSCRIPTEN_VERSION)
402-
code = subprocess.call([shared.CLANG, '-v'])
401+
code = run_process([shared.CLANG, '-v'], check=False).returncode
403402
shared.check_sanity(force=True)
404403
return code
405404

@@ -418,7 +417,7 @@ def run():
418417
args = [x for x in sys.argv if x != '--cflags']
419418
with misc_temp_files.get_file(suffix='.o') as temp_target:
420419
input_file = 'hello_world.c'
421-
err = run_process([shared.PYTHON] + args + [shared.path_from_root('tests', input_file), '-c', '-o', temp_target], stderr=subprocess.PIPE, env=debug_env).stderr
420+
err = run_process([shared.PYTHON] + args + [shared.path_from_root('tests', input_file), '-c', '-o', temp_target], stderr=PIPE, env=debug_env).stderr
422421
lines = [x for x in err.split('\n') if shared.CLANG_CC in x and input_file in x]
423422
line = re.search('running: (.*)', lines[0]).group(1)
424423
parts = shlex.split(line.replace('\\', '\\\\'))
@@ -524,7 +523,7 @@ def filter_emscripten_options(argv):
524523
open(tempout, 'a').write('emcc, just configuring: ' + ' '.join(cmd) + '\n\n')
525524

526525
if not use_js:
527-
return subprocess.call(cmd)
526+
return run_process(cmd, check=False).returncode
528527
else:
529528
only_object = '-c' in cmd
530529
for i in reversed(range(len(cmd) - 1)): # Last -o directive should take precedence, if multiple are specified
@@ -536,7 +535,7 @@ def filter_emscripten_options(argv):
536535
if not target:
537536
target = 'a.out.js'
538537
os.environ['EMMAKEN_JUST_CONFIGURE_RECURSE'] = '1'
539-
ret = subprocess.call(cmd)
538+
ret = run_process(cmd, check=False).returncode
540539
os.environ['EMMAKEN_JUST_CONFIGURE_RECURSE'] = ''
541540
if not os.path.exists(target):
542541
# note that emcc -c will cause target to have the wrong value here;
@@ -1432,8 +1431,7 @@ def check(input_file):
14321431
args += ['-o', specified_target]
14331432
args = system_libs.process_args(args, shared.Settings)
14341433
logging.debug("running (for precompiled headers): " + call + ' ' + ' '.join(args))
1435-
execute([call] + args) # let compiler frontend print directly, so colors are saved (PIPE kills that)
1436-
return 0
1434+
return run_process([call] + args, check=False).returncode
14371435

14381436
def get_bitcode_file(input_file):
14391437
if final_suffix not in JS_CONTAINING_SUFFIXES:
@@ -1481,17 +1479,17 @@ def get_bitcode_args(input_files):
14811479
cmd += ['-o', specified_target]
14821480
# Do not compile, but just output the result from preprocessing stage or output the dependency rule. Warning: clang and gcc behave differently with -MF! (clang seems to not recognize it)
14831481
logging.debug(('just preprocessor ' if '-E' in newargs else 'just dependencies: ') + ' '.join(cmd))
1484-
return subprocess.call(cmd)
1482+
return run_process(cmd, check=False).returncode
14851483

14861484
def compile_source_file(i, input_file):
14871485
logging.debug('compiling source file: ' + input_file)
14881486
output_file = get_bitcode_file(input_file)
14891487
temp_files.append((i, output_file))
14901488
args = get_bitcode_args([input_file]) + ['-emit-llvm', '-c', '-o', output_file]
14911489
logging.debug("running: " + ' '.join(shared.Building.doublequote_spaces(args))) # NOTE: Printing this line here in this specific format is important, it is parsed to implement the "emcc --cflags" command
1492-
execute(args) # let compiler frontend print directly, so colors are saved (PIPE kills that)
1493-
if not os.path.exists(output_file):
1490+
if run_process(args, check=False).returncode != 0:
14941491
exit_with_error('compiler frontend failed to generate LLVM bitcode, halting')
1492+
assert(os.path.exists(output_file))
14951493

14961494
# First, generate LLVM bitcode. For each input file, we get base.o with bitcode
14971495
for i, input_file in input_files:
@@ -1707,7 +1705,7 @@ def get_final():
17071705
if AUTODEBUG:
17081706
logging.debug('autodebug')
17091707
next = get_final() + '.ad.ll'
1710-
execute([shared.PYTHON, shared.AUTODEBUGGER, final, next])
1708+
run_process([shared.PYTHON, shared.AUTODEBUGGER, final, next])
17111709
final = next
17121710
save_intermediate('autodebug', 'll')
17131711

@@ -1783,7 +1781,7 @@ def get_final():
17831781
file_args.append('--lz4')
17841782
if options.use_preload_plugins:
17851783
file_args.append('--use-preload-plugins')
1786-
file_code = execute([shared.PYTHON, shared.FILE_PACKAGER, unsuffixed(target) + '.data'] + file_args, stdout=PIPE)[0]
1784+
file_code = run_process([shared.PYTHON, shared.FILE_PACKAGER, unsuffixed(target) + '.data'] + file_args, stdout=PIPE).stdout
17871785
options.pre_js = file_code + options.pre_js
17881786

17891787
# Apply pre and postjs files
@@ -1811,7 +1809,7 @@ def get_final():
18111809
final += '.tr.js'
18121810
posix = not shared.WINDOWS
18131811
logging.debug('applying transform: %s', options.js_transform)
1814-
subprocess.check_call(shared.Building.remove_quotes(shlex.split(options.js_transform, posix=posix) + [os.path.abspath(final)]))
1812+
shared.check_call(shared.Building.remove_quotes(shlex.split(options.js_transform, posix=posix) + [os.path.abspath(final)]))
18151813
save_intermediate('transformed')
18161814

18171815
js_transform_tempfiles = [final]
@@ -1986,7 +1984,7 @@ def get_eliminate():
19861984

19871985
# Bundle symbol data in with the cyberdwarf file
19881986
if shared.Settings.CYBERDWARF:
1989-
execute([shared.PYTHON, shared.path_from_root('tools', 'emdebug_cd_merger.py'), target + '.cd', target + '.symbols'])
1987+
run_process([shared.PYTHON, shared.path_from_root('tools', 'emdebug_cd_merger.py'), target + '.cd', target + '.symbols'])
19901988

19911989
if use_source_map(options) and not shared.Settings.WASM:
19921990
emit_js_source_maps(target, optimizer.js_transform_tempfiles)
@@ -2336,7 +2334,7 @@ def emterpretify(js_target, optimizer, options):
23362334
args += ['MEMORY_SAFE=1']
23372335
if shared.Settings.EMTERPRETIFY_FILE:
23382336
args += ['FILE="' + shared.Settings.EMTERPRETIFY_FILE + '"']
2339-
execute(args)
2337+
run_process(args)
23402338
final = final + '.em.js'
23412339
finally:
23422340
shared.try_delete(js_target)
@@ -2379,7 +2377,7 @@ def emit_js_source_maps(target, js_transform_tempfiles):
23792377
def separate_asm_js(final, asm_target):
23802378
"""Separate out the asm.js code, if asked. Or, if necessary for another option"""
23812379
logging.debug('separating asm')
2382-
subprocess.check_call([shared.PYTHON, shared.path_from_root('tools', 'separate_asm.py'), final, asm_target, final])
2380+
shared.check_call([shared.PYTHON, shared.path_from_root('tools', 'separate_asm.py'), final, asm_target, final])
23832381

23842382
# extra only-my-code logic
23852383
if shared.Settings.ONLY_MY_CODE:
@@ -2473,7 +2471,7 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
24732471
wrote_wasm_text = True
24742472
logging.debug('asm2wasm (asm.js => WebAssembly): ' + ' '.join(cmd))
24752473
TimeLogger.update()
2476-
subprocess.check_call(cmd)
2474+
shared.check_call(cmd)
24772475

24782476
if not target_binary:
24792477
cmd = [os.path.join(binaryen_bin, 'wasm-as'), wasm_text_target, '-o', wasm_binary_target]
@@ -2484,7 +2482,7 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
24842482
if options.source_map_base:
24852483
cmd += ['--source-map-url=' + options.source_map_base + os.path.basename(wasm_binary_target) + '.map']
24862484
logging.debug('wasm-as (text => binary): ' + ' '.join(cmd))
2487-
subprocess.check_call(cmd)
2485+
shared.check_call(cmd)
24882486
if import_mem_init:
24892487
# remove the mem init file in later processing; it does not need to be prefetched in the html, etc.
24902488
if DEBUG:
@@ -2500,11 +2498,11 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
25002498
if debug_info:
25012499
cmd += ['-g'] # preserve the debug info
25022500
logging.debug('wasm-opt on BINARYEN_PASSES: ' + ' '.join(cmd))
2503-
subprocess.check_call(cmd)
2501+
shared.check_call(cmd)
25042502
if not wrote_wasm_text and 'interpret-s-expr' in shared.Settings.BINARYEN_METHOD:
25052503
cmd = [os.path.join(binaryen_bin, 'wasm-dis'), wasm_binary_target, '-o', wasm_text_target]
25062504
logging.debug('wasm-dis (binary => text): ' + ' '.join(cmd))
2507-
subprocess.check_call(cmd)
2505+
shared.check_call(cmd)
25082506
if shared.Settings.BINARYEN_SCRIPTS:
25092507
binaryen_scripts = os.path.join(shared.Settings.BINARYEN_ROOT, 'scripts')
25102508
script_env = os.environ.copy()
@@ -2515,7 +2513,7 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
25152513
script_env['PYTHONPATH'] = root_dir
25162514
for script in shared.Settings.BINARYEN_SCRIPTS.split(','):
25172515
logging.debug('running binaryen script: ' + script)
2518-
subprocess.check_call([shared.PYTHON, os.path.join(binaryen_scripts, script), final, wasm_text_target], env=script_env)
2516+
shared.check_call([shared.PYTHON, os.path.join(binaryen_scripts, script), final, wasm_text_target], env=script_env)
25192517
if shared.Settings.EVAL_CTORS:
25202518
if DEBUG:
25212519
save_intermediate('pre-eval-ctors', 'js')

emscripten.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def compile_js(infile, settings, temp_files, DEBUG):
110110
logging.debug('emscript: llvm backend: ' + ' '.join(backend_args))
111111
t = time.time()
112112
with ToolchainProfiler.profile_block('emscript_llvm_backend'):
113-
shared.jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE, universal_newlines=True), note_args=backend_args)
113+
jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE, universal_newlines=True), note_args=backend_args)
114114
if DEBUG:
115115
logging.debug(' emscript: llvm backend took %s seconds' % (time.time() - t))
116116

tests/test_core.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -2105,7 +2105,7 @@ def test_structbyval(self):
21052105
Building.link([supp_name + '.o', main_name + '.o'], all_name)
21062106

21072107
# This will fail! See explanation near the warning we check for, in the compiler source code
2108-
output = Popen([PYTHON, EMCC, all_name], stderr=PIPE).communicate()
2108+
run_process([PYTHON, EMCC, all_name], check=False, stderr=PIPE)
21092109

21102110
# Check for warning in the generated code
21112111
generated = open(os.path.join(self.get_dir(), 'src.cpp.o.js')).read()
@@ -3327,7 +3327,7 @@ def dylink_test(self, main, side, expected, header=None, main_emcc_args=[], forc
33273327
if isinstance(side, list):
33283328
# side is just a library
33293329
try_delete('liblib.cpp.o.' + side_suffix)
3330-
Popen([PYTHON, EMCC] + side + self.emcc_args + Settings.serialize() + ['-o', os.path.join(self.get_dir(), 'liblib.cpp.o.' + side_suffix)]).communicate()
3330+
run_process([PYTHON, EMCC] + side + self.emcc_args + Settings.serialize() + ['-o', os.path.join(self.get_dir(), 'liblib.cpp.o.' + side_suffix)])
33313331
else:
33323332
base = 'liblib.cpp' if not force_c else 'liblib.c'
33333333
try_delete(base + '.o.' + side_suffix)
@@ -3354,7 +3354,7 @@ def dylink_test(self, main, side, expected, header=None, main_emcc_args=[], forc
33543354
if isinstance(main, list):
33553355
# main is just a library
33563356
try_delete('src.cpp.o.js')
3357-
Popen([PYTHON, EMCC] + main + self.emcc_args + Settings.serialize() + ['-o', os.path.join(self.get_dir(), 'src.cpp.o.js')]).communicate()
3357+
run_process([PYTHON, EMCC] + main + self.emcc_args + Settings.serialize() + ['-o', os.path.join(self.get_dir(), 'src.cpp.o.js')])
33583358
self.do_run(None, expected, no_build=True)
33593359
else:
33603360
self.do_run(main, expected, force_c=force_c)
@@ -3960,7 +3960,7 @@ def test_dylink_hyper_dupe(self):
39603960
int sideg = 49;
39613961
int bsidef() { return 536; }
39623962
''')
3963-
Popen([PYTHON, EMCC, 'third.cpp', '-s', 'SIDE_MODULE=1'] + Building.COMPILER_TEST_OPTS + self.emcc_args + ['-o', 'third.js']).communicate()
3963+
run_process([PYTHON, EMCC, 'third.cpp', '-s', 'SIDE_MODULE=1'] + Building.COMPILER_TEST_OPTS + self.emcc_args + ['-o', 'third.js'])
39643964

39653965
self.dylink_test(main=r'''
39663966
#include <stdio.h>
@@ -3992,14 +3992,14 @@ def test_dylink_dot_a(self):
39923992
open('third.cpp', 'w').write(r'''
39933993
int sidef() { return 36; }
39943994
''')
3995-
Popen([PYTHON, EMCC, 'third.cpp'] + Building.COMPILER_TEST_OPTS + self.emcc_args + ['-o', 'third.o', '-c']).communicate()
3995+
run_process([PYTHON, EMCC, 'third.cpp'] + Building.COMPILER_TEST_OPTS + self.emcc_args + ['-o', 'third.o', '-c'])
39963996

39973997
open('fourth.cpp', 'w').write(r'''
39983998
int sideg() { return 17; }
39993999
''')
4000-
Popen([PYTHON, EMCC, 'fourth.cpp'] + Building.COMPILER_TEST_OPTS + self.emcc_args + ['-o', 'fourth.o', '-c']).communicate()
4000+
run_process([PYTHON, EMCC, 'fourth.cpp'] + Building.COMPILER_TEST_OPTS + self.emcc_args + ['-o', 'fourth.o', '-c'])
40014001

4002-
Popen([PYTHON, EMAR, 'rc', 'libfourth.a', 'fourth.o']).communicate()
4002+
run_process([PYTHON, EMAR, 'rc', 'libfourth.a', 'fourth.o'])
40034003

40044004
self.dylink_test(main=r'''
40054005
#include <stdio.h>
@@ -5256,8 +5256,7 @@ def test_dlmalloc(self):
52565256
# emcc should build in dlmalloc automatically, and do all the sign correction etc. for it
52575257

52585258
try_delete(os.path.join(self.get_dir(), 'src.cpp.o.js'))
5259-
output = Popen([PYTHON, EMCC, path_from_root('tests', 'dlmalloc_test.c'), '-s', 'TOTAL_MEMORY=128MB',
5260-
'-o', os.path.join(self.get_dir(), 'src.cpp.o.js')], stdout=PIPE, stderr=self.stderr_redirect).communicate()
5259+
run_process([PYTHON, EMCC, path_from_root('tests', 'dlmalloc_test.c'), '-s', 'TOTAL_MEMORY=128MB', '-o', os.path.join(self.get_dir(), 'src.cpp.o.js')], stdout=PIPE, stderr=self.stderr_redirect)
52615260

52625261
self.do_run('x', '*1,0*', ['200', '1'], no_build=True)
52635262
self.do_run('x', '*400,0*', ['400', '400'], no_build=True)
@@ -5414,7 +5413,7 @@ def ignore_nans(out, err = ''):
54145413
# Tests the full SSE1 API.
54155414
@SIMD
54165415
def test_sse1_full(self):
5417-
Popen([CLANG, path_from_root('tests', 'test_sse1_full.cpp'), '-o', 'test_sse1_full', '-D_CRT_SECURE_NO_WARNINGS=1'] + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE).communicate()
5416+
run_process([CLANG, path_from_root('tests', 'test_sse1_full.cpp'), '-o', 'test_sse1_full', '-D_CRT_SECURE_NO_WARNINGS=1'] + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE)
54185417
native_result = run_process('./test_sse1_full', stdout=PIPE).stdout
54195418

54205419
Settings.PRECISE_F32 = 1 # SIMD currently requires Math.fround
@@ -5435,7 +5434,7 @@ def test_sse2_full(self):
54355434

54365435
args = []
54375436
if '-O0' in self.emcc_args: args += ['-D_DEBUG=1']
5438-
Popen([CLANG, path_from_root('tests', 'test_sse2_full.cpp'), '-o', 'test_sse2_full', '-D_CRT_SECURE_NO_WARNINGS=1'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE).communicate()
5437+
run_process([CLANG, path_from_root('tests', 'test_sse2_full.cpp'), '-o', 'test_sse2_full', '-D_CRT_SECURE_NO_WARNINGS=1'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE)
54395438
native_result = run_process('./test_sse2_full', stdout=PIPE).stdout
54405439

54415440
Settings.PRECISE_F32 = 1 # SIMD currently requires Math.fround
@@ -5451,7 +5450,7 @@ def test_sse2_full(self):
54515450
def test_sse3_full(self):
54525451
args = []
54535452
if '-O0' in self.emcc_args: args += ['-D_DEBUG=1']
5454-
Popen([CLANG, path_from_root('tests', 'test_sse3_full.cpp'), '-o', 'test_sse3_full', '-D_CRT_SECURE_NO_WARNINGS=1', '-msse3'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE).communicate()
5453+
run_process([CLANG, path_from_root('tests', 'test_sse3_full.cpp'), '-o', 'test_sse3_full', '-D_CRT_SECURE_NO_WARNINGS=1', '-msse3'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE)
54555454
native_result = run_process('./test_sse3_full', stdout=PIPE).stdout
54565455

54575456
Settings.PRECISE_F32 = 1 # SIMD currently requires Math.fround
@@ -5464,7 +5463,7 @@ def test_sse3_full(self):
54645463
def test_ssse3_full(self):
54655464
args = []
54665465
if '-O0' in self.emcc_args: args += ['-D_DEBUG=1']
5467-
Popen([CLANG, path_from_root('tests', 'test_ssse3_full.cpp'), '-o', 'test_ssse3_full', '-D_CRT_SECURE_NO_WARNINGS=1', '-mssse3'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE).communicate()
5466+
run_process([CLANG, path_from_root('tests', 'test_ssse3_full.cpp'), '-o', 'test_ssse3_full', '-D_CRT_SECURE_NO_WARNINGS=1', '-mssse3'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE)
54685467
native_result = run_process('./test_ssse3_full', stdout=PIPE).stdout
54695468

54705469
Settings.PRECISE_F32 = 1 # SIMD currently requires Math.fround
@@ -5477,7 +5476,7 @@ def test_ssse3_full(self):
54775476
def test_sse4_1_full(self):
54785477
args = []
54795478
if '-O0' in self.emcc_args: args += ['-D_DEBUG=1']
5480-
Popen([CLANG, path_from_root('tests', 'test_sse4_1_full.cpp'), '-o', 'test_sse4_1_full', '-D_CRT_SECURE_NO_WARNINGS=1', '-msse4.1'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE).communicate()
5479+
run_process([CLANG, path_from_root('tests', 'test_sse4_1_full.cpp'), '-o', 'test_sse4_1_full', '-D_CRT_SECURE_NO_WARNINGS=1', '-msse4.1'] + args + get_clang_native_args(), env=get_clang_native_env(), stdout=PIPE)
54815480
native_result = run_process('./test_sse4_1_full', stdout=PIPE).stdout
54825481

54835482
Settings.PRECISE_F32 = 1 # SIMD currently requires Math.fround
@@ -6879,9 +6878,9 @@ def do_test_in_mode(mode, allow_memory_growth):
68796878
# Force IDL checks mode
68806879
os.environ['IDL_CHECKS'] = mode
68816880

6882-
output = Popen([PYTHON, path_from_root('tools', 'webidl_binder.py'),
6883-
path_from_root('tests', 'webidl', 'test.idl'),
6884-
'glue']).communicate()[0]
6881+
run_process([PYTHON, path_from_root('tools', 'webidl_binder.py'),
6882+
path_from_root('tests', 'webidl', 'test.idl'),
6883+
'glue'])
68856884
assert os.path.exists('glue.cpp')
68866885
assert os.path.exists('glue.js')
68876886

@@ -7580,7 +7579,7 @@ def test_cxx_self_assign(self):
75807579
}
75817580
}
75827581
''')
7583-
Popen([PYTHON, EMCC, 'src.cpp']).communicate()
7582+
run_process([PYTHON, EMCC, 'src.cpp'])
75847583
self.assertContained('ok.', run_js('a.out.js', args=['C']))
75857584

75867585
def test_memprof_requirements(self):

0 commit comments

Comments
 (0)