diff --git a/.github/workflows/compilation_on_android_ubuntu.yml b/.github/workflows/compilation_on_android_ubuntu.yml index 01356dc66e..acaa4d0553 100644 --- a/.github/workflows/compilation_on_android_ubuntu.yml +++ b/.github/workflows/compilation_on_android_ubuntu.yml @@ -350,7 +350,7 @@ jobs: uses: ./.github/actions/install-wasi-sdk-wabt with: os: ${{ matrix.os }} - + - name: Build wamrc run: | mkdir build && cd build @@ -361,15 +361,16 @@ jobs: - name: Install dependencies for X86_32 if: matrix.build_target == 'X86_32' run: | + sudo dpkg --add-architecture i386 sudo apt-get update - sudo apt-get install -y g++-multilib + sudo apt-get install -y g++-multilib libzstd-dev:i386 zlib1g-dev:i386 - name: Build and run unit tests run: | mkdir build && cd build cmake .. -DWAMR_BUILD_TARGET=${{ matrix.build_target }} - cmake --build . --config Release --parallel 4 - ctest + cmake --build . --parallel 4 + ctest --output-on-failure working-directory: tests/unit build_regression_tests: diff --git a/build-scripts/code_coverage.cmake b/build-scripts/code_coverage.cmake new file mode 100644 index 0000000000..e122201df0 --- /dev/null +++ b/build-scripts/code_coverage.cmake @@ -0,0 +1,37 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +function(check_ubuntu_version) +# ubuntu 2204 is the recommended environment for collecting coverage data for now +# otherwise, there will be ERRORs, when using 2404, like below and +# +# geninfo: ERROR: ('mismatch') mismatched end line for _ZN63compilation_aot_emit_memory_test_aot_check_memory_overflow_Test8TestBodyEv at /workspaces/wasm-micro-runtime/tests/unit/compilation/aot_emit_memory_test.cc:96: 96 -> 106 +# (use "geninfo --ignore-errors mismatch,mismatch ..." to suppress this warning) +# geninfo: ERROR: ('negative') Unexpected negative count '-3' for /workspaces/wasm-micro-runtime/core/iwasm/interpreter/wasm_interp_classic.c:5473. +# Perhaps you need to compile with '-fprofile-update=atomic +# (use "geninfo --ignore-errors negative,negative ..." to suppress this warning) +# +# For sure, `--ignore-errors` can be used to ignore these errors, but better to use the recommended environment. + file(READ "/etc/os-release" OS_RELEASE_CONTENT) + string(REGEX MATCH "VERSION_ID=\"([0-9]+)\\.([0-9]+)\"" _ ${OS_RELEASE_CONTENT}) + if(NOT DEFINED CMAKE_MATCH_1 OR NOT DEFINED CMAKE_MATCH_2) + message(WARNING "Unable to detect Ubuntu version. Please ensure you are using Ubuntu 22.04.") + return() + endif() + + set(UBUNTU_MAJOR_VERSION ${CMAKE_MATCH_1}) + set(UBUNTU_MINOR_VERSION ${CMAKE_MATCH_2}) + + if(NOT (UBUNTU_MAJOR_VERSION EQUAL 22 AND UBUNTU_MINOR_VERSION EQUAL 04)) + message(WARNING "Ubuntu ${UBUNTU_MAJOR_VERSION}.${UBUNTU_MINOR_VERSION} detected. Ubuntu 22.04 is recommended for collecting coverage data.") + else() + message(STATUS "Ubuntu 22.04 detected. Proceeding with coverage data collection.") + endif() +endfunction() + +check_ubuntu_version() + +# add compile options for code coverage globally +add_compile_options(--coverage -O0 -g) +link_libraries(gcov) +add_definitions (-DCOLLECT_CODE_COVERAGE) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 52c6d94afa..c087a0e673 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -640,9 +640,7 @@ if (WAMR_BUILD_GC_HEAP_VERIFY EQUAL 1) message (" GC heap verification enabled") endif () if ("$ENV{COLLECT_CODE_COVERAGE}" STREQUAL "1" OR COLLECT_CODE_COVERAGE EQUAL 1) - set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") - add_definitions (-DCOLLECT_CODE_COVERAGE) + include(${CMAKE_CURRENT_LIST_DIR}/code_coverage.cmake) message (" Collect code coverage enabled") endif () if (WAMR_BUILD_STATIC_PGO EQUAL 1) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index b726f83a12..9c641049b4 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -30,13 +30,8 @@ if(WAMR_BUILD_TARGET STREQUAL "X86_32") set(CMAKE_LIBRARY_ARCHITECTURE "i386-linux-gnu" CACHE STRING "" FORCE) endif() -# Prevent overriding the parent project's compiler/linker -# settings on Windows -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - # Fetch Google test include (FetchContent) - if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24") FetchContent_Declare ( googletest @@ -50,10 +45,11 @@ else() ) endif() +# Prevent overriding the parent project's compiler/linker +# settings on Windows +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest) -SET(GOOGLETEST_INCLUDED 1) - include(GoogleTest) enable_testing() @@ -64,12 +60,13 @@ add_subdirectory(libc-builtin) add_subdirectory(shared-utils) add_subdirectory(linear-memory-wasm) add_subdirectory(linear-memory-aot) -add_subdirectory(aot-stack-frame) add_subdirectory(linux-perf) add_subdirectory(gc) add_subdirectory(tid-allocator) if (NOT WAMR_BUILD_TARGET STREQUAL "X86_32") + add_subdirectory(aot-stack-frame) + # should enable 32-bit llvm when X86_32 add_subdirectory (aot) add_subdirectory (custom-section) diff --git a/tests/unit/README.md b/tests/unit/README.md new file mode 100644 index 0000000000..1bacbb40d5 --- /dev/null +++ b/tests/unit/README.md @@ -0,0 +1,194 @@ +# Guide to Creating a Test Suite for a New Feature in WAMR + +This guide provides instructions for contributors on how to create a test suite for a new feature in the WAMR project. Follow these steps to ensure consistency and maintainability across the test framework. + +--- + +## General Guidelines + +- **Create a New Directory**: + Always create a dedicated directory for a new feature under the `tests/unit/` directory. + + - Reuse existing test cases and patch them when possible to avoid redundancy. + - Name the directory in lowercase with words separated by hyphens (e.g., `new-feature`). + +- **Avoid Committing `.wasm` Files**: + Do not commit precompiled `.wasm` files. Instead: + + - Generate `.wasm` files from `.wat` or `.c` source files. + - Use `ExternalProject` and the `wasi-sdk` toolchain to compile `.wasm` files during the build process. + +- **Keep Using `ctest` as the framework**: + Continue to use `ctest` for running the test cases, as it is already integrated into the existing test framework. + +--- + +## Writing `CMakeLists.txt` for the Test Suite + +When creating a `CMakeLists.txt` file for your test suite, follow these best practices: + +1. **Do Not Fetch Googletest Again**: + The root `unit/CMakeLists.txt` already fetches Googletest. Avoid including or fetching it again in your test suite. + +2. **Find LLVM on Demand**: + If your test suite requires LLVM, use `find_package` to locate LLVM components as needed. Do not include LLVM globally unless required. + +3. **Include `unit_common.cmake`**: + Always include `../unit_common.cmake` in your `CMakeLists.txt` to avoid duplicating common configurations and utilities. + + Example: + + ```cmake + include("../unit_common.cmake") + ``` + +4. **Use `WAMR_RUNTIME_LIB_SOURCE`**: + Replace long lists of runtime source files with the `WAMR_RUNTIME_LIB_SOURCE` variable to simplify your configuration. + + Example: + + ```cmake + target_sources(your_test_target PRIVATE ${WAMR_RUNTIME_LIB_SOURCE}) + ``` + +5. **Avoid Global Compilation Flags**: + Do not define global compilation flags in the `unit` directory. Each test case should specify its own compilation flags based on its unique requirements. + +--- + +## Generating `.wasm` Files + +- **Compile `.wasm` Files Dynamically**: + Use `ExternalProject` in your `CMakeLists.txt` to compile `.wasm` files from `.wat` or `.c` source files. + - Use the `wasi-sdk` toolchain for `.c` or `.cc` source files. + - Example configuration: + ```cmake + ExternalProject_Add( + generate_wasm + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps + BUILD_ALWAYS YES + CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build + -DWASI_SDK_PREFIX=${WASI_SDK_DIR} + -DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN} + BUILD_COMMAND ${CMAKE_COMMAND} --build build + INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps + ) + ``` +- **Example for `wasm-apps` Directory**: + Place your source files in a `wasm-apps/` subdirectory within your test suite directory. + + - Create a `CMakeLists.txt` in `wasm-apps/` to handle the compilation of these files. + - Example `CMakeLists.txt` for `wasm-apps/`: + + ```cmake + cmake_minimum_required(VERSION 3.13) + project(wasm_apps) + + add_executable(example example.c) + set_target_properties(example PROPERTIES SUFFIX .wasm) + install(TARGETS example DESTINATION .) + ``` + +--- + +## Compiling and Running Test Cases + +To compile and run the test cases, follow these steps: + +1. **Generate Build Files**: + + ```bash + cmake -S . -B build + ``` + +2. **Build the Test Suite**: + + ```bash + cmake --build build + ``` + +3. **Run the Tests**: + + ```bash + ctest --test-dir build --output-on-failure + ``` + + This will compile and execute all test cases in the test suite, displaying detailed output for any failures. + +4. **List all Tests**: + To see all available test cases, use: + + ```bash + ctest --test-dir build -N + ``` + +5. **Run a Specific Test**: + To run a specific test case, use: + ```bash + ctest --test-dir build -R --output-on-failure + ``` + +--- + +## Collecting Code Coverage Data + +To collect code coverage data using `lcov`, follow these steps: + +1. **Build with Coverage Flags**: + Ensure the test suite is built with coverage flags enabled: + + ```bash + cmake -S . -B build -DCOLLECT_CODE_COVERAGE=1 + cmake --build build + ``` + +2. **Run the Tests**: + Execute the test cases as described above. + +3. **Generate Coverage Report**: + Use `lcov` to collect and generate the coverage report: + + ```bash + lcov --capture --directory build --output-file coverage.all.info + lcov --extract coverage.all.info "*/core/iwasm/*" "*/core/shared/*" --output-file coverage.info + genhtml coverage.info --output-directory coverage-report + ``` + +4. **View the Report**: + Open the `index.html` file in the `coverage-report` directory to view the coverage results in your browser. + +5. **Summary of Coverage**: + To get a summary of the coverage data, use: + + ```bash + lcov --summary coverage.info + ``` + +--- + +## Example Directory Structure + +Here’s an example of how your test suite directory might look: + +``` +new-feature/ +├── CMakeLists.txt +├── new_feature_test.cc +├── wasm-apps/ +| ├── CMakeLists.txt +│ ├── example.c +│ └── example.wat +``` + +--- + +## Additional Notes + +- **Testing Framework**: Use Googletest for writing unit tests. Refer to existing test cases in the `tests/unit/` directory for examples. +- **Documentation**: Add comments in your test code to explain the purpose of each test case. +- **Edge Cases**: Ensure your test suite covers edge cases and potential failure scenarios. +- **Reuse Utilities**: Leverage existing utilities in `common/` (e.g., `mock_allocator.h`, `test_helper.h`) to simplify your test code. + +--- + +By following these guidelines, you can create a well-structured and maintainable test suite that integrates seamlessly with the WAMR testing framework. diff --git a/tests/unit/aot-stack-frame/CMakeLists.txt b/tests/unit/aot-stack-frame/CMakeLists.txt index 90a61aea76..b20bb67b9a 100644 --- a/tests/unit/aot-stack-frame/CMakeLists.txt +++ b/tests/unit/aot-stack-frame/CMakeLists.txt @@ -8,7 +8,7 @@ project (test-aot-stack-frame) add_definitions (-DRUN_ON_LINUX) set (WAMR_BUILD_AOT 1) -set (WAMR_BUILD_INTERP 0) +set (WAMR_BUILD_INTERP 1) set (WAMR_BUILD_JIT 0) set (WAMR_BUILD_SIMD 1) set (WAMR_BUILD_REF_TYPES 1) @@ -21,6 +21,10 @@ set (WAMR_BUILD_GC 1) include (../unit_common.cmake) +find_package(LLVM REQUIRED CONFIG) +include_directories(${LLVM_INCLUDE_DIRS}) +add_definitions(${LLVM_DEFINITIONS}) + include_directories (${CMAKE_CURRENT_SOURCE_DIR}) add_definitions (-DWASM_ENABLE_AOT_STACK_FRAME=1) @@ -32,22 +36,10 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${LIB_HOST_AGENT_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} - ${IWASM_COMPL_SOURCE} - ${WASM_APP_LIB_SOURCE_ALL} - ) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${IWASM_COMPL_SOURCE} +) # Automatically build wasm-apps for this test add_subdirectory(wasm-apps) @@ -59,4 +51,4 @@ add_dependencies (aot_stack_frame_test aot-stack-frame-test-wasm) target_link_libraries (aot_stack_frame_test ${LLVM_AVAILABLE_LIBS} gtest_main ) -#gtest_discover_tests(aot_stack_frame_test) \ No newline at end of file +gtest_discover_tests(aot_stack_frame_test) \ No newline at end of file diff --git a/tests/unit/aot/CMakeLists.txt b/tests/unit/aot/CMakeLists.txt index bb2216bddf..e45d4c0032 100644 --- a/tests/unit/aot/CMakeLists.txt +++ b/tests/unit/aot/CMakeLists.txt @@ -13,21 +13,18 @@ add_definitions (-DWASM_ENABLE_WAMR_COMPILER=1) add_definitions (-DWASM_ENABLE_DUMP_CALL_STACK=1) add_definitions (-DWASM_ENABLE_AOT_STACK_FRAME=1) +set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_FAST_INTERP 0) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_JIT 0) set (WAMR_BUILD_LIBC_WASI 0) -set (WAMR_BUILD_APP_FRAMEWORK 1) +set (WAMR_BUILD_APP_FRAMEWORK 0) include (../unit_common.cmake) -set (LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") -if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif () -set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") include (${IWASM_DIR}/compilation/iwasm_compl.cmake) @@ -39,16 +36,9 @@ set (UNIT_SOURCE ${source_all}) set (unit_test_sources ${UNIT_SOURCE} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} ${IWASM_COMPL_SOURCE} - ) +) # Now simply link against gtest or gtest_main as needed. Eg add_executable (aot_test ${unit_test_sources}) diff --git a/tests/unit/compilation/CMakeLists.txt b/tests/unit/compilation/CMakeLists.txt index 65a9dfbdaa..f6912122d9 100644 --- a/tests/unit/compilation/CMakeLists.txt +++ b/tests/unit/compilation/CMakeLists.txt @@ -14,22 +14,20 @@ add_definitions (-DWASM_ENABLE_DUMP_CALL_STACK=1) add_definitions (-DWASM_ENABLE_AOT_STACK_FRAME=1) set (WAMR_BUILD_LIBC_WASI 0) +set (WAMR_BUILD_LIBC_BUILTIN 1) +set (WAMR_BUILD_MULTI_MODULE 1) set (WAMR_BUILD_APP_FRAMEWORK 0) set (WAMR_BUILD_THREAD_MGR 1) set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_FAST_INTERP 0) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_JIT 0) include (../unit_common.cmake) -set (LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") -if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif () -set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") include (${IWASM_DIR}/compilation/iwasm_compl.cmake) @@ -40,22 +38,11 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${LIB_HOST_AGENT_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} - ${IWASM_COMPL_SOURCE} - ${WASM_APP_LIB_SOURCE_ALL} - ) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${IWASM_COMPL_SOURCE} + ${UNCOMMON_SHARED_SOURCE} +) # Now simply link against gtest or gtest_main as needed. Eg add_executable (compilation_test ${unit_test_sources}) diff --git a/tests/unit/custom-section/CMakeLists.txt b/tests/unit/custom-section/CMakeLists.txt index 747a8b1263..b57fb8bffb 100644 --- a/tests/unit/custom-section/CMakeLists.txt +++ b/tests/unit/custom-section/CMakeLists.txt @@ -2,16 +2,31 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception cmake_minimum_required(VERSION 3.14) - project (test-custom-section) +set(WASI_SDK_DIR "/opt/wasi-sdk") +set(WASISDK_TOOLCHAIN "${WASI_SDK_DIR}/share/cmake/wasi-sdk.cmake") + +include(ExternalProject) +ExternalProject_Add( + custom-section-test-wasm-apps + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps + BUILD_ALWAYS YES + CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build + -DWASI_SDK_PREFIX=${WASI_SDK_DIR} + -DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN} + BUILD_COMMAND ${CMAKE_COMMAND} --build build + INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps +) + add_definitions (-DRUN_ON_LINUX) set (WAMR_BUILD_LIBC_WASI 0) set (WAMR_BUILD_LIBC_BUILTIN 0) -set (WAMR_BUILD_JIT 0) -set (WAMR_BUILD_LAZY_JIT 0) set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_FAST_INTERP 0) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_JIT 0) add_definitions(-DWASM_ENABLE_WAMR_COMPILER=1) add_definitions (-DWASM_ENABLE_DUMP_CALL_STACK=1) @@ -22,16 +37,9 @@ set (WAMR_BUILD_LOAD_CUSTOM_SECTION 1) include (../unit_common.cmake) -set (LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") -if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif () -set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") include (${IWASM_DIR}/compilation/iwasm_compl.cmake) @@ -42,20 +50,10 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} - ${IWASM_COMPL_SOURCE} - ${WASM_APP_LIB_SOURCE_ALL} - ) - -# Automatically build wasm-apps for this test -add_subdirectory(wasm-apps) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${IWASM_COMPL_SOURCE} +) # Now simply link against gtest or gtest_main as needed. Eg add_executable (custom_section_test ${unit_test_sources}) diff --git a/tests/unit/custom-section/wasm-apps/CMakeLists.txt b/tests/unit/custom-section/wasm-apps/CMakeLists.txt index 6455db554d..6f80d6968a 100644 --- a/tests/unit/custom-section/wasm-apps/CMakeLists.txt +++ b/tests/unit/custom-section/wasm-apps/CMakeLists.txt @@ -2,13 +2,20 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception cmake_minimum_required(VERSION 3.14) +project(wasm-apps) -project(wasm-apps-custom-section) +add_executable(app.wasm app.c) +target_compile_options(app.wasm PUBLIC -g -nostdlib) +target_link_options(app.wasm PRIVATE + -nostdlib + LINKER:--allow-undefined + LINKER:--export-all + LINKER:--no-entry +) -# Add -g option so there will be debugger related custom sections -add_custom_target(app.wasm ALL - COMMAND /opt/wasi-sdk/bin/clang -g -nostdlib - -Wl,--no-entry,--export-all - -o ${CMAKE_CURRENT_BINARY_DIR}/app.wasm - ${CMAKE_CURRENT_LIST_DIR}/app.c +# install .wasm +set( + WASM_FILES + ${CMAKE_CURRENT_BINARY_DIR}/app.wasm ) +install(FILES ${WASM_FILES} DESTINATION .) diff --git a/tests/unit/gc/CMakeLists.txt b/tests/unit/gc/CMakeLists.txt index 6d9670dfe5..c8b865a384 100644 --- a/tests/unit/gc/CMakeLists.txt +++ b/tests/unit/gc/CMakeLists.txt @@ -21,21 +21,9 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${LIB_HOST_AGENT_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} - ${IWASM_COMPL_SOURCE} - ${WASM_APP_LIB_SOURCE_ALL} + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${UNCOMMON_SHARED_SOURCE} ) add_executable (gc_test ${unit_test_sources}) @@ -48,4 +36,4 @@ add_custom_command(TARGET gc_test POST_BUILD COMMENT "Copy wasm files to directory ${CMAKE_CURRENT_BINARY_DIR}" ) -#gtest_discover_tests(gc_test) \ No newline at end of file +gtest_discover_tests(gc_test) \ No newline at end of file diff --git a/tests/unit/interpreter/CMakeLists.txt b/tests/unit/interpreter/CMakeLists.txt index f0a1d5e22a..ef65e443f7 100644 --- a/tests/unit/interpreter/CMakeLists.txt +++ b/tests/unit/interpreter/CMakeLists.txt @@ -11,9 +11,12 @@ add_definitions (-Dattr_container_malloc=malloc) add_definitions (-Dattr_container_free=free) # add_definitions (-DWASM_ENABLE_WAMR_COMPILER=1) +set(WAMR_BUILD_AOT 0) +set(WAMR_BUILD_FAST_INTERP 0) +set(WAMR_BUILD_INTERP 1) +set(WAMR_BUILD_JIT 0) set (WAMR_BUILD_LIBC_WASI 0) set (WAMR_BUILD_APP_FRAMEWORK 1) -set (WAMR_BUILD_AOT 0) include (../unit_common.cmake) @@ -24,19 +27,13 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} +) # Now simply link against gtest or gtest_main as needed. Eg add_executable (interpreter_test ${unit_test_sources}) -target_link_libraries (interpreter_test ${LLVM_AVAILABLE_LIBS} gtest_main ) +target_link_libraries (interpreter_test gtest_main ) gtest_discover_tests(interpreter_test) diff --git a/tests/unit/libc-builtin/CMakeLists.txt b/tests/unit/libc-builtin/CMakeLists.txt index f39ed44444..789018c3bc 100644 --- a/tests/unit/libc-builtin/CMakeLists.txt +++ b/tests/unit/libc-builtin/CMakeLists.txt @@ -7,10 +7,15 @@ project (test-libc-builtin) add_definitions (-DRUN_ON_LINUX) -set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - +set (WAMR_BUILD_AOT 0) +set (WAMR_BUILD_FAST_INTERP 0) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_JIT 0) +set (WAMR_BUILD_LIBC_BUILTIN 1) set (WAMR_BUILD_LIBC_WASI 0) set (WAMR_BUILD_APP_FRAMEWORK 0) +# FIXME: if removed, LibcBuiltinTest.calloc will be failed +set (WAMR_BUILD_MULTI_MODULE 1) include (../unit_common.cmake) @@ -21,9 +26,9 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} +) add_executable (libc_builtin_test ${unit_test_sources}) diff --git a/tests/unit/linear-memory-aot/CMakeLists.txt b/tests/unit/linear-memory-aot/CMakeLists.txt index 02e96c6d10..b76881f7f3 100644 --- a/tests/unit/linear-memory-aot/CMakeLists.txt +++ b/tests/unit/linear-memory-aot/CMakeLists.txt @@ -12,6 +12,7 @@ set (WAMR_BUILD_APP_FRAMEWORK 0) set (WAMR_BUILD_MEMORY_PROFILING 1) set (WAMR_BUILD_INTERP 0) set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_FAST_INTERP 0) include (../unit_common.cmake) @@ -22,21 +23,10 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${LIB_HOST_AGENT_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} - ${IWASM_COMPL_SOURCE} - ${WASM_APP_LIB_SOURCE_ALL} + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${UNCOMMON_SHARED_SOURCE} + ${IWASM_COMPL_SOURCE} ) # Test case: .aot file with hardware bound check. diff --git a/tests/unit/linear-memory-wasm/CMakeLists.txt b/tests/unit/linear-memory-wasm/CMakeLists.txt index a899b8fbe4..084d0c0942 100644 --- a/tests/unit/linear-memory-wasm/CMakeLists.txt +++ b/tests/unit/linear-memory-wasm/CMakeLists.txt @@ -25,18 +25,6 @@ set (unit_test_sources ${UNIT_SOURCE} ${WAMR_RUNTIME_LIB_SOURCE} ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${LIB_HOST_AGENT_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} - ${IWASM_COMPL_SOURCE} - ${WASM_APP_LIB_SOURCE_ALL} ) # Test case: .wasm file with hardware bound check. diff --git a/tests/unit/linux-perf/CMakeLists.txt b/tests/unit/linux-perf/CMakeLists.txt index 572a087050..e5e2d56921 100644 --- a/tests/unit/linux-perf/CMakeLists.txt +++ b/tests/unit/linux-perf/CMakeLists.txt @@ -10,8 +10,9 @@ add_definitions (-DRUN_ON_LINUX) set (WAMR_BUILD_LIBC_WASI 0) set (WAMR_BUILD_LIBC_BUILTIN 0) set (WAMR_BUILD_JIT 0) -set (WAMR_BUILD_LAZY_JIT 0) set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_FAST_INTERP 0) +set (WAMR_BUILD_INTERP 1) set (WAMR_BUILD_MULTI_MODULE 0) set (WAMR_BUILD_LINUX_PERF 1) @@ -22,22 +23,15 @@ set (WAMR_BUILD_DUMP_CALL_STACK 1) include (../unit_common.cmake) -set (LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") -if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif () -set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") include (${IWASM_DIR}/compilation/iwasm_compl.cmake) add_executable (linux_perf_test test_sort_func_ptrs.cc) target_compile_options(linux_perf_test PUBLIC -fpermissive) -target_link_libraries(linux_perf_test gtest_main ) +target_link_libraries(linux_perf_test ${LLVM_AVAILABLE_LIBS} gtest_main ) target_link_options(linux_perf_test PUBLIC LINKER:--unresolved-symbols=ignore-all diff --git a/tests/unit/memory64/CMakeLists.txt b/tests/unit/memory64/CMakeLists.txt index f3629a7c40..b19c9d9cfa 100644 --- a/tests/unit/memory64/CMakeLists.txt +++ b/tests/unit/memory64/CMakeLists.txt @@ -22,20 +22,9 @@ set(WAMR_BUILD_SHARED_MEMORY 1) # include(GoogleTest) include(../unit_common.cmake) -set(LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") - -if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message(FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif () - -set(CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") - -include(${IWASM_DIR}/compilation/iwasm_compl.cmake) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) @@ -43,14 +32,11 @@ file(GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set(UNIT_SOURCE ${source_all}) -aux_source_directory(. SRC_LIST) - set(unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${UNCOMMON_SHARED_SOURCE} +) # Now simply link against gtest or gtest_main as needed. Eg add_executable(memory64_test ${unit_test_sources}) diff --git a/tests/unit/running-modes/CMakeLists.txt b/tests/unit/running-modes/CMakeLists.txt index 5fc6a80bce..3a5ca4745a 100644 --- a/tests/unit/running-modes/CMakeLists.txt +++ b/tests/unit/running-modes/CMakeLists.txt @@ -5,52 +5,51 @@ cmake_minimum_required(VERSION 3.14) project(test-running-modes) -# Compile wasm modules -add_subdirectory(wasm-apps) +set(WASI_SDK_DIR "/opt/wasi-sdk") +set(WASISDK_TOOLCHAIN "${WASI_SDK_DIR}/share/cmake/wasi-sdk.cmake") + +include(ExternalProject) +ExternalProject_Add( + test-running-modes-wasm-apps + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps + BUILD_ALWAYS YES + CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build + -DWASI_SDK_PREFIX=${WASI_SDK_DIR} + -DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN} + BUILD_COMMAND ${CMAKE_COMMAND} --build build + INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR} +) add_definitions(-DRUN_ON_LINUX) +set(WAMR_BUILD_LIBC_BUILTIN 1) +set(WAMR_BUILD_MULTI_MODULE 0) set(WAMR_BUILD_LIBC_WASI 1) set(WAMR_BUILD_APP_FRAMEWORK 0) set(WAMR_BUILD_JIT 1) set(WAMR_BUILD_FAST_JIT 1) +set(WAMR_BUILD_REF_TYPES 1) # if only load this CMake other than load it as subdirectory include(../unit_common.cmake) -set(LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") - -if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message(FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif () - -set(CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") - -include(${IWASM_DIR}/compilation/iwasm_compl.cmake) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) -file(GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) - -set(UNIT_SOURCE ${source_all}) - -aux_source_directory(. SRC_LIST) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SRC_LIST) set(unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ) + ${WAMR_RUNTIME_LIB_SOURCE} + ${UNCOMMON_SHARED_SOURCE} + ${SRC_LIST} +) # Now simply link against gtest or gtest_main as needed. Eg add_executable(wasm_running_modes_test ${unit_test_sources}) - target_link_libraries(wasm_running_modes_test ${LLVM_AVAILABLE_LIBS} gtest_main) +add_dependencies(wasm_running_modes_test test-running-modes-wasm-apps) gtest_discover_tests(wasm_running_modes_test) diff --git a/tests/unit/running-modes/wasm-apps/CMakeLists.txt b/tests/unit/running-modes/wasm-apps/CMakeLists.txt index b2f1e9c0c5..c840169379 100644 --- a/tests/unit/running-modes/wasm-apps/CMakeLists.txt +++ b/tests/unit/running-modes/wasm-apps/CMakeLists.txt @@ -4,46 +4,30 @@ cmake_minimum_required(VERSION 3.14) project(wasm-apps) -set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) - -set(CMAKE_SYSTEM_PROCESSOR wasm32) -set(CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot) - -if (NOT DEFINED WASI_SDK_DIR) - set(WASI_SDK_DIR "/opt/wasi-sdk") -endif () - -set(CMAKE_C_FLAGS "-nostdlib -pthread -Qunused-arguments") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z stack-size=8192 -nostdlib") -set(CMAKE_C_COMPILER_TARGET "wasm32") -set(CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang") - -set(DEFINED_SYMBOLS - "${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt") - -set(CMAKE_EXE_LINKER_FLAGS - "-Wl,--no-entry \ - -Wl,--initial-memory=65536 \ - -Wl,--export-all \ - -Wl,--allow-undefined" - ) - add_executable(mytest.wasm mytest.c) -target_link_libraries(mytest.wasm) +target_compile_options(mytest.wasm PUBLIC -nostdlib) +target_link_options(mytest.wasm PRIVATE + -nostdlib + LINKER:--allow-undefined + LINKER:--export-all + LINKER:--initial-memory=131072 + LINKER:--no-entry +) add_executable(hello.wasm hello.c) -target_link_libraries(hello.wasm) - -add_custom_command(TARGET hello.wasm POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_CURRENT_BINARY_DIR}/hello.wasm - ${CMAKE_CURRENT_BINARY_DIR}/../ - COMMENT "Copy hello.wasm to the same directory of google test" - ) - -add_custom_command(TARGET mytest.wasm POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_CURRENT_BINARY_DIR}/mytest.wasm - ${CMAKE_CURRENT_BINARY_DIR}/../ - COMMENT "Copy mytest.wasm to the same directory of google test" - ) +target_compile_options(hello.wasm PUBLIC -nostdlib) +target_link_options(hello.wasm PRIVATE + -nostdlib + LINKER:--allow-undefined + LINKER:--export-all + LINKER:--initial-memory=131072 + LINKER:--no-entry +) + +# install .wasm +set( + WASM_FILES + ${CMAKE_CURRENT_BINARY_DIR}/hello.wasm + ${CMAKE_CURRENT_BINARY_DIR}/mytest.wasm +) +install(FILES ${WASM_FILES} DESTINATION .) diff --git a/tests/unit/runtime-common/CMakeLists.txt b/tests/unit/runtime-common/CMakeLists.txt index 21e5a917f2..9ef07ff50c 100644 --- a/tests/unit/runtime-common/CMakeLists.txt +++ b/tests/unit/runtime-common/CMakeLists.txt @@ -7,23 +7,20 @@ project(test-runtime-common) add_definitions(-DRUN_ON_LINUX) -set(WAMR_BUILD_LIBC_WASI 0) -set(WAMR_BUILD_APP_FRAMEWORK 0) +set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_FAST_INTERP 0) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_JIT 0) +set (WAMR_BUILD_LIBC_WASI 0) +set (WAMR_BUILD_LIBC_BUILTIN 1) +set (WAMR_BUILD_APP_FRAMEWORK 0) +set (WAMR_BUILD_MULTI_MODULE 1) include(../unit_common.cmake) -set(LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") - -if(NOT EXISTS "${LLVM_SRC_ROOT}/build") - message(FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif() - -set(CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") include_directories(${CMAKE_CURRENT_SOURCE_DIR}) @@ -31,13 +28,10 @@ file(GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set(UNIT_SOURCE ${source_all}) -aux_source_directory(. SRC_LIST) - set(unit_test_sources ${UNIT_SOURCE} ${WAMR_RUNTIME_LIB_SOURCE} ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} ) add_executable(runtime_common_test ${unit_test_sources}) @@ -45,11 +39,8 @@ add_executable(runtime_common_test ${unit_test_sources}) target_link_libraries(runtime_common_test ${LLVM_AVAILABLE_LIBS} gtest_main) # Ensure that aot compiled is completed before linear_memory_test_aot is built -set(dummy_output "${CMAKE_CURRENT_BINARY_DIR}/dummy_output") - -add_custom_command(OUTPUT ${dummy_output} +add_custom_command(OUTPUT ${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.aot COMMAND ./build_aot.sh - COMMAND ${CMAKE_COMMAND} -E touch ${dummy_output} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/build_aot.sh COMMENT "Executing script to compile aot files" @@ -58,15 +49,15 @@ add_custom_command(OUTPUT ${dummy_output} add_custom_target( BuildAot ALL - DEPENDS ${dummy_output} + DEPENDS ${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.aot ) add_dependencies(runtime_common_test BuildAot) add_custom_command(TARGET runtime_common_test POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.wasm ${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.aot - ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.wasm ${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.aot + ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Copy main.wasm and main.aot to the directory: build/runtime-common." ) diff --git a/tests/unit/runtime-common/wasm_runtime_common_test.cc b/tests/unit/runtime-common/wasm_runtime_common_test.cc index 15a02673b2..01a7b5aa49 100644 --- a/tests/unit/runtime-common/wasm_runtime_common_test.cc +++ b/tests/unit/runtime-common/wasm_runtime_common_test.cc @@ -360,39 +360,6 @@ TEST_F(wasm_runtime_common_test_suite, functions_on_wasm_module) exception_test = wasm_runtime_get_exception(wasm_module_inst); EXPECT_NE(nullptr, exception_test); - WASMFunctionInstance func_test_1; - WASMFunction wasm_func_test; - WASMType wasm_type_test; - wasm_func_test.func_type = &wasm_type_test; - func_test_1.u.func = &wasm_func_test; - func_test_1.u.func->func_type->param_count = 1; - func_test_1.u.func->func_type->param_cell_num = 2; - func_test_1.u.func->func_type->types[0] = VALUE_TYPE_I64; - func_test_1.u.func->max_stack_cell_num = 10; - EXPECT_EQ(false, wasm_runtime_call_wasm_v( - exec_env, (WASMFunctionInstanceCommon *)(&func_test_1), - 0, nullptr, 1, arguments)); - func_test_1.u.func->func_type->types[0] = VALUE_TYPE_F32; - EXPECT_EQ(false, wasm_runtime_call_wasm_v( - exec_env, (WASMFunctionInstanceCommon *)(&func_test_1), - 0, nullptr, 1, arguments)); - func_test_1.u.func->func_type->types[0] = VALUE_TYPE_F64; - EXPECT_EQ(false, wasm_runtime_call_wasm_v( - exec_env, (WASMFunctionInstanceCommon *)(&func_test_1), - 0, nullptr, 1, arguments)); - -#if 0 - WASMFunctionInstance func_test; - WASMFunctionImport func_import_test; - WASMType *func_type_1 = nullptr; - func_import_test.func_type = func_type; - func_test.u.func_import = &func_import_test; - func_test.is_import_func = true; - func_type_1 = wasm_runtime_get_function_type(&func_test, - wasm_module_inst->module_type); - EXPECT_NE(func_type_1, nullptr); -#endif - EXPECT_EQ(true, wasm_runtime_create_exec_env_singleton(wasm_module_inst)); EXPECT_NE(nullptr, wasm_runtime_get_exec_env_singleton(wasm_module_inst)); diff --git a/tests/unit/shared-heap/CMakeLists.txt b/tests/unit/shared-heap/CMakeLists.txt index fa7067918a..e1e7a1a9a3 100644 --- a/tests/unit/shared-heap/CMakeLists.txt +++ b/tests/unit/shared-heap/CMakeLists.txt @@ -29,19 +29,6 @@ endif () # if only load this CMake other than load it as subdirectory include(../unit_common.cmake) -set(LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") - -if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message(FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") -endif () - -set(CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") -find_package(LLVM REQUIRED CONFIG) -include_directories(${LLVM_INCLUDE_DIRS}) -add_definitions(${LLVM_DEFINITIONS}) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") - include(${IWASM_DIR}/compilation/iwasm_compl.cmake) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) @@ -50,18 +37,15 @@ file(GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set(UNIT_SOURCE ${source_all}) -aux_source_directory(. SRC_LIST) - set(unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} - ${UNCOMMON_SHARED_SOURCE} - ${SRC_LIST} - ) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${UNCOMMON_SHARED_SOURCE} +) # Now simply link against gtest or gtest_main as needed. Eg add_executable(shared_heap_test ${unit_test_sources}) -target_link_libraries(shared_heap_test ${LLVM_AVAILABLE_LIBS} gtest_main) +target_link_libraries(shared_heap_test gtest_main) gtest_discover_tests(shared_heap_test) diff --git a/tests/unit/shared-utils/CMakeLists.txt b/tests/unit/shared-utils/CMakeLists.txt index 47b6b835ba..c6f250e6df 100644 --- a/tests/unit/shared-utils/CMakeLists.txt +++ b/tests/unit/shared-utils/CMakeLists.txt @@ -7,6 +7,10 @@ project (test-shared-utils) add_definitions (-DRUN_ON_LINUX) +set (WAMR_BUILD_AOT 0) +set (WAMR_BUILD_FAST_INTERP 0) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_JIT 0) set (WAMR_BUILD_LIBC_WASI 0) set (WAMR_BUILD_APP_FRAMEWORK 0) @@ -19,8 +23,8 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${WAMR_RUNTIME_LIB_SOURCE} + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} ) add_executable (shared_utils_test ${unit_test_sources}) diff --git a/tests/unit/unit_common.cmake b/tests/unit/unit_common.cmake index 66c50b7e8c..53e9ae50df 100644 --- a/tests/unit/unit_common.cmake +++ b/tests/unit/unit_common.cmake @@ -1,26 +1,14 @@ # Copyright (C) 2019 Intel Corporation. All rights reserved. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -# Yes. To solve the compatibility issue with CMAKE (>= 4.0), we need to update -# our `cmake_minimum_required()` to 3.5. However, there are CMakeLists.txt -# from 3rd parties that we should not alter. Therefore, in addition to -# changing the `cmake_minimum_required()`, we should also add a configuration -# here that is compatible with earlier versions. -set(CMAKE_POLICY_VERSION_MINIMUM 3.5 FORCE) - if (NOT DEFINED WAMR_BUILD_PLATFORM) set (WAMR_BUILD_PLATFORM "linux") endif () -set (UNIT_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}) - -include_directories(${UNIT_ROOT_DIR}) - enable_language (ASM) -# Reset default linker flags -set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") -set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") +# Usually, test cases should identify their unique +# complation flags to implement their test plan # Set WAMR_BUILD_TARGET, currently values supported: # "X86_64", "AMD_64", "X86_32", "ARM_32", "MIPS_32", "XTENSA_32" @@ -34,52 +22,6 @@ if (NOT DEFINED WAMR_BUILD_TARGET) endif () endif () -if (NOT CMAKE_BUILD_TYPE) - set (CMAKE_BUILD_TYPE Debug) -endif () - -if (NOT DEFINED WAMR_BUILD_INTERP) - # Enable Interpreter by default - set (WAMR_BUILD_INTERP 1) -endif () - -if (NOT DEFINED WAMR_BUILD_AOT) - # Enable AOT by default. - set (WAMR_BUILD_AOT 1) -endif () - -if (NOT DEFINED WAMR_BUILD_JIT) - # Disable JIT by default. - set (WAMR_BUILD_JIT 0) -endif () - -if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN) - # Enable libc builtin support by default - set (WAMR_BUILD_LIBC_BUILTIN 1) -endif () - -if (NOT DEFINED WAMR_BUILD_LIBC_WASI) - # Enable libc wasi support by default - set (WAMR_BUILD_LIBC_WASI 1) -endif () - -if (NOT DEFINED WAMR_BUILD_MULTI_MODULE) - set (WAMR_BUILD_MULTI_MODULE 1) -endif() - -if (NOT DEFINED WAMR_BUILD_APP_FRAMEWORK) - set (WAMR_BUILD_APP_FRAMEWORK 1) -endif () - -if (COLLECT_CODE_COVERAGE EQUAL 1) - set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") -endif () - -set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") -set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -ffunction-sections -fdata-sections \ - -Wall -Wno-unused-parameter -Wno-pedantic") - set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) # include the build config template file @@ -90,23 +32,20 @@ include_directories (${SHARED_DIR}/include include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) -if (NOT (GOOGLETEST_INCLUDED EQUAL 1)) -# Prevent overriding the parent project's compiler/linker -# settings on Windows -set (gtest_force_shared_crt ON CACHE BOOL "" FORCE) - -# Fetch Google test -include (FetchContent) -FetchContent_Declare ( - googletest - URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip -) -FetchContent_MakeAvailable (googletest) - -endif() - # Add helper classes include_directories(${CMAKE_CURRENT_LIST_DIR}/common) -message ("unit_common.cmake included") +# config_common.cmake only sets up the llvm environment when +# JIT is enabled. but in unit tests, we need llvm environment +# for aot compilation. +if (NOT DEFINED LLVM_DIR) + set (LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") + set (LLVM_BUILD_ROOT "${LLVM_SRC_ROOT}/build") + if (NOT EXISTS "${LLVM_BUILD_ROOT}") + message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_BUILD_ROOT}") + endif () + set (CMAKE_PREFIX_PATH "${LLVM_BUILD_ROOT};${CMAKE_PREFIX_PATH}") + set (LLVM_DIR ${LLVM_BUILD_ROOT}/lib/cmake/llvm) +endif () +message(STATUS "unit_common.cmake included") diff --git a/tests/unit/wasm-c-api/CMakeLists.txt b/tests/unit/wasm-c-api/CMakeLists.txt index 95cbfbd544..3150c93254 100644 --- a/tests/unit/wasm-c-api/CMakeLists.txt +++ b/tests/unit/wasm-c-api/CMakeLists.txt @@ -4,14 +4,8 @@ cmake_minimum_required (VERSION 3.14) project (wasm_c_api_test) -################ runtime settings ################ -set(CMAKE_BUILD_TYPE Debug) set(WAMR_BUILD_PLATFORM "linux") -# Resetdefault linker flags -set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") -set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") - # WAMR features switch if (NOT DEFINED WAMR_BUILD_TARGET) set(WAMR_BUILD_TARGET "X86_64") @@ -23,49 +17,8 @@ set(WAMR_BUILD_LIBC_BUILTIN 1) set(WAMR_BUILD_LIBC_WASI 0) set(WAMR_BUILD_FAST_INTERP 0) -# compiling and linking flags -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE") - -# build out vmlib -# hard code path here -set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..) -include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) - -add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE}) -################################################ - -################ unit test related ################ - -# Yes. To solve the compatibility issue with CMAKE (>= 4.0), we need to update -# our `cmake_minimum_required()` to 3.5. However, there are CMakeLists.txt -# from 3rd parties that we should not alter. Therefore, in addition to -# changing the `cmake_minimum_required()`, we should also add a configuration -# here that is compatible with earlier versions. -set(CMAKE_POLICY_VERSION_MINIMUM 3.5 FORCE) - -# Add googletest directly to our build. This defines -# the gtest and gtest_main targets. - -if (NOT (GOOGLETEST_INCLUDED EQUAL 1)) -# Prevent overriding the parent project's compiler/linker -# settings on Windows -set (gtest_force_shared_crt ON CACHE BOOL "" FORCE) - -# Fetch Google test -include (FetchContent) -FetchContent_Declare ( - googletest - URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip -) -FetchContent_MakeAvailable (googletest) -endif() - -enable_testing() - -add_executable(wasm_c_api_test - basic.cc -) - -target_link_libraries(wasm_c_api_test vmlib gtest_main) +include (../unit_common.cmake) +add_executable(wasm_c_api_test basic.cc ${WAMR_RUNTIME_LIB_SOURCE}) +target_link_libraries(wasm_c_api_test gtest_main) gtest_discover_tests(wasm_c_api_test) diff --git a/tests/unit/wasm-vm/CMakeLists.txt b/tests/unit/wasm-vm/CMakeLists.txt index 6242a48b99..431b945450 100644 --- a/tests/unit/wasm-vm/CMakeLists.txt +++ b/tests/unit/wasm-vm/CMakeLists.txt @@ -7,14 +7,21 @@ project (test-wasm-vm) add_definitions (-DRUN_ON_LINUX) +set(WAMR_BUILD_AOT 0) +set(WAMR_BUILD_FAST_INTERP 0) +set(WAMR_BUILD_INTERP 1) +set(WAMR_BUILD_JIT 0) +set(WAMR_BUILD_LIBC_BUILTIN 1) + add_definitions (-Dattr_container_malloc=malloc) add_definitions (-Dattr_container_free=free) -set (WAMR_BUILD_APP_FRAMEWORK 1) -set (CMAKE_BUILD_TYPE Release) - include (../unit_common.cmake) +find_package(LLVM REQUIRED CONFIG) +include_directories(${LLVM_INCLUDE_DIRS}) +add_definitions(${LLVM_DEFINITIONS}) + include_directories (${CMAKE_CURRENT_SOURCE_DIR}) file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) @@ -22,21 +29,10 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) set (UNIT_SOURCE ${source_all}) set (unit_test_sources - ${UNIT_SOURCE} - ${PLATFORM_SHARED_SOURCE} - ${UTILS_SHARED_SOURCE} - ${MEM_ALLOC_SHARED_SOURCE} - ${LIB_HOST_AGENT_SOURCE} - ${NATIVE_INTERFACE_SOURCE} - ${LIBC_BUILTIN_SOURCE} - ${LIBC_WASI_SOURCE} - ${IWASM_COMMON_SOURCE} - ${IWASM_INTERP_SOURCE} - ${IWASM_AOT_SOURCE} - ${IWASM_COMPL_SOURCE} - ${WASM_APP_LIB_SOURCE_ALL} - ${UNCOMMON_SHARED_SOURCE} - ) + ${UNIT_SOURCE} + ${WAMR_RUNTIME_LIB_SOURCE} + ${UNCOMMON_SHARED_SOURCE} +) # Now simply link against gtest or gtest_main as needed. Eg add_executable(wasm_vm_test ${unit_test_sources}) diff --git a/tests/wamr-test-suites/spec-test-script/collect_coverage.sh b/tests/wamr-test-suites/spec-test-script/collect_coverage.sh index 0091e76496..12bee733a4 100755 --- a/tests/wamr-test-suites/spec-test-script/collect_coverage.sh +++ b/tests/wamr-test-suites/spec-test-script/collect_coverage.sh @@ -28,10 +28,12 @@ echo "Start to collect code coverage of ${SRC_COV_DIR} .." pushd ${SRC_COV_DIR} > /dev/null 2>&1 # collect all code coverage data -lcov -q -o ${SRC_TEMP_COV_FILE} -c -d . --rc lcov_branch_coverage=1 +# for lcov 2.x: ignore-errors mismatch,negative +lcov -q -o ${SRC_TEMP_COV_FILE} -c -d . --rc lcov_branch_coverage=1 --rc geninfo_unexecuted_blocks=1 # extract code coverage data of WAMR source files +# for lcov 2.x: ignore-errors unused lcov -q -r ${SRC_TEMP_COV_FILE} -o ${SRC_TEMP_COV_FILE} \ - -rc lcov_branch_coverage=1 \ + -rc lcov_branch_coverage=1\ "*/usr/*" "*/_deps/*" "*/deps/*" "*/tests/unit/*" \ "*/llvm/include/*" "*/include/llvm/*" "*/samples/*" \ "*/test-tools/*" "*/tests/standalone/*" "*/tests/*" diff --git a/tests/wamr-test-suites/test_wamr.sh b/tests/wamr-test-suites/test_wamr.sh index 1edf363bcb..974db1d2c1 100755 --- a/tests/wamr-test-suites/test_wamr.sh +++ b/tests/wamr-test-suites/test_wamr.sh @@ -39,8 +39,8 @@ function help() echo "-F set the firmware path used by qemu" echo "-C enable code coverage collect" echo "-j set the platform to test" - echo "-T set the sanitizer(s) used during testing. It can be either a comma-separated list - (e.g., ubsan, asan) or a single option + echo "-T set the sanitizer(s) used during testing. It can be either a comma-separated list + (e.g., ubsan, asan) or a single option (e.g., ubsan, tsan, asan, posan)." echo "-A use the specified wamrc command instead of building it" echo "-N enable extended const expression feature" @@ -329,14 +329,14 @@ function unit_test() echo "Now start unit tests" cd ${WORK_DIR} - rm -fr unittest-build && mkdir unittest-build - cd unittest-build + rm -fr unittest-build echo "Build unit test" touch ${REPORT_DIR}/unit_test_report.txt - cmake ${WORK_DIR}/../../unit -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE} - make -j - make test | tee -a ${REPORT_DIR}/unit_test_report.txt + cmake -S ${WORK_DIR}/../../unit -B unittest-build \ + -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE} + cmake --build unittest-build + ctest --test-dir unittest-build --output-on-failure | tee -a ${REPORT_DIR}/unit_test_report.txt echo "Finish unit tests" } @@ -503,7 +503,7 @@ function spec_test() git reset --hard 8d4f6aa2b00a8e7c0174410028625c6a176db8a1 # ignore import table cases git apply --ignore-whitespace ../../spec-test-script/extended_const.patch || exit 1 - + elif [[ ${ENABLE_MEMORY64} == 1 ]]; then echo "checkout spec for memory64 proposal" @@ -1187,7 +1187,15 @@ function trigger() } # if collect code coverage, ignore -s, test all test cases. -if [[ $TEST_CASE_ARR ]];then +if [[ $TEST_CASE_ARR == "unit" ]];then + # unit test cases are designed with specific compilation flags + # and run under specific modes. + # There is no need to loop through all running modes in this script. + unit_test || (echo "TEST FAILED"; exit 1) + collect_coverage unit + echo "JUST SKIP UNIT TEST NOW" +elif [[ $TEST_CASE_ARR == "spec" ]];then + # loop through all running modes trigger || (echo "TEST FAILED"; exit 1) else # test all suite, ignore polybench and libsodium because of long time cost @@ -1200,6 +1208,7 @@ else TEST_CASE_ARR+=("libsodium") fi ' + # loop through all running modes trigger || (echo "TEST FAILED"; exit 1) # Add more suites here fi