Skip to content

Commit aacc0c9

Browse files
authored
Fix for collisions between output name and secondary output names (#13735)
Fixes #13729
1 parent c470514 commit aacc0c9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

emcc.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,18 @@ def get_file_suffix(filename):
817817
return ''
818818

819819

820+
def get_secondary_target(target, ext):
821+
# Depending on the output format emscripten creates zero or more secondary
822+
# output files (e.g. the .wasm file when creating JS output, or the
823+
# .js and the .wasm file when creating html output.
824+
# Thus function names the secondary output files, while ensuring they
825+
# never collide with the primary one.
826+
base = unsuffixed(target)
827+
if get_file_suffix(target) == ext:
828+
base += '_'
829+
return base + ext
830+
831+
820832
def in_temp(name):
821833
temp_dir = shared.get_emscripten_temp_dir()
822834
return os.path.join(temp_dir, os.path.basename(name))
@@ -829,8 +841,6 @@ def in_temp(name):
829841
# Main run() function
830842
#
831843
def run(args):
832-
target = None
833-
834844
# Additional compiler flags that we treat as if they were passed to us on the
835845
# commandline
836846
EMCC_CFLAGS = os.environ.get('EMCC_CFLAGS')
@@ -1227,7 +1237,7 @@ def add_link_flag(i, f):
12271237
wasm_target = target
12281238
else:
12291239
# Otherwise the wasm file is produced alongside the final target.
1230-
wasm_target = unsuffixed(target) + '.wasm'
1240+
wasm_target = get_secondary_target(target, '.wasm')
12311241

12321242
# Apply user -jsD settings
12331243
for s in user_js_defines:
@@ -2438,7 +2448,7 @@ def post_link(options, in_wasm, wasm_target, target):
24382448
if options.oformat in (OFormat.JS, OFormat.MJS):
24392449
js_target = target
24402450
else:
2441-
js_target = unsuffixed(target) + '.js'
2451+
js_target = get_secondary_target(target, '.js')
24422452

24432453
# The JS is now final. Move it to its final location
24442454
move_file(final_js, js_target)

tests/test_other.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10179,3 +10179,13 @@ def create_o(name, i):
1017910179

1018010180
self.run_process(building.get_command_with_possible_response_file([EMCC, 'main.c'] + files))
1018110181
self.assertContained(str(count * (count - 1) // 2), self.run_js('a.out.js'))
10182+
10183+
def test_output_name_collision(self):
10184+
# Ensure that the seconday filenames never collide with the primary output filename
10185+
# In this case we explcitly ask for JS to be ceated in a file with the `.wasm` suffix.
10186+
# Even though this doesn't make much sense the `--oformat` flag is designed to overide
10187+
# any implict type that we might infer from the output name.
10188+
self.run_process([EMCC, '-o', 'hello.wasm', '--oformat=js', test_file('hello_world.c')])
10189+
self.assertExists('hello.wasm')
10190+
self.assertExists('hello_.wasm')
10191+
self.assertContained('hello, world!', self.run_js('hello.wasm'))

0 commit comments

Comments
 (0)