Skip to content

Fix for collisions between output name and secondary output names #13735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,18 @@ def get_file_suffix(filename):
return ''


def get_secondary_target(target, ext):
# Depending on the output format emscripten creates zero or more secondary
# output files (e.g. the .wasm file when creating JS output, or the
# .js and the .wasm file when creating html output.
# Thus function names the secondary output files, while ensuring they
# never collide with the primary one.
base = unsuffixed(target)
if get_file_suffix(target) == ext:
base += '_'
return base + ext


def in_temp(name):
temp_dir = shared.get_emscripten_temp_dir()
return os.path.join(temp_dir, os.path.basename(name))
Expand All @@ -829,8 +841,6 @@ def in_temp(name):
# Main run() function
#
def run(args):
target = None

# Additional compiler flags that we treat as if they were passed to us on the
# commandline
EMCC_CFLAGS = os.environ.get('EMCC_CFLAGS')
Expand Down Expand Up @@ -1227,7 +1237,7 @@ def add_link_flag(i, f):
wasm_target = target
else:
# Otherwise the wasm file is produced alongside the final target.
wasm_target = unsuffixed(target) + '.wasm'
wasm_target = get_secondary_target(target, '.wasm')

# Apply user -jsD settings
for s in user_js_defines:
Expand Down Expand Up @@ -2438,7 +2448,7 @@ def post_link(options, in_wasm, wasm_target, target):
if options.oformat in (OFormat.JS, OFormat.MJS):
js_target = target
else:
js_target = unsuffixed(target) + '.js'
js_target = get_secondary_target(target, '.js')

# The JS is now final. Move it to its final location
move_file(final_js, js_target)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -10179,3 +10179,13 @@ def create_o(name, i):

self.run_process(building.get_command_with_possible_response_file([EMCC, 'main.c'] + files))
self.assertContained(str(count * (count - 1) // 2), self.run_js('a.out.js'))

def test_output_name_collision(self):
# Ensure that the seconday filenames never collide with the primary output filename
# In this case we explcitly ask for JS to be ceated in a file with the `.wasm` suffix.
# Even though this doesn't make much sense the `--oformat` flag is designed to overide
# any implict type that we might infer from the output name.
self.run_process([EMCC, '-o', 'hello.wasm', '--oformat=js', test_file('hello_world.c')])
self.assertExists('hello.wasm')
self.assertExists('hello_.wasm')
self.assertContained('hello, world!', self.run_js('hello.wasm'))