Skip to content

Commit

Permalink
Don't pass -Xlinker flags to clang when compiling (emscripten-core#…
Browse files Browse the repository at this point in the history
…22716)

This avoids a warning about unused linker flags.
  • Loading branch information
sbc100 authored Oct 11, 2024
1 parent cd60934 commit 6956a0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
30 changes: 23 additions & 7 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = {}

Expand Down
6 changes: 6 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6956a0c

Please sign in to comment.