Skip to content
Open
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
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ See docs/process.md for more on how version tagging works.

7.0.2 (in development)
----------------------
- Fixed a warning logging issue that printed JS compiler warnings multiple
times. (#28462)
- Removed built-in SCons support (`emscons` and `tools/scons`), which was
deprecated in 6.0.11 (#27848). Projects using SCons with Emscripten can
configure `CC`, `CXX`, `AR`, and `RANLIB` directly to use `emcc`, `em++`,
Expand Down
1 change: 1 addition & 0 deletions src/jsifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,7 @@ var proxiedFunctionTable = [
deps: symbolDeps,
asyncFuncs,
extraLibraryFuncs,
warnings: warningOccured(),
}),
);
} else {
Expand Down
17 changes: 16 additions & 1 deletion src/utility.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export function warningOccured() {
return warnings;
}

let showWarnings = true;
let warningPrefix = '';
let warningSuffix = '';

export function configureWarnings({show, prefix, suffix}) {
showWarnings = show;
warningPrefix = prefix;
warningSuffix = suffix;
}

let currentFile = [];

export function pushCurrentFile(f) {
Expand All @@ -64,7 +74,12 @@ function errorPrefix(lineNo) {

export function warn(msg, lineNo) {
warnings = true;
printErr(`warning: ${errorPrefix(lineNo)}${msg}`);
if (showWarnings) {
// Some warnings have a specific flag that controls them. (e.g. -Wfoo-bar).
// Others don't, use the generic JS warning suffix for those.
const suffix = /\[-W[\w-]+\]$/.test(msg) ? '' : warningSuffix;
printErr(`${warningPrefix}warning: ${errorPrefix(lineNo)}${msg}${suffix}`);
}
}

const seenWarnings = new Set();
Expand Down
37 changes: 28 additions & 9 deletions test/test_jslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,18 +708,37 @@ def test_jslib_search_path(self):
# Tests using the #warning directive in JS library files
def test_jslib_warnings(self):
copy_asset('warning_in_js_libraries.js')
proc = self.run_process([EMCC, test_file('hello_world.c'), '--js-library', 'warning_in_js_libraries.js'], stdout=PIPE, stderr=PIPE)
self.assertNotContained('This warning should not be present!', proc.stderr)
self.assertContained('warning: warning_in_js_libraries.js:5: #warning This is a warning string!', proc.stderr)
self.assertContained('warning: warning_in_js_libraries.js:7: #warning This is a second warning string!', proc.stderr)
self.assertContained('emcc: warning: warnings in JS library compilation [-Wjs-compiler]', proc.stderr)

err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library', 'warning_in_js_libraries.js', '-Werror'])
cmd = [EMCC, test_file('hello_world.c'), '--js-library', 'warning_in_js_libraries.js']
first = 'emcc: warning: warning_in_js_libraries.js:5: #warning This is a warning string! [-Wjs-compiler]\n'
second = 'emcc: warning: warning_in_js_libraries.js:7: #warning This is a second warning string! [-Wjs-compiler]\n'

# Each warning is reported exactly once, both on the first build and on
# later builds that can reuse cached results.
for _ in range(2):
err = self.run_process(cmd, stdout=PIPE, stderr=PIPE).stderr
self.assertNotContained('This warning should not be present!', err)
self.assertEqual(err.count(first), 1)
self.assertEqual(err.count(second), 1)
self.assertNotContained('warnings in JS library compilation', err)

# The warnings can be disabled.
err = self.run_process(cmd + ['-Wno-js-compiler'], stdout=PIPE, stderr=PIPE).stderr
self.assertNotContained('warning:', err)

# The warnings are still reported when no JS output is generated.
err = self.run_process(cmd + ['-o', 'out.wasm'], stdout=PIPE, stderr=PIPE).stderr
self.assertEqual(err.count(first), 1)
self.assertEqual(err.count(second), 1)

err = self.expect_fail(cmd + ['-Werror'])
self.assertNotContained('This warning should not be present!', err)
self.assertContained('warning: warning_in_js_libraries.js:5: #warning This is a warning string!', err)
self.assertContained('warning: warning_in_js_libraries.js:7: #warning This is a second warning string!', err)
self.assertEqual(err.count(first), 1)
self.assertEqual(err.count(second), 1)
self.assertContained('emcc: error: warnings in JS library compilation [-Wjs-compiler] [-Werror]', err)

# Disabling the warnings takes precedence over -Werror.
self.run_process(cmd + ['-Werror', '-Wno-js-compiler'])

# Tests using the #error directive in JS library files
def test_jslib_errors(self):
copy_asset('jslib/error_in_js_libraries.js')
Expand Down
5 changes: 2 additions & 3 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -14363,9 +14363,8 @@ def test_legacy_runtime(self):
def test_legacy_jslib(self, args):
# Warn about usage of legacy library function
err = self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORTED_RUNTIME_METHODS=allocateUTF8'] + args, stderr=PIPE).stderr
self.assertContained("warning: JS library symbol '$allocateUTF8' is deprecated. Please open a bug if you have a continuing need for this symbol", err)

self.assertContained('emcc: warning: warnings in JS library compilation [-Wjs-compiler]', err)
self.assertContained("emcc: warning: JS library symbol '$allocateUTF8' is deprecated. Please open a bug if you have a continuing need for this symbol [-Wdeprecated]\n", err)
self.assertNotContained('warnings in JS library compilation', err)

# In strict mode legacy library functions are not available, so we get a build time error
expected = 'undefined exported symbol: "allocateUTF8" in EXPORTED_RUNTIME_METHODS'
Expand Down
19 changes: 17 additions & 2 deletions tools/compiler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

import assert from 'node:assert';
import {parseArgs} from 'node:util';
import {applySettings, loadDefaultSettings, printErr, readFile, timer} from '../src/utility.mjs';
import {
applySettings,
configureWarnings,
loadDefaultSettings,
printErr,
readFile,
timer,
} from '../src/utility.mjs';
import '../src/parseTools_legacy.mjs';
import {runJSify} from '../src/jsifier.mjs';

Expand All @@ -20,6 +27,8 @@ loadDefaultSettings();
const options = {
help: {type: 'boolean', short: 'h'},
'symbols-only': {type: 'boolean'},
'no-warnings': {type: 'boolean'},
'tool-name': {type: 'string'},
output: {type: 'string', short: 'o'},
};
const {values, positionals} = parseArgs({options, allowPositionals: true});
Expand All @@ -30,7 +39,7 @@ Main entry point for JS compiler

If no -o file is specified then the generated code is written to stdout.

Usage: compiler.mjs <settings.json> [-o out.js] [--symbols-only]`);
Usage: compiler.mjs <settings.json> [-o out.js] [--symbols-only] [--no-warnings] [--tool-name=emcc]`);
process.exit(0);
}

Expand All @@ -50,6 +59,12 @@ timer.stop('read settings');

export const symbolsOnly = values['symbols-only'];

configureWarnings({
show: !values['no-warnings'],
prefix: values['tool-name'] ? `${values['tool-name']}: ` : '',
suffix: ' [-Wjs-compiler]',
});

// TODO(sbc): Remove EMCC_BUILD_DIR at some point. It used to be required
// back when ran the JS compiler with overridden CWD.
process.env['EMCC_BUILD_DIR'] = process.cwd();
Expand Down
5 changes: 5 additions & 0 deletions tools/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ def is_enabled(name):
return manager.warnings[name]['enabled']


def is_error(name):
warning_info = manager.warnings[name]
return warning_info['enabled'] and warning_info['error']


def warning(warning_type, message, *args):
manager.warning(warning_type, message, *args)

Expand Down
11 changes: 9 additions & 2 deletions tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def generate_js_compiler_input_hash(symbols_only=False):


@ToolchainProfiler.profile()
def compile_javascript(symbols_only=False):
def compile_javascript(symbols_only=False, suppress_warnings=False):
# Save settings to a file to work around v8 issue 1579
settings_json = json.dumps(settings.external_dict(), sort_keys=True, indent=2)
building.write_intermediate(settings_json, 'settings.json')
Expand All @@ -268,6 +268,11 @@ def compile_javascript(symbols_only=False):
args = ['-']
if symbols_only:
args += ['--symbols-only']
# The JS compiler emits warnings (via a [-Wjs-compiler] tag),
# so it needs to know the name of the tool that is running.
args += ['--tool-name', diagnostics.tool_name]
if suppress_warnings or not diagnostics.is_enabled('js-compiler'):
args += ['--no-warnings']
return shared.run_js_tool(path_from_root('dist/compiler.mjs'),
args, input=settings_json, stdout=subprocess.PIPE)

Expand Down Expand Up @@ -489,7 +494,9 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat

forwarded_json = json.loads(forwarded_data)

if forwarded_json['warnings']:
# The JS compiler has already printed each individual JS lib warning, so there is
# nothing more to report here unless -Werror=js-compiler is in effect.
if forwarded_json['warnings'] and diagnostics.is_error('js-compiler'):
diagnostics.warning('js-compiler', 'warnings in JS library compilation')

pre, post = glue.split('// EMSCRIPTEN_END_FUNCS')
Expand Down
21 changes: 19 additions & 2 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ def setup_environment_settings():
settings[key] = feature_matrix.UNSUPPORTED


def js_compiler_runs_again():
"""Return True if the JS compiler will run again, later on, to generate JS output.

That later run covers the same JS libraries as the one that generates the
list of JS symbols, and so reports the same warnings.
"""
return options.oformat != OFormat.WASM and not settings.SIDE_MODULE


def generate_js_sym_info():
"""Run the JS compiler to generate a list of all available JS symbols.

Expand All @@ -212,7 +221,9 @@ def generate_js_sym_info():
mode of the JS compiler that would generate a list of all possible symbols
that could be checked in.
"""
output = emscripten.compile_javascript(symbols_only=True)
# Suppress warnings here if they are going to be reported later on, so that
# they are not reported twice.
output = emscripten.compile_javascript(symbols_only=True, suppress_warnings=js_compiler_runs_again())
# When running in symbols_only mode compiler.mjs outputs symbol metadata as JSON.
return json.loads(output)

Expand All @@ -233,7 +244,13 @@ def generate_json():
# Limit of the overall size of the cache.
# This code will get test coverage since a full test run of `other` or `core`
# generates ~1000 unique symbol lists.
file_content = emscripten.get_cached_file('symbol_lists', f'{content_hash}.json', generate_json, cache_limit=500)
# If this is the only run of the JS compiler then this is the only time that
# its warnings get reported, so avoid caching a result that would hide them
# on subsequent builds.
def has_warnings(content):
return not js_compiler_runs_again() and json.loads(content).get('warnings')

file_content = emscripten.get_cached_file('symbol_lists', f'{content_hash}.json', generate_json, cache_limit=500, has_warnings=has_warnings)
return json.loads(file_content)


Expand Down
Loading