From 1c79a9b827c2f22e839b137eca45cfc625e2b4ec Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 7 Jan 2025 09:59:05 -0800 Subject: [PATCH] Refactor CMake-based testing of examples * Replace hard tabs in file with ` ` * Move building of example executables to build-time instead of configure-time * Move examples to being built as part of the "all" target * Use a CMake shorthand for adding tests on executables generated Closes #9932 Closes #9933 --- examples/CMakeLists.txt | 64 +++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 46df6db5eb43..fd9e4b119fca 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -6,45 +6,52 @@ set(CMAKE_CXX_STANDARD 11) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../crates/c-api ${CMAKE_CURRENT_BINARY_DIR}/wasmtime) function(CREATE_TARGET TARGET TARGET_PATH) - add_executable(wasmtime-${TARGET} ${TARGET_PATH}) + add_executable(wasmtime-${TARGET} ${TARGET_PATH}) - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(wasmtime-${TARGET} PRIVATE -Wall -Wextra -Wno-deprecated-declarations) - elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - target_compile_options(wasmtime-${TARGET} PRIVATE /W3) - endif() + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + target_compile_options(wasmtime-${TARGET} PRIVATE -Wall -Wextra -Wno-deprecated-declarations) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + target_compile_options(wasmtime-${TARGET} PRIVATE /W3) + endif() - target_compile_definitions(wasmtime-${TARGET} PRIVATE WASMTIME_TEST_ONLY) + target_compile_definitions(wasmtime-${TARGET} PRIVATE WASMTIME_TEST_ONLY) - set_target_properties(wasmtime-${TARGET} PROPERTIES - OUTPUT_NAME wasmtime-${TARGET} - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<0:> - CXX_VISIBILITY_PRESET hidden - POSITION_INDEPENDENT_CODE ON) + set_target_properties(wasmtime-${TARGET} PROPERTIES + OUTPUT_NAME wasmtime-${TARGET} + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<0:> + CXX_VISIBILITY_PRESET hidden + POSITION_INDEPENDENT_CODE ON) - target_include_directories(wasmtime-${TARGET} PUBLIC wasmtime) - target_link_libraries(wasmtime-${TARGET} PUBLIC wasmtime) - if(APPLE) - target_link_libraries(wasmtime-${TARGET} PRIVATE "-framework CoreFoundation") - endif() - add_test(NAME ${TARGET}-c COMMAND $ WORKING_DIRECTORY ../..) + target_include_directories(wasmtime-${TARGET} PUBLIC wasmtime) + target_link_libraries(wasmtime-${TARGET} PUBLIC wasmtime) + if(APPLE) + target_link_libraries(wasmtime-${TARGET} PRIVATE "-framework CoreFoundation") + endif() + add_test(NAME ${TARGET}-c COMMAND wasmtime-${TARGET} WORKING_DIRECTORY ../..) endfunction() function(CREATE_RUST_TEST EXAMPLE) - if(ARGC GREATER 1) - add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} --features ${ARGV1} WORKING_DIRECTORY ../..) - else() - add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} WORKING_DIRECTORY ../..) - endif() + if(ARGC GREATER 1) + add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} --features ${ARGV1} WORKING_DIRECTORY ../..) + else() + add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} WORKING_DIRECTORY ../..) + endif() endfunction() function(CREATE_RUST_WASM EXAMPLE TARGET) - execute_process(COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET}) + add_custom_target(${EXAMPLE}-wasm-${TARGET} ALL COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET}) endfunction() # Enable testing enable_testing() -# Add all examples +# Wasm files required by tests +create_rust_wasm(fib-debug wasm32-unknown-unknown) +create_rust_wasm(tokio wasm32-wasip1) +create_rust_wasm(wasi wasm32-wasip1) +create_rust_wasm(wasi wasm32-wasip2) +create_rust_wasm(component wasm32-unknown-unknown) + +# C/C++ examples/tests create_target(anyref anyref.c) create_target(async async.cpp) create_target(externref externref.c) @@ -61,13 +68,8 @@ create_target(serialize serialize.c) create_target(threads threads.c) create_target(wasip1 wasip1/main.c) -# Add rust tests +# Rust examples/tests create_rust_test(anyref) -create_rust_wasm(fib-debug wasm32-unknown-unknown) -create_rust_wasm(tokio wasm32-wasip1) -create_rust_wasm(wasi wasm32-wasip1) -create_rust_wasm(wasi wasm32-wasip2) -create_rust_wasm(component wasm32-unknown-unknown) create_rust_test(epochs) create_rust_test(externref) create_rust_test(fib-debug)