Skip to content

Commit

Permalink
Add test of cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
arvid-norlander committed Jul 17, 2023
1 parent c64198a commit 45b0fe0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ default: build
check:
CC="clang --sysroot=$(BUILD_PREFIX)/share/wasi-sysroot" \
CXX="clang++ --sysroot=$(BUILD_PREFIX)/share/wasi-sysroot -fno-exceptions" \
PATH="$(PATH_PREFIX)/bin:$$PATH" tests/run.sh $(RUNTIME)
PATH="$(PATH_PREFIX)/bin:$$PATH" tests/run.sh "$(RUNTIME)" "$(BUILD_PREFIX)"

clean:
rm -rf build $(DESTDIR)
Expand Down
34 changes: 34 additions & 0 deletions tests/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.22)

project(wasi-sdk-test)

# Sanity check setup
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL WASI)
message(FATAL_ERROR "Wrong system name (${CMAKE_SYSTEM_NAME}), wrong toolchain file in use?")
endif()

if(NOT DEFINED WASI)
message(FATAL_ERROR "WASI is not set, platform file likely not loaded")
endif()

set(RUNWASI "" CACHE STRING "Path to or name of WASM runner")

# Test build a C and C++ target respectively
add_executable(void_main_c ../general/void_main.c)
add_executable(void_main_cc ../general/void_main.cc)

include(CTest)
enable_testing()

add_test(NAME void_main_c
COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/test_driver.sh
${RUNWASI}
$<TARGET_FILE:void_main_c>
${CMAKE_CURRENT_SOURCE_DIR}/../general/void_main.c.stdout.expected)
add_test(NAME void_main_cc
COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/test_driver.sh
${RUNWASI}
$<TARGET_FILE:void_main_cc>
${CMAKE_CURRENT_SOURCE_DIR}/../general/void_main.cc.stdout.expected)
17 changes: 17 additions & 0 deletions tests/cmake/test_driver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# Simplified runner for cmake

set -ex

runwasi="$1"
target="$2"
stdout_expected="$3"
stderr_expected="/dev/null"

stdout_observed="$target.stdout.observed"
stderr_observed="$target.stderr.observed"

"$runwasi" "$target" > "$stdout_observed" 2> "$stderr_observed"

diff -u "$stderr_expected" "$stderr_observed"
diff -u "$stdout_expected" "$stdout_observed"
27 changes: 27 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ else
runwasi=""
fi

if [ $# -gt 1 ]; then
wasi_sdk="$2"
else
wasi_sdk=""
fi

testdir=$(dirname $0)
CC=${CC:=clang}
CXX=${CXX:=clang++}

echo $CC
echo $CXX
echo "SDK: $wasi_sdk"

cd $testdir/compile-only
for options in -O0 -O2 "-O2 -flto"; do
Expand Down Expand Up @@ -54,3 +61,23 @@ for options in -O0 -O2 "-O2 -flto"; do
done
done
cd - >/dev/null

if [[ -n "$wasi_sdk" ]]; then
for option in Debug Release; do
rm -rf "$testdir/cmake/build/$option"
mkdir -p "$testdir/cmake/build/$option"
cd "$testdir/cmake/build/$option"
cmake \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE="$option" \
-DRUNWASI="$runwasi" \
-DWASI_SDK_PREFIX="$wasi_sdk" \
-DCMAKE_TOOLCHAIN_FILE="$wasi_sdk/share/cmake/wasi-sdk.cmake" \
../..
make
if [[ -n "$runwasi" ]]; then
ctest --output-on-failure
fi
cd - >/dev/null
done
fi

0 comments on commit 45b0fe0

Please sign in to comment.