Skip to content

Commit a16640f

Browse files
authored
Build embind/fetch/etc as system libraries rather than compile-on-demand during linking (#8817)
1 parent 3850bd0 commit a16640f

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

emcc.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ def __init__(self):
224224
self.shell_path = shared.path_from_root('src', 'shell.html')
225225
self.source_map_base = ''
226226
self.js_libraries = []
227-
self.bind = False
228227
self.emrun = False
229228
self.cpu_profiler = False
230229
self.thread_profiler = False
@@ -1077,13 +1076,6 @@ def check(input_file):
10771076
# Note the exports the user requested
10781077
shared.Building.user_requested_exports = shared.Settings.EXPORTED_FUNCTIONS[:]
10791078

1080-
if options.bind:
1081-
shared.Settings.EMBIND = 1
1082-
# If we are using embind and generating JS, now is the time to link in bind.cpp
1083-
if final_suffix in JS_CONTAINING_ENDINGS:
1084-
input_files.append((next_arg_index, shared.path_from_root('system', 'lib', 'embind', 'bind.cpp')))
1085-
next_arg_index += 1
1086-
10871079
# -s ASSERTIONS=1 implies the heaviest stack overflow check mode. Set the implication here explicitly to avoid having to
10881080
# do preprocessor "#if defined(ASSERTIONS) || defined(STACK_OVERFLOW_CHECK)" in .js files, which is not supported.
10891081
if shared.Settings.ASSERTIONS:
@@ -1183,8 +1175,10 @@ def check(input_file):
11831175
# stb_image 2.x need to have STB_IMAGE_IMPLEMENTATION defined to include the implementation when compiling
11841176
newargs.append('-DSTB_IMAGE_IMPLEMENTATION')
11851177

1178+
forced_stdlibs = []
1179+
11861180
if shared.Settings.ASMFS and final_suffix in JS_CONTAINING_ENDINGS:
1187-
input_files.append((next_arg_index, shared.path_from_root('system', 'lib', 'fetch', 'asmfs.cpp')))
1181+
forced_stdlibs.append('libasmfs')
11881182
newargs.append('-D__EMSCRIPTEN_ASMFS__=1')
11891183
next_arg_index += 1
11901184
shared.Settings.FILESYSTEM = 0
@@ -1193,16 +1187,18 @@ def check(input_file):
11931187
options.js_libraries.append(shared.path_from_root('src', 'library_asmfs.js'))
11941188

11951189
if shared.Settings.FETCH and final_suffix in JS_CONTAINING_ENDINGS:
1196-
input_files.append((next_arg_index, shared.path_from_root('system', 'lib', 'fetch', 'emscripten_fetch.cpp')))
1190+
forced_stdlibs.append('libfetch')
11971191
next_arg_index += 1
11981192
options.js_libraries.append(shared.path_from_root('src', 'library_fetch.js'))
11991193
if shared.Settings.USE_PTHREADS:
12001194
shared.Settings.FETCH_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.fetch.js'
12011195

1202-
forced_stdlibs = []
12031196
if shared.Settings.DEMANGLE_SUPPORT:
12041197
shared.Settings.EXPORTED_FUNCTIONS += ['___cxa_demangle']
1205-
forced_stdlibs += ['libc++abi']
1198+
forced_stdlibs.append('libc++abi')
1199+
1200+
if shared.Settings.EMBIND:
1201+
forced_stdlibs.append('libembind')
12061202

12071203
if not shared.Settings.ONLY_MY_CODE and not shared.Settings.MINIMAL_RUNTIME:
12081204
# Always need malloc and free to be kept alive and exported, for internal use and other modules
@@ -2503,7 +2499,7 @@ def check_bad_eq(arg):
25032499
shared.Settings.EMIT_SYMBOL_MAP = 1
25042500
newargs[i] = ''
25052501
elif newargs[i] == '--bind':
2506-
options.bind = True
2502+
shared.Settings.EMBIND = 1
25072503
newargs[i] = ''
25082504
options.js_libraries.append(shared.path_from_root('src', 'embind', 'emval.js'))
25092505
options.js_libraries.append(shared.path_from_root('src', 'embind', 'embind.js'))

tools/system_libs.py

+38-8
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,13 @@ def get_ext(self):
341341
"""
342342
Return the appropriate file extension for this library.
343343
"""
344-
return 'a' if shared.Settings.WASM_BACKEND and shared.Settings.WASM_OBJECT_FILES else 'bc'
344+
return '.a' if shared.Settings.WASM_BACKEND and shared.Settings.WASM_OBJECT_FILES else '.bc'
345345

346346
def get_name(self):
347347
"""
348348
Return the full name of the library file, including the file extension.
349349
"""
350-
return self.get_base_name() + '.' + self.get_ext()
350+
return self.get_base_name() + self.get_ext()
351351

352352
def get_symbols(self):
353353
"""
@@ -549,7 +549,7 @@ class NoBCLibrary(Library):
549549
# libraries, only the object files, and by extension, their contained global constructors, that are actually needed
550550
# will be linked in.
551551
def get_ext(self):
552-
return 'a'
552+
return '.a'
553553

554554

555555
class libcompiler_rt(Library):
@@ -907,6 +907,34 @@ def get_default_variation(cls, **kwargs):
907907
)
908908

909909

910+
class libembind(CXXLibrary):
911+
name = 'libembind'
912+
cflags = ['-std=c++11']
913+
depends = ['libc++abi']
914+
never_force = True
915+
916+
def get_files(self):
917+
return [shared.path_from_root('system', 'lib', 'embind', 'bind.cpp')]
918+
919+
920+
class libfetch(CXXLibrary, MTLibrary):
921+
name = 'libfetch'
922+
depends = ['libc++abi']
923+
never_force = True
924+
925+
def get_files(self):
926+
return [shared.path_from_root('system', 'lib', 'fetch', 'emscripten_fetch.cpp')]
927+
928+
929+
class libasmfs(CXXLibrary, MTLibrary):
930+
name = 'libasmfs'
931+
depends = ['libc++abi']
932+
never_force = True
933+
934+
def get_files(self):
935+
return [shared.path_from_root('system', 'lib', 'fetch', 'asmfs.cpp')]
936+
937+
910938
class libhtml5(Library):
911939
name = 'libhtml5'
912940
symbols = read_symbols(shared.path_from_root('system', 'lib', 'html5.symbols'))
@@ -1162,12 +1190,14 @@ class Dummy(object):
11621190
system_libs_map = Library.get_usable_variations()
11631191
system_libs = sorted(system_libs_map.values(), key=lambda lib: lib.name)
11641192

1165-
# Setting this in the environment will avoid checking dependencies and make building big projects a little faster
1166-
# 1 means include everything; otherwise it can be the name of a lib (libc++, etc.)
1167-
# You can provide 1 to include everything, or a comma-separated list with the ones you want
1193+
# Setting this in the environment will avoid checking dependencies and make
1194+
# building big projects a little faster 1 means include everything; otherwise
1195+
# it can be the name of a lib (libc++, etc.).
1196+
# You can provide 1 to include everything, or a comma-separated list with the
1197+
# ones you want
11681198
force = os.environ.get('EMCC_FORCE_STDLIBS')
11691199
if force == '1':
1170-
force = ','.join(key for key, lib in system_libs_map.items() if not lib.never_force)
1200+
force = ','.join(name for name, lib in system_libs_map.items() if not lib.never_force)
11711201
force_include = set((force.split(',') if force else []) + forced)
11721202
if force_include:
11731203
logger.debug('forcing stdlibs: ' + str(force_include))
@@ -1186,7 +1216,7 @@ def add_library(lib):
11861216

11871217
logger.debug('including %s (%s)' % (lib.name, lib.get_name()))
11881218

1189-
need_whole_archive = lib.name in force_include and lib.get_ext() != 'bc'
1219+
need_whole_archive = lib.name in force_include and lib.get_ext() == '.a'
11901220
libs_to_link.append((lib.get_path(), need_whole_archive))
11911221

11921222
# Recursively add dependencies

0 commit comments

Comments
 (0)