### WASI SDK Version Information Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md An example of the content found in the VERSION file, detailing component versions. ```text 27.0 wasi-libc: a1b2c3d llvm: e4f5g6h llvm-version: 18.1.0 config: i7j8k9l ``` -------------------------------- ### Basic C Compilation Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/INDEX.md A simple example of compiling a C file to a WebAssembly module using clang. ```bash clang hello.c -o hello.wasm ``` -------------------------------- ### Prerelease Version Examples Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Examples of prerelease version formats used for testing before a stable release. ```text 27.0-rc1 27.0-beta 27.0-alpha ``` -------------------------------- ### Shell Code Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/README.md Demonstrates a basic shell command. Use ```bash``` for shell code examples. ```bash ```bash ``` ``` -------------------------------- ### CMake Configuration Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/INDEX.md Demonstrates the basic syntax for configuring a CMake build with options. ```bash cmake -DOPTION=VALUE ``` -------------------------------- ### Compiler-rt Installation Paths Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Shows the default and alternative installation paths for compiler-rt libraries within the clang resource directory. ```bash ${CLANG_RESOURCE_DIR}/lib/wasm32-unknown-${target}/libclang_rt.*.a ``` ```bash ${CMAKE_INSTALL_PREFIX}/clang-resource-dir/lib/wasm32-unknown-${target}/ ``` -------------------------------- ### Clang Configuration File Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md Example content for a Clang configuration file used by WASI SDK. These files set default compiler flags, including resource directory and sysroot paths. ```text -resource-dir ${WASI_SDK_PREFIX}/clang-resource-dir --sysroot=${WASI_SDK_PREFIX}/share/wasi-sysroot ``` -------------------------------- ### Build and Install a Library for WASI Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Build a C library for WASI and install it into the WASI SDK sysroot. Ensure the library is compiled with the correct host target. ```bash ./configure --host=wasm32-wasip1 make make install DESTDIR=${WASI_SDK_PATH}/share/wasi-sysroot ``` -------------------------------- ### CMake Code Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/README.md Demonstrates a basic CMake code block. Use ```cmake``` for CMake code examples. ```cmake ```cmake ``` ``` -------------------------------- ### WASI SDK Version Examples Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Illustrates the conversion of Git tag format to semantic versioning for WASI SDK. ```text wasi-sdk-27-0-0-gABCD123 → 27.0 wasi-sdk-27-1-0-gABCD123 → 27.1 wasi-sdk-27-1-gABCD123 → 27.1gABCD123 (development) wasi-sdk-23-tag-0-gABCD123 → 23.0-tag wasi-sdk-23-some-tag-0-gABCD123 → 23.0-some-tag ``` -------------------------------- ### Development Version Examples Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Examples of development version formats, indicating commits after a tag, with or without uncommitted changes. ```text 27.1g317548590b40 # 1 commit after 27.0 tag 27.1g317548590b40+m # With uncommitted changes ``` -------------------------------- ### C Code Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/README.md Demonstrates a basic C code block. Use ```c``` for C code examples. ```c ```c ``` ``` -------------------------------- ### Build C Threading Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/compiler-targets.md Build the C threading example using clang with the wasm32-wasip1-threads target, including necessary flags for threading and memory. ```bash ${SDK}/bin/clang --target=wasm32-wasip1-threads \ -pthread \ -Wl,--import-memory,--export-memory \ hello.c -o hello.wasm ``` -------------------------------- ### Set Installation Prefix Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Specify the installation directory for the WASI SDK using CMake. This determines the root path for all installed components. ```bash -DCMAKE_INSTALL_PREFIX=/opt/wasi-sdk-27 ``` -------------------------------- ### Python Code Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/README.md Demonstrates a basic Python code block. Use ```python``` for Python code examples. ```python ```python ``` ``` -------------------------------- ### Install and Run WASM with Wasmtime Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Install the Wasmtime runtime using cargo and then execute a WASM file. This is a straightforward way to run WASM binaries. ```bash # Install wasmtime cargo install wasmtime # Run WASM wasmtime hello.wasm ``` -------------------------------- ### C Threading Usage Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/compiler-targets.md A basic C example demonstrating thread creation and joining using the `threads.h` library for the wasm32-wasip1-threads target. ```c #include int thread_func(void *arg) { printf("Hello from thread\n"); return 0; } int main() { thrd_t thread; thrd_create(&thread, thread_func, NULL); thrd_join(thread, NULL); return 0; } ``` -------------------------------- ### C++ Code Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/README.md Demonstrates a basic C++ code block. Use ```cpp``` for C++ code examples. ```cpp ```cpp ``` ``` -------------------------------- ### Combined Flag Example for SetJmp/LongJmp Compilation Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/setjmp-longjmp.md This example shows how to combine common flags for both compilation and linking when using setjmp/longjmp with clang. It demonstrates a typical command-line invocation for building a WebAssembly module that utilizes these functions. ```bash COMMON_FLAGS="-mllvm -wasm-enable-sjlj -mllvm -wasm-use-legacy-eh=false" ${SDK}/bin/clang $COMMON_FLAGS hello.c -lsetjmp $COMMON_FLAGS -o hello.wasm ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md Configure a basic C project with CMake, specifying the minimum version and project name. Includes a comment on how to use a WASI SDK toolchain file. ```cmake cmake_minimum_required(VERSION 3.16) project(wasi-project C) # Use toolchain file # cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/wasi-sdk-p1.cmake .. ``` -------------------------------- ### Install WASI SDK Binaries Source: https://github.com/webassembly/wasi-sdk/blob/main/README.md Download and extract the WASI SDK release binaries for your system. Ensure you set the correct WASI_OS and WASI_ARCH variables. ```shell WASI_OS=linux WASI_ARCH=x86_64 # or 'arm64' if running on arm64 host WASI_VERSION=27 WASI_VERSION_FULL=${WASI_VERSION}.0 wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz tar xvf wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz ``` -------------------------------- ### Link with wasm-component-ld for wasm32-wasip2 Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/compiler-targets.md Example of using the component model linker with wasm32-wasip2. ```bash ${SDK}/bin/wasm-component-ld ... # Component model linker ``` -------------------------------- ### Verify WASI SDK Installation Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Check that the clang and wasm-ld commands are accessible and show their version information after setting up the environment. ```bash clang --version # Should show LLVM/Clang version wasm-ld --version # Should show wasm-ld version ``` -------------------------------- ### WASI SDK Build Configuration Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/PROJECT_OVERVIEW.md This CMakeLists.txt file is the main build configuration for the WASI SDK project. ```cmake wasi-sdk/ ├── CMakeLists.txt # Main build configuration ├── cmake/ # CMake helper modules │ ├── wasi-sdk-toolchain.cmake # Toolchain build logic │ ├── wasi-sdk-sysroot.cmake # Sysroot build logic │ ├── wasi-sdk-dist.cmake # Distribution tarball creation │ ├── Platform/WASI.cmake # WASI platform definition │ └── wasi-sdk-enable-ccache.cmake ├── src/ # Source submodules and patches │ ├── llvm-project/ # LLVM submodule │ ├── wasi-libc/ # wasi-libc submodule │ ├── config/ # Configuration helpers │ └── *.patch # Applied upstream patches ├── wasi-sdk-p1.cmake # WASIp1 toolchain file ├── wasi-sdk-p2.cmake # WASIp2 toolchain file ├── wasi-sdk-p3.cmake # WASIp3 toolchain file ├── wasi-sdk-pthread.cmake # Threading toolchain file ├── version.py # Version management script ├── ci/ # Continuous integration scripts ├── tests/ # Compilation tests └── README.md # Quick start documentation ``` -------------------------------- ### WASI SDK CI Build Script: Full Build Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Performs a full build including toolchain, sysroot, and tests. Outputs artifacts to the default './build/dist/' directory. ```bash ./ci/build.sh # Outputs to: ./build/dist/ ``` -------------------------------- ### File Path Variables Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/INDEX.md Illustrates common variables used to denote paths relative to the SDK installation or repository root. ```bash # Terminal commands shown in code blocks clang hello.c -o hello.wasm # CMake shown as: cmake -DOPTION=VALUE # Variables shown as: ${VARIABLE_NAME} ``` -------------------------------- ### Build Dependency Flow Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/build-system.md Illustrates the dependency flow between build phases and within Phase 2. Phase 1 must be installed before Phase 2 can utilize its toolchain. ```text Phase 1 (install) → CMAKE_TOOLCHAIN_FILE → Phase 2 ``` ```text compiler-rt → wasi-libc → libcxx ``` -------------------------------- ### WASI SDK Version Naming Examples Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Illustrates different formats for WASI SDK version naming, including release, full semantic, and development versions derived from git tags. ```text wasi-sdk-27 # Release version wasi-sdk-27.0 # Full semantic version wasi-sdk-27.1gABCD123+m # Development version with git hash ``` -------------------------------- ### Configure Cross-Compilation for WASI SDK Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/build-system.md Set up the WASI SDK build system for cross-compilation using CMake. This example configures for aarch64-unknown-linux-gnu target and enables ccache. ```bash cmake -G Ninja -B build -S . \ -DWASI_SDK_BUILD_TOOLCHAIN=ON \ -DRUST_TARGET=aarch64-unknown-linux-gnu \ -DCMAKE_C_COMPILER_LAUNCHER=ccache ``` -------------------------------- ### C Code with SetJmp/LongJmp Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Example C code demonstrating the use of setjmp and longjmp for non-local gotos. Ensure the -lsetjmp flag is used during compilation. ```c #include #include static jmp_buf buf; void deep_function(void) { longjmp(buf, 1); } int main(void) { if (setjmp(buf) == 0) { deep_function(); } else { printf("longjmp caught\n"); } return 0; } ``` -------------------------------- ### Toolchain Build Targets Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-configuration.md Commands to build all toolchain components, install them, create distribution tarballs, or build specific libraries like LLVM/Clang, libedit, libxml2, and wasm-component-ld. ```bash make/ninja build # Build all toolchain components make/ninja install # Install toolchain to CMAKE_INSTALL_PREFIX make/ninja dist # Create distribution tarball make/ninja llvm-build # Build LLVM/Clang make/ninja libedit # Build libedit (optional) make/ninja libxml2 # Build libxml2 (optional) make/ninja wasm-component-ld # Build wasm-component-ld make/ninja misc-files # Copy configuration files ``` -------------------------------- ### Clone and Build WASI SDK Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Clone the WASI SDK repository recursively and then use CMake to build the toolchain and sysroot. Ensure you have Ninja and CMake installed. ```bash git clone --recursive https://github.com/WebAssembly/wasi-sdk.git cd wasi-sdk # Build toolchain cmake -G Ninja -B build/toolchain -S . \ -DWASI_SDK_BUILD_TOOLCHAIN=ON \ -DCMAKE_INSTALL_PREFIX=build/install cmake --build build/toolchain # Build sysroot cmake -G Ninja -B build/sysroot -S . \ -DCMAKE_TOOLCHAIN_FILE=build/install/share/cmake/wasi-sdk-p1.cmake \ -DCMAKE_INSTALL_PREFIX=build/install cmake --build build/sysroot ``` -------------------------------- ### Compile C Example with Legacy Exceptions Source: https://github.com/webassembly/wasi-sdk/blob/main/SetjmpLongjmp.md Compiles the C source code using legacy WebAssembly exception-handling instructions. This is useful for older runtimes. ```shell clang -Os -o test.wasm test.c \ -mllvm -wasm-enable-sjlj -lsetjmp -mllvm -wasm-use-legacy-eh=true ``` -------------------------------- ### Compile C Example with Standard Exceptions Source: https://github.com/webassembly/wasi-sdk/blob/main/SetjmpLongjmp.md Compiles the C source code using standard WebAssembly exception-handling instructions. Ensure your WebAssembly runtime supports these. ```shell clang -Os -o test.wasm test.c \ -mllvm -wasm-enable-sjlj -lsetjmp -mllvm -wasm-use-legacy-eh=false ``` -------------------------------- ### WASI SDK CI Build Script: Skip Sysroot Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Builds only the toolchain and skips the sysroot compilation. The output will contain only the toolchain artifact. ```bash export WASI_SDK_CI_SKIP_SYSROOT=1 ./ci/build.sh # Outputs: ./build/dist/wasi-toolchain-*.tar.gz only ``` -------------------------------- ### Build WASI SDK Toolchain with CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/build-system.md Use this command to compile the LLVM toolchain, including Clang and wasm-ld. Ensure `-DWASI_SDK_BUILD_TOOLCHAIN=ON` is set. The output is installed to the specified CMAKE_INSTALL_PREFIX. ```bash cmake -G Ninja -B build/toolchain -S . \ -DWASI_SDK_BUILD_TOOLCHAIN=ON \ -DCMAKE_INSTALL_PREFIX=build/install ninja -C build/toolchain install ``` -------------------------------- ### Access WASI SDK Version at Runtime Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Reads the VERSION file from the installed sysroot path to determine the SDK version at runtime. ```bash cat ${WASI_SDK_PREFIX}/share/wasi-sysroot/VERSION ``` -------------------------------- ### WASI SDK CI Build Script: Extra LLVM Options Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Configures the build with additional CMake arguments for LLVM. This is useful for enabling specific LLVM features during the toolchain build. ```bash export WASI_SDK_CI_TOOLCHAIN_LLVM_CMAKE_ARGS="-DLLVM_ENABLE_ASSERTIONS=ON" ./ci/build.sh ``` -------------------------------- ### CMake build command with toolchain file Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/setjmp-longjmp.md Example of how to invoke CMake to build a project using a specific WASI SDK toolchain file. This command sets up the build directory and specifies the toolchain. ```bash cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${SDK}/share/cmake/wasi-sdk-p1.cmake ``` -------------------------------- ### Get WASI SDK Version Information Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Use the `version.py` script to query specific version details or dump all available information. You can specify a custom LLVM directory if needed. ```bash ./version.py wasi-sdk ./version.py llvm ./version.py llvm-major ./version.py dump ./version.py --llvm-dir= ``` -------------------------------- ### Cross-Compile to WebAssembly with Clang Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/compiler-targets.md Compile C code to WebAssembly using Clang. No specific cross-compilation setup is needed as all targets are WebAssembly. ```bash # macOS → WebAssembly (same command as Linux → WebAssembly) ${SDK}/bin/clang --target=wasm32-wasip1 hello.c ``` -------------------------------- ### C Source Code Example for setjmp/longjmp Source: https://github.com/webassembly/wasi-sdk/blob/main/SetjmpLongjmp.md A C program demonstrating the usage of setjmp and longjmp functions, including a test function to verify longjmp behavior. ```c #include #include #include #include static jmp_buf env; static bool test_if_longjmp(void(*f)(void)) { if (setjmp(env)) return true; f(); return false; } static void do_not_longjmp() { } static void do_longjmp() { longjmp(env, 1); } int main() { bool longjmped = test_if_longjmp(do_not_longjmp); assert(!longjmped); longjmped = test_if_longjmp(do_longjmp); assert(longjmped); return 0; } ``` -------------------------------- ### WASI SDK CI Build Script: Custom Build Directory Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Specifies a custom directory for the build process. Artifacts will be located in the 'dist/' subdirectory of the specified path. ```bash ./ci/build.sh /tmp/wasi-build # Outputs to: /tmp/wasi-build/dist/ ``` -------------------------------- ### Compile C Example with LTO and Standard Exceptions Source: https://github.com/webassembly/wasi-sdk/blob/main/SetjmpLongjmp.md Compiles the C source code with Link-Time Optimization (LTO) enabled, passing necessary flags to both Clang and the linker. ```shell clang -Os -flto=full -o test.wasm test.c \ -mllvm -wasm-enable-sjlj -lsetjmp -mllvm -wasm-use-legacy-eh=false \ -Wl,-mllvm,-wasm-enable-sjlj,-mllvm,-wasm-use-legacy-eh=false ``` -------------------------------- ### Run WASM Program with Node.js Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Load and instantiate a WASM module in Node.js using the WebAssembly API. This example includes basic WASI import handling. ```javascript const fs = require('fs'); const wasmModule = fs.readFileSync('hello.wasm'); const buffer = new ArrayBuffer(wasmModule.length); const view = new Uint8Array(buffer); view.set(wasmModule); const importObject = { wasi_snapshot_preview1: { // WASI functions here } }; WebAssembly.instantiate(buffer, importObject) .then(({ instance }) => { if (instance.exports._start) { instance.exports._start(); } }); ``` -------------------------------- ### Build WASI SDK Toolchain Source: https://github.com/webassembly/wasi-sdk/blob/main/README.md Builds the WASI SDK toolchain using CMake and Ninja. Specify CMAKE_INSTALL_PREFIX for the installation directory. The -DWASI_SDK_BUILD_TOOLCHAIN=ON flag is essential for this step. ```shell cmake -G Ninja -B build/toolchain -S . -DWASI_SDK_BUILD_TOOLCHAIN=ON -DCMAKE_INSTALL_PREFIX=build/install cmake --build build/toolchain --target install ``` -------------------------------- ### Build WASI SDK Toolchain with CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-configuration.md Use this command to configure and build the WASI SDK toolchain. It specifies Ninja as the generator, sets the installation prefix, build type, and includes LLVM LTO options. ```bash cmake -G Ninja -B build/toolchain -S . \ -DWASI_SDK_BUILD_TOOLCHAIN=ON \ -DCMAKE_INSTALL_PREFIX=build/install \ -DCMAKE_BUILD_TYPE=MinSizeRel \ -DLLVM_CMAKE_FLAGS="-DLLVM_ENABLE_LTO=ON" ``` ```bash cmake --build build/toolchain --target install ``` -------------------------------- ### Configure Distribution Builds with CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Use these CMake flags for release artifacts. This configuration optimizes for minimal size, enables Link-Time Optimization (LTO), builds shared libraries, and sets a specific installation prefix. ```bash cmake -G Ninja -B build -S . \ -DCMAKE_BUILD_TYPE=MinSizeRel \ -DWASI_SDK_LTO=ON \ -DWASI_SDK_BUILD_SHARED=ON \ -DCMAKE_INSTALL_PREFIX=/usr/local/wasi-sdk-27 ``` -------------------------------- ### Configure Custom Feature Set with CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Use these CMake flags to tailor the build for specific use cases. This example sets the target architecture, disables exceptions, and enables LTO. ```bash cmake -G Ninja -B build -S . \ -DWASI_SDK_TARGETS="wasm32-wasip1" \ -DWASI_SDK_EXCEPTIONS=OFF \ -DWASI_SDK_LTO=ON ``` -------------------------------- ### Using multiple jmp_buf variables Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/setjmp-longjmp.md Demonstrates the use of multiple `jmp_buf` variables for non-local jumps. This example shows how to set up and use two distinct jump environments, `env1` and `env2`, to handle nested function calls and jumps. ```c #include #include static jmp_buf env1, env2; void nested_function(void) { printf("In nested function\n"); longjmp(env2, 2); } void function_with_env2(void) { if (setjmp(env2) == 0) { nested_function(); } else { printf("Caught jump to env2\n"); } } int main(void) { if (setjmp(env1) == 0) { function_with_env2(); } else { printf("Caught jump to env1\n"); } return 0; } ``` -------------------------------- ### Print all version information Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Display all available version details in a key=value format. This is comprehensive and useful for documentation or detailed debugging. ```bash ./version.py dump 27.0 # WASI SDK version wasi-libc: a1b2c3d # wasi-libc git commit (short) llvm: e4f5g6h # LLVM git commit (short) llvm-version: 18.1.0 # Full LLVM version config: i7j8k9l # config submodule git commit ``` -------------------------------- ### Build a Make-based Project with Docker Source: https://github.com/webassembly/wasi-sdk/blob/main/README.md This command demonstrates how to build a make-based project using the WASI SDK Docker image. It mounts the current directory into the container and executes the 'make' command. ```bash docker run -v `pwd`:/src -w /src ghcr.io/webassembly/wasi-sdk make ``` -------------------------------- ### Get Config Version Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Retrieves the short 12-character commit hash for the config submodule. ```bash git -C src/config rev-parse --short=12 HEAD ``` -------------------------------- ### Compile with Link-Time Optimization (LTO) Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Enable full link-time optimization with -flto=full and optimize for size with -Os to produce smaller and potentially faster binaries. ```bash clang -flto=full -Os hello.c -o hello.wasm ``` -------------------------------- ### Get wasi-libc Version Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Retrieves the short 12-character commit hash for the wasi-libc submodule. ```bash git -C src/wasi-libc rev-parse --short=12 HEAD ``` -------------------------------- ### Conditional WASI Code Block Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md Example of how to use the WASI platform identifier in CMakeLists.txt to conditionally include WASI-specific code. ```cmake if(WASI) # WASI-specific code endif() ``` -------------------------------- ### Check Compiler Version Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Display the version of the installed Clang compiler. Note that this reflects the LLVM version, not the specific WASI SDK version. ```bash ${SDK}/bin/clang --version # Output: clang version 18.1.0 ``` -------------------------------- ### Set Minimum CMake Version Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-configuration.md Specifies the minimum required CMake version for the build system. Ensure your CMake installation meets this requirement. ```cmake cmake_minimum_required(VERSION 3.26) ``` -------------------------------- ### Handle WASI_SDK_PREFIX in CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md Ensures the WASI_SDK_PREFIX is set, either using a relative path for in-tree builds or a provided path for out-of-tree installations. ```cmake if(NOT WASI_SDK_PREFIX) set(WASI_SDK_PREFIX ${CMAKE_CURRENT_LIST_DIR}/../../) endif() ``` -------------------------------- ### Download and Extract WASI SDK Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Download the WASI SDK archive for your system and extract it. Replace placeholders with your desired version and architecture. ```bash WASI_VERSION=27 WASI_VERSION_FULL=${WASI_VERSION}.0 WASI_ARCH=x86_64 WASI_OS=linux wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz tar xvf wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz ``` -------------------------------- ### Build WASI SDK in Docker (Custom Image) Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Builds the WASI SDK Docker image using a custom image name and specifies an output directory. ```bash ./ci/docker-build.sh \ --docker-image-name my-wasi-builder \ --output-dir ./artifacts ``` -------------------------------- ### Set C/C++ Compiler Launcher Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Configure CMake to use a compiler launcher like ccache for C and C++ builds. This is often auto-detected if ccache is installed. ```bash CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache ``` -------------------------------- ### WASI SDK CI Build Script Execution Flow Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md The script outlines the steps for building the toolchain and sysroot, including directory creation, CMake configuration, Ninja builds, and test execution. ```bash 1. Create build directories ${build_dir}/toolchain/ ${build_dir}/sysroot/ ${build_dir}/install/ ${build_dir}/dist/ 2. Build toolchain cmake ... -DWASI_SDK_BUILD_TOOLCHAIN=ON ninja install dist 3. (Optional) Build sysroot Skip if WASI_SDK_CI_SKIP_SYSROOT=1 cmake ... -DCMAKE_TOOLCHAIN_FILE=${build_dir}/install/share/cmake/wasi-sdk-p1.cmake ninja install dist 4. (Optional) Run tests Skip if WASI_SDK_CI_SKIP_TESTS=1 ctest --parallel 10 --timeout 60 5. Output distribution artifacts ${build_dir}/dist/wasi-toolchain-*.tar.gz ${build_dir}/dist/wasi-sysroot-*.tar.gz ``` -------------------------------- ### Get SDK Version in CI/Automation Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md A script to capture the WASI SDK version from the VERSION file and echo it, suitable for use in CI pipelines or automation scripts. ```bash INSTALLED_VERSION=$(cat ${WASI_SDK_PREFIX}/share/wasi-sysroot/VERSION | head -1) echo "WASI SDK version: ${INSTALLED_VERSION}" ``` -------------------------------- ### Retrieve WASI SDK Version in CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Executes a Python script to get the WASI SDK version during the CMake build process. The output is stored in a variable. ```cmake execute_process( COMMAND ${PYTHON} ${version_script} OUTPUT_VARIABLE wasi_sdk_version OUTPUT_STRIP_TRAILING_WHITESPACE) ``` -------------------------------- ### WASI SDK pthread Toolchain Configuration Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md Configures CMake to target WASIp1 with experimental threading support. This setup is for projects that need to utilize WebAssembly threading. ```cmake set(CMAKE_SYSTEM_NAME WASI) set(CMAKE_SYSTEM_VERSION 1) set(CMAKE_SYSTEM_PROCESSOR wasm32) set(CMAKE_EXECUTABLE_SUFFIX .wasm) set(triple wasm32-wasip1-threads) ``` -------------------------------- ### Run WebAssembly Module with Wasmtime Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Execute a compiled WebAssembly module using the wasmtime runtime. The output of the program will be displayed. ```bash wasmtime hello.wasm # Output: Hello from WASI! ``` -------------------------------- ### Build Static Library with SetJmp Support Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/setjmp-longjmp.md Compiles C source code into an object file and then archives it into a static library. Requires specific LLVM flags for setjmp/longjmp support in WebAssembly. ```bash clang -c lib.c \ -mllvm -wasm-enable-sjlj \ -mllvm -wasm-use-legacy-eh=false \ -o lib.o ar rcs libmylib.a lib.o ``` -------------------------------- ### WASI SDK Build Script Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/DELIVERABLES.txt Reference for the main build script used in CI automation. ```Bash # ci/build.sh # This script is a reference for CI automation. ``` -------------------------------- ### Define and Throw Custom Exception Classes Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cpp-exceptions.md Example of defining a custom exception class inheriting from `std::exception` and throwing it. This works when exception support is enabled during compilation. ```cpp class MyException : public std::exception { public: virtual const char* what() const noexcept { return "My exception"; } }; void function_that_throws() { throw MyException(); } ``` -------------------------------- ### Configure CMake Project for WASI SDK Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Set up a CMakeLists.txt file for a C project and configure the build process to use the WASI SDK toolchain. ```cmake cmake_minimum_required(VERSION 3.16) project(my_app C) add_executable(hello hello.c) ``` ```bash cmake -B build -S . \ -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk-p1.cmake cmake --build build ``` -------------------------------- ### Compile with wasm32-wasip3 using clang Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/compiler-targets.md Compile a C file to WebAssembly using the latest wasm32-wasip3 target with clang. ```bash ${SDK}/bin/clang --target=wasm32-wasip3 hello.c -o hello.wasm ``` -------------------------------- ### CMake Project Configuration for Exceptions Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cpp-exceptions.md Configures a CMake project to use C++ exceptions by setting compiler and linker flags. This example uses the standard exception handling mechanism. ```cmake project(my_wasi_app CXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fwasm-exceptions") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm -wasm-use-legacy-eh=false") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fwasm-exceptions") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lunwind") add_executable(myapp main.cpp) ``` -------------------------------- ### Build WASI SDK for RISC-V Cross-Compile Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Builds the WASI SDK Docker image for RISC-V cross-compilation, specifying a custom Dockerfile and an output directory. ```bash ./ci/docker-build.sh \ --build-args "-f ci/docker/Dockerfile.riscv64-linux" \ --output-dir ./artifacts ``` -------------------------------- ### CMakeLists.txt for setjmp/longjmp Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/setjmp-longjmp.md Configures a CMake project to use setjmp/longjmp by setting specific compile and link options. This example assumes a toolchain file is used to set up the WASI SDK. ```cmake cmake_minimum_required(VERSION 3.16) project(my_setjmp_app C) # Assuming toolchain file sets up compiler add_executable(myapp main.c) # SetJmp-specific flags target_compile_options(myapp PRIVATE -mllvm -wasm-enable-sjlj -mllvm -wasm-use-legacy-eh=false ) target_link_options(myapp PRIVATE -lsetjmp -mllvm -wasm-enable-sjlj -mllvm -wasm-use-legacy-eh=false ) ``` -------------------------------- ### libcxx/libcxxabi Build Configuration Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-configuration.md Configure the C++ standard library (libcxx/libcxxabi) for multi-threading, filesystem support, and exception handling. Enables position-independent code and uses compiler-rt for unwinding. ```cmake LIBCXX_ENABLE_THREADS = ON LIBCXX_HAS_PTHREAD_API = ON LIBCXX_ENABLE_FILESYSTEM = ON LIBCXX_ENABLE_SHARED = ${pic} # ON if position-independent code enabled LIBCXX_ENABLE_EXCEPTIONS = ${exceptions} # Depends on exception mode LIBCXX_CXX_ABI = libcxxabi LIBCXXABI_ENABLE_EXCEPTIONS = ${exceptions} LIBUNWIND_USE_COMPILER_RT = ON ``` -------------------------------- ### Set WASI SDK and LLVM versions in build scripts Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/version-management.md Automate the retrieval of WASI SDK and LLVM versions within build scripts. This ensures consistency and provides informative build messages. ```bash WASI_SDK_VERSION=$(./version.py wasi-sdk) LLVM_VERSION=$(./version.py llvm) echo "Building WASI SDK ${WASI_SDK_VERSION} with LLVM ${LLVM_VERSION}" ``` -------------------------------- ### Sysroot Build Targets Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-configuration.md Commands to build sysroot components, including specific targets, installation, distribution tarballs, and individual libraries like compiler-rt, wasi-libc, and C++ standard library. ```bash make/ninja build # Build all sysroot components make/ninja build-${target} # Build for specific target make/ninja install # Install sysroot make/ninja dist # Create distribution tarballs make/ninja compiler-rt # Build compiler-rt make/ninja compiler-rt-build # Build compiler-rt components make/ninja wasi-libc-${target} # Build wasi-libc for target make/ninja libcxx-${target} # Build C++ standard library make/ninja version-file # Generate version metadata ``` -------------------------------- ### Default Include Directories Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md These are the default include directories searched by the toolchain file for system headers. ```text ${WASI_SDK_PREFIX}/share/wasi-sysroot/include/ ${WASI_SDK_PREFIX}/share/wasi-sysroot/include/c++/v1/ ${WASI_SDK_PREFIX}/share/wasi-sysroot/include/${target}/ ``` -------------------------------- ### Select Build Generator with CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/build-system.md Choose between Ninja and Unix Makefiles generators using the `-G` flag with CMake. Ninja is recommended for faster builds and better parallelization. ```bash cmake -G Ninja ... # Use Ninja (faster) ``` ```bash cmake -G "Unix Makefiles" ... # Use Make (slower) ``` -------------------------------- ### Build WASI SDK Sysroot with CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/build-system.md Compile the C/C++ standard libraries (compiler-rt, wasi-libc, libcxx) using the previously built toolchain. Point `CMAKE_TOOLCHAIN_FILE` to the Phase 1 output. This command assumes Phase 1 has already been completed. ```bash cmake -G Ninja -B build/sysroot -S . \ -DCMAKE_TOOLCHAIN_FILE=build/install/share/cmake/wasi-sdk-p1.cmake \ -DCMAKE_INSTALL_PREFIX=build/install ninja -C build/sysroot install ``` -------------------------------- ### Run Unit Tests with WASI SDK Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/build-system.md Execute unit tests for the WASI SDK sysroot. Ensure WASI_SDK_INCLUDE_TESTS is set to ON before running. ```bash ctest --test-dir build/sysroot/tests --verbose ``` -------------------------------- ### Compiler Binaries Configuration Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md Configures the compiler and linker paths for WASI SDK builds. Ensure WASI_SDK_PREFIX is correctly set. ```cmake CMAKE_C_COMPILER = ${WASI_SDK_PREFIX}/bin/clang CMAKE_CXX_COMPILER = ${WASI_SDK_PREFIX}/bin/clang++ CMAKE_ASM_COMPILER = ${WASI_SDK_PREFIX}/bin/clang CMAKE_AR = ${WASI_SDK_PREFIX}/bin/llvm-ar CMAKE_RANLIB = ${WASI_SDK_PREFIX}/bin/llvm-ranlib ``` -------------------------------- ### WASI SDK CI Build Script: Skip Tests Example Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Executes the build process up to the sysroot compilation but skips the test execution phase. This can be used to speed up builds when testing is not immediately required. ```bash export WASI_SDK_CI_SKIP_TESTS=1 ./ci/build.sh ``` -------------------------------- ### Enable LTO and Exceptions Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cpp-exceptions.md Compile with LTO and WebAssembly exceptions enabled. Ensure `-lunwind` is linked. Note potential issues with LTO and exceptions. ```bash clang++ \ -flto=full \ -fwasm-exceptions \ -mllvm -wasm-enable-sjlj \ -mllvm -wasm-use-legacy-eh=false \ -lunwind \ -Wl,-mllvm,-wasm-enable-sjlj,-mllvm,-wasm-use-legacy-eh=false \ hello.cpp -o hello.wasm ``` -------------------------------- ### Configure WASI SDK with DUAL Exception Support Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cpp-exceptions.md This command configures the WASI SDK build system using CMake to enable dual exception support. Ensure the SDK path and toolchain file are correctly specified. ```bash cmake -G Ninja -B build/sysroot -S . \ -DCMAKE_TOOLCHAIN_FILE=${SDK}/share/cmake/wasi-sdk-p1.cmake \ -DWASI_SDK_EXCEPTIONS=DUAL \ -DCMAKE_INSTALL_PREFIX=${SDK} ``` -------------------------------- ### Build WASI SDK in Docker (Default) Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/ci-automation.md Executes the Docker build script with default image name and specifies an output directory for artifacts. ```bash ./ci/docker-build.sh --output-dir ./artifacts ``` -------------------------------- ### CPU Feature Flags Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Examples of CPU feature flags that can be passed to the compiler to control CPU features. '-mcpu=mvp' is the baseline, '-mcpu=lime1' offers older hardware support, and '-mcpu=bleeding_edge' uses the latest WebAssembly features. ```text -mcpu=mvp # Minimal viable product (baseline) -mcpu=lime1 # Default; older hardware support -mcpu=bleeding_edge # Latest WebAssembly features ``` -------------------------------- ### Build WASI SDK using CI Script Source: https://github.com/webassembly/wasi-sdk/blob/main/README.md A convenience script that automates both the toolchain and sysroot build process, creating a complete SDK package. The final package is located in the 'build/dist' directory. ```shell ./ci/build.sh ``` -------------------------------- ### Configure Cargo/Rust Project for WASI Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Set up a Cargo.toml and src/main.rs for a Rust project targeting WebAssembly. Build the project using the specified target. ```toml [package] name = "hello" version = "0.1.0" [dependencies] ``` ```rust fn main() { println!("Hello from Rust!"); } ``` ```bash cargo build --target wasm32-wasip1 --release ``` -------------------------------- ### WASI SDK Project File Organization Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/INDEX.md This tree displays the directory structure of the WASI SDK project, showing the location of the main index file and other documentation modules. ```text /workspace/home/output/ ├── INDEX.md # This file ├── PROJECT_OVERVIEW.md # High-level overview ├── QUICK_START.md # Practical getting started ├── configuration.md # Configuration reference └── api-reference/ # Detailed API documentation ├── build-system.md # Build architecture ├── cmake-configuration.md # CMake variables ├── cmake-toolchain-files.md # Toolchain files ├── ci-automation.md # CI/CD scripts ├── compiler-targets.md # Compiler targets ├── version-management.md # Version system ├── cpp-exceptions.md # C++ exceptions └── setjmp-longjmp.md # SetJmp/LongJmp ``` -------------------------------- ### Configure WASIp1 Toolchain Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/cmake-toolchain-files.md Sets up CMake to target the WASIp1 specification. This is the stable and recommended configuration for most users. ```cmake set(CMAKE_SYSTEM_NAME WASI) set(CMAKE_SYSTEM_VERSION 1) set(CMAKE_SYSTEM_PROCESSOR wasm32) set(CMAKE_EXECUTABLE_SUFFIX .wasm) set(triple wasm32-wasip1) ``` -------------------------------- ### Perform a Clean Build of WASI SDK Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/build-system.md Remove the existing build directory and reconfigure/rebuild the WASI SDK toolchain and sysroot. ```bash rm -rf build/ cmake -G Ninja -B build/toolchain -S . -DWASI_SDK_BUILD_TOOLCHAIN=ON ... cmake --build build/toolchain # Then build sysroot ``` -------------------------------- ### Print Available Clang Targets Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/api-reference/compiler-targets.md Query clang to list all supported targets, filtering for WASM targets. ```bash clang -print-targets | grep -i wasm ``` -------------------------------- ### WASI SDK Docker Build Script Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/DELIVERABLES.txt Reference for the Docker build script used in CI automation. ```Bash # ci/docker-build.sh # This script is a reference for Docker builds in CI. ``` -------------------------------- ### WASI SDK P1 Configuration Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/DELIVERABLES.txt Configuration file for WASI SDK P1. ```CMake # wasi-sdk-p1.cmake # Configuration for WASI SDK P1. ``` -------------------------------- ### Configure Maximum Compatibility with CMake Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/configuration.md Use this CMake flag to ensure the build supports the widest range of hardware by specifying the 'mvp' CPU. This is useful for broad deployment. ```bash cmake -G Ninja -B build -S . \ -DWASI_SDK_CPU_CFLAGS="-mcpu=mvp" ``` -------------------------------- ### Optimize C Code with LTO and Speed Source: https://github.com/webassembly/wasi-sdk/blob/main/_autodocs/QUICK_START.md Combine full link-time optimization (-flto=full) with aggressive speed optimization (-O3) for potentially the fastest execution speed. ```bash clang -flto=full -O3 hello.c -o hello.wasm ```