Set CMAKE_MODULE_PATH in the cmake toolchain file (#335)

Also add some basic testing for the cmake toolchain file.

Fixes: #181
pull/349/head
Arvid Norlander 1 year ago committed by GitHub
parent bbfb9736af
commit 8d4dbb19fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 "$(BUILD_PREFIX)" "$(RUNTIME)"
clean:
rm -rf build $(DESTDIR)
@ -219,9 +219,10 @@ build/libcxx.BUILT: build/llvm.BUILT build/compiler-rt.BUILT build/wasi-libc.BUI
build/config.BUILT:
mkdir -p $(BUILD_PREFIX)/share/misc
cp src/config/config.sub src/config/config.guess $(BUILD_PREFIX)/share/misc
mkdir -p $(BUILD_PREFIX)/share/cmake
mkdir -p $(BUILD_PREFIX)/share/cmake/Platform
cp wasi-sdk.cmake $(BUILD_PREFIX)/share/cmake
cp wasi-sdk-pthread.cmake $(BUILD_PREFIX)/share/cmake
cp cmake/Platform/WASI.cmake $(BUILD_PREFIX)/share/cmake/Platform
touch build/config.BUILT
build: build/llvm.BUILT build/wasi-libc.BUILT build/compiler-rt.BUILT build/libcxx.BUILT build/config.BUILT

@ -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)

@ -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"

@ -1,9 +1,10 @@
#!/bin/bash
set -ueo pipefail
# Top-level test runner. Usage is "run.sh" to run tests in compile-only mode,
# or "run.sh <runwasi>" where <runwasi> is a WASI-capable runtime to run the
# tests in full compile and execute mode.
# Top-level test runner. Usage is "run.sh <path to wasi-sdk>" to run tests
# in compile-only mode, or "run.sh <path to wasi-sdk> <runwasi>" where
# <runwasi> is a WASI-capable runtime to run the tests in full compile and
# execute mode.
#
# By default this script will look for `clang` and `clang++` in $PATH and
# assume that they are correctly configured with the sysroot in the default
@ -12,10 +13,16 @@ set -ueo pipefail
# export CXX="<wasi-sdk>/bin/clang++ --sysroot <wasi-sdk>/share/wasi-sysroot"
# export CC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
#
if [ $# -lt 1 ]; then
echo "Path to WASI SDK is required"
exit 1
fi
wasi_sdk="$1"
# Determine the wasm runtime to use, if one is provided.
if [ $# -gt 0 ]; then
runwasi="$1"
if [ $# -gt 1 ]; then
runwasi="$2"
else
runwasi=""
fi
@ -26,6 +33,7 @@ CXX=${CXX:=clang++}
echo $CC
echo $CXX
echo "SDK: $wasi_sdk"
cd $testdir/compile-only
for options in -O0 -O2 "-O2 -flto"; do
@ -54,3 +62,27 @@ for options in -O0 -O2 "-O2 -flto"; do
done
done
cd - >/dev/null
# Test cmake build system for wasi-sdk
test_cmake() {
local option
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
}
test_cmake

@ -3,6 +3,10 @@
# This is arbitrary, AFAIK, for now.
cmake_minimum_required(VERSION 3.4.0)
# Until Platform/WASI.cmake is upstream we need to inject the path to it
# into CMAKE_MODULE_PATH.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
set(CMAKE_SYSTEM_NAME WASI)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR wasm32)

Loading…
Cancel
Save