[wasi] Build cross compiler for wasi. (#82702)
authorZoltan Varga <vargaz@gmail.com>
Mon, 27 Feb 2023 20:01:53 +0000 (15:01 -0500)
committerGitHub <noreply@github.com>
Mon, 27 Feb 2023 20:01:53 +0000 (15:01 -0500)
* [wasi] Build cross compiler for wasi.

* Disable building the cross compiler on windows for now.

src/mono/CMakeLists.txt
src/mono/Directory.Build.props
src/mono/mono.proj
src/mono/mono/tools/offsets-tool/offsets-tool.py

index 3438291..6835569 100644 (file)
@@ -143,6 +143,9 @@ 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")
+    set(TARGET_SYSTEM_NAME "wasi")
+    set(TARGET_ARCH "wasm")
   elseif(AOT_TARGET_TRIPLE STREQUAL "x86_64-none-linux-android")
     set(TARGET_SYSTEM_NAME "android")
     set(TARGET_ARCH "x86_64")
index aec8a66..a7a5977 100644 (file)
@@ -49,6 +49,7 @@
     <MonoAOTEnableLLVM Condition="'$(TargetsMacCatalyst)' == 'true'">true</MonoAOTEnableLLVM>
     <MonoAOTEnableLLVM Condition="'$(TargetsBrowser)' == 'true'">true</MonoAOTEnableLLVM>
     <MonoAOTEnableLLVM Condition="'$(TargetsAndroid)' == 'true'">true</MonoAOTEnableLLVM>
+    <MonoAOTEnableLLVM Condition="'$(TargetsWasi)' == 'true'">true</MonoAOTEnableLLVM>
     <_MonoUseLLVMPackage Condition="'$(MonoLLVMDir)' == '' and ('$(MonoEnableLLVM)' == 'true' or $(MonoAOTEnableLLVM) == 'true')">true</_MonoUseLLVMPackage>
     <MonoLLVMDir Condition="'$(MonoLLVMDir)' == '' or '$(_MonoUseLLVMPackage)' == 'true'">$([MSBuild]::NormalizeDirectory('$(MonoObjDir)', 'llvm'))</MonoLLVMDir>
   </PropertyGroup>
index 2ff33c1..5a56949 100644 (file)
@@ -42,6 +42,7 @@
     <BuildMonoAOTCrossCompiler Condition="'$(TargetsOSX)' == 'true'">true</BuildMonoAOTCrossCompiler>
     <BuildMonoAOTCrossCompiler Condition="'$(TargetsBrowser)' == 'true'">true</BuildMonoAOTCrossCompiler>
     <BuildMonoAOTCrossCompiler Condition="'$(TargetsAndroid)' == 'true'">true</BuildMonoAOTCrossCompiler>
+    <BuildMonoAOTCrossCompiler Condition="'$(TargetsWasi)' == 'true' and !$([MSBuild]::IsOSPlatform('Windows'))">true</BuildMonoAOTCrossCompiler>
     <MonoObjCrossDir>$([MSBuild]::NormalizeDirectory('$(MonoObjDir)', 'cross'))</MonoObjCrossDir>
     <CrossConfigH Condition="'$(BuildMonoAOTCrossCompiler)' == 'true'">$([MSBuild]::NormalizePath('$(MonoObjCrossDir)', 'config.h'))</CrossConfigH>
     <MonoBundleLLVMOptimizer Condition="'$(MonoEnableLLVM)' == 'true'">true</MonoBundleLLVMOptimizer>
     </ItemGroup>
 
     <!-- WASM specific options -->
-    <PropertyGroup Condition="'$(TargetsBrowser)' == 'true'">
+    <PropertyGroup Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'">
       <MonoUseCrossTool>true</MonoUseCrossTool>
       <_MonoSkipInitCompiler>true</_MonoSkipInitCompiler>
-      <MonoAotAbi>wasm32-unknown-none</MonoAotAbi>
+      <MonoAotAbi Condition="'$(TargetsBrowser)' == 'true'">wasm32-unknown-none</MonoAotAbi>
+      <MonoAotAbi Condition="'$(TargetsWasi)' == 'true'">wasm32-unknown-wasi</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>
       <MonoLibClang Condition="$([MSBuild]::IsOSPlatform('Windows'))">$([MSBuild]::NormalizePath('$(MonoLLVMDir)', '$(BuildArchitecture)', 'bin', 'libclang.dll'))</MonoLibClang>
-      <PythonCmd Condition="'$(HostOS)' == 'windows'">setlocal EnableDelayedExpansion &amp;&amp; call &quot;$([MSBuild]::NormalizePath('$(EMSDK_PATH)', 'emsdk_env.bat'))&quot; &amp;&amp; !EMSDK_PYTHON!</PythonCmd>
+      <PythonCmd Condition="'$(HostOS)' == 'windows'and '$(TargetsBrowser)' == 'true'">setlocal EnableDelayedExpansion &amp;&amp; call &quot;$([MSBuild]::NormalizePath('$(EMSDK_PATH)', 'emsdk_env.bat'))&quot; &amp;&amp; !EMSDK_PYTHON!</PythonCmd>
       <_ForceRelease Condition="$([MSBuild]::IsOSPlatform('Windows')) and '$(TargetArchitecture)' == 'wasm' and '$(Configuration)' == 'Debug'">true</_ForceRelease>
     </PropertyGroup>
 
index 4d3a6cc..5f52ab2 100644 (file)
@@ -10,6 +10,7 @@ import clang.cindex
 IOS_DEFINES = ["HOST_DARWIN", "TARGET_MACH", "MONO_CROSS_COMPILE", "USE_MONO_CTX", "_XOPEN_SOURCE"]
 ANDROID_DEFINES = ["HOST_ANDROID", "MONO_CROSS_COMPILE", "USE_MONO_CTX", "BIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD"]
 LINUX_DEFINES = ["HOST_LINUX", "MONO_CROSS_COMPILE", "USE_MONO_CTX"]
+WASI_DEFINES = ["_WASI_EMULATED_PROCESS_CLOCKS", "_WASI_EMULATED_SIGNAL", "_WASI_EMULATED_MMAN"]
 
 class Target:
        def __init__(self, arch, platform, others):
@@ -57,6 +58,7 @@ class OffsetsTool:
                parser = argparse.ArgumentParser ()
                parser.add_argument ('--libclang', dest='libclang', help='path to shared library of libclang.{so,dylib}', required=True)
                parser.add_argument ('--emscripten-sdk', dest='emscripten_path', help='path to emscripten sdk')
+               parser.add_argument ('--wasi-sdk', dest='wasi_path', help='path to wasi sdk')
                parser.add_argument ('--outfile', dest='outfile', help='path to output file', required=True)
                parser.add_argument ('--monodir', dest='mono_path', help='path to mono source tree', required=True)
                parser.add_argument ('--nativedir', dest='native_path', help='path to src/native', required=True)
@@ -83,10 +85,15 @@ class OffsetsTool:
                android_api_level = "-D__ANDROID_API=21"
 
                if "wasm" in args.abi:
-                       require_emscipten_path (args)
-                       self.sys_includes = [args.emscripten_path + "/system/include", args.emscripten_path + "/system/include/libc", args.emscripten_path + "/system/lib/libc/musl/arch/emscripten", args.emscripten_path + "/system/lib/libc/musl/include", args.emscripten_path + "/system/lib/libc/musl/arch/generic"]
-                       self.target = Target ("TARGET_WASM", None, [])
-                       self.target_args += ["-target", args.abi]
+                       if args.wasi_path != None:
+                               self.sys_includes = [args.wasi_path + "/share/wasi-sysroot/include", args.wasi_path + "/lib/clang/14.0.4/include", args.mono_path + "/wasi/mono-include"]
+                               self.target = Target ("TARGET_WASI", None, ["TARGET_WASM"] + WASI_DEFINES)
+                               self.target_args += ["-target", args.abi]
+                       else:
+                               require_emscipten_path (args)
+                               self.sys_includes = [args.emscripten_path + "/system/include", args.emscripten_path + "/system/include/libc", args.emscripten_path + "/system/lib/libc/musl/arch/emscripten", args.emscripten_path + "/system/lib/libc/musl/include", args.emscripten_path + "/system/lib/libc/musl/arch/generic"]
+                               self.target = Target ("TARGET_WASM", None, [])
+                               self.target_args += ["-target", args.abi]
 
                # Linux
                elif "arm-linux-gnueabihf" == args.abi: