Skip to content

Commit 2477845

Browse files
committed
Honor -Bstatic and -Bdynamic and pass these down the wasm-ld
1 parent 0cbab82 commit 2477845

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def get_next_arg():
370370
add_link_arg(flag)
371371
elif arg == '-Xlinker':
372372
add_link_arg(get_next_arg())
373-
elif arg == '-s' or arg.startswith(('-l', '-L', '--js-library=', '-z', '-u')):
373+
elif arg in {'-s', '-Bstatic', '-Bdynamic'} or arg.startswith(('-l', '-L', '--js-library=', '-z', '-u')):
374374
add_link_arg(arg)
375375
elif not arg.startswith('-o') and arg not in {'-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'}:
376376
# All other flags are for the compiler

test/test_other.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,44 @@ def test_dylink_LEGACY_GL_EMULATION(self):
24282428
}''')
24292429
self.do_runf('test.c', 'done\n', cflags=['-sLEGACY_GL_EMULATION', '-sMAIN_MODULE=2'])
24302430

2431+
def test_dylink_library_search(self):
2432+
# Test library resolution in the case when both static and dynamic library are present.
2433+
create_file('side_dyn.c', r'''
2434+
#include <stdio.h>
2435+
void side_func() {
2436+
printf("dynamic linking used\n");
2437+
}
2438+
''')
2439+
create_file('side_static.c', r'''
2440+
#include <stdio.h>
2441+
void side_func() {
2442+
printf("static linking used\n");
2443+
}
2444+
''')
2445+
self.emcc('side_dyn.c', ['-sSIDE_MODULE', '-olibside.so'])
2446+
self.emcc('side_static.c', ['-oside_static.o', '-c'])
2447+
self.run_process([EMAR, 'rc', 'libside.a', 'side_static.o'])
2448+
2449+
create_file('main.c', '''
2450+
void side_func();
2451+
int main() {
2452+
side_func();
2453+
return 0;
2454+
}
2455+
''')
2456+
2457+
# By deafult we use static linking and prefer libside.a
2458+
self.do_runf('main.c', 'static linking used\n', cflags=['-L.', '-lside'])
2459+
2460+
# When using -sMAIN_MODULE we choose the dyanmic library
2461+
self.do_runf('main.c', 'dynamic linking used\n', cflags=['-sMAIN_MODULE=2', '-L.', '-lside'])
2462+
2463+
# Same for `-sFAKE_DYLIBC=0
2464+
self.do_runf('main.c', 'dynamic linking used\n', cflags=['-sFAKE_DYLIBS=0', '-L.', '-lside'])
2465+
2466+
# With can also force static linking using `-Bstatic` linker falgs
2467+
self.do_runf('main.c', 'static linking used\n', cflags=['-sMAIN_MODULE=2', '-L.', '-Bstatic', '-lside'])
2468+
24312469
def test_js_link(self):
24322470
create_file('before.js', '''
24332471
var MESSAGE = 'hello from js';
@@ -12086,7 +12124,7 @@ def test_shared_flag(self):
1208612124
self.assertContained('emcc: warning: ignoring dynamic library libother.so when generating an object file, this will need to be included explicitly in the final link', err)
1208712125
self.assertIsObjectFile('out.foo')
1208812126

12089-
# Test that adding `-sFAKE_DYIBS=0` build a real side module
12127+
# Test that adding `-sFAKE_DYLIBS=0` build a real side module
1209012128
err = self.run_process([EMCC, '-shared', '-fPIC', '-sFAKE_DYLIBS=0', test_file('hello_world.c'), '-o', 'out.foo', 'libother.so'], stderr=PIPE).stderr
1209112129
self.assertNotContained('linking a library with `-shared` will emit a static object', err)
1209212130
self.assertNotContained('emcc: warning: ignoring dynamic library libother.so when generating an object file, this will need to be included explicitly in the final link', err)

tools/link.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,19 @@ def get_dylibs(options, linker_args):
773773
This can either be via `-lfoo` or via `libfoo.so` directly.
774774
"""
775775
dylibs = []
776+
# Mimic the behavior of the native linker WRT to the `-Bstack/-Bdynamic` flags.
777+
search_for_dylibs = True
776778
for arg in linker_args:
777-
if arg.startswith('-l'):
778-
for ext in DYLIB_EXTENSIONS:
779-
path = find_library('lib' + arg[2:] + ext, options.lib_dirs)
780-
if path and building.is_wasm_dylib(path):
781-
dylibs.append(path)
779+
if arg in {'-Bstatic', '-static'}:
780+
search_for_dylibs = False
781+
elif arg == '-Bdynamic':
782+
search_for_dylibs = True
783+
elif arg.startswith('-l'):
784+
if search_for_dylibs:
785+
for ext in DYLIB_EXTENSIONS:
786+
path = find_library('lib' + arg[2:] + ext, options.lib_dirs)
787+
if path and building.is_wasm_dylib(path):
788+
dylibs.append(path)
782789
elif building.is_wasm_dylib(arg):
783790
dylibs.append(arg)
784791
return dylibs

0 commit comments

Comments
 (0)