Skip to content

Commit

Permalink
Set CMAKE_MODULE_PATH in the cmake toolchain file (#335)
Browse files Browse the repository at this point in the history
Also add some basic testing for the cmake toolchain file.

Fixes: #181
  • Loading branch information
arvid-norlander authored Sep 6, 2023
1 parent bbfb973 commit 8d4dbb1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
5 changes: 3 additions & 2 deletions 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 "$(BUILD_PREFIX)" "$(RUNTIME)"

clean:
rm -rf build $(DESTDIR)
Expand Down Expand Up @@ -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
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"
42 changes: 37 additions & 5 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions wasi-sdk.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8d4dbb1

Please sign in to comment.