Skip to content

Commit 1ccae3e

Browse files
authored
Use -lxxx internally rather than full library path (#14342)
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 in line with other compilers.
1 parent 98a8cbf commit 1ccae3e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ See docs/process.md for more on how version tagging works.
2121
2.0.24
2222
------
2323
- Support `--preload-file` in Node.js. (#11785)
24+
- System libraries are now passed to the linker internally via `-lfoo` rather
25+
than using their full path. This is in line with how gcc and clang pass system
26+
libraries to the linker. This should not effect any builds unless a project a
27+
happens to have, for example, a file called `libc.a` in one of its library
28+
paths. This would have the effect of overriding the system library (as it
29+
would with gcc or clang) (#14342).
2430
- CMake projects (those that either use emcmake or use Emscripten.cmake
2531
directly) are new configured to install (by default) directly into the
2632
emscripten sysroot. This means that running `cmake --install` (or running the

tools/system_libs.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,20 @@ def get_path(self):
300300
"""
301301
return shared.Cache.get_lib(self.get_filename(), self.build)
302302

303+
def get_link_flag(self):
304+
"""
305+
Gets the link flags needed to use the library.
306+
307+
This will trigger a build if this library is not in the cache.
308+
"""
309+
fullpath = self.get_path()
310+
# For non-libaries (e.g. crt1.o) we pass the entire path to the linker
311+
if self.get_ext() != '.a':
312+
return fullpath
313+
# For libraries (.a) files, we pass the abbreviated `-l` form.
314+
base = shared.unsuffixed_basename(fullpath)
315+
return '-l' + shared.strip_prefix(base, 'lib')
316+
303317
def get_files(self):
304318
"""
305319
Gets a list of source files for this library.
@@ -1487,7 +1501,7 @@ def add_library(libname):
14871501
logger.debug('including %s (%s)' % (lib.name, lib.get_filename()))
14881502

14891503
need_whole_archive = lib.name in force_include and lib.get_ext() == '.a'
1490-
libs_to_link.append((lib.get_path(), need_whole_archive))
1504+
libs_to_link.append((lib.get_link_flag(), need_whole_archive))
14911505

14921506
if settings.USE_PTHREADS:
14931507
add_library('crtbegin')

0 commit comments

Comments
 (0)