emscripten-core / emscripten-core/emscripten
Compiling with `-sENVIRONMENT='webview'` results in broken .js file
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
Compiling the [embind quick example](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html?highlight=embind#a-quick-example) with `em++ -lembind -g -sENVIRONMENT='webview' -o quick_example.js quick_example.cpp` results in code that doesn't work and instead outputs "environment detection error".
**Version of emscripten/emsdk:**
3.1.69 (also tried with 3.1.64)
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.69 (c55ff404dbe1eb9f263c48b584d5ba627224763a)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 50866e84d1da8462aeb96607bf6d9e5bbd5869c5)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/user/bin/emsdk/upstream/bin
```
**Full link command and output with `-v` appended:**
```
$ em++ -lembind -g -sENVIRONMENT='webview' -o quick_example.js quick_example.cpp -v
"/home/user/bin/emsdk/upstream/bin/clang++" -target wasm32-unknown-emscripten -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/user/bin/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -g3 -v quick_example.cpp -c -o /tmp/emscripten_temp_5kkbin70/quick_example_0.o
clang version 20.0.0git (https:/github.com/llvm/llvm-project 50866e84d1da8462aeb96607bf6d9e5bbd5869c5)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/user/bin/emsdk/upstream/bin
(in-process)
"/home/user/bin/emsdk/upstream/bin/clang-20" -cc1 -triple wasm32-unknown-emscripten -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name quick_example.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -fvisibility=hidden -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/home/user/Projects/TrashBin/emscripten-bug -v -fcoverage-compilation-dir=/home/user/Projects/TrashBin/emscripten-bug -resource-dir /home/user/bin/emsdk/upstream/lib/clang/20 -D EMSCRIPTEN -isysroot /home/user/bin/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem /home/user/bin/emsdk/upstream/lib/clang/20/include -internal-isystem /home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_5kkbin70/quick_example_0.o -x c++ quick_example.cpp
clang -cc1 version 20.0.0git based upon LLVM 20.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/compat
/home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
/home/user/bin/emsdk/upstream/lib/clang/20/include
/home/user/bin/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/home/user/bin/emsdk/upstream/bin/clang --version
/home/user/bin/emsdk/upstream/bin/wasm-ld -o quick_example.wasm -lembind-rtti -L/home/user/bin/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /tmp/emscripten_temp_5kkbin70/quick_example_0.o -lGL-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /tmp/tmp_wwfwxptlibemscripten_js_symbols.so --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=_emscripten_stack_alloc --export=__getTypeName --export=__get_temp_ret --export=__set_temp_ret --export=__wasm_call_ctors --export=_emscripten_stack_restore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=65536 --no-growable-memory --initial-heap=16777216 --no-entry --stack-first --table-base=1
/home/user/bin/emsdk/upstream/bin/llvm-objcopy quick_example.wasm quick_example.wasm --remove-section=producers
/home/user/bin/emsdk/upstream/bin/wasm-emscripten-finalize -g --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers --dwarf quick_example.wasm -o quick_example.wasm --detect-features
/home/user/bin/emsdk/node/18.20.3_64bit/bin/node /home/user/bin/emsdk/upstream/emscripten/src/compiler.mjs /tmp/tmp_b1l03sy.json
```
The resulting `quick_example.js` file sets the variables with
```javascript
var ENVIRONMENT_IS_WEB = false;
var ENVIRONMENT_IS_WORKER = false;
var ENVIRONMENT_IS_NODE = false;
var ENVIRONMENT_IS_SHELL = false;
```
and a bit further down has
```javascript
if (ENVIRONMENT_IS_SHELL) {
if ((typeof process == 'object' && typeof require === 'function') || typeof window == 'object' || typeof importScripts == 'function') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
} else
// Note that this includes Node.js workers when relevant (pthreads is enabled).
// Node.js workers are detected as a combination of ENVIRONMENT_IS_WORKER and
// ENVIRONMENT_IS_NODE.
{
throw new Error('environment detection error');
}
```
Contributor guide
Assessment
This issue has not been assessed yet.