google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.52k stars 10.09k forks source link

[Bug]: WASM code not support clone #4441

Closed oatgnauh closed 7 months ago

oatgnauh commented 8 months ago

Describe the issue

I am tring to compile gtest source code using emscripten to get wasm code. But I get a link error: [ 20%] Built target gtest [ 30%] Linking CXX executable testgtest.js em++: warning: -pthread + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271 [-Wpthreads-mem-growth] wasm-ld: error: lib/libgtest.a(gtest-all.cc.o): undefined symbol: clone em++: error: '/media/compiler/harddisk/emsdk/upstream/bin/wasm-ld -o testgtest.wasm CMakeFiles/testgtest.dir/main.cpp.o lib/libgtest.a -L/media/compiler/harddisk/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /media/compiler/harddisk/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crtbegin.o -lGL-mt -lal -lhtml5 -lstubs-debug -lnoexit -lc-mt-debug -ldlmalloc-mt -lcompiler_rt-mt -lc++-mt-noexcept -lc++abi-debug-mt-noexcept -lsockets-mt -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined-file=/tmp/tmp8cg2r8bb.undefined --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=_emscripten_thread_init --export-if-defined=_emscripten_thread_exit --export-if-defined=_emscripten_thread_crashed --export-if-defined=_emscripten_tls_init --export-if-defined=pthread_self --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_argc_argv --export-if-defined=fflush --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=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export=emscripten_dispatch_to_thread_ --export=_emscripten_thread_free_data --export=emscripten_main_runtime_thread_id --export=emscripten_main_thread_process_queued_calls --export=_emscripten_run_in_main_runtime_thread_js --export=emscripten_stack_set_limits --export=__get_temp_ret --export=__set_temp_ret --export=malloc --export=free --export=__wasm_call_ctors --export=ntohs --export=htons --export=htonl --export=__cxa_is_pointer_type --export=__cxa_free_exception --export=__cxa_can_catch --export=setTempRet0 --export=emscripten_builtin_memalign --export-table -z stack-size=65536 --initial-memory=16777216 --no-entry --max-memory=2147483648 --stack-first' failed (returned 1) make[2]: *** [CMakeFiles/testgtest.dir/build.make:88:testgtest.js] 错误 1 make[1]: *** [CMakeFiles/Makefile2:138:CMakeFiles/testgtest.dir/all] 错误 2 make: *** [Makefile:130:all] 错误 2 emmake: error: 'make' failed (returned 2) which indicate that clone function is undefined

Since I don't need process clone, and also process clone is not support by emscripten, I believe it is a good choice to remove the related code calls clone in Web platform. Which under macro define __wasm__

Steps to reproduce the problem

here is my CMakeLists.txt.

cmake_minimum_required(VERSION 3.6)
project("testgtest")
set(CMAKE_CXX_STANDARD 17)

add_definitions(-DGTEST_OS_LINUX)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1 -s FORCE_FILESYSTEM=1 -s ALLOW_MEMORY_GROWTH=1 -s USE_PTHREADS=1 -s ENVIRONMENT=web,worker")
add_subdirectory(/media/compiler/harddisk/cpp/VESDK-ToolSet/PAT/deps/googletest gtest)

include_directories(/media/compiler/harddisk/cpp/VESDK-ToolSet/PAT/deps/googletest/include)

add_executable(
    testgtest
    ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)

target_link_libraries(
    testgtest
    gtest
    pthread
)

main.cpp

#include <gtest/gtest.h>
#include "emscripten/emscripten.h"

TEST(MathTest, AddTest) {
    int a = 2;
    int b = 3;
    int result = a + b;

    ASSERT_EQ(result, 5);
}

TEST(MathTest, MultiplyTest) {
    int a = 2;
    int b = 3;
    int result = a * b;

    ASSERT_EQ(result, 6);
}

int EMSCRIPTEN_KEEPALIVE main(int argc, char** argv) {

    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

you have to install emsdk to compile this.here I am using emsdk 3.1.34

What version of GoogleTest are you using?

Release 1.11.0

What operating system and version are you using?

Linux rubbish 5.15.0-91-generic #101~20.04.1-Ubuntu SMP Thu Nov 16 14:22:28 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

What compiler and version are you using?

emsdk 3.1.34, which including clang.

What build system are you using?

cmake version 3.16.3

Additional context

No response

oatgnauh commented 8 months ago

By the way, I fix this error by giving an empty implement of clone library-xx.js

mergeInto(LibraryManager.library, {

    clone: function(fn, stack, flags, arg, ext) {
        console.warn("not support clone on web!!")
    },

    // end of  custom
});

passing --js-library pathto/library-xx.js to CMAKE_CXX_FLAGS Since we don't need it, and never using it

derekmauro commented 8 months ago

Release 1.11.0 is pretty old. Does it work in the latest version?

derekmauro commented 7 months ago

Closing due to no response to the question about the version. Please re-open if this is still and issue.