Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Jun 20, 2024
1 parent ae6ad88 commit b95ccf8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 9 deletions.
3 changes: 2 additions & 1 deletion eng/native/gen-buildsys.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion eng/native/gen-buildsys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#

scriptroot="$( cd -P "$( dirname "$0" )" && pwd )"
reporoot="$(cd "$scriptroot"/../..; pwd -P)"

if [[ "$#" -lt 4 ]]; then
echo "Usage..."
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 1 addition & 4 deletions src/mono/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@

<!-- Directory to provision and use WASI sdk if WASI_SDK_PATH env variable is not set -->
<PropertyGroup Condition="'$(TargetsWasi)' == 'true'">
<!-- force download temmporarily https://github.com/dotnet/runtime/issues/101528
<WASI_SDK_PATH Condition="'$(WASI_SDK_PATH)' == ''">$([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), 'wasi', 'wasi-sdk'))</WASI_SDK_PATH>
-->
<WASI_SDK_PATH>$([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), 'wasi', 'wasi-sdk'))</WASI_SDK_PATH>
<WASI_SDK_PATH>$([MSBuild]::EnsureTrailingSlash('$(WASI_SDK_PATH)').Replace('\', '/'))</WASI_SDK_PATH>
<ShouldProvisionWasiSdk Condition="!Exists($(_ProvisionWasiSdkDir))">true</ShouldProvisionWasiSdk>
<ShouldProvisionWasiSdk Condition="!Exists($(WASI_SDK_PATH))">true</ShouldProvisionWasiSdk>
</PropertyGroup>

<PropertyGroup>
Expand Down
7 changes: 6 additions & 1 deletion src/mono/mono.proj
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ JS_ENGINES = [NODE_JS]
Condition="'$(HostOS)' == 'windows'"
WorkingDirectory="$(ArtifactsObjDir)"
IgnoreStandardErrorWarningFormat="true" />

<Copy SourceFiles="$(MSBuildThisFileDirectory)/wasi/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi-threads/pthread.h"
DestinationFiles="$(MSBuildThisFileDirectory)/wasi/wasi-sdk/share/wasi-sysroot/include/wasm32-wasip2/pthread.h"
/>

</Target>

<Target Name="ValidateWasiSdk" Condition="'$(SkipMonoCrossJitConfigure)' != 'true'">
Expand Down Expand Up @@ -851,7 +856,7 @@ JS_ENGINES = [NODE_JS]
<PropertyGroup Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'">
<MonoUseCrossTool>true</MonoUseCrossTool>
<MonoAotAbi Condition="'$(TargetsBrowser)' == 'true'">wasm32-unknown-none</MonoAotAbi>
<MonoAotAbi Condition="'$(TargetsWasi)' == 'true'">wasm32-unknown-wasi</MonoAotAbi>
<MonoAotAbi Condition="'$(TargetsWasi)' == 'true'">wasm32-unknown-wasip2</MonoAotAbi>
<MonoAotOffsetsFile>$(MonoObjCrossDir)offsets-wasm32-unknown-none.h</MonoAotOffsetsFile>
<MonoLibClang Condition="$([MSBuild]::IsOSPlatform('OSX'))">$(MonoLLVMDir)/$(BuildArchitecture)/lib/libclang.dylib</MonoLibClang>
<MonoLibClang Condition="$([MSBuild]::IsOSPlatform('Linux'))">$(MonoLLVMDir)/$(BuildArchitecture)/lib/libclang.so</MonoLibClang>
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/mini/mini-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/mono/wasi/provision.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 45 additions & 0 deletions src/native/external/wasi-sdk-p2.cmake
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b95ccf8

Please sign in to comment.