Skip to content

Commit 7e0584e

Browse files
authored
Simplify --cflags emcc flag (#25534)
See #25524
1 parent 6ec477e commit 7e0584e

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

emcc.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
import tarfile
3232
from dataclasses import dataclass
3333
from enum import Enum, auto, unique
34-
from subprocess import PIPE
3534

3635

3736
from tools import shared, system_libs, utils, cmdline
3837
from tools import diagnostics, building, compile
3938
from tools.shared import unsuffixed_basename, get_file_suffix
40-
from tools.shared import run_process, exit_with_error, DEBUG
39+
from tools.shared import exit_with_error, DEBUG
4140
from tools.shared import in_temp
4241
from tools.shared import DYLIB_EXTENSIONS
4342
from tools.cmdline import CLANG_FLAGS_WITH_ARGS
@@ -242,27 +241,6 @@ def run(args):
242241
print(utils.EMSCRIPTEN_VERSION)
243242
return 0
244243

245-
if '--cflags' in args:
246-
# fake running the command, to see the full args we pass to clang
247-
args = [x for x in args if x != '--cflags']
248-
with shared.get_temp_files().get_file(suffix='.o') as temp_target:
249-
input_file = 'hello_world.c'
250-
compiler = shared.EMCC
251-
if shared.run_via_emxx:
252-
compiler = shared.EMXX
253-
cmd = [compiler, utils.path_from_root('test', input_file), '-v', '-c', '-o', temp_target] + args
254-
proc = run_process(cmd, stderr=PIPE, check=False)
255-
if proc.returncode != 0:
256-
print(proc.stderr)
257-
exit_with_error('error getting cflags')
258-
lines = [x for x in proc.stderr.splitlines() if clang in x and input_file in x]
259-
if not lines:
260-
exit_with_error(f'unable to parse output of `{cmd}`:\n{proc.stderr}')
261-
parts = shlex.split(lines[0].replace('\\', '\\\\'))
262-
parts = [x for x in parts if x not in ['-c', '-o', '-v', '-emit-llvm'] and input_file not in x and temp_target not in x]
263-
print(shlex.join(parts[1:]))
264-
return 0
265-
266244
if '-dumpmachine' in args or '-print-target-triple' in args or '--print-target-triple' in args:
267245
print(shared.get_llvm_target())
268246
return 0
@@ -314,6 +292,13 @@ def run(args):
314292

315293
phase_setup(options, state)
316294

295+
if '--cflags' in args:
296+
# Just print the flags we pass to clang and exit. We need to do this after
297+
# phase_setup because the setup sets things like SUPPORT_LONGJMP.
298+
cflags = compile.get_cflags(x for x in args if x != '--cflags')
299+
print(shlex.join(cflags))
300+
return 0
301+
317302
if options.reproduce:
318303
create_reproduce_file(options.reproduce, args)
319304

test/test_other.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -689,19 +689,20 @@ def test_js_transform(self):
689689
self.assertIn('transformed!', read_file('a.out.js'))
690690

691691
@crossplatform
692+
@also_with_wasm64
692693
def test_emcc_cflags(self):
693-
output = self.run_process([EMCC, '--cflags'], stdout=PIPE)
694+
output = self.run_process([EMCC, '--cflags'] + self.get_cflags(), stdout=PIPE)
694695
flags = output.stdout.strip()
695-
self.assertContained('-target wasm32-unknown-emscripten', flags)
696+
self.assertContained(r'-target wasm\d\d-unknown-emscripten', flags, regex=True)
696697
self.assertContained('--sysroot=', flags)
697-
output = self.run_process([EMXX, '--cflags'], stdout=PIPE)
698+
output = self.run_process([EMXX, '--cflags'] + self.get_cflags(), stdout=PIPE)
698699
flags = output.stdout.strip()
699-
self.assertContained('-target wasm32-unknown-emscripten', flags)
700+
self.assertContained(r'-target wasm\d\d-unknown-emscripten', flags, regex=True)
700701
self.assertContained('--sysroot=', flags)
701702
# check they work
702703
cmd = [CLANG_CC, test_file('hello_world.c')] + shlex.split(flags.replace('\\', '\\\\')) + ['-c', '-o', 'out.o']
703704
self.run_process(cmd)
704-
self.run_process([EMCC, 'out.o'])
705+
self.run_process([EMCC, 'out.o'] + self.get_cflags())
705706
self.assertContained('hello, world!', self.run_js('a.out.js'))
706707

707708
@crossplatform

0 commit comments

Comments
 (0)