diff --git a/emcc.py b/emcc.py index 2925985f22f6c..b519281e72e84 100644 --- a/emcc.py +++ b/emcc.py @@ -1268,6 +1268,13 @@ def consume_arg_file(): if is_int(requested_level): # the -gX value is the debug level (-g1, -g2, etc.) settings.DEBUG_LEVEL = validate_arg_level(requested_level, 4, 'invalid debug level: ' + arg) + if settings.DEBUG_LEVEL == 0: + # Set these explicitly so -g0 overrides previous -g on the cmdline + settings.GENERATE_DWARF = 0 + settings.GENERATE_SOURCE_MAP = 0 + settings.EMIT_NAME_SECTION = 0 + elif settings.DEBUG_LEVEL > 1: + settings.EMIT_NAME_SECTION = 1 # if we don't need to preserve LLVM debug info, do not keep this flag # for clang if settings.DEBUG_LEVEL < 3: @@ -1298,17 +1305,20 @@ def consume_arg_file(): settings.GENERATE_DWARF = 1 elif requested_level == 'source-map': settings.GENERATE_SOURCE_MAP = 1 + settings.EMIT_NAME_SECTION = 1 newargs[i] = '-g' else: # Other non-integer levels (e.g. -gline-tables-only or -gdwarf-5) are # usually clang flags that emit DWARF. So we pass them through to # clang and make the emscripten code treat it like any other DWARF. settings.GENERATE_DWARF = 1 + settings.EMIT_NAME_SECTION = 1 # In all cases set the emscripten debug level to 3 so that we do not # strip during link (during compile, this does not make a difference). settings.DEBUG_LEVEL = 3 elif check_flag('-profiling') or check_flag('--profiling'): settings.DEBUG_LEVEL = max(settings.DEBUG_LEVEL, 2) + settings.EMIT_NAME_SECTION = 1 elif check_flag('-profiling-funcs') or check_flag('--profiling-funcs'): settings.EMIT_NAME_SECTION = 1 elif newargs[i] == '--tracing' or newargs[i] == '--memoryprofiler': diff --git a/test/test_other.py b/test/test_other.py index 8343a3d02f0c0..dd80bac65556f 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -3095,8 +3095,7 @@ def test_dwarf_sourcemap_names(self): # TODO: It seems odd that -gsource-map leaves behind a name section. Should it? (['-gsource-map'], False, True, True), (['-g1', '-Oz', '-gsource-map'], False, True, True), - # -g0 does not override -gsource-map but does remove name section. TODO: should it? - (['-gsource-map', '-g0'], False, True, False), + (['-gsource-map', '-g0'], False, False, False), # --emit-symbol-map should not affect the results (['--emit-symbol-map', '-gsource-map'], False, True, True), (['--emit-symbol-map'], False, False, False), @@ -3105,7 +3104,7 @@ def test_dwarf_sourcemap_names(self): (['-sASYNCIFY=1', '-gsource-map'], False, True, True), (['-g', '-gsource-map'], True, True, True), (['-g2', '-gsource-map'], False, True, True), - # (['-gsplit-dwarf', '-gsource-map'], True, True, True), TODO this currently fails! + (['-gsplit-dwarf', '-gsource-map'], True, True, True), (['-gsource-map', '-sWASM_BIGINT', '-sERROR_ON_WASM_CHANGES_AFTER_LINK'], False, True, True), ]: print(flags, expect_dwarf, expect_sourcemap, expect_names) diff --git a/tools/emscripten.py b/tools/emscripten.py index c2d36561d7776..c60a3ee71c975 100644 --- a/tools/emscripten.py +++ b/tools/emscripten.py @@ -501,12 +501,14 @@ def finalize_wasm(infile, outfile, js_syms): # if we don't need to modify the wasm, don't tell finalize to emit a wasm file modify_wasm = False + need_name_section = False if settings.WASM2JS: # wasm2js requires full legalization (and will do extra wasm binary # later processing later anyhow) modify_wasm = True if settings.DEBUG_LEVEL >= 2 or settings.ASYNCIFY_ADD or settings.ASYNCIFY_ADVISE or settings.ASYNCIFY_ONLY or settings.ASYNCIFY_REMOVE or settings.EMIT_SYMBOL_MAP or settings.EMIT_NAME_SECTION: + need_name_section = True args.append('-g') if settings.WASM_BIGINT: args.append('--bigint') @@ -568,12 +570,18 @@ def finalize_wasm(infile, outfile, js_syms): infile] shared.check_call(cmd) - if not settings.GENERATE_DWARF or not settings.EMIT_PRODUCERS_SECTION: - # For sections we no longer need, strip now to speed subsequent passes + # For sections we no longer need, strip now to speed subsequent passes. + # If Binaryen is not needed, this is also our last chance to strip. + strip_sections = [] + if not settings.EMIT_PRODUCERS_SECTION: + strip_sections += ['producers'] + if not need_name_section: + strip_sections += ['name'] + + if strip_sections or not settings.GENERATE_DWARF: building.save_intermediate(outfile, 'strip.wasm') - sections = ['producers'] if not settings.EMIT_PRODUCERS_SECTION else [] building.strip(infile, outfile, debug=not settings.GENERATE_DWARF, - sections=sections) + sections=strip_sections) metadata = get_metadata(outfile, outfile, modify_wasm, args)