From 6956a0c3c817c9647d285324ddb46d0d1186a721 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 10 Oct 2024 22:10:20 -0700 Subject: [PATCH] Don't pass `-Xlinker` flags to clang when compiling (#22716) This avoids a warning about unused linker flags. --- emcc.py | 30 +++++++++++++++++++++++------- test/test_other.py | 6 ++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/emcc.py b/emcc.py index 8ac79fd5efba..617be15c106d 100644 --- a/emcc.py +++ b/emcc.py @@ -937,6 +937,28 @@ def get_clang_output_extension(state): return '.o' +def filter_out_link_flags(args): + rtn = [] + + def is_link_flag(flag): + if flag in ('-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'): + return True + return flag.startswith(('-l', '-L', '-Wl,', '-z')) + + skip = False + for arg in args: + if skip: + skip = False + continue + if is_link_flag(arg): + continue + if arg == '-Xlinker': + skip = True + continue + rtn.append(arg) + return rtn + + @ToolchainProfiler.profile_block('compile inputs') def phase_compile_inputs(options, state, newargs, input_files): if shared.run_via_emxx: @@ -1016,13 +1038,7 @@ def get_clang_command_asm(): # In COMPILE_AND_LINK we need to compile source files too, but we also need to # filter out the link flags - - def is_link_flag(flag): - if flag in ('-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'): - return True - return flag.startswith(('-l', '-L', '-Wl,', '-z')) - - compile_args = [a for a in compile_args if a and not is_link_flag(a)] + compile_args = filter_out_link_flags(compile_args) linker_inputs = [] seen_names = {} diff --git a/test/test_other.py b/test/test_other.py index 4b4b17a5a4d1..923a49d1a98e 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -11946,6 +11946,12 @@ def test_linker_flags_unused(self): err = self.run_process([EMCC, test_file('hello_world.c'), '-c', '-lbar'], stderr=PIPE).stderr self.assertContained("warning: -lbar: 'linker' input unused [-Wunused-command-line-argument]", err) + # Check that we don't see these "input unused" errors for linker flags when + # compiling and linking in single step (i.e. ensure that we don't pass them to clang when + # compiling internally). + err = self.run_process([EMCC, test_file('hello_world.c'), '-Wl,-static', '-Xlinker', '-static'], stderr=PIPE).stderr + self.assertNotContained("input unused", err) + def test_linker_input_unused(self): self.run_process([EMCC, '-c', test_file('hello_world.c')]) err = self.run_process([EMCC, 'hello_world.o', '-c', '-o', 'out.o'], stderr=PIPE).stderr