Skip to content

Commit c662994

Browse files
committed
Fix for -lGL + AUTO_NATIVE_LIBRARIES=0
When calling `map_to_js_libs('GL')` we were mapping this to the JS libraries, and not also including the native `libgl` library. Normally this doesn't matter since `libgl` is normally included by default but not when `AUTO_NATIVE_LIBRARIES=0` is used. This also happens to fix an old bug (#10171) where we would look on the file system for `libGL.a` before looking in `map_to_js_libs`. Fixes: #10171 Fixes: #14335
1 parent 2911fd7 commit c662994

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

emcc.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ def run(args):
10541054
if state.mode == Mode.POST_LINK_ONLY:
10551055
settings.limit_settings(None)
10561056
target, wasm_target = phase_linker_setup(options, state, newargs, settings_map)
1057-
process_libraries(state.libs, state.lib_dirs, [])
1057+
process_libraries(state, [])
10581058
if len(input_files) != 1:
10591059
exit_with_error('--post-link requires a single input file')
10601060
phase_post_link(options, state, input_files[0][1], wasm_target, target)
@@ -1117,7 +1117,7 @@ def phase_calculate_linker_inputs(options, state, linker_inputs):
11171117
state.link_flags = filter_link_flags(state.link_flags, using_lld)
11181118

11191119
# Decide what we will link
1120-
consumed = process_libraries(state.libs, state.lib_dirs, linker_inputs)
1120+
consumed = process_libraries(state, linker_inputs)
11211121
# Filter out libraries that are actually JS libs
11221122
state.link_flags = [l for l in state.link_flags if l[0] not in consumed]
11231123

@@ -3507,32 +3507,32 @@ def find_library(lib, lib_dirs):
35073507
return None
35083508

35093509

3510-
def process_libraries(libs, lib_dirs, linker_inputs):
3510+
def process_libraries(state, linker_inputs):
35113511
libraries = []
35123512
consumed = []
35133513
suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS
35143514

35153515
# Find library files
3516-
for i, lib in libs:
3516+
for i, lib in state.libs:
35173517
logger.debug('looking for library "%s"', lib)
35183518

3519-
path = None
3520-
for suff in suffixes:
3521-
name = 'lib' + lib + suff
3522-
path = find_library(name, lib_dirs)
3523-
if path:
3524-
break
3525-
3526-
if path:
3527-
linker_inputs.append((i, path))
3519+
js_libs, native_lib = building.map_to_js_libs(lib)
3520+
if js_libs is not None:
3521+
consumed.append(i)
3522+
libraries += [(i, js_lib) for js_lib in js_libs]
3523+
# If native_lib is returned then proceed to look that
3524+
# up using `find_library` below.
3525+
if native_lib:
3526+
state.forced_stdlibs.append(native_lib)
3527+
elif building.map_and_apply_to_settings(lib):
35283528
consumed.append(i)
35293529
else:
3530-
jslibs = building.map_to_js_libs(lib)
3531-
if jslibs is not None:
3532-
libraries += [(i, jslib) for jslib in jslibs]
3533-
consumed.append(i)
3534-
elif building.map_and_apply_to_settings(lib):
3535-
consumed.append(i)
3530+
for suff in suffixes:
3531+
name = 'lib' + lib + suff
3532+
path = find_library(name, state.lib_dirs)
3533+
if path:
3534+
linker_inputs.append((i, path))
3535+
consumed.append(i)
35363536

35373537
settings.SYSTEM_JS_LIBRARIES += libraries
35383538

tests/other/test_explict_gl_linking.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <EGL/egl.h>
2+
3+
int main() {
4+
return (int)eglGetProcAddress("foo");
5+
}

tests/test_other.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from runner import RunnerCore, path_from_root, is_slow_test, ensure_dir, disabled, make_executable
3636
from runner import env_modify, no_mac, no_windows, requires_native_clang, with_env_modify
3737
from runner import create_file, parameterized, NON_ZERO, node_pthreads, TEST_ROOT, test_file
38-
from runner import compiler_for, read_file, read_binary
38+
from runner import compiler_for, read_file, read_binary, EMBUILDER
3939
from tools import shared, building, utils, deps_info
4040
import jsrun
4141
import clang_native
@@ -10607,3 +10607,8 @@ def test_no_deprecated(self):
1060710607
def test_bad_export_name(self):
1060810608
err = self.expect_fail([EMCC, '-sEXPORT_NAME=foo bar', test_file('hello_world.c')])
1060910609
self.assertContained('error: EXPORT_NAME is not a valid JS identifier: `foo bar`', err)
10610+
10611+
def test_explict_gl_linking(self):
10612+
# ensure libgl exists
10613+
self.run_process([EMBUILDER, 'build', 'libgl'])
10614+
self.run_process([EMCC, test_file('other/test_explict_gl_linking.c'), '-sNO_AUTO_NATIVE_LIBRARIES', '-lGL'])

tools/building.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,6 @@ def is_wasm_dylib(filename):
13391339
def map_to_js_libs(library_name):
13401340
# Some native libraries are implemented in Emscripten as system side JS libraries
13411341
library_map = {
1342-
'c': [],
13431342
'dl': [],
13441343
'EGL': ['library_egl.js'],
13451344
'GL': ['library_webgl.js', 'library_html5_webgl.js'],
@@ -1357,20 +1356,23 @@ def map_to_js_libs(library_name):
13571356
'pthread': [],
13581357
'X11': ['library_xlib.js'],
13591358
'SDL': ['library_sdl.js'],
1360-
'stdc++': [],
13611359
'uuid': ['library_uuid.js'],
13621360
'websocket': ['library_websocket.js']
13631361
}
1362+
# And some are hybrid and require JS and native libraries to be included
1363+
native_library_map = {
1364+
'GL': 'libgl',
1365+
}
13641366

13651367
if library_name in library_map:
13661368
libs = library_map[library_name]
13671369
logger.debug('Mapping library `%s` to JS libraries: %s' % (library_name, libs))
1368-
return libs
1370+
return (libs, native_library_map.get(library_name))
13691371

13701372
if library_name.endswith('.js') and os.path.isfile(path_from_root('src', 'library_' + library_name)):
1371-
return ['library_' + library_name]
1373+
return (['library_' + library_name], None)
13721374

1373-
return None
1375+
return (None, None)
13741376

13751377

13761378
# Map a linker flag to a settings. This lets a user write -lSDL2 and it will have the same effect as

0 commit comments

Comments
 (0)