diff --git a/eng/native/gen-buildsys.cmd b/eng/native/gen-buildsys.cmd index b81ef55cbe49bb..4c50b42d0274cd 100644 --- a/eng/native/gen-buildsys.cmd +++ b/eng/native/gen-buildsys.cmd @@ -60,6 +60,7 @@ if /i "%__Arch%" == "wasm" ( set __UseEmcmake=1 ) if /i "%__Os%" == "wasi" ( + set "__repoRoot=!__repoRoot:\=/!" if "%WASI_SDK_PATH%" == "" ( if not exist "%__repoRoot%\src\mono\wasi\wasi-sdk" ( echo Error: Should set WASI_SDK_PATH environment variable pointing to WASI SDK root. @@ -72,7 +73,7 @@ if /i "%__Arch%" == "wasm" ( set "WASI_SDK_PATH=!WASI_SDK_PATH:\=/!" if not "!WASI_SDK_PATH:~-1!" == "/" set "WASI_SDK_PATH=!WASI_SDK_PATH!/" set __CmakeGenerator=Ninja - set __ExtraCmakeParams=%__ExtraCmakeParams% -DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm "-DWASI_SDK_PREFIX=!WASI_SDK_PATH!" "-DCMAKE_TOOLCHAIN_FILE=!WASI_SDK_PATH!/share/cmake/wasi-sdk.cmake" "-DCMAKE_SYSROOT=!WASI_SDK_PATH!/share/wasi-sysroot" + set __ExtraCmakeParams=%__ExtraCmakeParams% -DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm "-DWASI_SDK_PREFIX=!WASI_SDK_PATH!" "-DCMAKE_TOOLCHAIN_FILE=!__repoRoot!/src/native/external/wasi-sdk-p2.cmake" "-DCMAKE_SYSROOT=!WASI_SDK_PATH!/share/wasip2-sysroot" ) ) else ( set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCMAKE_SYSTEM_VERSION=10.0" diff --git a/eng/native/gen-buildsys.sh b/eng/native/gen-buildsys.sh index 4c1ed0943c45ac..ba2d87a7f74645 100755 --- a/eng/native/gen-buildsys.sh +++ b/eng/native/gen-buildsys.sh @@ -4,6 +4,7 @@ # scriptroot="$( cd -P "$( dirname "$0" )" && pwd )" +reporoot="$(cd "$scriptroot"/../..; pwd -P)" if [[ "$#" -lt 4 ]]; then echo "Usage..." @@ -97,7 +98,7 @@ if [[ "$host_arch" == "wasm" ]]; then if [[ "$target_os" == "browser" ]]; then cmake_command="emcmake $cmake_command" elif [[ "$target_os" == "wasi" ]]; then - true + cmake_extra_defines="$cmake_extra_defines -DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm -DWASI_SDK_PREFIX=$WASI_SDK_PATH -DCMAKE_TOOLCHAIN_FILE=$reporoot/src/native/external/wasi-sdk-p2.cmake" "-DCMAKE_SYSROOT=!WASI_SDK_PATH!/share/wasip2-sysroot" else echo "target_os was not specified" exit 1 diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt index ba41fa4d9a489f..3e964741a5dbeb 100644 --- a/src/mono/CMakeLists.txt +++ b/src/mono/CMakeLists.txt @@ -150,7 +150,7 @@ if(NOT AOT_TARGET_TRIPLE STREQUAL "") elseif(AOT_TARGET_TRIPLE STREQUAL "wasm32-unknown-none") set(TARGET_SYSTEM_NAME "emscripten") set(TARGET_ARCH "wasm") - elseif(AOT_TARGET_TRIPLE STREQUAL "wasm32-unknown-wasi") + elseif(AOT_TARGET_TRIPLE STREQUAL "wasm32-unknown-wasip2") set(TARGET_SYSTEM_NAME "wasi") set(TARGET_ARCH "wasm") elseif(AOT_TARGET_TRIPLE STREQUAL "x86_64-none-linux-android") diff --git a/src/mono/Directory.Build.props b/src/mono/Directory.Build.props index d97bf84a1da660..5ae32b4bdd225e 100644 --- a/src/mono/Directory.Build.props +++ b/src/mono/Directory.Build.props @@ -34,12 +34,9 @@ - $([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), 'wasi', 'wasi-sdk')) $([MSBuild]::EnsureTrailingSlash('$(WASI_SDK_PATH)').Replace('\', '/')) - true + true diff --git a/src/mono/mono.proj b/src/mono/mono.proj index 14056eb3a7e24e..c671d48e85af9c 100644 --- a/src/mono/mono.proj +++ b/src/mono/mono.proj @@ -327,6 +327,11 @@ JS_ENGINES = [NODE_JS] Condition="'$(HostOS)' == 'windows'" WorkingDirectory="$(ArtifactsObjDir)" IgnoreStandardErrorWarningFormat="true" /> + + + @@ -851,7 +856,7 @@ JS_ENGINES = [NODE_JS] true wasm32-unknown-none - wasm32-unknown-wasi + wasm32-unknown-wasip2 $(MonoObjCrossDir)offsets-wasm32-unknown-none.h $(MonoLLVMDir)/$(BuildArchitecture)/lib/libclang.dylib $(MonoLLVMDir)/$(BuildArchitecture)/lib/libclang.so diff --git a/src/mono/mono/mini/mini-wasm.h b/src/mono/mono/mini/mini-wasm.h index 95bafda3336c04..cef95c98ed4fe5 100644 --- a/src/mono/mono/mini/mini-wasm.h +++ b/src/mono/mono/mini/mini-wasm.h @@ -92,7 +92,7 @@ typedef struct { #define MONO_ARCH_HAVE_SDB_TRAMPOLINES 1 #define MONO_ARCH_LLVM_TARGET_LAYOUT "e-m:e-p:32:32-i64:64-n32:64-S128" #ifdef TARGET_WASI -#define MONO_ARCH_LLVM_TARGET_TRIPLE "wasm32-unknown-wasi" +#define MONO_ARCH_LLVM_TARGET_TRIPLE "wasm32-unknown-wasip2" #else #define MONO_ARCH_LLVM_TARGET_TRIPLE "wasm32-unknown-emscripten" #endif diff --git a/src/mono/wasi/provision.ps1 b/src/mono/wasi/provision.ps1 index ff336ef4bc22b5..1a6f40cb462de2 100644 --- a/src/mono/wasi/provision.ps1 +++ b/src/mono/wasi/provision.ps1 @@ -17,3 +17,11 @@ Invoke-WebRequest -Uri $WasiSdkUrl -OutFile ./wasi-sdk-$WasiSdkVersion.0-mingw.t tar --strip-components=1 -xzf ./wasi-sdk-$WasiSdkVersion.0-mingw.tar.gz -C $WasiSdkPath Copy-Item $WasiLocalPath/wasi-sdk-version.txt $WasiSdkPath/wasi-sdk-version.txt Remove-Item ./wasi-sdk-$WasiSdkVersion.0-mingw.tar.gz -fo + +# Temporary WASI-SDK 22 workaround #2: The version of `wasm-component-ld` that +# ships with WASI-SDK 22 contains a +# [bug](https://github.com/bytecodealliance/wasm-component-ld/issues/22) which +# has been fixed in a v0.5.3 of that utility, so we upgrade it here. +Invoke-WebRequest -Uri https://github.com/bytecodealliance/wasm-component-ld/releases/download/v0.5.3/wasm-component-ld-v0.5.3-x86_64-windows.zip -OutFile wasm-component-ld-v0.5.3-x86_64-windows.zip +Expand-Archive -LiteralPath wasm-component-ld-v0.5.3-x86_64-windows.zip -DestinationPath . +Copy-Item wasm-component-ld-v0.5.3-x86_64-windows/wasm-component-ld.exe $WasiSdkPath/bin \ No newline at end of file diff --git a/src/native/external/wasi-sdk-p2.cmake b/src/native/external/wasi-sdk-p2.cmake new file mode 100644 index 00000000000000..263f20896e303d --- /dev/null +++ b/src/native/external/wasi-sdk-p2.cmake @@ -0,0 +1,45 @@ +# TODO: WASI-SDK doesn't provide a CMake toolchain file for `wasm32-wasip2`, so +# we must provide it ourselves. Once +# https://github.com/WebAssembly/wasi-sdk/pull/430 is merged and included in an +# upcoming release, we'll be able to remove this file and use that instead. + +# Cmake toolchain description file for the Makefile + +# This is arbitrary, AFAIK, for now. +cmake_minimum_required(VERSION 3.4.0) + +# Until Platform/WASI.cmake is upstream we need to inject the path to it +# into CMAKE_MODULE_PATH. +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + +set(CMAKE_SYSTEM_NAME WASI) +set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_SYSTEM_PROCESSOR wasm32) +set(triple wasm32-wasip2) + +if(WIN32) + set(WASI_HOST_EXE_SUFFIX ".exe") +else() + set(WASI_HOST_EXE_SUFFIX "") +endif() + +# When building from source, WASI_SDK_PREFIX represents the generated directory +if(NOT WASI_SDK_PREFIX) + set(WASI_SDK_PREFIX ${CMAKE_CURRENT_LIST_DIR}/../../) +endif() + +set(CMAKE_C_COMPILER ${WASI_SDK_PREFIX}/bin/clang${WASI_HOST_EXE_SUFFIX}) +set(CMAKE_CXX_COMPILER ${WASI_SDK_PREFIX}/bin/clang++${WASI_HOST_EXE_SUFFIX}) +set(CMAKE_ASM_COMPILER ${WASI_SDK_PREFIX}/bin/clang${WASI_HOST_EXE_SUFFIX}) +set(CMAKE_AR ${WASI_SDK_PREFIX}/bin/llvm-ar${WASI_HOST_EXE_SUFFIX}) +set(CMAKE_RANLIB ${WASI_SDK_PREFIX}/bin/llvm-ranlib${WASI_HOST_EXE_SUFFIX}) +set(CMAKE_C_COMPILER_TARGET ${triple}) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) +set(CMAKE_ASM_COMPILER_TARGET ${triple}) + +# Don't look in the sysroot for executables to run during the build +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +# Only look in the sysroot (not in the host paths) for the rest +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)