Skip to content

Commit a609387

Browse files
Add test of cmake
1 parent b6a237a commit a609387

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ default: build
5353
check:
5454
CC="clang --sysroot=$(BUILD_PREFIX)/share/wasi-sysroot" \
5555
CXX="clang++ --sysroot=$(BUILD_PREFIX)/share/wasi-sysroot -fno-exceptions" \
56-
PATH="$(PATH_PREFIX)/bin:$$PATH" tests/run.sh $(RUNTIME)
56+
PATH="$(PATH_PREFIX)/bin:$$PATH" tests/run.sh "$(BUILD_PREFIX)" "$(RUNTIME)"
5757

5858
clean:
5959
rm -rf build $(DESTDIR)

tests/cmake/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
3+
project(wasi-sdk-test)
4+
5+
# Sanity check setup
6+
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL WASI)
7+
message(FATAL_ERROR "Wrong system name (${CMAKE_SYSTEM_NAME}), wrong toolchain file in use?")
8+
endif()
9+
10+
if(NOT DEFINED WASI)
11+
message(FATAL_ERROR "WASI is not set, platform file likely not loaded")
12+
endif()
13+
14+
set(RUNWASI "" CACHE STRING "Path to or name of WASM runner")
15+
16+
# Test build a C and C++ target respectively
17+
add_executable(void_main_c ../general/void_main.c)
18+
add_executable(void_main_cc ../general/void_main.cc)
19+
20+
include(CTest)
21+
enable_testing()
22+
23+
add_test(NAME void_main_c
24+
COMMAND
25+
${CMAKE_CURRENT_SOURCE_DIR}/test_driver.sh
26+
${RUNWASI}
27+
$<TARGET_FILE:void_main_c>
28+
${CMAKE_CURRENT_SOURCE_DIR}/../general/void_main.c.stdout.expected)
29+
add_test(NAME void_main_cc
30+
COMMAND
31+
${CMAKE_CURRENT_SOURCE_DIR}/test_driver.sh
32+
${RUNWASI}
33+
$<TARGET_FILE:void_main_cc>
34+
${CMAKE_CURRENT_SOURCE_DIR}/../general/void_main.cc.stdout.expected)

tests/cmake/test_driver.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
# Simplified runner for cmake
3+
4+
set -ex
5+
6+
runwasi="$1"
7+
target="$2"
8+
stdout_expected="$3"
9+
stderr_expected="/dev/null"
10+
11+
stdout_observed="$target.stdout.observed"
12+
stderr_observed="$target.stderr.observed"
13+
14+
"$runwasi" "$target" > "$stdout_observed" 2> "$stderr_observed"
15+
16+
diff -u "$stderr_expected" "$stderr_observed"
17+
diff -u "$stdout_expected" "$stdout_observed"

tests/run.sh

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/bin/bash
22
set -ueo pipefail
33

4-
# Top-level test runner. Usage is "run.sh" to run tests in compile-only mode,
5-
# or "run.sh <runwasi>" where <runwasi> is a WASI-capable runtime to run the
6-
# tests in full compile and execute mode.
4+
# Top-level test runner. Usage is "run.sh <path to wasi-sdk>" to run tests
5+
# in compile-only mode, or "run.sh <path to wasi-sdk> <runwasi>" where
6+
# <runwasi> is a WASI-capable runtime to run the tests in full compile and
7+
# execute mode.
78
#
89
# By default this script will look for `clang` and `clang++` in $PATH and
910
# assume that they are correctly configured with the sysroot in the default
@@ -12,10 +13,16 @@ set -ueo pipefail
1213
# export CXX="<wasi-sdk>/bin/clang++ --sysroot <wasi-sdk>/share/wasi-sysroot"
1314
# export CC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
1415
#
16+
if [ $# -lt 1 ]; then
17+
echo "Path to WASI SDK is required"
18+
exit 1
19+
fi
20+
21+
wasi_sdk="$1"
1522

1623
# Determine the wasm runtime to use, if one is provided.
17-
if [ $# -gt 0 ]; then
18-
runwasi="$1"
24+
if [ $# -gt 1 ]; then
25+
runwasi="$2"
1926
else
2027
runwasi=""
2128
fi
@@ -26,6 +33,7 @@ CXX=${CXX:=clang++}
2633

2734
echo $CC
2835
echo $CXX
36+
echo "SDK: $wasi_sdk"
2937

3038
cd $testdir/compile-only
3139
for options in -O0 -O2 "-O2 -flto"; do
@@ -54,3 +62,27 @@ for options in -O0 -O2 "-O2 -flto"; do
5462
done
5563
done
5664
cd - >/dev/null
65+
66+
# Test cmake build system for wasi-sdk
67+
test_cmake() {
68+
local option
69+
for option in Debug Release; do
70+
rm -rf "$testdir/cmake/build/$option"
71+
mkdir -p "$testdir/cmake/build/$option"
72+
cd "$testdir/cmake/build/$option"
73+
cmake \
74+
-G "Unix Makefiles" \
75+
-DCMAKE_BUILD_TYPE="$option" \
76+
-DRUNWASI="$runwasi" \
77+
-DWASI_SDK_PREFIX="$wasi_sdk" \
78+
-DCMAKE_TOOLCHAIN_FILE="$wasi_sdk/share/cmake/wasi-sdk.cmake" \
79+
../..
80+
make
81+
if [[ -n "$runwasi" ]]; then
82+
ctest --output-on-failure
83+
fi
84+
cd - >/dev/null
85+
done
86+
}
87+
88+
test_cmake

0 commit comments

Comments
 (0)