Skip to content

Commit a7d01cd

Browse files
committed
Use -lxxx internally rather than full library path
Since we already supply the correct `-L` path we can rely on wasm-ld to find, for example, libc.a by simply pasing `-lc` rather than its full path. This is in line with what gcc and clang do. This is almost NFC, except in that case where the user supplies and library path via `-L` which happens to contains a file called `libc.a`. In this case, as with clang or gcc, that file will be chosen over the system one. This makes the linker command lines shorter, more readable, more inline with other compilers.
1 parent bdaee66 commit a7d01cd

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ See docs/process.md for more on how version tagging works.
2020

2121
2.0.24
2222
------
23+
- System libraries are now passed to the linker internally via `-lfoo` rather
24+
than using their full path. This is in line with how gcc and clang pass system
25+
libraries to the linker. This should not effect any builds unless a project a
26+
happens to have, for example, a file called `libc.a` in one of its library
27+
paths. This would have the effect of overriding the system library (as it
28+
would with gcc or clang) (#14342).
2329
- CMake projects (those that either use emcmake or use Emscripten.cmake
2430
directly) are new configured to install (by default) directly into the
2531
emscripten sysroot. This means that running `cmake --install` (or running the

embuilder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ def main():
176176
library = SYSTEM_LIBRARIES[what]
177177
if force:
178178
library.erase()
179-
library.get_path()
179+
# This will trigger a build of the library if it doesn't already
180+
# exist.
181+
library.get_link_flag()
180182
elif what == 'sysroot':
181183
if force:
182184
shared.Cache.erase_file('sysroot_install.stamp')

emcc.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,11 @@ def is_supported(f):
462462
# lld allows various flags to have either a single -foo or double --foo
463463
if f.startswith(flag) or f.startswith('-' + flag):
464464
diagnostics.warning('linkflags', 'ignoring unsupported linker flag: `%s`', f)
465-
return False, takes_arg
465+
# Skip the next argument if this linker flag takes and argument and that
466+
# argument was not specified as a separately (i.e. it was specified as
467+
# single arg containing an `=` char.)
468+
skip_next = takes_arg and '=' not in f
469+
return False, skip_next
466470
return True, False
467471
else:
468472
if f in SUPPORTED_LINKER_FLAGS:
@@ -749,11 +753,6 @@ def emsdk_ldflags(user_args):
749753
if '-nostdlib' in user_args:
750754
return ldflags
751755

752-
# TODO(sbc): Add system libraries here rather than conditionally including
753-
# them via .symbols files.
754-
libraries = []
755-
ldflags += ['-l' + l for l in libraries]
756-
757756
return ldflags
758757

759758

tools/system_libs.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,20 @@ def can_build(self):
292292
def erase(self):
293293
shared.Cache.erase_file(shared.Cache.get_lib_name(self.get_filename()))
294294

295-
def get_path(self):
295+
def get_link_flag(self):
296296
"""
297-
Gets the cached path of this library.
297+
Gets the link flags needed to use the library.
298298
299299
This will trigger a build if this library is not in the cache.
300300
"""
301-
return shared.Cache.get_lib(self.get_filename(), self.build)
301+
fullpath = shared.Cache.get_lib(self.get_filename(), self.build)
302+
# For non-libaries (e.g. crt1.o) we pass the entire path to the linker
303+
if self.get_ext() != '.a':
304+
return fullpath
305+
# For libraries (.a) files, we pass the abbreviated `-l` form.
306+
base = shared.unsuffixed_basename(fullpath)
307+
assert base.startswith('lib')
308+
return '-l' + base[len('lib'):]
302309

303310
def get_files(self):
304311
"""
@@ -1486,8 +1493,9 @@ def add_library(libname):
14861493

14871494
logger.debug('including %s (%s)' % (lib.name, lib.get_filename()))
14881495

1489-
need_whole_archive = lib.name in force_include and lib.get_ext() == '.a'
1490-
libs_to_link.append((lib.get_path(), need_whole_archive))
1496+
is_archive = lib.get_ext() == '.a'
1497+
need_whole_archive = lib.name in force_include and is_archive
1498+
libs_to_link.append((lib.get_link_flag(), need_whole_archive))
14911499

14921500
if settings.USE_PTHREADS:
14931501
add_library('crtbegin')

0 commit comments

Comments
 (0)