From f353399db83270c7af7262e14ac7b651d872957b Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Sat, 2 Jul 2022 18:26:16 -0500 Subject: [PATCH 01/16] Add cmake compatibility to c-api --- .github/workflows/main.yml | 6 ++++ README.md | 2 +- crates/c-api/CMakeLists.txt | 57 +++++++++++++++++++++++++++++++++++++ examples/.gitignore | 1 + examples/CMakeLists.txt | 38 +++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 crates/c-api/CMakeLists.txt create mode 100644 examples/.gitignore create mode 100644 examples/CMakeLists.txt diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 63084b04f150..74f5affb2247 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -401,6 +401,12 @@ jobs: # Build `libwasmtime.so` - run: $CENTOS cargo build --release --manifest-path crates/c-api/Cargo.toml + # Prepare and build examples with both static and dynamic linking + - run: cmake -S ${{ github.workspace }}/examples -B ${{ github.workspace }}/examples/build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF + - run: cmake --build ${{ github.workspace }}/examples/build --config Release + - run: cmake -S ${{ github.workspace }}/examples -B ${{ github.workspace }}/examples/build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON + - run: cmake --build ${{ github.workspace }}/examples/build --config Release + # Assemble release artifats appropriate for this platform, then upload them # unconditionally to this workflow's files so we have a copy of them. - run: ./ci/build-tarballs.sh "${{ matrix.build }}" "${{ matrix.target }}" diff --git a/README.md b/README.md index cd9d3790565a..74daa0aeced1 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ You can use Wasmtime from a variety of different languages through embeddings of the implementation: * **[Rust]** - the [`wasmtime` crate] -* **[C]** - the [`wasm.h`, `wasi.h`, and `wasmtime.h` headers][c-headers] or use [`wasmtime` Conan package] +* **[C]** - the [`wasm.h`, `wasi.h`, and `wasmtime.h` headers][c-headers], [CMake](crates/c-api/CMakeLists.txt) or [`wasmtime` Conan package] * **C++** - the [`wasmtime-cpp` repository][wasmtime-cpp] or use [`wasmtime-cpp` Conan package] * **[Python]** - the [`wasmtime` PyPI package] * **[.NET]** - the [`Wasmtime` NuGet package] diff --git a/crates/c-api/CMakeLists.txt b/crates/c-api/CMakeLists.txt new file mode 100644 index 000000000000..3352c27f4001 --- /dev/null +++ b/crates/c-api/CMakeLists.txt @@ -0,0 +1,57 @@ +cmake_minimum_required(VERSION 3.10) + +option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) + +if (BUILD_SHARED_LIBS) + # Copy shared library into build directory + if(WIN32) + set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtime.dll + ${CMAKE_BINARY_DIR}) + elseif(APPLE) + set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.dylib + ${CMAKE_BINARY_DIR}) + else() + set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.so + ${CMAKE_BINARY_DIR}) + endif() +endif() + +include(ExternalProject) +ExternalProject_Add( + wasmtime-crate + DOWNLOAD_COMMAND "" + CONFIGURE_COMMAND "" + INSTALL_COMMAND "${WASMTIME_INSTALL_COMMAND}" + BUILD_COMMAND cargo build --release + BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR} + BUILD_ALWAYS ON) +add_library(wasmtime INTERFACE) +add_dependencies(wasmtime wasmtime-crate) + +if (BUILD_SHARED_LIBS) + if(WIN32) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtime.dll.lib) + elseif(APPLE) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.dylib) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'") + else() + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.so) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'") + endif() +else() + if(WIN32) + target_compile_options(wasmtime INTERFACE -DWASM_API_EXTERN= -DWASI_API_EXTERN=) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtime.lib + ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt) + elseif(APPLE) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.a) + else() + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.a + pthread dl m) + endif() +endif() + +target_include_directories(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/include) \ No newline at end of file diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 000000000000..c795b054e5ad --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 000000000000..47f1050ed069 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.10) +project(wasmtime-examples) + +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}) + + 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 /W4) + endif() + + set_target_properties(wasmtime-${TARGET} PROPERTIES + OUTPUT_NAME wasmtime-${TARGET} + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + CXX_VISIBILITY_PRESET hidden + POSITION_INDEPENDENT_CODE ON) + + target_include_directories(wasmtime-${TARGET} PUBLIC wasmtime) + target_link_libraries(wasmtime-${TARGET} PUBLIC wasmtime) +endfunction() + +# Add all examples +create_target(externref externref.c) +create_target(fib-debug fib-debug/main.c) +create_target(fuel fuel.c) +create_target(gcd gcd.c) +create_target(hello hello.c) +create_target(interrupt interrupt.c) +create_target(linking linking.c) +create_target(memory memory.c) +create_target(multi multi.c) +create_target(multimemory multimemory.c) +create_target(serialize serialize.c) +create_target(threads threads.c) +create_target(wasi wasi/main.c) \ No newline at end of file From f08fa51e362a022c01efd4f9a93f31fd29d71a2a Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Sat, 2 Jul 2022 18:47:02 -0500 Subject: [PATCH 02/16] Add CMake documentation to wasmtime.h --- .gitignore | 1 + crates/c-api/include/wasmtime.h | 39 ++++++++++++++++++++++----------- examples/.gitignore | 1 - 3 files changed, 27 insertions(+), 14 deletions(-) delete mode 100644 examples/.gitignore diff --git a/.gitignore b/.gitignore index 41bec43a188d..9cad237bc54f 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ foo .cargo publish vendor +examples/build diff --git a/crates/c-api/include/wasmtime.h b/crates/c-api/include/wasmtime.h index 1f20beac2000..79b4a6382e45 100644 --- a/crates/c-api/include/wasmtime.h +++ b/crates/c-api/include/wasmtime.h @@ -28,6 +28,22 @@ * as a `lib` directory with both a static archive and a dynamic library of * Wasmtime. You can link to either of them as you see fit. * + * ## Installing the C API through CMake + * + * CMake can be used to make the process of linking and compiling easier. An + * example of this if you have wasmtime as a git submodule at + * `third_party/wasmtime`: + * ``` + * add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/wasmtime/crates/c-api + * ${CMAKE_CURRENT_BINARY_DIR}/wasmtime) + * ... + * target_include_directories(YourProject PUBLIC wasmtime) + * target_link_libraries(YourProject PUBLIC wasmtime) + * ``` + * `BUILD_SHARED_LIBS` is provided as a define if you would like to build a + * shared library instead. You must distribute the appropriate shared library + * for your platform if you do this. + * * ## Linking against the C API * * You'll want to arrange the `include` directory of the C API to be in your @@ -41,7 +57,8 @@ * * * Linux - `-lpthread -ldl -lm` * * macOS - no extra flags needed - * * Windows - `ws2_32.lib advapi32.lib userenv.lib ntdll.lib shell32.lib ole32.lib bcrypt.lib` + * * Windows - `ws2_32.lib advapi32.lib userenv.lib ntdll.lib shell32.lib + * ole32.lib bcrypt.lib` * * ## Building from Source * @@ -166,8 +183,8 @@ #include #include -#include #include +#include #include #include #include @@ -187,11 +204,10 @@ extern "C" { /** * \brief Converts from the text format of WebAssembly to to the binary format. * - * \param wat this it the input pointer with the WebAssembly Text Format inside of - * it. This will be parsed and converted to the binary format. - * \param wat_len this it the length of `wat`, in bytes. - * \param ret if the conversion is successful, this byte vector is filled in with - * the WebAssembly binary format. + * \param wat this it the input pointer with the WebAssembly Text Format inside + * of it. This will be parsed and converted to the binary format. \param wat_len + * this it the length of `wat`, in bytes. \param ret if the conversion is + * successful, this byte vector is filled in with the WebAssembly binary format. * * \return a non-null error if parsing fails, or returns `NULL`. If parsing * fails then `ret` isn't touched. @@ -199,14 +215,11 @@ extern "C" { * This function does not take ownership of `wat`, and the caller is expected to * deallocate the returned #wasmtime_error_t and #wasm_byte_vec_t. */ -WASM_API_EXTERN wasmtime_error_t* wasmtime_wat2wasm( - const char *wat, - size_t wat_len, - wasm_byte_vec_t *ret -); +WASM_API_EXTERN wasmtime_error_t * +wasmtime_wat2wasm(const char *wat, size_t wat_len, wasm_byte_vec_t *ret); #ifdef __cplusplus -} // extern "C" +} // extern "C" #endif #endif // WASMTIME_API_H diff --git a/examples/.gitignore b/examples/.gitignore deleted file mode 100644 index c795b054e5ad..000000000000 --- a/examples/.gitignore +++ /dev/null @@ -1 +0,0 @@ -build \ No newline at end of file From eee4ad189873a815578b6294ce7b6193c7d9ead5 Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Sat, 2 Jul 2022 19:06:29 -0500 Subject: [PATCH 03/16] Add CMake instructions in examples --- crates/misc/run-examples/src/main.rs | 2 +- examples/README.md | 3 +-- examples/externref.c | 4 ++++ examples/fuel.c | 4 ++++ examples/gcd.c | 4 ++++ examples/hello.c | 4 ++++ examples/interrupt.c | 4 ++++ examples/linking.c | 4 ++++ examples/memory.c | 4 ++++ examples/multi.c | 4 ++++ examples/multimemory.c | 4 ++++ examples/serialize.c | 4 ++++ examples/threads.c | 24 ++++++++++++++++++++++++ examples/wasi/main.c | 4 ++++ 14 files changed, 70 insertions(+), 3 deletions(-) diff --git a/crates/misc/run-examples/src/main.rs b/crates/misc/run-examples/src/main.rs index fe54be0184d6..6131625ca956 100644 --- a/crates/misc/run-examples/src/main.rs +++ b/crates/misc/run-examples/src/main.rs @@ -22,7 +22,7 @@ fn main() -> anyhow::Result<()> { .current_dir("crates/c-api"))?; for (example, is_dir) in examples { - if example == "README" { + if example == "README" || example == "CMakeLists" { continue; } if let Some(example_to_run) = &example_to_run { diff --git a/examples/README.md b/examples/README.md index b888c322acd8..a1308481c8f7 100644 --- a/examples/README.md +++ b/examples/README.md @@ -8,7 +8,6 @@ Each example is available in both C and in Rust. Examples are accompanied with a `*.wat` file which is the wasm input, or a Rust project in a `wasm` folder which is the source code for the original wasm file. -Rust examples can be executed with `cargo run --example $name`, and C examples -need to be compiled using your system compiler and appropriate header files. +Rust examples can be executed with `cargo run --example $name`. C examples can be built with `mkdir build && cd build && cmake ..`. You can run `cmake --build .` to build all examples or `cmake --build . --target wasmtime-$name`, replacing the name as you wish. They can also be [built manually](https://docs.wasmtime.dev/c-api/). For more information see the examples themselves! diff --git a/examples/externref.c b/examples/externref.c index f240cdfb4a0b..b0107c8db437 100644 --- a/examples/externref.c +++ b/examples/externref.c @@ -15,6 +15,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations as well as the name of the `libwasmtime.a` file on Windows. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-externref */ #include diff --git a/examples/fuel.c b/examples/fuel.c index 3c5558cb3f8b..9d0fb3bc4610 100644 --- a/examples/fuel.c +++ b/examples/fuel.c @@ -15,6 +15,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-fuel */ #include diff --git a/examples/gcd.c b/examples/gcd.c index 714eb96263a1..cac7f6bb0dd4 100644 --- a/examples/gcd.c +++ b/examples/gcd.c @@ -15,6 +15,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-gcd */ #include diff --git a/examples/hello.c b/examples/hello.c index 5c24e18db370..5adf97b9b9ad 100644 --- a/examples/hello.c +++ b/examples/hello.c @@ -16,6 +16,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations as well as the name of the `libwasmtime.a` file on Windows. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-hello */ #include diff --git a/examples/interrupt.c b/examples/interrupt.c index f09e47ebe41e..900bd2b64cd5 100644 --- a/examples/interrupt.c +++ b/examples/interrupt.c @@ -16,6 +16,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations as well as the name of the `libwasmtime.a` file on Windows. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-interrupt */ #include diff --git a/examples/linking.c b/examples/linking.c index 330102c5c3c8..b32d46890c0a 100644 --- a/examples/linking.c +++ b/examples/linking.c @@ -15,6 +15,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-linking */ #include diff --git a/examples/memory.c b/examples/memory.c index e9cb221cde2c..f59a1afb389f 100644 --- a/examples/memory.c +++ b/examples/memory.c @@ -16,6 +16,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations. +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-memory + Also note that this example was taken from https://github.com/WebAssembly/wasm-c-api/blob/master/example/memory.c originally diff --git a/examples/multi.c b/examples/multi.c index b4b962ef2eaf..4039e2a89eb8 100644 --- a/examples/multi.c +++ b/examples/multi.c @@ -16,6 +16,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations. +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-multi + Also note that this example was taken from https://github.com/WebAssembly/wasm-c-api/blob/master/example/multi.c originally diff --git a/examples/multimemory.c b/examples/multimemory.c index 5d4ea982e30a..b350e7b681f1 100644 --- a/examples/multimemory.c +++ b/examples/multimemory.c @@ -14,6 +14,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-multimemory */ #include diff --git a/examples/serialize.c b/examples/serialize.c index e4f444764603..19052571d432 100644 --- a/examples/serialize.c +++ b/examples/serialize.c @@ -16,6 +16,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations as well as the name of the `libwasmtime.a` file on Windows. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-serialize */ #include diff --git a/examples/threads.c b/examples/threads.c index 0fbef5a4c3b3..707601be53cf 100644 --- a/examples/threads.c +++ b/examples/threads.c @@ -1,3 +1,27 @@ +/* +Example of instantiating of the WebAssembly module and invoking its exported +function in a separate thread. + +You can compile and run this example on Linux with: + + cargo build --release -p wasmtime-c-api + cc examples/threads.c \ + -I crates/c-api/include \ + -I crates/c-api/wasm-c-api/include \ + target/release/libwasmtime.a \ + -lpthread -ldl -lm \ + -o threads + ./threads + +Note that on Windows and macOS the command will be similar, but you'll need +to tweak the `-lpthread` and such annotations as well as the name of the +`libwasmtime.a` file on Windows. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-threads +*/ + #ifndef _WIN32 #include diff --git a/examples/wasi/main.c b/examples/wasi/main.c index b44e44308991..a52a0afc6830 100644 --- a/examples/wasi/main.c +++ b/examples/wasi/main.c @@ -14,6 +14,10 @@ You can compile and run this example on Linux with: Note that on Windows and macOS the command will be similar, but you'll need to tweak the `-lpthread` and such annotations. + +You can also build using cmake: + +mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-wasi */ #include From 0de7143e1430583937ed493093a2c964205502eb Mon Sep 17 00:00:00 2001 From: TheGreatRambler Date: Fri, 15 Jul 2022 02:06:57 -0500 Subject: [PATCH 04/16] Modify CI for CMake support --- .github/actions/free-disk-space/LICENSE | 201 ++++++++++++++++++ .github/actions/free-disk-space/action.yml | 12 ++ .github/actions/free-disk-space/main_linux.sh | 50 +++++ .../actions/free-disk-space/main_windows.bat | 11 + .github/workflows/main.yml | 18 +- crates/c-api/CMakeLists.txt | 27 ++- crates/c-api/include/wasmtime.h | 21 +- crates/misc/run-examples/src/main.rs | 78 +++---- examples/CMakeLists.txt | 5 +- 9 files changed, 338 insertions(+), 85 deletions(-) create mode 100644 .github/actions/free-disk-space/LICENSE create mode 100644 .github/actions/free-disk-space/action.yml create mode 100755 .github/actions/free-disk-space/main_linux.sh create mode 100644 .github/actions/free-disk-space/main_windows.bat diff --git a/.github/actions/free-disk-space/LICENSE b/.github/actions/free-disk-space/LICENSE new file mode 100644 index 000000000000..261eeb9e9f8b --- /dev/null +++ b/.github/actions/free-disk-space/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/.github/actions/free-disk-space/action.yml b/.github/actions/free-disk-space/action.yml new file mode 100644 index 000000000000..87e4b4fe4d3b --- /dev/null +++ b/.github/actions/free-disk-space/action.yml @@ -0,0 +1,12 @@ +name: 'Free Disk Space' +description: 'Deletes unneccesary packages to save disk space, sourced partially from https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh' + +runs: + using: composite + steps: + - run: ${GITHUB_ACTION_PATH//\\//}/main_linux.sh + if: runner.os == 'Linux' + shell: bash + - run: ${{ github.action_path }}/main_windows.bat + if: runner.os == 'Windows' + shell: cmd diff --git a/.github/actions/free-disk-space/main_linux.sh b/.github/actions/free-disk-space/main_linux.sh new file mode 100755 index 000000000000..c7356b284feb --- /dev/null +++ b/.github/actions/free-disk-space/main_linux.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# +# The Azure provided machines typically have the following disk allocation: +# Total space: 85GB +# Allocated: 67 GB +# Free: 17 GB +# This script frees up 28 GB of disk space by deleting unneeded packages and +# large directories. +# The Flink end to end tests download and generate more than 17 GB of files, +# causing unpredictable behavior and build failures. +# + +set -e + +echo "==============================================================================" +echo "Freeing up disk space on CI system" +echo "==============================================================================" + +echo "Listing 100 largest packages" +sudo apt install -y dpkg +dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 100 +df -h +echo "Removing large packages" +sudo apt remove -y '^dotnet-.*' +sudo apt remove -y '^mongodb-.*' +sudo apt remove -y '^mysql-.*' +sudo apt remove -y azure-cli google-cloud-sdk hhvm google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri +sudo apt autoremove -y +sudo apt clean +df -h +echo "Removing large directories" +# deleting 15GB +rm -rf /usr/share/dotnet/ +df -h diff --git a/.github/actions/free-disk-space/main_windows.bat b/.github/actions/free-disk-space/main_windows.bat new file mode 100644 index 000000000000..10b2a753a0b5 --- /dev/null +++ b/.github/actions/free-disk-space/main_windows.bat @@ -0,0 +1,11 @@ +:: Using https://github.com/actions/virtual-environments/blob/main/images/win/Windows2022-Readme.md +:: Remove databases +rmdir /s /q C:\PROGRA~1\POSTGR~1 +net stop MongoDB +rmdir /s /q C:\PROGRA~1\MongoDB +:: Remove browsers +rmdir /s /q C:\PROGRA~1\Google\Chrome +rmdir /s /q C:\PROGRA~1\MOZILL~1 +:: Remove other +net stop docker +rmdir /s /q C:\PROGRA~1\Docker \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 74f5affb2247..d09e1cdb64bd 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -221,6 +221,7 @@ jobs: - uses: actions/checkout@v2 with: submodules: true + - uses: ./.github/actions/free-disk-space - uses: ./.github/actions/install-rust # Install targets in order to build various tests throughout the repo @@ -324,8 +325,7 @@ jobs: with: submodules: true - run: rustup target add wasm32-wasi - - name: Install Rust - run: rustup update stable && rustup default stable + - uses: ./.github/actions/install-rust - run: ./ci/run-wasi-crypto-example.sh env: RUST_BACKTRACE: 1 @@ -338,8 +338,7 @@ jobs: with: submodules: true - run: rustup target add wasm32-wasi - - name: Install Rust - run: rustup update stable && rustup default stable + - uses: ./.github/actions/install-rust - run: cargo test --benches --release # Verify that cranelift's code generation is deterministic @@ -350,8 +349,7 @@ jobs: - uses: actions/checkout@v2 with: submodules: true - - name: Install Rust - run: rustup update stable && rustup default stable + - uses: ./.github/actions/install-rust - run: cd cranelift/codegen && cargo build --features all-arch - run: ci/ensure_deterministic_build.sh @@ -401,12 +399,6 @@ jobs: # Build `libwasmtime.so` - run: $CENTOS cargo build --release --manifest-path crates/c-api/Cargo.toml - # Prepare and build examples with both static and dynamic linking - - run: cmake -S ${{ github.workspace }}/examples -B ${{ github.workspace }}/examples/build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF - - run: cmake --build ${{ github.workspace }}/examples/build --config Release - - run: cmake -S ${{ github.workspace }}/examples -B ${{ github.workspace }}/examples/build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON - - run: cmake --build ${{ github.workspace }}/examples/build --config Release - # Assemble release artifats appropriate for this platform, then upload them # unconditionally to this workflow's files so we have a copy of them. - run: ./ci/build-tarballs.sh "${{ matrix.build }}" "${{ matrix.target }}" @@ -435,7 +427,7 @@ jobs: - uses: actions/checkout@v2 with: submodules: true - - run: rustup update stable && rustup default stable + - uses: ./.github/actions/install-rust - run: | cd ${{ runner.tool_cache }} curl -L https://github.com/mozilla/sccache/releases/download/0.2.13/sccache-0.2.13-x86_64-unknown-linux-musl.tar.gz | tar xzf - diff --git a/crates/c-api/CMakeLists.txt b/crates/c-api/CMakeLists.txt index 3352c27f4001..1e2ddd50f723 100644 --- a/crates/c-api/CMakeLists.txt +++ b/crates/c-api/CMakeLists.txt @@ -2,19 +2,26 @@ cmake_minimum_required(VERSION 3.10) option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) +if (CMAKE_BUILD_TYPE STREQUAL "Release") + set(WASMTIME_BUILD_TYPE_FLAG "--release") + set(WASMTIME_BUILD_TYPE "release") +else() + set(WASMTIME_BUILD_TYPE "debug") +endif() + if (BUILD_SHARED_LIBS) # Copy shared library into build directory if(WIN32) set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtime.dll + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.dll ${CMAKE_BINARY_DIR}) elseif(APPLE) set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.dylib + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.dylib ${CMAKE_BINARY_DIR}) else() set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.so + ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.so ${CMAKE_BINARY_DIR}) endif() endif() @@ -25,7 +32,7 @@ ExternalProject_Add( DOWNLOAD_COMMAND "" CONFIGURE_COMMAND "" INSTALL_COMMAND "${WASMTIME_INSTALL_COMMAND}" - BUILD_COMMAND cargo build --release + BUILD_COMMAND cargo build ${WASMTIME_BUILD_TYPE_FLAG} BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR} BUILD_ALWAYS ON) add_library(wasmtime INTERFACE) @@ -33,23 +40,23 @@ add_dependencies(wasmtime wasmtime-crate) if (BUILD_SHARED_LIBS) if(WIN32) - target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtime.dll.lib) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.dll.lib) elseif(APPLE) - target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.dylib) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.dylib) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'") else() - target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.so) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.so) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'") endif() else() if(WIN32) target_compile_options(wasmtime INTERFACE -DWASM_API_EXTERN= -DWASI_API_EXTERN=) - target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/wasmtime.lib + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.lib ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt) elseif(APPLE) - target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.a) + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.a) else() - target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/libwasmtime.a + target_link_libraries(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.a pthread dl m) endif() endif() diff --git a/crates/c-api/include/wasmtime.h b/crates/c-api/include/wasmtime.h index 79b4a6382e45..c70fd8b71339 100644 --- a/crates/c-api/include/wasmtime.h +++ b/crates/c-api/include/wasmtime.h @@ -57,8 +57,7 @@ * * * Linux - `-lpthread -ldl -lm` * * macOS - no extra flags needed - * * Windows - `ws2_32.lib advapi32.lib userenv.lib ntdll.lib shell32.lib - * ole32.lib bcrypt.lib` + * * Windows - `ws2_32.lib advapi32.lib userenv.lib ntdll.lib shell32.lib ole32.lib bcrypt.lib` * * ## Building from Source * @@ -204,10 +203,11 @@ extern "C" { /** * \brief Converts from the text format of WebAssembly to to the binary format. * - * \param wat this it the input pointer with the WebAssembly Text Format inside - * of it. This will be parsed and converted to the binary format. \param wat_len - * this it the length of `wat`, in bytes. \param ret if the conversion is - * successful, this byte vector is filled in with the WebAssembly binary format. + * \param wat this it the input pointer with the WebAssembly Text Format inside of + * it. This will be parsed and converted to the binary format. + * \param wat_len this it the length of `wat`, in bytes. + * \param ret if the conversion is successful, this byte vector is filled in with + * the WebAssembly binary format. * * \return a non-null error if parsing fails, or returns `NULL`. If parsing * fails then `ret` isn't touched. @@ -215,11 +215,14 @@ extern "C" { * This function does not take ownership of `wat`, and the caller is expected to * deallocate the returned #wasmtime_error_t and #wasm_byte_vec_t. */ -WASM_API_EXTERN wasmtime_error_t * -wasmtime_wat2wasm(const char *wat, size_t wat_len, wasm_byte_vec_t *ret); +WASM_API_EXTERN wasmtime_error_t* wasmtime_wat2wasm( + const char *wat, + size_t wat_len, + wasm_byte_vec_t *ret +); #ifdef __cplusplus -} // extern "C" +} // extern "C" #endif #endif // WASMTIME_API_H diff --git a/crates/misc/run-examples/src/main.rs b/crates/misc/run-examples/src/main.rs index 6131625ca956..08752703bb16 100644 --- a/crates/misc/run-examples/src/main.rs +++ b/crates/misc/run-examples/src/main.rs @@ -16,13 +16,14 @@ fn main() -> anyhow::Result<()> { examples.insert((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir)); } - println!("======== Building libwasmtime.a ==========="); - run(Command::new("cargo") - .args(&["build"]) - .current_dir("crates/c-api"))?; + println!("======== Prepare C/C++ CMake project ==========="); + run(Command::new("cmake") + .arg("-Sexamples") + .arg("-Bexamples/build") + .arg("-DBUILD_SHARED_LIBS=OFF"))?; for (example, is_dir) in examples { - if example == "README" || example == "CMakeLists" { + if example == "README" || example == "CMakeLists" || example == "build" { continue; } if let Some(example_to_run) = &example_to_run { @@ -54,58 +55,33 @@ fn main() -> anyhow::Result<()> { run(&mut cargo_cmd)?; println!("======== C/C++ example `{}` ============", example); - for extension in ["c", "cc"].iter() { - let mut cmd = cc::Build::new() - .opt_level(0) - .cargo_metadata(false) - .target(env!("TARGET")) - .host(env!("TARGET")) - .include("crates/c-api/include") - .include("crates/c-api/wasm-c-api/include") - .define("WASM_API_EXTERN", Some("")) // static linkage, not dynamic - .warnings(false) - .get_compiler() - .to_command(); - - let file = if is_dir { - format!("examples/{}/main.{}", example, extension) - } else { - format!("examples/{}.{}", example, extension) - }; - - if !std::path::Path::new(&file).exists() { - // C and C++ files are optional so we can skip them. - continue; - } + let c_file = format!("examples/{}.c", example); + if std::path::Path::new(&c_file).exists() { + run(Command::new("cmake") + .arg("--build") + .arg("examples/build") + .arg("--target") + .arg(format!("wasmtime-{}", example)) + .arg("--config") + .arg("Debug"))?; - cmd.arg(file); - let exe = if cfg!(windows) { - cmd.arg("target/debug/wasmtime.lib") - .arg("ws2_32.lib") - .arg("advapi32.lib") - .arg("userenv.lib") - .arg("ntdll.lib") - .arg("shell32.lib") - .arg("ole32.lib") - .arg("bcrypt.lib"); - if is_dir { - "./main.exe".to_string() - } else { - format!("./{}.exe", example) - } + if cfg!(windows) { + run(&mut Command::new(format!( + "examples/build/wasmtime-{}.exe", + example + )))?; } else { - cmd.arg("target/debug/libwasmtime.a").arg("-o").arg("foo"); - "./foo".to_string() + run(&mut Command::new(format!( + "examples/build/wasmtime-{}", + example + )))?; }; - if cfg!(target_os = "linux") { - cmd.arg("-lpthread").arg("-ldl").arg("-lm"); - } - run(&mut cmd)?; - - run(&mut Command::new(exe))?; } } + println!("======== Remove examples binaries ==========="); + std::fs::remove_dir_all("examples/build")?; + Ok(()) } diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 47f1050ed069..bd7dedc722d9 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -14,7 +14,7 @@ function(CREATE_TARGET TARGET TARGET_PATH) set_target_properties(wasmtime-${TARGET} PROPERTIES OUTPUT_NAME wasmtime-${TARGET} - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<0:> CXX_VISIBILITY_PRESET hidden POSITION_INDEPENDENT_CODE ON) @@ -35,4 +35,5 @@ create_target(multi multi.c) create_target(multimemory multimemory.c) create_target(serialize serialize.c) create_target(threads threads.c) -create_target(wasi wasi/main.c) \ No newline at end of file +create_target(tokio tokio/main.c) +create_target(wasi wasi/main.c) From e173d98f8649c0ac925d98ecd256bfb50f3235fb Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Fri, 15 Jul 2022 02:17:07 -0500 Subject: [PATCH 05/16] Use correct rust in CI --- .github/workflows/main.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d09e1cdb64bd..5260f8f89edc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -325,7 +325,8 @@ jobs: with: submodules: true - run: rustup target add wasm32-wasi - - uses: ./.github/actions/install-rust + - name: Install Rust + run: rustup update stable && rustup default stable - run: ./ci/run-wasi-crypto-example.sh env: RUST_BACKTRACE: 1 @@ -338,7 +339,8 @@ jobs: with: submodules: true - run: rustup target add wasm32-wasi - - uses: ./.github/actions/install-rust + - name: Install Rust + run: rustup update stable && rustup default stable - run: cargo test --benches --release # Verify that cranelift's code generation is deterministic @@ -349,7 +351,8 @@ jobs: - uses: actions/checkout@v2 with: submodules: true - - uses: ./.github/actions/install-rust + - name: Install Rust + run: rustup update stable && rustup default stable - run: cd cranelift/codegen && cargo build --features all-arch - run: ci/ensure_deterministic_build.sh @@ -427,7 +430,7 @@ jobs: - uses: actions/checkout@v2 with: submodules: true - - uses: ./.github/actions/install-rust + - run: rustup update stable && rustup default stable - run: | cd ${{ runner.tool_cache }} curl -L https://github.com/mozilla/sccache/releases/download/0.2.13/sccache-0.2.13-x86_64-unknown-linux-musl.tar.gz | tar xzf - From 27fce3a19bbfd4202dd0c1bae477b1a1842dde9b Mon Sep 17 00:00:00 2001 From: TheGreatRambler Date: Tue, 19 Jul 2022 03:18:00 -0500 Subject: [PATCH 06/16] Trigger build From 0becdc2dab14e580dc16ab5c0be0dfbf7c504792 Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Wed, 20 Jul 2022 18:19:51 -0500 Subject: [PATCH 07/16] Refactor run-examples --- Cargo.lock | 2 +- crates/misc/run-examples/Cargo.toml | 2 +- crates/misc/run-examples/build.rs | 7 -- crates/misc/run-examples/src/main.rs | 110 ++++++++++++++++----------- examples/CMakeLists.txt | 3 +- examples/tokio/main.c | 5 -- 6 files changed, 70 insertions(+), 59 deletions(-) delete mode 100644 crates/misc/run-examples/build.rs delete mode 100644 examples/tokio/main.c diff --git a/Cargo.lock b/Cargo.lock index cf4e6122a070..040f477f4245 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2432,7 +2432,7 @@ name = "run-examples" version = "0.19.0" dependencies = [ "anyhow", - "cc", + "serde_json", ] [[package]] diff --git a/crates/misc/run-examples/Cargo.toml b/crates/misc/run-examples/Cargo.toml index 01167683d698..61b24d372bd6 100644 --- a/crates/misc/run-examples/Cargo.toml +++ b/crates/misc/run-examples/Cargo.toml @@ -7,4 +7,4 @@ publish = false [dependencies] anyhow = "1.0.31" -cc = "1.0" +serde_json = "1.0" \ No newline at end of file diff --git a/crates/misc/run-examples/build.rs b/crates/misc/run-examples/build.rs deleted file mode 100644 index a2c6171e4a49..000000000000 --- a/crates/misc/run-examples/build.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - println!( - "cargo:rustc-env=TARGET={}", - std::env::var("TARGET").unwrap() - ); - println!("cargo:rerun-if-changed=build.rs"); -} diff --git a/crates/misc/run-examples/src/main.rs b/crates/misc/run-examples/src/main.rs index 08752703bb16..6ed10e46ae8f 100644 --- a/crates/misc/run-examples/src/main.rs +++ b/crates/misc/run-examples/src/main.rs @@ -1,20 +1,26 @@ use anyhow::Context; -use std::collections::BTreeSet; +use serde_json::Value; use std::process::Command; +use std::str::from_utf8; fn main() -> anyhow::Result<()> { - let example_to_run = std::env::args().nth(1); - let mut examples = BTreeSet::new(); - for e in std::fs::read_dir("examples")? { - let e = e?; - let path = e.path(); - let dir = e.metadata()?.is_dir(); - if let Some("wat") = path.extension().and_then(|s| s.to_str()) { - continue; - } - - examples.insert((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir)); - } + let mut rust_targets: Vec = Vec::new(); + match Command::new("cargo").arg("read-manifest").output() { + Ok(cargo_manifest_output) => match from_utf8(cargo_manifest_output.stdout.as_slice()) { + Ok(stdout) => { + let cargo_manifest: Value = serde_json::from_str(stdout)?; + for target in cargo_manifest["targets"].as_array().unwrap() { + let is_example = target["kind"].is_array() + && target["kind"].as_array().unwrap()[0].as_str().unwrap() == "example"; + if is_example { + rust_targets.push(target["name"].as_str().unwrap().to_string()); + } + } + } + Err(error) => panic!("Problem getting cargo manifest stdout: {:?}", error), + }, + Err(error) => panic!("Problem getting cargo manifest: {:?}", error), + }; println!("======== Prepare C/C++ CMake project ==========="); run(Command::new("cmake") @@ -22,16 +28,35 @@ fn main() -> anyhow::Result<()> { .arg("-Bexamples/build") .arg("-DBUILD_SHARED_LIBS=OFF"))?; - for (example, is_dir) in examples { - if example == "README" || example == "CMakeLists" || example == "build" { - continue; - } - if let Some(example_to_run) = &example_to_run { - if !example.contains(&example_to_run[..]) { - continue; + let mut c_targets: Vec = Vec::new(); + match Command::new("cmake") + .arg("--build") + .arg("examples/build") + .arg("--target") + .arg("help") + .output() + { + Ok(cmake_help_output) => match from_utf8(cmake_help_output.stdout.as_slice()) { + Ok(stdout) => { + for possible_target_line in stdout.lines() { + let possible_location = possible_target_line.find("wasmtime-"); + if let Some(location) = possible_location { + let line = &possible_target_line + [(location + "wasmtime-".len())..possible_target_line.len()]; + // "crate" is the wasmtime-c-api itself + if line != "crate" { + c_targets.push(line.to_string()); + } + } + } } - } - if is_dir { + Err(error) => panic!("Problem getting cmake help stdout: {:?}", error), + }, + Err(error) => panic!("Problem getting cmake help: {:?}", error), + }; + + for example in rust_targets { + if example == "fib-debug" || example == "tokio" || example == "wasi" { println!("======== Rust wasm file `{}` ============", example); let target = if example == "fib-debug" { "wasm32-unknown-unknown" @@ -53,30 +78,29 @@ fn main() -> anyhow::Result<()> { cargo_cmd.arg("--features").arg("wasmtime-wasi/tokio"); } run(&mut cargo_cmd)?; + } + for example in c_targets { println!("======== C/C++ example `{}` ============", example); - let c_file = format!("examples/{}.c", example); - if std::path::Path::new(&c_file).exists() { - run(Command::new("cmake") - .arg("--build") - .arg("examples/build") - .arg("--target") - .arg(format!("wasmtime-{}", example)) - .arg("--config") - .arg("Debug"))?; + run(Command::new("cmake") + .arg("--build") + .arg("examples/build") + .arg("--target") + .arg(format!("wasmtime-{}", example)) + .arg("--config") + .arg("Debug"))?; - if cfg!(windows) { - run(&mut Command::new(format!( - "examples/build/wasmtime-{}.exe", - example - )))?; - } else { - run(&mut Command::new(format!( - "examples/build/wasmtime-{}", - example - )))?; - }; - } + if cfg!(windows) { + run(&mut Command::new(format!( + "examples/build/wasmtime-{}.exe", + example + )))?; + } else { + run(&mut Command::new(format!( + "examples/build/wasmtime-{}", + example + )))?; + }; } println!("======== Remove examples binaries ==========="); diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index bd7dedc722d9..ea09ebc7fe20 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -9,7 +9,7 @@ function(CREATE_TARGET 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 /W4) + target_compile_options(wasmtime-${TARGET} PRIVATE /W3) endif() set_target_properties(wasmtime-${TARGET} PROPERTIES @@ -35,5 +35,4 @@ create_target(multi multi.c) create_target(multimemory multimemory.c) create_target(serialize serialize.c) create_target(threads threads.c) -create_target(tokio tokio/main.c) create_target(wasi wasi/main.c) diff --git a/examples/tokio/main.c b/examples/tokio/main.c deleted file mode 100644 index f6920d8bae4a..000000000000 --- a/examples/tokio/main.c +++ /dev/null @@ -1,5 +0,0 @@ -int main(int argc, char *argv[]) { - // This example is specific to integrating with Rust's tokio ecosystem, so - // it isnt applicable to C/C++. - return 0; -} From 587be9e526e8f125e3872c99d82569b4987248ad Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Wed, 20 Jul 2022 19:17:37 -0500 Subject: [PATCH 08/16] Reintroduce example_to_run in run-examples --- crates/misc/run-examples/src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/misc/run-examples/src/main.rs b/crates/misc/run-examples/src/main.rs index 6ed10e46ae8f..38ae061e0f21 100644 --- a/crates/misc/run-examples/src/main.rs +++ b/crates/misc/run-examples/src/main.rs @@ -4,6 +4,8 @@ use std::process::Command; use std::str::from_utf8; fn main() -> anyhow::Result<()> { + let example_to_run = std::env::args().nth(1); + let mut rust_targets: Vec = Vec::new(); match Command::new("cargo").arg("read-manifest").output() { Ok(cargo_manifest_output) => match from_utf8(cargo_manifest_output.stdout.as_slice()) { @@ -22,6 +24,11 @@ fn main() -> anyhow::Result<()> { Err(error) => panic!("Problem getting cargo manifest: {:?}", error), }; + if let Some(example) = &example_to_run { + // If explicit example is provided, use that instead + rust_targets.retain(|e| *e == *example); + } + println!("======== Prepare C/C++ CMake project ==========="); run(Command::new("cmake") .arg("-Sexamples") @@ -55,6 +62,11 @@ fn main() -> anyhow::Result<()> { Err(error) => panic!("Problem getting cmake help: {:?}", error), }; + if let Some(example) = &example_to_run { + // If explicit example is provided, use that instead + c_targets.retain(|e| *e == *example); + } + for example in rust_targets { if example == "fib-debug" || example == "tokio" || example == "wasi" { println!("======== Rust wasm file `{}` ============", example); From 49b867d1be14deefe43403e088273b3ad95349ca Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Thu, 21 Jul 2022 15:43:33 -0500 Subject: [PATCH 09/16] Replace run-examples crate with cmake --- .github/actions/free-disk-space/LICENSE | 201 ------------------ .github/actions/free-disk-space/action.yml | 12 -- .github/actions/free-disk-space/main_linux.sh | 50 ----- .../actions/free-disk-space/main_windows.bat | 11 - .github/workflows/main.yml | 11 +- Cargo.lock | 8 - Cargo.toml | 1 - crates/misc/run-examples/Cargo.toml | 10 - crates/misc/run-examples/src/main.rs | 133 ------------ examples/CMakeLists.txt | 35 +++ 10 files changed, 43 insertions(+), 429 deletions(-) delete mode 100644 .github/actions/free-disk-space/LICENSE delete mode 100644 .github/actions/free-disk-space/action.yml delete mode 100755 .github/actions/free-disk-space/main_linux.sh delete mode 100644 .github/actions/free-disk-space/main_windows.bat delete mode 100644 crates/misc/run-examples/Cargo.toml delete mode 100644 crates/misc/run-examples/src/main.rs diff --git a/.github/actions/free-disk-space/LICENSE b/.github/actions/free-disk-space/LICENSE deleted file mode 100644 index 261eeb9e9f8b..000000000000 --- a/.github/actions/free-disk-space/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/.github/actions/free-disk-space/action.yml b/.github/actions/free-disk-space/action.yml deleted file mode 100644 index 87e4b4fe4d3b..000000000000 --- a/.github/actions/free-disk-space/action.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: 'Free Disk Space' -description: 'Deletes unneccesary packages to save disk space, sourced partially from https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh' - -runs: - using: composite - steps: - - run: ${GITHUB_ACTION_PATH//\\//}/main_linux.sh - if: runner.os == 'Linux' - shell: bash - - run: ${{ github.action_path }}/main_windows.bat - if: runner.os == 'Windows' - shell: cmd diff --git a/.github/actions/free-disk-space/main_linux.sh b/.github/actions/free-disk-space/main_linux.sh deleted file mode 100755 index c7356b284feb..000000000000 --- a/.github/actions/free-disk-space/main_linux.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -# -# The Azure provided machines typically have the following disk allocation: -# Total space: 85GB -# Allocated: 67 GB -# Free: 17 GB -# This script frees up 28 GB of disk space by deleting unneeded packages and -# large directories. -# The Flink end to end tests download and generate more than 17 GB of files, -# causing unpredictable behavior and build failures. -# - -set -e - -echo "==============================================================================" -echo "Freeing up disk space on CI system" -echo "==============================================================================" - -echo "Listing 100 largest packages" -sudo apt install -y dpkg -dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 100 -df -h -echo "Removing large packages" -sudo apt remove -y '^dotnet-.*' -sudo apt remove -y '^mongodb-.*' -sudo apt remove -y '^mysql-.*' -sudo apt remove -y azure-cli google-cloud-sdk hhvm google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri -sudo apt autoremove -y -sudo apt clean -df -h -echo "Removing large directories" -# deleting 15GB -rm -rf /usr/share/dotnet/ -df -h diff --git a/.github/actions/free-disk-space/main_windows.bat b/.github/actions/free-disk-space/main_windows.bat deleted file mode 100644 index 10b2a753a0b5..000000000000 --- a/.github/actions/free-disk-space/main_windows.bat +++ /dev/null @@ -1,11 +0,0 @@ -:: Using https://github.com/actions/virtual-environments/blob/main/images/win/Windows2022-Readme.md -:: Remove databases -rmdir /s /q C:\PROGRA~1\POSTGR~1 -net stop MongoDB -rmdir /s /q C:\PROGRA~1\MongoDB -:: Remove browsers -rmdir /s /q C:\PROGRA~1\Google\Chrome -rmdir /s /q C:\PROGRA~1\MOZILL~1 -:: Remove other -net stop docker -rmdir /s /q C:\PROGRA~1\Docker \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 92b10e7e2491..9a930ca58770 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -217,7 +217,6 @@ jobs: - uses: actions/checkout@v2 with: submodules: true - - uses: ./.github/actions/free-disk-space - uses: ./.github/actions/install-rust # Install targets in order to build various tests throughout the repo @@ -274,8 +273,14 @@ jobs: touch ${{ runner.tool_cache }}/qemu/built if: matrix.gcc != '' - # Ensure all our examples build and execute - - run: cargo run -p run-examples + # Prepare tests in CMake + - run: cmake -Sexamples -Bexamples/build -DBUILD_SHARED_LIBS=OFF + if: matrix.target == '' + # Build tests + - run: cmake --build examples/build --config Debug + if: matrix.target == '' + # Run tests + - run: cmake --build examples/build --target test -- ARGS="-j4 --output-on-failure" env: RUST_BACKTRACE: 1 if: matrix.target == '' diff --git a/Cargo.lock b/Cargo.lock index 040f477f4245..6024799b88c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2427,14 +2427,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "run-examples" -version = "0.19.0" -dependencies = [ - "anyhow", - "serde_json", -] - [[package]] name = "rustc-demangle" version = "0.1.21" diff --git a/Cargo.toml b/Cargo.toml index e1e19a871f1f..ef7dcca4307d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,7 +81,6 @@ members = [ "crates/bench-api", "crates/c-api", "crates/cli-flags", - "crates/misc/run-examples", "examples/fib-debug/wasm", "examples/wasi/wasm", "examples/tokio/wasm", diff --git a/crates/misc/run-examples/Cargo.toml b/crates/misc/run-examples/Cargo.toml deleted file mode 100644 index 61b24d372bd6..000000000000 --- a/crates/misc/run-examples/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "run-examples" -version = "0.19.0" -authors = ["The Wasmtime Project Developers"] -edition = "2021" -publish = false - -[dependencies] -anyhow = "1.0.31" -serde_json = "1.0" \ No newline at end of file diff --git a/crates/misc/run-examples/src/main.rs b/crates/misc/run-examples/src/main.rs deleted file mode 100644 index 38ae061e0f21..000000000000 --- a/crates/misc/run-examples/src/main.rs +++ /dev/null @@ -1,133 +0,0 @@ -use anyhow::Context; -use serde_json::Value; -use std::process::Command; -use std::str::from_utf8; - -fn main() -> anyhow::Result<()> { - let example_to_run = std::env::args().nth(1); - - let mut rust_targets: Vec = Vec::new(); - match Command::new("cargo").arg("read-manifest").output() { - Ok(cargo_manifest_output) => match from_utf8(cargo_manifest_output.stdout.as_slice()) { - Ok(stdout) => { - let cargo_manifest: Value = serde_json::from_str(stdout)?; - for target in cargo_manifest["targets"].as_array().unwrap() { - let is_example = target["kind"].is_array() - && target["kind"].as_array().unwrap()[0].as_str().unwrap() == "example"; - if is_example { - rust_targets.push(target["name"].as_str().unwrap().to_string()); - } - } - } - Err(error) => panic!("Problem getting cargo manifest stdout: {:?}", error), - }, - Err(error) => panic!("Problem getting cargo manifest: {:?}", error), - }; - - if let Some(example) = &example_to_run { - // If explicit example is provided, use that instead - rust_targets.retain(|e| *e == *example); - } - - println!("======== Prepare C/C++ CMake project ==========="); - run(Command::new("cmake") - .arg("-Sexamples") - .arg("-Bexamples/build") - .arg("-DBUILD_SHARED_LIBS=OFF"))?; - - let mut c_targets: Vec = Vec::new(); - match Command::new("cmake") - .arg("--build") - .arg("examples/build") - .arg("--target") - .arg("help") - .output() - { - Ok(cmake_help_output) => match from_utf8(cmake_help_output.stdout.as_slice()) { - Ok(stdout) => { - for possible_target_line in stdout.lines() { - let possible_location = possible_target_line.find("wasmtime-"); - if let Some(location) = possible_location { - let line = &possible_target_line - [(location + "wasmtime-".len())..possible_target_line.len()]; - // "crate" is the wasmtime-c-api itself - if line != "crate" { - c_targets.push(line.to_string()); - } - } - } - } - Err(error) => panic!("Problem getting cmake help stdout: {:?}", error), - }, - Err(error) => panic!("Problem getting cmake help: {:?}", error), - }; - - if let Some(example) = &example_to_run { - // If explicit example is provided, use that instead - c_targets.retain(|e| *e == *example); - } - - for example in rust_targets { - if example == "fib-debug" || example == "tokio" || example == "wasi" { - println!("======== Rust wasm file `{}` ============", example); - let target = if example == "fib-debug" { - "wasm32-unknown-unknown" - } else { - "wasm32-wasi" - }; - run(Command::new("cargo") - .arg("build") - .arg("-p") - .arg(format!("example-{}-wasm", example)) - .arg("--target") - .arg(target))?; - } - println!("======== Rust example `{}` ============", example); - let mut cargo_cmd = Command::new("cargo"); - cargo_cmd.arg("run").arg("--example").arg(&example); - - if example.contains("tokio") { - cargo_cmd.arg("--features").arg("wasmtime-wasi/tokio"); - } - run(&mut cargo_cmd)?; - } - - for example in c_targets { - println!("======== C/C++ example `{}` ============", example); - run(Command::new("cmake") - .arg("--build") - .arg("examples/build") - .arg("--target") - .arg(format!("wasmtime-{}", example)) - .arg("--config") - .arg("Debug"))?; - - if cfg!(windows) { - run(&mut Command::new(format!( - "examples/build/wasmtime-{}.exe", - example - )))?; - } else { - run(&mut Command::new(format!( - "examples/build/wasmtime-{}", - example - )))?; - }; - } - - println!("======== Remove examples binaries ==========="); - std::fs::remove_dir_all("examples/build")?; - - Ok(()) -} - -fn run(cmd: &mut Command) -> anyhow::Result<()> { - (|| -> anyhow::Result<()> { - let s = cmd.status()?; - if !s.success() { - anyhow::bail!("Exited with failure status: {}", s); - } - Ok(()) - })() - .with_context(|| format!("failed to run `{:?}`", cmd)) -} diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index ea09ebc7fe20..afaca67633a4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -20,8 +20,23 @@ 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 $ 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() +endfunction() +function(CREATE_RUST_TEST_WASM EXAMPLE TARGET) + add_test(NAME ${EXAMPLE}-rust-wasm COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET} WORKING_DIRECTORY ../..) +endfunction() + +# Enable testing +enable_testing() + # Add all examples create_target(externref externref.c) create_target(fib-debug fib-debug/main.c) @@ -36,3 +51,23 @@ create_target(multimemory multimemory.c) create_target(serialize serialize.c) create_target(threads threads.c) create_target(wasi wasi/main.c) + +# Add rust tests +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(wasi) +create_rust_test(tokio wasmtime-wasi/tokio) +create_rust_test_wasm(fib-debug wasm32-unknown-unknown) +create_rust_test_wasm(tokio wasm32-wasi) +create_rust_test_wasm(wasi wasm32-wasi) \ No newline at end of file From fed50b8402513ca96ee9ce01ef76da96a2f92bcb Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Thu, 21 Jul 2022 15:48:28 -0500 Subject: [PATCH 10/16] Fix markdown formatting in examples readme --- examples/README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index a1308481c8f7..e37175bf24b1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -8,6 +8,10 @@ Each example is available in both C and in Rust. Examples are accompanied with a `*.wat` file which is the wasm input, or a Rust project in a `wasm` folder which is the source code for the original wasm file. -Rust examples can be executed with `cargo run --example $name`. C examples can be built with `mkdir build && cd build && cmake ..`. You can run `cmake --build .` to build all examples or `cmake --build . --target wasmtime-$name`, replacing the name as you wish. They can also be [built manually](https://docs.wasmtime.dev/c-api/). +Rust examples can be executed with `cargo run --example $name`. C examples can +be built with `mkdir build && cd build && cmake ..`. You can run +`cmake --build .` to build all examples or +`cmake --build . --target wasmtime-$name`, replacing the name as you wish. They +can also be [built manually](https://docs.wasmtime.dev/c-api/). For more information see the examples themselves! From e4ef91ef8be4176e15eea92274d09e00b1b63c49 Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Thu, 21 Jul 2022 18:34:58 -0500 Subject: [PATCH 11/16] Fix cmake test quotes --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9a930ca58770..058a4b7d1c36 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -280,7 +280,7 @@ jobs: - run: cmake --build examples/build --config Debug if: matrix.target == '' # Run tests - - run: cmake --build examples/build --target test -- ARGS="-j4 --output-on-failure" + - run: cmake --build examples/build --target test -- ARGS='--output-on-failure' env: RUST_BACKTRACE: 1 if: matrix.target == '' From 47ad7a7869c50e99285ba2b4930ffb5d7ab5606c Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Thu, 21 Jul 2022 19:15:01 -0500 Subject: [PATCH 12/16] Build rust wasm before cmake tests --- .github/workflows/main.yml | 2 +- examples/CMakeLists.txt | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 058a4b7d1c36..1f088574637b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -280,7 +280,7 @@ jobs: - run: cmake --build examples/build --config Debug if: matrix.target == '' # Run tests - - run: cmake --build examples/build --target test -- ARGS='--output-on-failure' + - run: cmake --build examples/build --target test -- ARGS='-j4 --output-on-failure' env: RUST_BACKTRACE: 1 if: matrix.target == '' diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index afaca67633a4..8e61cff315c7 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -30,8 +30,8 @@ function(CREATE_RUST_TEST EXAMPLE) add_test(NAME ${EXAMPLE}-rust COMMAND cargo run --example ${EXAMPLE} WORKING_DIRECTORY ../..) endif() endfunction() -function(CREATE_RUST_TEST_WASM EXAMPLE TARGET) - add_test(NAME ${EXAMPLE}-rust-wasm COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET} WORKING_DIRECTORY ../..) +function(CREATE_RUST_WASM EXAMPLE TARGET) + execute_process(COMMAND cargo build -p example-${EXAMPLE}-wasm --target ${TARGET}) endfunction() # Enable testing @@ -53,6 +53,9 @@ create_target(threads threads.c) create_target(wasi wasi/main.c) # Add rust tests +create_rust_wasm(fib-debug wasm32-unknown-unknown) +create_rust_wasm(tokio wasm32-wasi) +create_rust_wasm(wasi wasm32-wasi) create_rust_test(epochs) create_rust_test(externref) create_rust_test(fib-debug) @@ -67,7 +70,4 @@ create_rust_test(multimemory) create_rust_test(serialize) create_rust_test(threads) create_rust_test(wasi) -create_rust_test(tokio wasmtime-wasi/tokio) -create_rust_test_wasm(fib-debug wasm32-unknown-unknown) -create_rust_test_wasm(tokio wasm32-wasi) -create_rust_test_wasm(wasi wasm32-wasi) \ No newline at end of file +create_rust_test(tokio wasmtime-wasi/tokio) \ No newline at end of file From f2e1c3deecb325f6cf790c32b290e61fc35c5d23 Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Thu, 21 Jul 2022 19:47:01 -0500 Subject: [PATCH 13/16] Pass CTEST_OUTPUT_ON_FAILURE --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1f088574637b..229abc3e4f1e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -280,7 +280,7 @@ jobs: - run: cmake --build examples/build --config Debug if: matrix.target == '' # Run tests - - run: cmake --build examples/build --target test -- ARGS='-j4 --output-on-failure' + - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --target test env: RUST_BACKTRACE: 1 if: matrix.target == '' From db076bee8d0abce1a15f3303290bff8ea4a47a30 Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Thu, 21 Jul 2022 20:05:25 -0500 Subject: [PATCH 14/16] Another cmake test --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 229abc3e4f1e..f5523e152d38 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -280,7 +280,7 @@ jobs: - run: cmake --build examples/build --config Debug if: matrix.target == '' # Run tests - - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --target test + - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target test env: RUST_BACKTRACE: 1 if: matrix.target == '' From e83fcaac8c697a7c678f3a540ded8a15b76d833b Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Thu, 21 Jul 2022 20:53:12 -0500 Subject: [PATCH 15/16] Handle os differences in cmake test --- .github/workflows/main.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f5523e152d38..fa069b142151 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -280,10 +280,14 @@ jobs: - run: cmake --build examples/build --config Debug if: matrix.target == '' # Run tests + - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target RUN_TESTS + env: + RUST_BACKTRACE: 1 + if: matrix.target == '' && matrix.os == 'windows-2019' - run: cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build examples/build --config Debug --target test env: RUST_BACKTRACE: 1 - if: matrix.target == '' + if: matrix.target == '' && matrix.os != 'windows-2019' # Build and test all features - run: ./ci/run-tests.sh --locked From e25ce069b76c5ea352e3f41b21d863c5a34c8f97 Mon Sep 17 00:00:00 2001 From: TheGreatRambler <31906920+TheGreatRambler@users.noreply.github.com> Date: Fri, 22 Jul 2022 02:00:56 -0500 Subject: [PATCH 16/16] Fix bugs in memory and multimemory examples --- examples/memory.c | 2 +- examples/multimemory.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/memory.c b/examples/memory.c index f59a1afb389f..37eaf0e13744 100644 --- a/examples/memory.c +++ b/examples/memory.c @@ -224,7 +224,7 @@ int main(int argc, const char* argv[]) { // Grow memory. printf("Growing memory...\n"); - uint32_t old_size; + uint64_t old_size; error = wasmtime_memory_grow(context, &memory, 1, &old_size); if (error != NULL) exit_with_error("failed to grow memory", error, trap); diff --git a/examples/multimemory.c b/examples/multimemory.c index b350e7b681f1..3f1823d6d326 100644 --- a/examples/multimemory.c +++ b/examples/multimemory.c @@ -268,7 +268,7 @@ int main(int argc, const char* argv[]) { // Grow memory. printf("Growing memory...\n"); - uint32_t old_size; + uint64_t old_size; error = wasmtime_memory_grow(context, &memory0, 1, &old_size); if (error != NULL) exit_with_error("failed to grow memory", error, trap);