@@ -480,21 +480,36 @@ def do_emscripten(infile, memfile):
480
480
return outfile
481
481
482
482
483
+ def is_ar_file_with_missing_index (archive_file ):
484
+ # We parse the archive header outselves because llvm-nm --print-armap is slower and less
485
+ # reliable.
486
+ # See: https://github.com/emscripten-core/emscripten/issues/10195
487
+ archive_header = b'!<arch>\n '
488
+ file_header_size = 60
489
+
490
+ with open (archive_file , 'rb' ) as f :
491
+ header = f .read (len (archive_header ))
492
+ if header != archive_header :
493
+ # This is not even an ar file
494
+ return False
495
+ file_header = f .read (file_header_size )
496
+ if len (file_header ) != file_header_size :
497
+ # We don't have any file entires at all so we don't consider the index missing
498
+ return False
499
+
500
+ name = file_header [:16 ].strip ()
501
+ # If '/' is the name of the first file we have an index
502
+ return name != '/'
503
+
504
+
483
505
def ensure_archive_index (archive_file ):
484
506
# Fastcomp linking works without archive indexes.
485
507
if not shared .Settings .WASM_BACKEND or not shared .Settings .AUTO_ARCHIVE_INDEXES :
486
508
return
487
- # Ignore stderr since llvm-nm prints "no symbols" to stderr for each object that has no symbols
488
- stdout = run_process ([shared .LLVM_NM , '--print-armap' , archive_file ], stdout = PIPE , stderr = PIPE ).stdout
489
- stdout = stdout .strip ()
490
- # Ignore empty archives
491
- if not stdout :
492
- return
493
- if stdout .startswith ('Archive map\n ' ) or stdout .startswith ('Archive index\n ' ):
494
- return
495
- shared .warning ('%s: archive is missing an index; Use emar when creating libraries to ensure an index is created' , archive_file )
496
- shared .warning ('%s: adding index' , archive_file )
497
- run_process ([shared .LLVM_RANLIB , archive_file ])
509
+ if is_ar_file_with_missing_index (archive_file ):
510
+ shared .warning ('%s: archive is missing an index; Use emar when creating libraries to ensure an index is created' , archive_file )
511
+ shared .warning ('%s: adding index' , archive_file )
512
+ run_process ([shared .LLVM_RANLIB , archive_file ])
498
513
499
514
500
515
#
0 commit comments