|
| 1 | +# CTest driver for tests/wasm/*.c. |
| 2 | +# Mirrors tests/wasm/Makefile: |
| 3 | +# - compile with wasm32-tcc |
| 4 | +# - run with Node via run-wasm.mjs |
| 5 | +# - compare stdout with *.expect |
| 6 | + |
| 7 | +cmake_minimum_required(VERSION 3.20) |
| 8 | + |
| 9 | +foreach(_req TEST_NAME WASM_TCC NODE_EXECUTABLE SOURCE_DIR BINARY_DIR) |
| 10 | + if(NOT DEFINED ${_req} OR "${${_req}}" STREQUAL "") |
| 11 | + message(FATAL_ERROR "RunWasmTest.cmake: missing required -D${_req}=...") |
| 12 | + endif() |
| 13 | +endforeach() |
| 14 | + |
| 15 | +set(_wasm_src "${SOURCE_DIR}/tests/wasm/${TEST_NAME}.c") |
| 16 | +set(_expect_file "${SOURCE_DIR}/tests/wasm/${TEST_NAME}.expect") |
| 17 | +set(_runner "${SOURCE_DIR}/tests/wasm/run-wasm.mjs") |
| 18 | +set(_out_dir "${BINARY_DIR}/tests/wasm/out") |
| 19 | +set(_out_wasm "${_out_dir}/${TEST_NAME}.wasm") |
| 20 | +set(_out_txt "${_out_dir}/${TEST_NAME}.out") |
| 21 | + |
| 22 | +foreach(_path _wasm_src _expect_file _runner) |
| 23 | + if(NOT EXISTS "${${_path}}") |
| 24 | + message(FATAL_ERROR "RunWasmTest.cmake: missing input file: ${${_path}}") |
| 25 | + endif() |
| 26 | +endforeach() |
| 27 | + |
| 28 | +file(MAKE_DIRECTORY "${_out_dir}") |
| 29 | + |
| 30 | +execute_process( |
| 31 | + COMMAND "${WASM_TCC}" -nostdlib "-B${BINARY_DIR}" "-I${SOURCE_DIR}/include" "${_wasm_src}" -o "${_out_wasm}" |
| 32 | + RESULT_VARIABLE _cc_rv |
| 33 | + OUTPUT_VARIABLE _cc_stdout |
| 34 | + ERROR_VARIABLE _cc_stderr |
| 35 | +) |
| 36 | +if(NOT _cc_rv EQUAL 0) |
| 37 | + message(FATAL_ERROR |
| 38 | + "Wasm compile failed for ${TEST_NAME}\n" |
| 39 | + "compiler: ${WASM_TCC}\n" |
| 40 | + "stdout:\n${_cc_stdout}\n" |
| 41 | + "stderr:\n${_cc_stderr}") |
| 42 | +endif() |
| 43 | + |
| 44 | +execute_process( |
| 45 | + COMMAND "${NODE_EXECUTABLE}" "${_runner}" "${_out_wasm}" |
| 46 | + RESULT_VARIABLE _node_rv |
| 47 | + OUTPUT_VARIABLE _node_stdout |
| 48 | + ERROR_VARIABLE _node_stderr |
| 49 | +) |
| 50 | +if(NOT _node_rv EQUAL 0) |
| 51 | + message(FATAL_ERROR |
| 52 | + "Wasm run failed for ${TEST_NAME}\n" |
| 53 | + "node: ${NODE_EXECUTABLE}\n" |
| 54 | + "stdout:\n${_node_stdout}\n" |
| 55 | + "stderr:\n${_node_stderr}") |
| 56 | +endif() |
| 57 | + |
| 58 | +file(WRITE "${_out_txt}" "${_node_stdout}") |
| 59 | +execute_process( |
| 60 | + COMMAND "${CMAKE_COMMAND}" -E compare_files "${_expect_file}" "${_out_txt}" |
| 61 | + RESULT_VARIABLE _cmp_rv |
| 62 | +) |
| 63 | +if(NOT _cmp_rv EQUAL 0) |
| 64 | + file(READ "${_expect_file}" _expect_contents) |
| 65 | + file(READ "${_out_txt}" _actual_contents) |
| 66 | + message(FATAL_ERROR |
| 67 | + "Unexpected wasm test output for ${TEST_NAME}\n" |
| 68 | + "expected:\n${_expect_contents}\n" |
| 69 | + "actual:\n${_actual_contents}") |
| 70 | +endif() |
0 commit comments