Skip to content

Commit de4b950

Browse files
authored
Refactor the CMake and related scripts for unit tests (#4605)
- only unit/CMakeLists.txt fetches googletest - only unit/unit_common.cmake find LLVM dependencies - use `WAMR_RUNTIME_LIB_SOURCE` to replace a long list - enable LLVM support across various CMake configurations - remove redundant LLVM dependency installation and add i386 support for libraries - CMake configurations for better build output and module support - Find llvm on demand - Add test suite guidelines
1 parent 238f45d commit de4b950

File tree

28 files changed

+464
-465
lines changed

28 files changed

+464
-465
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ jobs:
350350
uses: ./.github/actions/install-wasi-sdk-wabt
351351
with:
352352
os: ${{ matrix.os }}
353-
353+
354354
- name: Build wamrc
355355
run: |
356356
mkdir build && cd build
@@ -361,15 +361,16 @@ jobs:
361361
- name: Install dependencies for X86_32
362362
if: matrix.build_target == 'X86_32'
363363
run: |
364+
sudo dpkg --add-architecture i386
364365
sudo apt-get update
365-
sudo apt-get install -y g++-multilib
366+
sudo apt-get install -y g++-multilib libzstd-dev:i386 zlib1g-dev:i386
366367
367368
- name: Build and run unit tests
368369
run: |
369370
mkdir build && cd build
370371
cmake .. -DWAMR_BUILD_TARGET=${{ matrix.build_target }}
371-
cmake --build . --config Release --parallel 4
372-
ctest
372+
cmake --build . --parallel 4
373+
ctest --output-on-failure
373374
working-directory: tests/unit
374375

375376
build_regression_tests:

build-scripts/code_coverage.cmake

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
function(check_ubuntu_version)
5+
# ubuntu 2204 is the recommended environment for collecting coverage data for now
6+
# otherwise, there will be ERRORs, when using 2404, like below and
7+
#
8+
# 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
9+
# (use "geninfo --ignore-errors mismatch,mismatch ..." to suppress this warning)
10+
# geninfo: ERROR: ('negative') Unexpected negative count '-3' for /workspaces/wasm-micro-runtime/core/iwasm/interpreter/wasm_interp_classic.c:5473.
11+
# Perhaps you need to compile with '-fprofile-update=atomic
12+
# (use "geninfo --ignore-errors negative,negative ..." to suppress this warning)
13+
#
14+
# For sure, `--ignore-errors` can be used to ignore these errors, but better to use the recommended environment.
15+
file(READ "/etc/os-release" OS_RELEASE_CONTENT)
16+
string(REGEX MATCH "VERSION_ID=\"([0-9]+)\\.([0-9]+)\"" _ ${OS_RELEASE_CONTENT})
17+
if(NOT DEFINED CMAKE_MATCH_1 OR NOT DEFINED CMAKE_MATCH_2)
18+
message(WARNING "Unable to detect Ubuntu version. Please ensure you are using Ubuntu 22.04.")
19+
return()
20+
endif()
21+
22+
set(UBUNTU_MAJOR_VERSION ${CMAKE_MATCH_1})
23+
set(UBUNTU_MINOR_VERSION ${CMAKE_MATCH_2})
24+
25+
if(NOT (UBUNTU_MAJOR_VERSION EQUAL 22 AND UBUNTU_MINOR_VERSION EQUAL 04))
26+
message(WARNING "Ubuntu ${UBUNTU_MAJOR_VERSION}.${UBUNTU_MINOR_VERSION} detected. Ubuntu 22.04 is recommended for collecting coverage data.")
27+
else()
28+
message(STATUS "Ubuntu 22.04 detected. Proceeding with coverage data collection.")
29+
endif()
30+
endfunction()
31+
32+
check_ubuntu_version()
33+
34+
# add compile options for code coverage globally
35+
add_compile_options(--coverage -O0 -g)
36+
link_libraries(gcov)
37+
add_definitions (-DCOLLECT_CODE_COVERAGE)

build-scripts/config_common.cmake

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,7 @@ if (WAMR_BUILD_GC_HEAP_VERIFY EQUAL 1)
640640
message (" GC heap verification enabled")
641641
endif ()
642642
if ("$ENV{COLLECT_CODE_COVERAGE}" STREQUAL "1" OR COLLECT_CODE_COVERAGE EQUAL 1)
643-
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
644-
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
645-
add_definitions (-DCOLLECT_CODE_COVERAGE)
643+
include(${CMAKE_CURRENT_LIST_DIR}/code_coverage.cmake)
646644
message (" Collect code coverage enabled")
647645
endif ()
648646
if (WAMR_BUILD_STATIC_PGO EQUAL 1)

tests/unit/CMakeLists.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@ if(WAMR_BUILD_TARGET STREQUAL "X86_32")
3030
set(CMAKE_LIBRARY_ARCHITECTURE "i386-linux-gnu" CACHE STRING "" FORCE)
3131
endif()
3232

33-
# Prevent overriding the parent project's compiler/linker
34-
# settings on Windows
35-
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
36-
3733
# Fetch Google test
3834
include (FetchContent)
39-
4035
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24")
4136
FetchContent_Declare (
4237
googletest
@@ -50,10 +45,11 @@ else()
5045
)
5146
endif()
5247

48+
# Prevent overriding the parent project's compiler/linker
49+
# settings on Windows
50+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
5351
FetchContent_MakeAvailable(googletest)
5452

55-
SET(GOOGLETEST_INCLUDED 1)
56-
5753
include(GoogleTest)
5854
enable_testing()
5955

@@ -64,12 +60,13 @@ add_subdirectory(libc-builtin)
6460
add_subdirectory(shared-utils)
6561
add_subdirectory(linear-memory-wasm)
6662
add_subdirectory(linear-memory-aot)
67-
add_subdirectory(aot-stack-frame)
6863
add_subdirectory(linux-perf)
6964
add_subdirectory(gc)
7065
add_subdirectory(tid-allocator)
7166

7267
if (NOT WAMR_BUILD_TARGET STREQUAL "X86_32")
68+
add_subdirectory(aot-stack-frame)
69+
7370
# should enable 32-bit llvm when X86_32
7471
add_subdirectory (aot)
7572
add_subdirectory (custom-section)

tests/unit/README.md

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# Guide to Creating a Test Suite for a New Feature in WAMR
2+
3+
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.
4+
5+
---
6+
7+
## General Guidelines
8+
9+
- **Create a New Directory**:
10+
Always create a dedicated directory for a new feature under the `tests/unit/` directory.
11+
12+
- Reuse existing test cases and patch them when possible to avoid redundancy.
13+
- Name the directory in lowercase with words separated by hyphens (e.g., `new-feature`).
14+
- Name the test source file in lowercase with words separated by underscore (e.g., `new_test.cc`).
15+
16+
- **Avoid Committing `.wasm` Files**:
17+
Do not commit precompiled `.wasm` files. Instead:
18+
19+
- Generate `.wasm` files from `.wat` or `.c` source files.
20+
- Use `ExternalProject` and the `wasi-sdk` toolchain to compile `.wasm` files during the build process.
21+
22+
- **Keep Using `ctest` as the framework**:
23+
Continue to use `ctest` for running the test cases, as it is already integrated into the existing test framework.
24+
25+
---
26+
27+
## Writing `CMakeLists.txt` for the Test Suite
28+
29+
When creating a `CMakeLists.txt` file for your test suite, follow these best practices:
30+
31+
1. **Do Not Fetch Googletest Again**:
32+
The root `unit/CMakeLists.txt` already fetches Googletest. Avoid including or fetching it again in your test suite.
33+
34+
2. **Find LLVM on Demand**:
35+
If your test suite requires LLVM, use `find_package` to locate LLVM components as needed. Do not include LLVM globally unless required.
36+
37+
3. **Include `unit_common.cmake`**:
38+
Always include `../unit_common.cmake` in your `CMakeLists.txt` to avoid duplicating common configurations and utilities.
39+
40+
Example:
41+
42+
```cmake
43+
include("../unit_common.cmake")
44+
```
45+
46+
4. **Use `WAMR_RUNTIME_LIB_SOURCE`**:
47+
Replace long lists of runtime source files with the `WAMR_RUNTIME_LIB_SOURCE` variable to simplify your configuration.
48+
49+
Example:
50+
51+
```cmake
52+
target_sources(your_test_target PRIVATE ${WAMR_RUNTIME_LIB_SOURCE})
53+
```
54+
55+
5. **Avoid Global Compilation Flags**:
56+
Do not define global compilation flags in the `unit` directory. Each test case should specify its own compilation flags based on its unique requirements.
57+
58+
---
59+
60+
## Generating `.wasm` Files
61+
62+
- **Compile `.wasm` Files Dynamically**:
63+
Use `ExternalProject` in your `CMakeLists.txt` to compile `.wasm` files from `.wat` or `.c` source files.
64+
- Use the `wasi-sdk` toolchain for `.c` or `.cc` source files.
65+
- Example configuration:
66+
```cmake
67+
ExternalProject_Add(
68+
generate_wasm
69+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps
70+
BUILD_ALWAYS YES
71+
CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build
72+
-DWASI_SDK_PREFIX=${WASI_SDK_DIR}
73+
-DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN}
74+
BUILD_COMMAND ${CMAKE_COMMAND} --build build
75+
INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps
76+
)
77+
```
78+
- **Example for `wasm-apps` Directory**:
79+
Place your source files in a `wasm-apps/` subdirectory within your test suite directory.
80+
81+
- Create a `CMakeLists.txt` in `wasm-apps/` to handle the compilation of these files.
82+
- Example `CMakeLists.txt` for `wasm-apps/`:
83+
84+
```cmake
85+
cmake_minimum_required(VERSION 3.13)
86+
project(wasm_apps)
87+
88+
add_executable(example example.c)
89+
set_target_properties(example PROPERTIES SUFFIX .wasm)
90+
install(TARGETS example DESTINATION .)
91+
```
92+
93+
---
94+
95+
## Compiling and Running Test Cases
96+
97+
To compile and run the test cases, follow these steps:
98+
99+
1. **Generate Build Files**:
100+
101+
```bash
102+
cmake -S . -B build
103+
```
104+
105+
2. **Build the Test Suite**:
106+
107+
```bash
108+
cmake --build build
109+
```
110+
111+
3. **Run the Tests**:
112+
113+
```bash
114+
ctest --test-dir build --output-on-failure
115+
```
116+
117+
This will compile and execute all test cases in the test suite, displaying detailed output for any failures.
118+
119+
4. **List all Tests**:
120+
To see all available test cases, use:
121+
122+
```bash
123+
ctest --test-dir build -N
124+
```
125+
126+
5. **Run a Specific Test**:
127+
To run a specific test case, use:
128+
```bash
129+
ctest --test-dir build -R <test_name> --output-on-failure
130+
```
131+
132+
---
133+
134+
## Collecting Code Coverage Data
135+
136+
To collect code coverage data using `lcov`, follow these steps:
137+
138+
1. **Build with Coverage Flags**:
139+
Ensure the test suite is built with coverage flags enabled:
140+
141+
```bash
142+
cmake -S . -B build -DCOLLECT_CODE_COVERAGE=1
143+
cmake --build build
144+
```
145+
146+
2. **Run the Tests**:
147+
Execute the test cases as described above.
148+
149+
3. **Generate Coverage Report**:
150+
Use `lcov` to collect and generate the coverage report:
151+
152+
```bash
153+
lcov --capture --directory build --output-file coverage.all.info
154+
lcov --extract coverage.all.info "*/core/iwasm/*" "*/core/shared/*" --output-file coverage.info
155+
genhtml coverage.info --output-directory coverage-report
156+
```
157+
158+
4. **View the Report**:
159+
Open the `index.html` file in the `coverage-report` directory to view the coverage results in your browser.
160+
161+
5. **Summary of Coverage**:
162+
To get a summary of the coverage data, use:
163+
164+
```bash
165+
lcov --summary coverage.info
166+
```
167+
168+
---
169+
170+
## Example Directory Structure
171+
172+
Here’s an example of how your test suite directory might look:
173+
174+
```
175+
new-feature/
176+
├── CMakeLists.txt
177+
├── new_feature_test.cc
178+
├── wasm-apps/
179+
| ├── CMakeLists.txt
180+
│ ├── example.c
181+
│ └── example.wat
182+
```
183+
184+
---
185+
186+
## Additional Notes
187+
188+
- **Testing Framework**: Use Googletest for writing unit tests. Refer to existing test cases in the `tests/unit/` directory for examples.
189+
- **Documentation**: Add comments in your test code to explain the purpose of each test case.
190+
- **Edge Cases**: Ensure your test suite covers edge cases and potential failure scenarios.
191+
- **Reuse Utilities**: Leverage existing utilities in `common/` (e.g., `mock_allocator.h`, `test_helper.h`) to simplify your test code.
192+
193+
---
194+
195+
By following these guidelines, you can create a well-structured and maintainable test suite that integrates seamlessly with the WAMR testing framework.

tests/unit/aot-stack-frame/CMakeLists.txt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project (test-aot-stack-frame)
88
add_definitions (-DRUN_ON_LINUX)
99

1010
set (WAMR_BUILD_AOT 1)
11-
set (WAMR_BUILD_INTERP 0)
11+
set (WAMR_BUILD_INTERP 1)
1212
set (WAMR_BUILD_JIT 0)
1313
set (WAMR_BUILD_SIMD 1)
1414
set (WAMR_BUILD_REF_TYPES 1)
@@ -21,6 +21,10 @@ set (WAMR_BUILD_GC 1)
2121

2222
include (../unit_common.cmake)
2323

24+
find_package(LLVM REQUIRED CONFIG)
25+
include_directories(${LLVM_INCLUDE_DIRS})
26+
add_definitions(${LLVM_DEFINITIONS})
27+
2428
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
2529

2630
add_definitions (-DWASM_ENABLE_AOT_STACK_FRAME=1)
@@ -32,22 +36,10 @@ file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
3236
set (UNIT_SOURCE ${source_all})
3337

3438
set (unit_test_sources
35-
${UNIT_SOURCE}
36-
${WAMR_RUNTIME_LIB_SOURCE}
37-
${UNCOMMON_SHARED_SOURCE}
38-
${SRC_LIST}
39-
${PLATFORM_SHARED_SOURCE}
40-
${UTILS_SHARED_SOURCE}
41-
${MEM_ALLOC_SHARED_SOURCE}
42-
${LIB_HOST_AGENT_SOURCE}
43-
${NATIVE_INTERFACE_SOURCE}
44-
${LIBC_BUILTIN_SOURCE}
45-
${IWASM_COMMON_SOURCE}
46-
${IWASM_INTERP_SOURCE}
47-
${IWASM_AOT_SOURCE}
48-
${IWASM_COMPL_SOURCE}
49-
${WASM_APP_LIB_SOURCE_ALL}
50-
)
39+
${UNIT_SOURCE}
40+
${WAMR_RUNTIME_LIB_SOURCE}
41+
${IWASM_COMPL_SOURCE}
42+
)
5143

5244
# Automatically build wasm-apps for this test
5345
add_subdirectory(wasm-apps)
@@ -59,4 +51,4 @@ add_dependencies (aot_stack_frame_test aot-stack-frame-test-wasm)
5951

6052
target_link_libraries (aot_stack_frame_test ${LLVM_AVAILABLE_LIBS} gtest_main )
6153

62-
#gtest_discover_tests(aot_stack_frame_test)
54+
gtest_discover_tests(aot_stack_frame_test)

0 commit comments

Comments
 (0)