From 8d4dbb19fbda67ec227d33c4058b889ccfaee5ce Mon Sep 17 00:00:00 2001 From: Arvid Norlander <132438847+arvid-norlander@users.noreply.github.com> Date: Wed, 6 Sep 2023 21:11:31 +0200 Subject: [PATCH] Set CMAKE_MODULE_PATH in the cmake toolchain file (#335) Also add some basic testing for the cmake toolchain file. Fixes: #181 --- Makefile | 5 +++-- tests/cmake/CMakeLists.txt | 34 ++++++++++++++++++++++++++++++ tests/cmake/test_driver.sh | 17 +++++++++++++++ tests/run.sh | 42 +++++++++++++++++++++++++++++++++----- wasi-sdk.cmake | 4 ++++ 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 tests/cmake/CMakeLists.txt create mode 100755 tests/cmake/test_driver.sh diff --git a/Makefile b/Makefile index 3aab204ee..319cabd90 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/tests/cmake/CMakeLists.txt b/tests/cmake/CMakeLists.txt new file mode 100644 index 000000000..28c9b839b --- /dev/null +++ b/tests/cmake/CMakeLists.txt @@ -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} + $ + ${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} + $ + ${CMAKE_CURRENT_SOURCE_DIR}/../general/void_main.cc.stdout.expected) diff --git a/tests/cmake/test_driver.sh b/tests/cmake/test_driver.sh new file mode 100755 index 000000000..6e469a1ae --- /dev/null +++ b/tests/cmake/test_driver.sh @@ -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" diff --git a/tests/run.sh b/tests/run.sh index 6379b581c..36bcda55f 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -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 " where is a WASI-capable runtime to run the -# tests in full compile and execute mode. +# Top-level test runner. Usage is "run.sh " to run tests +# in compile-only mode, or "run.sh " where +# 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="/bin/clang++ --sysroot /share/wasi-sysroot" # export CC="/bin/clang --sysroot /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 diff --git a/wasi-sdk.cmake b/wasi-sdk.cmake index 83ee9b0ac..f5021a5e1 100644 --- a/wasi-sdk.cmake +++ b/wasi-sdk.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)