Skip to content

Fix unexported main warning for main functions that take arguments #21011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -7923,14 +7923,18 @@ def test(contents, expected, args=[], assert_returncode=0): # noqa
# test backwards compatibility
test("Module['print']('print'); Module['printErr']('err'); ", 'print\nerr', ['-sEXPORTED_RUNTIME_METHODS=print,printErr', '-Wno-js-compiler'])

@parameterized({
'': ('hello_world.c',),
'argv': ('hello_world_argv.c',),
})
@parameterized({
'': ([],),
'O2': (['-O2'],),
})
def test_warn_unexported_main(self, args):
def test_warn_unexported_main(self, args, filename):
warning = 'emcc: warning: `main` is defined in the input files, but `_main` is not in `EXPORTED_FUNCTIONS`. Add it to this list if you want `main` to run. [-Wunused-main]'

proc = self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORTED_FUNCTIONS=[]'] + args, stderr=PIPE)
proc = self.run_process([EMCC, test_file(filename), '-sEXPORTED_FUNCTIONS=[]'] + args, stderr=PIPE)
# This warning only shows up when ASSERTIONS are enabled.
# We run both ways those to ensure that main doesn't get run in either case.
if '-O2' in args:
Expand Down
5 changes: 4 additions & 1 deletion tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,10 @@ def finalize_wasm(infile, outfile, js_syms):
if not settings.STANDALONE_WASM and '_main' in unexpected_exports:
diagnostics.warning('unused-main', '`main` is defined in the input files, but `_main` is not in `EXPORTED_FUNCTIONS`. Add it to this list if you want `main` to run.')
unexpected_exports.remove('_main')
metadata.all_exports.remove('main')
if 'main' in metadata.all_exports:
metadata.all_exports.remove('main')
else:
metadata.all_exports.remove('__main_argc_argv')

building.user_requested_exports.update(unexpected_exports)
settings.EXPORTED_FUNCTIONS.extend(unexpected_exports)
Expand Down
2 changes: 1 addition & 1 deletion tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def phase_linker_setup(options, state, newargs):
# See other.test_warn_unexported_main.
# This is not needed in STANDALONE_WASM mode since we export _start
# (unconditionally) rather than main.
settings.EXPORT_IF_DEFINED.append('main')
settings.EXPORT_IF_DEFINED += ['main', '__main_argc_argv']

if settings.ASSERTIONS:
# Exceptions are thrown with a stack trace by default when ASSERTIONS is
Expand Down