Skip to content

Commit a608686

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 adc2495 commit a608686

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

emcc.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,31 +3508,42 @@ def find_library(lib, lib_dirs):
35083508
return None
35093509

35103510

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

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

3520-
path = None
3521-
for suff in suffixes:
3522-
name = 'lib' + lib + suff
3523-
path = find_library(name, lib_dirs)
3524-
if path:
3525-
break
3526-
3527-
if path:
3528-
linker_inputs.append((i, path))
3531+
js_libs, native_lib = building.map_to_js_libs(lib)
3532+
if js_libs is not None:
3533+
libraries += [(i, js_lib) for js_lib in js_libs]
3534+
consumed.append(i)
3535+
# If native_lib is returned then proceed to look that
3536+
# up using `find_library` below.
3537+
if native_lib:
3538+
path = find_library_shortname(native_lib, lib_dirs)
3539+
if path:
3540+
linker_inputs.append((i, path))
3541+
elif building.map_and_apply_to_settings(lib):
35293542
consumed.append(i)
35303543
else:
3531-
jslibs = building.map_to_js_libs(lib)
3532-
if jslibs is not None:
3533-
libraries += [(i, jslib) for jslib in jslibs]
3534-
consumed.append(i)
3535-
elif building.map_and_apply_to_settings(lib):
3544+
path = find_library_shortname(lib, lib_dirs)
3545+
if path:
3546+
linker_inputs.append((i, path))
35363547
consumed.append(i)
35373548

35383549
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
@@ -10584,3 +10584,8 @@ def test_concepts(self):
1058410584
def test_link_only_setting_warning(self):
1058510585
err = self.run_process([EMCC, '-sALLOW_MEMORY_GROWTH', '-c', test_file('hello_world.c')], stderr=PIPE).stderr
1058610586
self.assertContained("warning: linker setting ignored during compilation: 'ALLOW_MEMORY_GROWTH' [-Wunused-command-line-argument]", err)
10587+
10588+
def test_explict_gl_linking(self):
10589+
# ensure libgl exists
10590+
self.run_process([EMBUILDER, 'build', 'libgl'])
10591+
self.run_process([EMCC, test_file('other/test_explict_gl_linking.c'), '-sNO_AUTO_NATIVE_LIBRARIES', '-lGL'])

tools/building.py

Lines changed: 6 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,22 @@ 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
}
13641362

13651363
if library_name in library_map:
13661364
libs = library_map[library_name]
13671365
logger.debug('Mapping library `%s` to JS libraries: %s' % (library_name, libs))
1368-
return libs
1366+
native_library_map = {
1367+
'GL': 'gl',
1368+
}
1369+
return (libs, native_library_map.get(library_name))
13691370

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

1373-
return None
1374+
return (None, None)
13741375

13751376

13761377
# 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)