Skip to content

Commit 72682cb

Browse files
committed
tweaks to enable NativeAOT-LLVM on Linux with WASI-SDK 22
Some of these changes are borrowed from dotnet#2569. Note that I had to manually copy pthread.h from the wasi-sdk-22/share/wasi-sysroot/include/wasm32-wasi-threads directory to the wasi-sdk-22/share/wasi-sysroot/include/wasm32-wasi directory as a workaround until WebAssembly/wasi-libc#501 is addressed. Signed-off-by: Joel Dice <[email protected]>
1 parent 07ff833 commit 72682cb

File tree

9 files changed

+38
-26
lines changed

9 files changed

+38
-26
lines changed

eng/native/gen-buildsys.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ for i in "${@:6}"; do
5959
esac
6060
done
6161

62-
cmake_extra_defines=
62+
cmake_extra_defines=()
6363
if [[ "$CROSSCOMPILE" == "1" ]]; then
6464
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
6565
# OSX doesn't use rootfs
@@ -71,17 +71,17 @@ if [[ "$CROSSCOMPILE" == "1" ]]; then
7171
TARGET_BUILD_ARCH="$host_arch"
7272
export TARGET_BUILD_ARCH
7373

74-
cmake_extra_defines="$cmake_extra_defines -C $scriptroot/tryrun.cmake"
74+
cmake_extra_defines+=("-C" "$scriptroot/tryrun.cmake")
7575

7676
if [[ "$platform" == "darwin" ]]; then
77-
cmake_extra_defines="$cmake_extra_defines -DCMAKE_SYSTEM_NAME=Darwin"
77+
cmake_extra_defines+=("-DCMAKE_SYSTEM_NAME=Darwin")
7878
else
79-
cmake_extra_defines="$cmake_extra_defines -DCMAKE_TOOLCHAIN_FILE=$scriptroot/../common/cross/toolchain.cmake"
79+
cmake_extra_defines+=("-DCMAKE_TOOLCHAIN_FILE=$scriptroot/../common/cross/toolchain.cmake")
8080
fi
8181
fi
8282

8383
if [[ "$host_arch" == "armel" ]]; then
84-
cmake_extra_defines="$cmake_extra_defines -DARM_SOFTFP=1"
84+
cmake_extra_defines+=("-DARM_SOFTFP=1")
8585
fi
8686

8787
if ! cmake_command=$(command -v cmake); then
@@ -97,7 +97,7 @@ if [[ "$host_arch" == "wasm" ]]; then
9797
if [[ "$target_os" == "browser" ]]; then
9898
cmake_command="emcmake $cmake_command"
9999
elif [[ "$target_os" == "wasi" ]]; then
100-
true
100+
cmake_extra_defines+=("-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_CROSSCOMPILING_EMULATOR=node --experimental-wasm-bigint --experimental-wasi-unstable-preview1")
101101
else
102102
echo "target_os was not specified"
103103
exit 1
@@ -109,7 +109,7 @@ $cmake_command \
109109
-G "$generator" \
110110
"-DCMAKE_BUILD_TYPE=$buildtype" \
111111
"-DCMAKE_INSTALL_PREFIX=$__CMakeBinDir" \
112-
$cmake_extra_defines \
112+
"${cmake_extra_defines[@]}" \
113113
$__UnprocessedCMakeArgs \
114114
-S "$1" \
115115
-B "$2"

src/coreclr/build-runtime.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ if [[ "$__CMakeTarget" == *"wasmjit"* ]]; then
189189
echo The LLVM_CMAKE_CONFIG environment variable pointing to llvm-build-dir/lib/cmake/llvm must be set. 1>&2
190190
exit 1
191191
fi
192+
193+
export LLVM_CMAKE_CONFIG
192194
fi
193195

194196
if [[ -z "$__CMakeTarget" ]]; then

src/coreclr/jit/CMakeLists.txt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,15 +558,24 @@ endif()
558558
set(JIT_DLL_MAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/dllmain.cpp)
559559

560560
if(CLR_CMAKE_TARGET_WIN32)
561-
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.exports)
562-
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.exports.def)
561+
if(CLR_CMAKE_BUILD_LLVM_JIT)
562+
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.Llvm.exports)
563+
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.Llvm.exports.def)
564+
else()
565+
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.exports)
566+
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.exports.def)
567+
endif()
563568
preprocess_file (${CLRJIT_EXPORTS} ${JIT_EXPORTS_FILE})
564569

565570
set(JIT_DEF_FILE ${JIT_EXPORTS_FILE})
566571
else()
567-
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.PAL.exports)
568-
569-
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/clrjit.exports)
572+
if(CLR_CMAKE_BUILD_LLVM_JIT)
573+
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.Llvm.exports)
574+
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.Llvm.exports.def)
575+
else()
576+
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.PAL.exports)
577+
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/clrjit.exports)
578+
endif()
570579
generate_exports_file(${CLRJIT_EXPORTS} ${JIT_EXPORTS_FILE})
571580

572581
if(CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_NETBSD OR CLR_CMAKE_TARGET_SUNOS)
@@ -706,16 +715,15 @@ else()
706715
create_standalone_jit(TARGET clrjit_win_x86_${ARCH_HOST_NAME} OS win ARCH x86 DESTINATIONS .)
707716
endif (CLR_CMAKE_TARGET_ARCH_RISCV64)
708717

709-
# Note that we currently do not support building the LLVM Jit on Unix.
710-
# Note as well that we need this "CLR_CMAKE_BUILD_LLVM_JIT", defined by the build scripts,
718+
# Note that we need this "CLR_CMAKE_BUILD_LLVM_JIT", defined by the build scripts,
711719
# because there is no way in CMake to ask "what --target's' am I being asked to configure?".
712720
if (CLR_CMAKE_BUILD_LLVM_JIT)
713721
# The LLVM clrjit needs to be the last clrjit to use create_standalone_jit as it modifies some cmake variables.
714722
# LLVM clrjit has an extra export - registerLlvmCallbacks.
715-
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.Llvm.exports)
716-
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.Llvm.exports.def)
717-
preprocess_file (${CLRJIT_EXPORTS} ${JIT_EXPORTS_FILE})
718-
set(JIT_DEF_FILE ${JIT_EXPORTS_FILE})
723+
if (CLR_CMAKE_TARGET_LINUX)
724+
add_compile_options(-Wno-switch)
725+
add_compile_options(-Wno-format)
726+
endif (CLR_CMAKE_TARGET_LINUX)
719727

720728
# Exclude cpp files that are not required when not processing beyond rationalized LIR.
721729
# Use REGEX as this list contains the absolute paths.

src/coreclr/jit/llvm.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ CORINFO_GENERIC_HANDLE Llvm::getSymbolHandleForClassToken(mdToken token)
770770
template <EEApiId Func, typename TReturn, typename... TArgs>
771771
TReturn CallEEApi(TArgs... args)
772772
{
773-
return static_cast<TReturn (*)(TArgs...)>(g_callbacks[static_cast<int>(Func)])(args...);
773+
return reinterpret_cast<TReturn (*)(TArgs...)>(g_callbacks[static_cast<int>(Func)])(args...);
774774
}
775775

776776
const char* Llvm::GetMangledMethodName(CORINFO_METHOD_HANDLE methodHandle)
@@ -866,8 +866,8 @@ extern "C" DLLEXPORT void registerLlvmCallbacks(void** jitImports, void** jitExp
866866
assert(jitExports != nullptr);
867867

868868
memcpy(g_callbacks, jitImports, static_cast<int>(EEApiId::Count) * sizeof(void*));
869-
jitExports[static_cast<int>(JitApiId::StartSingleThreadedCompilation)] = &Llvm::StartSingleThreadedCompilation;
870-
jitExports[static_cast<int>(JitApiId::FinishSingleThreadedCompilation)] = &Llvm::FinishSingleThreadedCompilation;
869+
jitExports[static_cast<int>(JitApiId::StartSingleThreadedCompilation)] = (void*)&Llvm::StartSingleThreadedCompilation;
870+
jitExports[static_cast<int>(JitApiId::FinishSingleThreadedCompilation)] = (void*)&Llvm::FinishSingleThreadedCompilation;
871871
jitExports[static_cast<int>(JitApiId::Count)] = (void*)0x1234;
872872
}
873873

src/coreclr/jit/llvmcodegen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ void Llvm::visitNode(GenTree* node)
11181118
break;
11191119
case GT_JMP:
11201120
NYI("LLVM/GT_JMP"); // Requires support for explicit tailcalls.
1121+
LLVM_FALLTHROUGH;
11211122
default:
11221123
unreached();
11231124
}

src/coreclr/jit/llvmlssa.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ class ShadowStackAllocator
14821482
{
14831483
if (block != m_currentBlock)
14841484
{
1485-
m_actions.Push({AllocationActionKind::Block, m_currentBlockIndex++});
1485+
m_actions.Push({AllocationActionKind::Block, {m_currentBlockIndex++}});
14861486
m_currentBlock = block;
14871487
}
14881488
}

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ The .NET Foundation licenses this file to you under the MIT license.
388388

389389
<Target Name="CheckWasmSdks">
390390
<Error Text="Emscripten not found, not compiling to WebAssembly. To enable WebAssembly compilation, install Emscripten and ensure the EMSDK environment variable points to the directory containing upstream/emscripten/emcc.bat"
391-
Condition="'$(EMSDK)' == ''" />
391+
Condition="'$(EMSDK)' == '' and '$(_targetOS)' == 'browser'" />
392392
<Error Text="Wasi SDK not found, not compiling to WebAssembly. To enable WebAssembly compilation, install Wasi SDK and ensure the WASI_SDK_PATH environment variable points to the directory containing share/wasi-sysroot"
393393
Condition="'$(WASI_SDK_PATH)' == '' and '$(_targetOS)' == 'wasi'" />
394394
</Target>
@@ -420,8 +420,7 @@ The .NET Foundation licenses this file to you under the MIT license.
420420
<CompileWasmArgs>$(CompileWasmArgs) -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -disable-lsr --sysroot=&quot;$(WASI_SDK_PATH)/share/wasi-sysroot&quot; -target $(IlcLlvmTarget)</CompileWasmArgs>
421421

422422
<ExeExt Condition="'$(OS)' == 'Windows_NT'">.exe</ExeExt>
423-
<!-- using Emscripten's clang++ because of a crash in wasi-sdk's clang++ (https://github.com/WebAssembly/wasi-sdk/issues/326) -->
424-
<WasmCompilerPath>&quot;$(EMSDK)/upstream/bin/clang++$(ExeExt)&quot;</WasmCompilerPath>
423+
<WasmCompilerPath>&quot;$(WASI_SDK_PATH)/bin/clang++$(ExeExt)&quot;</WasmCompilerPath>
425424
<WasmLinkerPath>&quot;$(WASI_SDK_PATH)/bin/clang&quot;</WasmLinkerPath>
426425
</PropertyGroup>
427426

src/native/libs/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI)
3030
endif ()
3131

3232
set(CMAKE_INSTALL_PREFIX $ENV{__CMakeBinDir})
33-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wno-declaration-after-statement")
33+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wno-declaration-after-statement -Wno-switch-default")
3434

3535
add_compile_options(-I${CMAKE_CURRENT_SOURCE_DIR}/Common)
3636
add_compile_options(-I${CMAKE_CURRENT_BINARY_DIR}/Common)

src/native/libs/System.Native/pal_io.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
#elif HAVE_SYS_STATVFS_H && !HAVE_NON_LEGACY_STATFS // SunOS
5050
#include <sys/types.h>
5151
#include <sys/statvfs.h>
52+
#ifndef TARGET_WASI
5253
#include <sys/vfs.h>
5354
#endif
55+
#endif
5456

5557
#ifdef _AIX
5658
#include <alloca.h>

0 commit comments

Comments
 (0)