Skip to content

Commit ff23b79

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 cecb106 commit ff23b79

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
@@ -3504,31 +3504,45 @@ def find_library(lib, lib_dirs):
35043504
return None
35053505

35063506

3507+
def find_library_shortname(lib, lib_dirs):
3508+
suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS
3509+
3510+
for suff in suffixes:
3511+
name = 'lib' + lib + suff
3512+
path = find_library(name, lib_dirs)
3513+
if path:
3514+
return path
3515+
3516+
return None
3517+
3518+
35073519
def process_libraries(libs, lib_dirs, linker_inputs):
35083520
libraries = []
35093521
consumed = []
3510-
suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS
35113522

35123523
# Find library files
35133524
for i, lib in libs:
35143525
logger.debug('looking for library "%s"', lib)
35153526

3516-
path = None
3517-
for suff in suffixes:
3518-
name = 'lib' + lib + suff
3519-
path = find_library(name, lib_dirs)
3520-
if path:
3521-
break
3522-
3523-
if path:
3524-
linker_inputs.append((i, path))
3527+
js_libs, native_lib = building.map_to_js_libs(lib)
3528+
if js_libs is not None:
3529+
libraries += [(i, js_lib) for js_lib in js_libs]
3530+
consumed.append(i)
3531+
# If native_lib is returned then proceed to look that
3532+
# up using `find_library` below.
3533+
# TODO(sbc): We should be able to remove
3534+
# settings.AUTO_NATIVE_LIBRARIES part of this condition once
3535+
# we fix: https://github.com/emscripten-core/emscripten/issues/14341
3536+
if native_lib and not settings.AUTO_NATIVE_LIBRARIES:
3537+
path = find_library_shortname(native_lib, lib_dirs)
3538+
if path:
3539+
linker_inputs.append((i, path))
3540+
elif building.map_and_apply_to_settings(lib):
35253541
consumed.append(i)
35263542
else:
3527-
jslibs = building.map_to_js_libs(lib)
3528-
if jslibs is not None:
3529-
libraries += [(i, jslib) for jslib in jslibs]
3530-
consumed.append(i)
3531-
elif building.map_and_apply_to_settings(lib):
3543+
path = find_library_shortname(lib, lib_dirs)
3544+
if path:
3545+
linker_inputs.append((i, path))
35323546
consumed.append(i)
35333547

35343548
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
@@ -10603,3 +10603,8 @@ def test_no_deprecated(self):
1060310603
err = self.expect_fail([EMCC, '-c', '-Werror', 'test.c'])
1060410604
self.assertContained("error: 'foo' is deprecated", err)
1060510605
self.run_process([EMCC, '-c', '-Werror', '-Wno-deprecated', 'test.c'])
10606+
10607+
def test_explict_gl_linking(self):
10608+
# ensure libgl exists
10609+
self.run_process([EMBUILDER, 'build', 'libgl'])
10610+
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)