A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Speed up testing the C API in CI (#11765)
Alex Crichtonfde5b75427e8
diff --git a/.github/workflows/main.yml b/.github/workflows/main.ymlindex 8645c9f82d..8b2d3d98f4 100644--- a/.github/workflows/main.yml+++ b/.github/workflows/main.yml@@ -200,6 +200,8 @@ jobs:echo platform-checks=true >> $GITHUB_OUTPUTelif grep -q 'prtest:miri' commits.log; thenecho test-miri=true >> $GITHUB_OUTPUT+ elif grep -q 'prtest:capi' commits.log; then+ echo test-capi=true >> $GITHUB_OUTPUTfiif grep -q crates.c-api names.log; thenecho test-capi=true >> $GITHUB_OUTPUT@@ -622,31 +624,56 @@ jobs:# Perform all tests of the c-apitest_capi:needs: determine- name: Test C-API ${{ matrix.os }}+ name: Test C-API ${{ matrix.name }}runs-on: ${{ matrix.os }}if: needs.determine.outputs.test-capistrategy:fail-fast: ${{ github.event_name != 'pull_request' }}matrix:- os: [ubuntu-24.04, macos-15, windows-2025]-+ include:+ # This configuration will build C/C++ tests, examples, and Rust tests.+ # Everything is linked against the C shared library to speed up+ # linking, and this is intended to be the "main set of tests".+ #+ # Run this on windows/mac/linux+ - os: ubuntu-24.04+ flags: -DBUILD_SHARED_LIBS=ON -DBUILD_TESTS=ON -DBUILD_RUST_EXAMPLES=ON+ name: Linux+ - os: macos-15+ flags: -DBUILD_SHARED_LIBS=ON -DBUILD_TESTS=ON -DBUILD_RUST_EXAMPLES=ON+ name: macOS+ - os: windows-2025+ # Note that dynamic linking is disabled here because I don't want to+ # have to deal with figuring out how to get `wasmtime.dll` onto+ # `PATH` for various tests.+ flags: -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=ON -DBUILD_RUST_EXAMPLES=ON+ name: Windows++ # This tests using static linking for examples but skips building the+ # Rust examples as well as C/C++ tests. Intended to be a faster build+ # which mostly just tests static linking.+ - os: ubuntu-24.04+ flags: -DBUILD_SHARED_LIBS=OFF -DBUILD_RUST_EXAMPLES=OFF+ name: Linux (static linking)steps:- uses: actions/checkout@v4with:submodules: true- uses: ./.github/actions/install-rust+ - run: rustup target add wasm32-wasip2- # Build and test the C API with example C programs along with the example- # Rust programs. Note that this only executes if the `determine` step told- # us to test the capi which is off-by-default for PRs.- - run: rustup target add wasm32-wasip2 # wasip2 target needed by example programs- - run: cmake -Sexamples -Bexamples/build -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=ON- - run: cmake --build examples/build --config Debug- - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target RUN_TESTS+ - uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756+ # An ode to MinGW: man you're a pain to work around sometimes+ - run: rm /usr/bin/linkif: runner.os == 'Windows'- - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target test- if: runner.os != 'Windows'++ - run: cmake -G Ninja -Sexamples -DCMAKE_BUILD_TYPE=Debug -Bexamples/build ${{ matrix.flags }}+ - run: ninja -C examples/build+ - run: ninja -C examples/build test+ env:+ CTEST_OUTPUT_ON_FAILURE: 1+ CTEST_PARALLEL_LEVEL: 4# Perform all tests (debug mode) for `wasmtime`.#diff --git a/crates/c-api/CMakeLists.txt b/crates/c-api/CMakeLists.txtindex bd41ca22b5..4b67dbef41 100644--- a/crates/c-api/CMakeLists.txt+++ b/crates/c-api/CMakeLists.txt@@ -86,7 +86,9 @@ add_dependencies(wasmtime wasmtime-crate)if(BUILD_SHARED_LIBS)if(NOT WASMTIME_TARGET MATCHES "windows")+ if(NOT WASMTIME_TARGET MATCHES "darwin")set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")+ endif()endif()list(GET WASMTIME_SHARED_FILES 0 WASMTIME_SHARED_LIB_TO_LINK)target_link_libraries(wasmtime INTERFACE ${WASMTIME_SHARED_LIB_TO_LINK})diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txtindex 605e921f6c..33e40b61f5 100644--- a/examples/CMakeLists.txt+++ b/examples/CMakeLists.txt@@ -14,7 +14,12 @@ function(CREATE_TARGET TARGET TARGET_PATH)target_compile_options(wasmtime-${TARGET} PRIVATE /W3)endif()- target_compile_definitions(wasmtime-${TARGET} PRIVATE WASMTIME_TEST_ONLY)+ # This is only used in fib-debug.c right now and it only works with static+ # linking beacuse the required symbols aren't exported in the shared library, so+ # only when shared libs are disabled is this turned on.+ if (NOT BUILD_SHARED_LIBS)+ target_compile_definitions(wasmtime-${TARGET} PRIVATE WASMTIME_TEST_ONLY)+ endif()set_target_properties(wasmtime-${TARGET} PROPERTIESOUTPUT_NAME wasmtime-${TARGET}@@ -24,6 +29,7 @@ function(CREATE_TARGET TARGET TARGET_PATH)target_include_directories(wasmtime-${TARGET} PUBLIC wasmtime)target_link_libraries(wasmtime-${TARGET} PUBLIC wasmtime)+add_test(NAME ${TARGET}-c COMMAND wasmtime-${TARGET} WORKING_DIRECTORY ../..)endfunction()@@ -34,6 +40,7 @@ function(CREATE_RUST_TEST EXAMPLE)add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} WORKING_DIRECTORY ../..)endif()endfunction()+function(CREATE_RUST_WASM EXAMPLE TARGET)add_custom_target(${EXAMPLE}-wasm-${TARGET} ALL COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET})endfunction()@@ -41,7 +48,7 @@ endfunction()# Enable testingenable_testing()-# Wasm files required by tests+# Wasm files required by tests, but only built if tests are actually being run.create_rust_wasm(fib-debug wasm32-unknown-unknown)create_rust_wasm(tokio wasm32-wasip1)create_rust_wasm(wasi wasm32-wasip1)@@ -80,24 +87,27 @@ create_target(wasip1 wasip1/main.c)create_target(wasip1-cpp wasip1/main.cc)# Rust examples/tests-create_rust_test(anyref)-create_rust_test(epochs)-create_rust_test(externref)-create_rust_test(fib-debug)-create_rust_test(fuel)-create_rust_test(gcd)-create_rust_test(hello)-create_rust_test(interrupt)-create_rust_test(linking)-create_rust_test(memory)-create_rust_test(multi)-create_rust_test(multimemory)-create_rust_test(serialize)-create_rust_test(threads)-create_rust_test(wasip1)-create_rust_test(wasip1-async)-create_rust_test(wasip2)-create_rust_test(wasip2-async)-create_rust_test(tokio wasi-common/tokio)-create_rust_test(component)-create_rust_test(resource-component)+if (BUILD_RUST_EXAMPLES)+ add_custom_target(rust-examples ALL COMMAND cargo build --examples WORKING_DIRECTORY ../..)+ create_rust_test(anyref)+ create_rust_test(epochs)+ create_rust_test(externref)+ create_rust_test(fib-debug)+ create_rust_test(fuel)+ create_rust_test(gcd)+ create_rust_test(hello)+ create_rust_test(interrupt)+ create_rust_test(linking)+ create_rust_test(memory)+ create_rust_test(multi)+ create_rust_test(multimemory)+ create_rust_test(serialize)+ create_rust_test(threads)+ create_rust_test(wasip1)+ create_rust_test(wasip1-async)+ create_rust_test(wasip2)+ create_rust_test(wasip2-async)+ create_rust_test(tokio wasi-common/tokio)+ create_rust_test(component)+ create_rust_test(resource-component)+endif()