Skip to content

Commit f8c7055

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 bdaee66 commit f8c7055

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

emcc.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,31 +3507,45 @@ def find_library(lib, lib_dirs):
35073507
return None
35083508

35093509

3510+
def find_library_shortname(lib, lib_dirs):
3511+
suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS
3512+
3513+
for suff in suffixes:
3514+
name = 'lib' + lib + suff
3515+
path = find_library(name, lib_dirs)
3516+
if path:
3517+
return path
3518+
3519+
return None
3520+
3521+
35103522
def process_libraries(libs, lib_dirs, linker_inputs):
35113523
libraries = []
35123524
consumed = []
3513-
suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS
35143525

35153526
# Find library files
35163527
for i, lib in libs:
35173528
logger.debug('looking for library "%s"', lib)
35183529

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))
3530+
js_libs, native_lib = building.map_to_js_libs(lib)
3531+
if js_libs is not None:
3532+
libraries += [(i, js_lib) for js_lib in js_libs]
3533+
consumed.append(i)
3534+
# If native_lib is returned then proceed to look that
3535+
# up using `find_library` below.
3536+
# TODO(sbc): We should be able to remove
3537+
# settings.AUTO_NATIVE_LIBRARIES part of this condition once
3538+
# we fix: https://github.com/emscripten-core/emscripten/issues/14341
3539+
if native_lib and not settings.AUTO_NATIVE_LIBRARIES:
3540+
path = find_library_shortname(native_lib, lib_dirs)
3541+
if path:
3542+
linker_inputs.append((i, path))
3543+
elif building.map_and_apply_to_settings(lib):
35283544
consumed.append(i)
35293545
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):
3546+
path = find_library_shortname(lib, lib_dirs)
3547+
if path:
3548+
linker_inputs.append((i, path))
35353549
consumed.append(i)
35363550

35373551
settings.SYSTEM_JS_LIBRARIES += libraries

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': 'gl',
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)