Skip to content

Commit c5baec4

Browse files
authored
Fix naming of temp object files in emcc. NFC (#25389)
The existing code was naming every object file with an `_N` suffix where N was the number files seen so far. This meant we were using suffixes for all files, not just files that were duplicates.
1 parent b29aada commit c5baec4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

emcc.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
from tools import shared, system_libs, utils, cmdline
3838
from tools import diagnostics, building, compile
39-
from tools.shared import unsuffixed, unsuffixed_basename, get_file_suffix
39+
from tools.shared import unsuffixed_basename, get_file_suffix
4040
from tools.shared import run_process, exit_with_error, DEBUG
4141
from tools.shared import in_temp
4242
from tools.shared import DYLIB_EXTENSIONS
@@ -538,15 +538,26 @@ def get_clang_command_asm():
538538
assert state.mode == Mode.COMPILE_AND_LINK
539539
assert not options.dash_c
540540
compile_args, linker_args = separate_linker_flags(newargs)
541+
542+
# Map of file basenames to how many times we've seen them. We use this to generate
543+
# unique `_NN` suffix for object files in cases when we are compiling multiple soures that
544+
# have the same basename. e.g. `foo/utils.c` and `bar/utils.c` on the same command line.
541545
seen_names = {}
542546

543547
def uniquename(name):
544548
if name not in seen_names:
545-
seen_names[name] = str(len(seen_names))
546-
return unsuffixed(name) + '_' + seen_names[name] + shared.suffix(name)
549+
# No suffix needed the firt time we see given name.
550+
seen_names[name] = 1
551+
return name
552+
553+
unique_suffix = '_%d' % seen_names[name]
554+
seen_names[name] += 1
555+
base, ext = os.path.splitext(name)
556+
return base + unique_suffix + ext
547557

548558
def get_object_filename(input_file):
549-
return in_temp(shared.replace_suffix(uniquename(input_file), '.o'))
559+
objfile = unsuffixed_basename(input_file) + '.o'
560+
return in_temp(uniquename(objfile))
550561

551562
def compile_source_file(input_file):
552563
logger.debug(f'compiling source file: {input_file}')

0 commit comments

Comments
 (0)