Skip to content

Commit 353d9ee

Browse files
authored
Allow -target to be specified on the command line. NFC (#19840)
This makes `-target wasm64` work and imply `-sMEMORY64`. Supporting these flags makes us more compatible with upstream clang and with wasi-sdk. See #19834
1 parent f38ebef commit 353d9ee

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

emcc.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def add_link_flag(state, i, f):
248248

249249
class EmccOptions:
250250
def __init__(self):
251+
self.target = ''
251252
self.output_file = None
252253
self.post_link = False
253254
self.executable = False
@@ -1460,14 +1461,14 @@ def phase_setup(options, state, newargs):
14601461
continue
14611462

14621463
arg = newargs[i]
1463-
if arg in ('-MT', '-MF', '-MJ', '-MQ', '-D', '-U', '-o', '-x',
1464+
if arg in {'-MT', '-MF', '-MJ', '-MQ', '-D', '-U', '-o', '-x',
14641465
'-Xpreprocessor', '-include', '-imacros', '-idirafter',
14651466
'-iprefix', '-iwithprefix', '-iwithprefixbefore',
14661467
'-isysroot', '-imultilib', '-A', '-isystem', '-iquote',
14671468
'-install_name', '-compatibility_version',
14681469
'-current_version', '-I', '-L', '-include-pch',
1469-
'-undefined',
1470-
'-Xlinker', '-Xclang', '-z'):
1470+
'-undefined', '-target',
1471+
'-Xlinker', '-Xclang', '-z'}:
14711472
skip = True
14721473

14731474
if not arg.startswith('-'):
@@ -1639,7 +1640,12 @@ def phase_setup(options, state, newargs):
16391640
if settings.DISABLE_EXCEPTION_THROWING and not settings.DISABLE_EXCEPTION_CATCHING:
16401641
exit_with_error("DISABLE_EXCEPTION_THROWING was set (probably from -fno-exceptions) but is not compatible with enabling exception catching (DISABLE_EXCEPTION_CATCHING=0). If you don't want exceptions, set DISABLE_EXCEPTION_CATCHING to 1; if you do want exceptions, don't link with -fno-exceptions")
16411642

1643+
if options.target.startswith('wasm64'):
1644+
default_setting('MEMORY64', 1)
1645+
16421646
if settings.MEMORY64:
1647+
if options.target.startswith('wasm32'):
1648+
exit_with_error('wasm32 target is not compatible with -sMEMORY64')
16431649
diagnostics.warning('experimental', '-sMEMORY64 is still experimental. Many features may not work.')
16441650

16451651
# Wasm SjLj cannot be used with Emscripten EH
@@ -3637,6 +3643,10 @@ def consume_arg_file():
36373643
elif arg.startswith('-o'):
36383644
options.output_file = removeprefix(arg, '-o')
36393645
newargs[i] = ''
3646+
elif check_arg('-target') or check_arg('--target'):
3647+
options.target = consume_arg()
3648+
if options.target not in ('wasm32', 'wasm64', 'wasm64-unknown-emscripten', 'wasm32-unknown-emscripten'):
3649+
exit_with_error(f'unsupported target: {options.target} (emcc only supports wasm64-unknown-emscripten and wasm32-unknown-emscripten)')
36403650
elif arg == '-mllvm':
36413651
# Ignore the next argument rather than trying to parse it. This is needed
36423652
# because llvm args could, for example, start with `-o` and we don't want

test/test_other.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13562,3 +13562,17 @@ def test_proxy_to_worker(self):
1356213562
@also_with_standalone_wasm()
1356313563
def test_console_out(self):
1356413564
self.do_other_test('test_console_out.c')
13565+
13566+
@requires_wasm64
13567+
def test_explicit_target(self):
13568+
self.do_runf(test_file('hello_world.c'), emcc_args=['-target', 'wasm32'])
13569+
self.do_runf(test_file('hello_world.c'), emcc_args=['-target', 'wasm64-unknown-emscripten', '-Wno-experimental'])
13570+
13571+
self.do_runf(test_file('hello_world.c'), emcc_args=['--target=wasm32'])
13572+
self.do_runf(test_file('hello_world.c'), emcc_args=['--target=wasm64-unknown-emscripten', '-Wno-experimental'])
13573+
13574+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-target', 'wasm32', '-sMEMORY64'])
13575+
self.assertContained('emcc: error: wasm32 target is not compatible with -sMEMORY64', err)
13576+
13577+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--target=arm64'])
13578+
self.assertContained('emcc: error: unsupported target: arm64 (emcc only supports wasm64-unknown-emscripten and wasm32-unknown-emscripten', err)

0 commit comments

Comments
 (0)