使用 emcc -O2 编译基本代码时崩溃
Posted
技术标签:
【中文标题】使用 emcc -O2 编译基本代码时崩溃【英文标题】:Crash when compiling basic code with emcc -O2 【发布时间】:2016-04-03 20:26:53 【问题描述】:我正在尝试使用 emcc -O2 编译一些基本的测试代码,但由于我不明白的原因,编译有时会失败:
D:\Projects\jsport>call emcc "../src/lib.cpp" -o emscripten_test-min.js -O2 -s EXPORTED_FUNCTIONS="['_testFct']" -s RESERVED_FUNCTION_POINTERS=1
WARNING:root:generating system asset: optimizer.exe...
Traceback (most recent call last):
File "D:\Program Files\Emscripten\emscripten\1.35.0\\emcc", line 1602, in <module>
flush_js_optimizer_queue()
File "D:\Program Files\Emscripten\emscripten\1.35.0\\emcc", line 1505, in flush_js_optimizer_queue
run_passes(chunks[0], title, just_split=False, just_concat=False)
File "D:\Program Files\Emscripten\emscripten\1.35.0\\emcc", line 1478, in run_passes
final = shared.Building.js_optimizer(final, passes, debug_level >= 4, js_optimizer_extra_info, just_split=just_split, just_concat=just_concat)
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\shared.py", line 1634, in js_optimizer
ret = js_optimizer.run(filename, passes, NODE_JS, debug, extra_info, just_split, just_concat)
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 538, in run
return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, source_map, extra_info, just_split, just_concat))
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\tempfiles.py", line 64, in run_and_clean
return func()
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 538, in <lambda>
return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, source_map, extra_info, just_split, just_concat))
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 428, in run_on_js
if not use_native(passes, source_map) or not get_native_optimizer():
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 182, in get_native_optimizer
return get_optimizer('optimizer.exe', [], ignore_build_errors)
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 166, in get_optimizer
return shared.Cache.get(name, create_optimizer_cmake, extension='exe')
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\cache.py", line 41, in get
temp = creator()
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 117, in create_optimizer_cmake
proc = subprocess.Popen(['cmake', '-G', cmake_generator, '-DCMAKE_BUILD_TYPE='+cmake_build_type, shared.path_from_root('tools', 'optimizer')], cwd=build_path, stdin=log_output, stdout=log_output, stderr=log_output)
File "D:\Program Files\Emscripten\python\2.7.5.3_64bit\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "D:\Program Files\Emscripten\python\2.7.5.3_64bit\lib\subprocess.py", line 948, in _execute_child
startupinfo)
如果我删除 O2 选项,完全相同的代码可以正常编译并且可以从 javascript 中调用。
知道如何解决这个问题吗?
我要编译的代码:
Lib.h:
#ifndef LIB_H
#define LIB_H
extern "C"
typedef void (*invoke_oncomplete_callback_t)(int, const char*, const char*);
void testFct(const void *arr_data_rgba, int arr_width, int arr_height, int job_id, invoke_oncomplete_callback_t callback);
#endif //LIB_H
Lib.cpp:
#include <sstream>
#include "lib.h"
// Internal C++ library functions
namespace impl
void testFct(const void *arr_data, int arr_width, int arr_height, int job_id, invoke_oncomplete_callback_t callback)
// Call the callback with the result
std::stringstream ss;
ss << "Inversion done in " << 0 << "s";
callback(job_id, "", ss.str().c_str());
// namespace impl
// Exposed C library function
void testFct(const void *arr_data, int arr_width, int arr_height, int job_id, invoke_oncomplete_callback_t callback)
impl::testFct(arr_data, arr_width, arr_height, job_id, callback);
EMCC_DEBUG=1 的输出:
D:\Projects\jsport>set EMCC_DEBUG=1
D:\Projects\jsport>call emcc "../src/lib.cpp" -o emscripten_test-min.js -O2 -s EXPORTED_FUNCTIONS="['_testFct']" -s RESERVED_FUNCTION_POINTERS=1
DEBUG:root:PYTHON not defined in C:\Users\MyUserName/.emscripten, using "D:\Program Files\Emscripten\python\2.7.5.3_64bit\python.exe"
WARNING:root:invocation: D:\Program Files\Emscripten\emscripten\1.35.0\\emcc ../src/lib.cpp -o emscripten_test-min.js -O2 -s EXPORTED_FUNCTIONS=['_testFct'] -s RESERVED_FUNCTION_POINTERS=1 (in D:\Projects\jsport)
INFO:root:(Emscripten: Running sanity checks)
DEBUG:root:compiling to bitcode
DEBUG:root:emcc step "parse arguments and setup" took 0.00 seconds
DEBUG:root:compiling source file: ../src/lib.cpp
DEBUG:root:running: D:\Program Files\Emscripten\clang\e1.35.0_64bit\clang++ -target asmjs-unknown-emscripten -D__EMSCRIPTEN_major__=1 -D__EMSCRIPTEN_minor__=35 -D__EMSCRIPTEN_tiny__=0 -Werror=implicit-function-declaration -nostdinc -Xclang -nobuiltininc -Xclang -nostdsysteminc -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\local\include -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\include\compat -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\include -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\include\emscripten -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\include\libc -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\lib\libc\musl\arch\emscripten -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\include\libcxx -O2 -mllvm -disable-llvm-optzns -std=c++03 ../src/lib.cpp -Xclang -isystemD:\Program Files\Emscripten\emscripten\1.35.0\system\include\SDL -emit-llvm -c -o C:\Users\MyUserName\AppData\Local\Temp\tmppmxhg5\lib_0.o
DEBUG:root:emcc step "bitcodeize inputs" took 0.61 seconds
DEBUG:root:optimizing ../src/lib.cpp
DEBUG:root:emcc: LLVM opts: -O3 -disable-loop-vectorization -disable-slp-vectorization -vectorize-loops=false -vectorize-slp=false -vectorize-slp-aggressive=false [num inputs: 1]
DEBUG:root:emcc step "process inputs" took 0.12 seconds
DEBUG:root:will generate JavaScript
DEBUG:root:including libcxx_noexcept.a
DEBUG:root:including libcxxabi.bc
DEBUG:root:including libc.bc
DEBUG:root:including dlmalloc.bc
DEBUG:root:emcc step "calculate system libraries" took 0.05 seconds
DEBUG:root:linking: ['C:\\Users\\MyUserName\\AppData\\Local\\Temp\\tmppmxhg5\\lib_0_1.o', 'C:\\Users\\MyUserName\\.emscripten_cache\\libcxxabi.bc', 'C:\\Users\\MyUserName\\.emscripten_cache\\libc.bc', 'C:\\Users\\MyUserName\\.emscripten_cache\\dlmalloc.bc', 'C:\\Users\\MyUserName\\.emscripten_cache\\libcxx_noexcept.a']
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\tmppmxhg5\lib_0_1.o to link
DEBUG:root:adding object C:\Users\MyUserName\.emscripten_cache\libcxxabi.bc to link
DEBUG:root:adding object C:\Users\MyUserName\.emscripten_cache\libc.bc to link
DEBUG:root:adding object C:\Users\MyUserName\.emscripten_cache\dlmalloc.bc to link
DEBUG:root:considering archive C:\Users\MyUserName\.emscripten_cache\libcxx_noexcept.a
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\string_fb1646b4.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\ios_1da97a69.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\locale_528bea5a.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\memory_8c4a4594.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\system_error_7d190770.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\mutex_50c49967.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\condition_variable_1b9bbc16.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\stdexcept_e242ef16.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\thread_b13652bb.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\future_fce87bc4.cpp.o to link
DEBUG:root:adding object C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmprfwi8k\exception_f3e64688.cpp.o to link
DEBUG:root:done running loop of archive C:\Users\MyUserName\.emscripten_cache\libcxx_noexcept.a
DEBUG:root:emcc step "link" took 0.81 seconds
DEBUG:root:saving intermediate processing steps to C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp
DEBUG:root:(not saving intermediate C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\emcc-0-basebc.bc because deferring linking)
DEBUG:root:emcc: LLVM opts: -strip-debug -disable-verify -internalize -internalize-public-api-list=testFct,malloc,free,__errno_location,fflush,__cxa_can_catch,__cxa_is_pointer_type -globaldce -pnacl-abi-simplify-preopt -pnacl-abi-simplify-postopt -disable-loop-vectorization -disable-slp-vectorization -vectorize-loops=false -vectorize-slp=false -vectorize-slp-aggressive=false [num inputs: 15]
DEBUG:root:emcc step "post-link" took 0.37 seconds
DEBUG:root:LLVM => JS
DEBUG:root:emscript: llvm backend: D:\Program Files\Emscripten\clang\e1.35.0_64bit\llc C:\Users\MyUserName\AppData\Local\Temp\tmppmxhg5\emscripten_test-min.bc -march=js -filetype=asm -o C:\Users\MyUserName\AppData\Local\Temp\emscripten_temp\tmp1zqxhc.4.js -emscripten-reserved-function-pointers=1 -O2
DEBUG:root: emscript: llvm backend took 0.236999988556 seconds
DEBUG:root:emscript: js compiler glue
DEBUG:root: emscript: glue took 0.319000005722 seconds
DEBUG:root:asm text sizes[[1463028, 17491], 5292, 200, 3702, 0, 5207, 23191, 1086, 268, 2736, 2080]
DEBUG:root: emscript: final python processing took 0.00999999046326 seconds
DEBUG:root:emcc step "emscript (llvm=>js)" took 0.60 seconds
DEBUG:root:wrote memory initialization to emscripten_test-min.js.mem
DEBUG:root:emcc step "source transforms" took 0.05 seconds
DEBUG:root:running js post-opts
DEBUG:root:applying js optimization passes: asm eliminate simplifyExpressions simplifyIfs registerize minifyNames asmLastOpts last minifyWhitespace cleanup
chunkification: num funcs: 707 actual num chunks: 3 chunk size range: 522935 - 450885
WARNING:root:generating system asset: optimizer.exe...
DEBUG:root:building native optimizer via CMake: optimizer.exe
Traceback (most recent call last):
File "D:\Program Files\Emscripten\emscripten\1.35.0\\emcc", line 1602, in <module>
flush_js_optimizer_queue()
File "D:\Program Files\Emscripten\emscripten\1.35.0\\emcc", line 1505, in flush_js_optimizer_queue
run_passes(chunks[0], title, just_split=False, just_concat=False)
File "D:\Program Files\Emscripten\emscripten\1.35.0\\emcc", line 1478, in run_passes
final = shared.Building.js_optimizer(final, passes, debug_level >= 4, js_optimizer_extra_info, just_split=just_split, just_concat=just_concat)
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\shared.py", line 1634, in js_optimizer
ret = js_optimizer.run(filename, passes, NODE_JS, debug, extra_info, just_split, just_concat)
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 538, in run
return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, source_map, extra_info, just_split, just_concat))
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\tempfiles.py", line 64, in run_and_clean
return func()
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 538, in <lambda>
return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, source_map, extra_info, just_split, just_concat))
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 428, in run_on_js
if not use_native(passes, source_map) or not get_native_optimizer():
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 182, in get_native_optimizer
return get_optimizer('optimizer.exe', [], ignore_build_errors)
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 166, in get_optimizer
return shared.Cache.get(name, create_optimizer_cmake, extension='exe')
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\cache.py", line 41, in get
temp = creator()
File "D:\Program Files\Emscripten\emscripten\1.35.0\tools\js_optimizer.py", line 117, in create_optimizer_cmake
proc = subprocess.Popen(['cmake', '-G', cmake_generator, '-DCMAKE_BUILD_TYPE='+cmake_build_type, shared.path_from_root('tools', 'optimizer')], cwd=build_path, stdin=log_output, stdout=log_output, stderr=log_output)
File "D:\Program Files\Emscripten\python\2.7.5.3_64bit\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "D:\Program Files\Emscripten\python\2.7.5.3_64bit\lib\subprocess.py", line 948, in _execute_child
startupinfo)
【问题讨论】:
【参考方案1】:如this post 所述,崩溃似乎是由于搜索本地编译器时出现问题所致。
设置环境变量 EMCC_NATIVE_OPTIMIZER = 0 可以解决问题,但会降低编译速度。
【讨论】:
【参考方案2】:查看 js_optimizer.py (emsdk\fastcomp\emscripten\tools\js_optimizer.py line 108) 文件,我注意到它不支持 Visual Studio 2019 CMake 生成器。 更改代码来自
if WINDOWS:
# Poor man's check for whether or not we should attempt 64 bit build
if os.environ.get('ProgramFiles(x86)'):
cmake_generators = [
'Visual Studio 15 2017 Win64',
'Visual Studio 15 2017',
'Visual Studio 14 2015 Win64',
'Visual Studio 14 2015',
'Visual Studio 12 Win64', # The year component is omitted for compatibility with older CMake.
'Visual Studio 12',
'Visual Studio 11 Win64',
'Visual Studio 11',
'MinGW Makefiles',
'Unix Makefiles',
]
else:
cmake_generators = [
'Visual Studio 15 2017',
'Visual Studio 14 2015',
'Visual Studio 12',
'Visual Studio 11',
'MinGW Makefiles',
'Unix Makefiles',
]
else:
cmake_generators = ['Unix Makefiles']
收件人:
if WINDOWS:
# Poor man's check for whether or not we should attempt 64 bit build
if os.environ.get('ProgramFiles(x86)'):
cmake_generators = [
"Visual Studio 16 2019 Win64",
"Visual Studio 16 2019",
'Visual Studio 15 2017 Win64',
'Visual Studio 15 2017',
'Visual Studio 14 2015 Win64',
'Visual Studio 14 2015',
'Visual Studio 12 Win64', # The year component is omitted for compatibility with older CMake.
'Visual Studio 12',
'Visual Studio 11 Win64',
'Visual Studio 11',
'MinGW Makefiles',
'Unix Makefiles',
]
else:
cmake_generators = [
"Visual Studio 16 2019",
'Visual Studio 15 2017',
'Visual Studio 14 2015',
'Visual Studio 12',
'Visual Studio 11',
'MinGW Makefiles',
'Unix Makefiles',
]
else:
cmake_generators = ['Unix Makefiles']
帮我解决了这个问题。
以下链接可获取一些额外信息:
查看支持的 Visual Studio 列表cmake generators
【讨论】:
以上是关于使用 emcc -O2 编译基本代码时崩溃的主要内容,如果未能解决你的问题,请参考以下文章