Skip to content

Commit

Permalink
Cleanup line_endings.py. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Nov 2, 2023
1 parent b7c778e commit 843927c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
16 changes: 5 additions & 11 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
'--bind', '--closure', '--cpuprofiler', '--embed-file',
'--emit-symbol-map', '--emrun', '--exclude-file', '--extern-post-js',
'--extern-pre-js', '--ignore-dynamic-linking', '--js-library',
'--js-transform', '--memory-init-file', '--oformat', '--output_eol',
'--js-transform', '--memory-init-file', '--oformat', '--output_eol', '--output-eol',
'--post-js', '--pre-js', '--preload-file', '--profiling-funcs',
'--proxy-to-worker', '--shell-file', '--source-map-base',
'--threadprofiler', '--use-preload-plugins'
Expand Down Expand Up @@ -3427,8 +3427,7 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
diagnostics.warning('experimental', 'The SPLIT_MODULE setting is experimental and subject to change')
do_split_module(wasm_target, options)

if not settings.SINGLE_FILE:
tools.line_endings.convert_line_endings_in_file(js_target, os.linesep, options.output_eol)
tools.line_endings.convert_line_endings_in_file(js_target, options.output_eol)

if options.executable:
make_js_executable(js_target)
Expand Down Expand Up @@ -3731,14 +3730,14 @@ def consume_arg_file():
options.default_object_extension = '.' + options.default_object_extension
elif arg.startswith('-fsanitize=cfi'):
exit_with_error('emscripten does not currently support -fsanitize=cfi')
elif check_arg('--output_eol'):
elif check_arg('--output_eol') or check_arg('--output-eol'):
style = consume_arg()
if style.lower() == 'windows':
options.output_eol = '\r\n'
elif style.lower() == 'linux':
options.output_eol = '\n'
else:
exit_with_error(f'Invalid value "{style}" to --output_eol!')
exit_with_error(f'invalid value for --output-eol: `{style}`')
# Record PTHREADS setting because it controls whether --shared-memory is passed to lld
elif arg == '-pthread':
settings.PTHREADS = 1
Expand Down Expand Up @@ -4198,12 +4197,7 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam

html_contents = do_replace(shell, '{{{ SCRIPT }}}', script.replacement())
html_contents = tools.line_endings.convert_line_endings(html_contents, '\n', options.output_eol)

try:
# Force UTF-8 output for consistency across platforms and with the web.
utils.write_binary(target, html_contents.encode('utf-8'))
except OSError as e:
exit_with_error(f'cannot write output file: {e}')
utils.write_file(target, html_contents)


def minify_html(filename):
Expand Down
14 changes: 7 additions & 7 deletions tools/line_endings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
import os
import sys

from . import utils


def convert_line_endings(text, from_eol, to_eol):
if from_eol == to_eol:
return text
return text.replace(from_eol, to_eol)


def convert_line_endings_in_file(filename, from_eol, to_eol):
if from_eol == to_eol:
def convert_line_endings_in_file(filename, to_eol):
if to_eol == os.linesep:
return # No conversion needed

with open(filename, 'rb') as f:
text = f.read()
text = convert_line_endings(text, from_eol.encode(), to_eol.encode())
with open(filename, 'wb') as f:
f.write(text)
text = utils.read_binary(filename)
text = convert_line_endings(text, os.linesep.encode(), to_eol.encode())
utils.write_binary(filename, text)


def check_line_endings(filename, expect_only=None, print_errors=True, print_info=False):
Expand Down

0 comments on commit 843927c

Please sign in to comment.