@@ -203,15 +203,12 @@ def __init__(self, args):
203
203
self .has_dash_c = False
204
204
self .has_dash_E = False
205
205
self .has_dash_S = False
206
- self .libs = []
207
206
self .link_flags = []
208
207
self .lib_dirs = []
209
208
self .forced_stdlibs = []
210
209
211
210
212
211
def add_link_flag (state , i , f ):
213
- if f .startswith ('-l' ):
214
- state .libs .append ((i , f [2 :]))
215
212
if f .startswith ('-L' ):
216
213
state .lib_dirs .append (f [2 :])
217
214
@@ -638,7 +635,7 @@ def check(input_file):
638
635
else :
639
636
return True
640
637
641
- return [f for f in inputs if check (f [ 1 ] )]
638
+ return [f for f in inputs if check (f )]
642
639
643
640
644
641
def filter_out_duplicate_dynamic_libs (inputs ):
@@ -654,7 +651,7 @@ def check(input_file):
654
651
seen .add (abspath )
655
652
return True
656
653
657
- return [f for f in inputs if check (f [ 1 ] )]
654
+ return [f for f in inputs if check (f )]
658
655
659
656
660
657
def process_dynamic_libs (dylibs , lib_dirs ):
@@ -1053,7 +1050,7 @@ def run(args):
1053
1050
if state .mode == Mode .POST_LINK_ONLY :
1054
1051
settings .limit_settings (None )
1055
1052
target , wasm_target = phase_linker_setup (options , state , newargs , settings_map )
1056
- process_libraries (state .libs , state .lib_dirs , [])
1053
+ process_libraries (state .link_flags , state .lib_dirs , [])
1057
1054
if len (input_files ) != 1 :
1058
1055
exit_with_error ('--post-link requires a single input file' )
1059
1056
phase_post_link (options , state , input_files [0 ][1 ], wasm_target , target )
@@ -1116,24 +1113,23 @@ def phase_calculate_linker_inputs(options, state, linker_inputs):
1116
1113
state .link_flags = filter_link_flags (state .link_flags , using_lld )
1117
1114
1118
1115
# Decide what we will link
1119
- consumed = process_libraries (state .libs , state .lib_dirs , linker_inputs )
1120
- # Filter out libraries that are actually JS libs
1121
- state . link_flags = [l for l in state .link_flags if l [ 0 ] not in consumed ]
1116
+ state . link_flags = process_libraries (state .link_flags , state .lib_dirs , linker_inputs )
1117
+
1118
+ linker_args = [val for _ , val in sorted ( linker_inputs + state .link_flags ) ]
1122
1119
1123
1120
# If we are linking to an intermediate object then ignore other
1124
1121
# "fake" dynamic libraries, since otherwise we will end up with
1125
1122
# multiple copies in the final executable.
1126
1123
if options .oformat == OFormat .OBJECT or options .ignore_dynamic_linking :
1127
- linker_inputs = filter_out_dynamic_libs (options , linker_inputs )
1124
+ linker_args = filter_out_dynamic_libs (options , linker_args )
1128
1125
else :
1129
- linker_inputs = filter_out_duplicate_dynamic_libs (linker_inputs )
1126
+ linker_args = filter_out_duplicate_dynamic_libs (linker_args )
1130
1127
1131
1128
if settings .MAIN_MODULE :
1132
- dylibs = [i [ 1 ] for i in linker_inputs if building .is_wasm_dylib (i [ 1 ] )]
1129
+ dylibs = [a for a in linker_args if building .is_wasm_dylib (a )]
1133
1130
process_dynamic_libs (dylibs , state .lib_dirs )
1134
1131
1135
- linker_arguments = [val for _ , val in sorted (linker_inputs + state .link_flags )]
1136
- return linker_arguments
1132
+ return linker_args
1137
1133
1138
1134
1139
1135
@ToolchainProfiler .profile_block ('parse arguments' )
@@ -1244,7 +1240,7 @@ def phase_setup(options, state, newargs, settings_map):
1244
1240
# library and attempt to find a library by the same name in our own library path.
1245
1241
# TODO(sbc): Do we really need this feature? See test_other.py:test_local_link
1246
1242
libname = strip_prefix (unsuffixed_basename (arg ), 'lib' )
1247
- state . libs . append (( i , libname ) )
1243
+ add_link_flag ( state , i , '-l' + libname )
1248
1244
else :
1249
1245
input_files .append ((i , arg ))
1250
1246
elif arg .startswith ('-L' ):
@@ -1986,7 +1982,7 @@ def check_memory_setting(setting):
1986
1982
# When not declaring wasm module exports in outer scope one by one, disable minifying
1987
1983
# wasm module export names so that the names can be passed directly to the outer scope.
1988
1984
# Also, if using library_exports.js API, disable minification so that the feature can work.
1989
- if not settings .DECLARE_ASM_MODULE_EXPORTS or 'exports .js' in [x for _ , x in state .libs ]:
1985
+ if not settings .DECLARE_ASM_MODULE_EXPORTS or '-lexports .js' in [x for _ , x in state .link_flags ]:
1990
1986
settings .MINIFY_ASMJS_EXPORT_NAMES = 0
1991
1987
1992
1988
# Enable minification of wasm imports and exports when appropriate, if we
@@ -3506,13 +3502,26 @@ def find_library(lib, lib_dirs):
3506
3502
return None
3507
3503
3508
3504
3509
- def process_libraries (libs , lib_dirs , linker_inputs ):
3505
+ def process_libraries (link_flags , lib_dirs , linker_inputs ):
3506
+ new_flags = []
3510
3507
libraries = []
3511
- consumed = []
3512
3508
suffixes = STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS
3509
+ system_libs_map = system_libs .Library .get_usable_variations ()
3513
3510
3514
3511
# Find library files
3515
- for i , lib in libs :
3512
+ for i , flag in link_flags :
3513
+ if not flag .startswith ('-l' ):
3514
+ new_flags .append ((i , flag ))
3515
+ continue
3516
+ lib = strip_prefix (flag , '-l' )
3517
+ # We don't need to resolve system libraries to absolute paths here, we can just
3518
+ # let wasm-ld handle that. However, we do want to map to the correct variant.
3519
+ # For example we map `-lc` to `-lc-mt` if we are building with threading support.
3520
+ if 'lib' + lib in system_libs_map :
3521
+ lib = system_libs_map ['lib' + lib ]
3522
+ new_flags .append ((i , '-l' + strip_prefix (lib .get_base_name (), 'lib' )))
3523
+ continue
3524
+
3516
3525
logger .debug ('looking for library "%s"' , lib )
3517
3526
3518
3527
path = None
@@ -3524,22 +3533,20 @@ def process_libraries(libs, lib_dirs, linker_inputs):
3524
3533
3525
3534
if path :
3526
3535
linker_inputs .append ((i , path ))
3527
- consumed .append (i )
3528
- else :
3529
- jslibs = building .map_to_js_libs (lib )
3530
- if jslibs is not None :
3531
- libraries += [(i , jslib ) for jslib in jslibs ]
3532
- consumed .append (i )
3533
- elif building .map_and_apply_to_settings (lib ):
3534
- consumed .append (i )
3536
+ continue
3537
+ jslibs = building .map_to_js_libs (lib )
3538
+ if jslibs is not None :
3539
+ libraries += [(i , jslib ) for jslib in jslibs ]
3540
+ elif not building .map_and_apply_to_settings (lib ):
3541
+ new_flags .append ((i , flag ))
3535
3542
3536
3543
settings .SYSTEM_JS_LIBRARIES += libraries
3537
3544
3538
3545
# At this point processing SYSTEM_JS_LIBRARIES is finished, no more items will be added to it.
3539
3546
# Sort the input list from (order, lib_name) pairs to a flat array in the right order.
3540
3547
settings .SYSTEM_JS_LIBRARIES .sort (key = lambda lib : lib [0 ])
3541
3548
settings .SYSTEM_JS_LIBRARIES = [lib [1 ] for lib in settings .SYSTEM_JS_LIBRARIES ]
3542
- return consumed
3549
+ return new_flags
3543
3550
3544
3551
3545
3552
class ScriptSource :
0 commit comments