Skip to content

Commit 051ef31

Browse files
authored
Merge pull request #4665 from juj/emscripten_strict
emscripten_strict
2 parents 6665be6 + 9b727e8 commit 051ef31

File tree

8 files changed

+378
-195
lines changed

8 files changed

+378
-195
lines changed

emcc.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ def filter_emscripten_options(argv):
268268
if compiler == shared.EMCC: compiler = [shared.PYTHON, shared.EMCC]
269269
else: compiler = [compiler]
270270
cmd = compiler + list(filter_emscripten_options(sys.argv[1:]))
271-
if not use_js: cmd += shared.EMSDK_OPTS + ['-D__EMSCRIPTEN__', '-DEMSCRIPTEN']
271+
if not use_js:
272+
cmd += shared.EMSDK_OPTS + ['-D__EMSCRIPTEN__']
273+
# The preprocessor define EMSCRIPTEN is deprecated. Don't pass it to code in strict mode. Code should use the define __EMSCRIPTEN__ instead.
274+
if not shared.Settings.STRICT:
275+
cmd += ['-DEMSCRIPTEN']
272276
if use_js: cmd += ['-s', 'ERROR_ON_UNDEFINED_SYMBOLS=1'] # configure tests should fail when an undefined symbol exists
273277

274278
logging.debug('just configuring: ' + ' '.join(cmd))
@@ -934,6 +938,29 @@ def detect_fixed_language_mode(args):
934938
if separate_asm:
935939
shared.Settings.SEPARATE_ASM = os.path.basename(asm_target)
936940

941+
if 'EMCC_STRICT' in os.environ:
942+
shared.Settings.STRICT = os.environ.get('EMCC_STRICT') != '0'
943+
944+
# Libraries are searched before settings_changes are applied, so apply the value for STRICT and ERROR_ON_MISSING_LIBRARIES from
945+
# command line already now.
946+
947+
def get_last_setting_change(setting):
948+
return ([None] + filter(lambda x: x.startswith(setting + '='), settings_changes))[-1]
949+
950+
strict_cmdline = get_last_setting_change('STRICT')
951+
if strict_cmdline:
952+
shared.Settings.STRICT = int(strict_cmdline[len('STRICT='):])
953+
954+
if shared.Settings.STRICT:
955+
shared.Settings.ERROR_ON_UNDEFINED_SYMBOLS = 1
956+
shared.Settings.ERROR_ON_MISSING_LIBRARIES = 1
957+
958+
error_on_missing_libraries_cmdline = get_last_setting_change('ERROR_ON_MISSING_LIBRARIES')
959+
if error_on_missing_libraries_cmdline:
960+
shared.Settings.ERROR_ON_MISSING_LIBRARIES = int(error_on_missing_libraries_cmdline[len('ERROR_ON_MISSING_LIBRARIES='):])
961+
962+
system_js_libraries = []
963+
937964
# Find library files
938965
for i, lib in libs:
939966
logging.debug('looking for library "%s"', lib)
@@ -950,8 +977,13 @@ def detect_fixed_language_mode(args):
950977
break
951978
if found: break
952979
if found: break
953-
if not found and lib not in ['GL', 'GLU', 'glut', 'm', 'c', 'SDL', 'stdc++', 'pthread']: # whitelist our default libraries
954-
logging.warning('emcc: cannot find library "%s"', lib)
980+
if not found:
981+
system_js_libraries += shared.Building.path_to_system_js_libraries(lib)
982+
983+
# Certain linker flags imply some link libraries to be pulled in by default.
984+
system_js_libraries += shared.Building.path_to_system_js_libraries_for_settings(settings_changes)
985+
986+
settings_changes.append('SYSTEM_JS_LIBRARIES="' + ','.join(system_js_libraries) + '"')
955987

956988
# If not compiling to JS, then we are compiling to an intermediate bitcode objects or library, so
957989
# ignore dynamic linking, since multiple dynamic linkings can interfere with each other
@@ -1015,6 +1047,15 @@ def check(input_file):
10151047
if shared.get_llvm_target() == shared.WASM_TARGET:
10161048
shared.Settings.WASM_BACKEND = 1
10171049

1050+
if not shared.Settings.STRICT:
1051+
# The preprocessor define EMSCRIPTEN is deprecated. Don't pass it to code in strict mode. Code should use the define __EMSCRIPTEN__ instead.
1052+
shared.COMPILER_OPTS += ['-DEMSCRIPTEN']
1053+
1054+
# The system include path system/include/emscripten/ is deprecated, i.e. instead of #include <emscripten.h>, one should pass in #include <emscripten/emscripten.h>.
1055+
# This path is not available in Emscripten strict mode.
1056+
if shared.USE_EMSDK:
1057+
shared.C_INCLUDE_PATHS += [shared.path_from_root('system', 'include', 'emscripten')]
1058+
10181059
# Use settings
10191060

10201061
try:

src/library_fs.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
mergeInto(LibraryManager.library, {
2-
$FS__deps: ['$ERRNO_CODES', '$ERRNO_MESSAGES', '__setErrNo', '$PATH', '$TTY', '$MEMFS', '$IDBFS', '$NODEFS', '$WORKERFS', 'stdin', 'stdout', 'stderr'],
2+
$FS__deps: ['$ERRNO_CODES', '$ERRNO_MESSAGES', '__setErrNo', '$PATH', '$TTY', '$MEMFS',
3+
#if __EMSCRIPTEN_HAS_idbfs_js__
4+
'$IDBFS',
5+
#endif
6+
#if __EMSCRIPTEN_HAS_nodefs_js__
7+
'$NODEFS',
8+
#endif
9+
#if __EMSCRIPTEN_HAS_workerfs_js__
10+
'$WORKERFS',
11+
#endif
12+
'stdin', 'stdout', 'stderr'],
313
$FS__postset: 'FS.staticInit();' +
414
'__ATINIT__.unshift(function() { if (!Module["noFSInit"] && !FS.init.initialized) FS.init() });' +
515
'__ATMAIN__.push(function() { FS.ignorePermissions = false });' +
@@ -1382,9 +1392,15 @@ mergeInto(LibraryManager.library, {
13821392

13831393
FS.filesystems = {
13841394
'MEMFS': MEMFS,
1395+
#if __EMSCRIPTEN_HAS_idbfs_js__
13851396
'IDBFS': IDBFS,
1397+
#endif
1398+
#if __EMSCRIPTEN_HAS_nodefs_js__
13861399
'NODEFS': NODEFS,
1400+
#endif
1401+
#if __EMSCRIPTEN_HAS_workerfs_js__
13871402
'WORKERFS': WORKERFS,
1403+
#endif
13881404
};
13891405
},
13901406
init: function(input, output, error) {

src/modules.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,41 +96,67 @@ var LibraryManager = {
9696
load: function() {
9797
if (this.library) return;
9898

99+
// Core system libraries (always linked against)
99100
var libraries = [
100101
'library.js',
101102
'library_browser.js',
102103
'library_formatString.js',
103104
'library_path.js',
104-
'library_syscall.js'
105+
'library_signals.js',
106+
'library_syscall.js',
107+
'library_html5.js'
105108
];
109+
106110
if (!NO_FILESYSTEM) {
111+
// Core filesystem libraries (always linked against, unless -s NO_FILESYSTEM=1 is specified)
107112
libraries = libraries.concat([
108113
'library_fs.js',
109-
'library_idbfs.js',
110114
'library_memfs.js',
111-
'library_nodefs.js',
112-
'library_sockfs.js',
113-
'library_workerfs.js',
114115
'library_tty.js',
115-
'library_lz4.js',
116116
]);
117+
118+
// Additional filesystem libraries (in strict mode, link to these explicitly via -lxxx.js)
119+
if (!STRICT) {
120+
libraries = libraries.concat([
121+
'library_idbfs.js',
122+
'library_nodefs.js',
123+
'library_sockfs.js',
124+
'library_workerfs.js',
125+
'library_lz4.js',
126+
]);
127+
}
128+
}
129+
130+
// Additional JS libraries (in strict mode, link to these explicitly via -lxxx.js)
131+
if (!STRICT) {
132+
libraries = libraries.concat([
133+
'library_sdl.js',
134+
'library_gl.js',
135+
'library_glut.js',
136+
'library_xlib.js',
137+
'library_egl.js',
138+
'library_openal.js',
139+
'library_glfw.js',
140+
'library_uuid.js',
141+
'library_glew.js',
142+
'library_idbstore.js',
143+
'library_async.js',
144+
'library_vr.js'
145+
]);
146+
}
147+
148+
// If there are any explicitly specified system JS libraries to link to, add those to link.
149+
if (SYSTEM_JS_LIBRARIES) {
150+
libraries = libraries.concat(SYSTEM_JS_LIBRARIES);
151+
}
152+
153+
libraries = libraries.concat(additionalLibraries);
154+
155+
// For each JS library library_xxx.js, add a preprocessor token __EMSCRIPTEN_HAS_xxx_js__ so that code can conditionally dead code eliminate out
156+
// if a particular feature is not being linked in.
157+
for (var i = 0; i < libraries.length; ++i) {
158+
global['__EMSCRIPTEN_HAS_' + libraries[i].replace('.', '_').replace('library_', '') + '__'] = 1
117159
}
118-
libraries = libraries.concat([
119-
'library_sdl.js',
120-
'library_gl.js',
121-
'library_glut.js',
122-
'library_xlib.js',
123-
'library_egl.js',
124-
'library_openal.js',
125-
'library_glfw.js',
126-
'library_uuid.js',
127-
'library_glew.js',
128-
'library_html5.js',
129-
'library_signals.js',
130-
'library_idbstore.js',
131-
'library_async.js',
132-
'library_vr.js'
133-
]).concat(additionalLibraries);
134160

135161
if (BOOTSTRAPPING_STRUCT_INFO) libraries = ['library_bootstrap_structInfo.js', 'library_formatString.js'];
136162
if (ONLY_MY_CODE) {

src/settings.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ var LINKABLE = 0; // If set to 1, this file can be linked with others, either as
487487
// LINKABLE of 0 is very useful in that we can reduce the size of the
488488
// generated code very significantly, by removing everything not actually used.
489489

490+
var STRICT = 0; // Emscripten 'strict' build mode: Drop supporting any deprecated build options.
491+
// Set the environment variable EMCC_STRICT=1 or pass -s STRICT=1
492+
// to test that a codebase builds nicely in forward compatible manner.
493+
490494
var WARN_ON_UNDEFINED_SYMBOLS = 1; // If set to 1, we will warn on any undefined symbols that
491495
// are not resolved by the library_*.js files. Note that
492496
// it is common in large projects to
@@ -501,6 +505,22 @@ var WARN_ON_UNDEFINED_SYMBOLS = 1; // If set to 1, we will warn on any undefined
501505
var ERROR_ON_UNDEFINED_SYMBOLS = 0; // If set to 1, we will give a compile-time error on any
502506
// undefined symbols (see WARN_ON_UNDEFINED_SYMBOLS).
503507

508+
// The default value for this is currently 0, but will be
509+
// transitioned to 1 in the future. To keep relying on
510+
// building with -s ERROR_ON_UNDEFINED_SYMBOLS=0 setting,
511+
// prefer to set that option explicitly in your build system.
512+
513+
var ERROR_ON_MISSING_LIBRARIES = 0; // If set to 1, any -lfoo directives pointing to nonexisting
514+
// library files will issue a linker error.
515+
516+
// The default value for this is currently 0, but will be
517+
// transitioned to 1 in the future. To keep relying on
518+
// building with -s ERROR_ON_MISSING_LIBRARIES=0 setting,
519+
// prefer to set that option explicitly in your build system.
520+
521+
var SYSTEM_JS_LIBRARIES = []; // Specifies a list of Emscripten-provided JS libraries to link against.
522+
// (internal, use -lfoo or -lfoo.js to link to Emscripten system JS libraries)
523+
504524
var SMALL_XHR_CHUNKS = 0; // Use small chunk size for binary synchronous XHR's in Web Workers.
505525
// Used for testing.
506526
// See test_chunked_synchronous_xhr in runner.py and library.js.

system/include/emscripten.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "emscripten/emscripten.h"

0 commit comments

Comments
 (0)