From 0b91d36a1e640a5784ce2e17a7261b8fb15b34a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Thu, 10 Aug 2023 20:34:47 +0200 Subject: [PATCH] Toolchain: Build Clang with RISC-V support --- Toolchain/BuildClang.sh | 3 ++- Toolchain/CMake/LLVMConfig.cmake | 4 ++-- ...03-Driver-Add-support-for-SerenityOS.patch | 24 +++++++++++++++---- ...o-ftls-model-initial-exec-on-Serenit.patch | 5 ++-- ...0005-libc-Add-support-for-SerenityOS.patch | 2 +- ...ilding-shared-libLLVM-and-libClang-f.patch | 4 ++-- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Toolchain/BuildClang.sh b/Toolchain/BuildClang.sh index df87bafc8da..dd42f903de0 100755 --- a/Toolchain/BuildClang.sh +++ b/Toolchain/BuildClang.sh @@ -15,7 +15,7 @@ echo "$DIR" PREFIX="$DIR/Local/clang/" BUILD="$DIR/../Build/" USERLAND_ARCHS="x86_64" -ARCHS="$USERLAND_ARCHS aarch64" +ARCHS="$USERLAND_ARCHS aarch64 riscv64" MD5SUM="md5sum" REALPATH="realpath" @@ -296,6 +296,7 @@ pushd "$DIR/Build/clang" -G Ninja \ -DSERENITY_x86_64-pc-serenity_SYSROOT="$BUILD/x86_64clang/Root" \ -DSERENITY_aarch64-pc-serenity_SYSROOT="$BUILD/aarch64clang/Root" \ + -DSERENITY_riscv64-pc-serenity_SYSROOT="$BUILD/riscv64clang/Root" \ -DCMAKE_INSTALL_PREFIX="$PREFIX" \ -DSERENITY_MODULE_PATH="$DIR/CMake" \ -C "$DIR/CMake/LLVMConfig.cmake" \ diff --git a/Toolchain/CMake/LLVMConfig.cmake b/Toolchain/CMake/LLVMConfig.cmake index 17ed4381e62..bba3b8bd75e 100644 --- a/Toolchain/CMake/LLVMConfig.cmake +++ b/Toolchain/CMake/LLVMConfig.cmake @@ -6,7 +6,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${SERENITY_MODULE_PATH}" CACHE STRIN set(CMAKE_BUILD_TYPE Release CACHE STRING "") -set(LLVM_TARGETS_TO_BUILD "X86;AArch64" CACHE STRING "") +set(LLVM_TARGETS_TO_BUILD "X86;AArch64;RISCV" CACHE STRING "") set(LLVM_ENABLE_PROJECTS "llvm;clang;lld;clang-tools-extra" CACHE STRING "") set(LLVM_ENABLE_RUNTIMES "compiler-rt" CACHE STRING "") @@ -25,7 +25,7 @@ set(LLVM_INSTALL_BINUTILS_SYMLINKS OFF CACHE BOOL "") set(CLANG_ENABLE_CLANGD OFF CACHE BOOL "") set(compiler_flags "-nostdlib -nostdlib++") -foreach(target x86_64-pc-serenity;aarch64-pc-serenity) +foreach(target x86_64-pc-serenity;aarch64-pc-serenity;riscv64-pc-serenity) list(APPEND targets "${target}") set(RUNTIMES_${target}_CMAKE_BUILD_TYPE Release CACHE STRING "") diff --git a/Toolchain/Patches/llvm/0003-Driver-Add-support-for-SerenityOS.patch b/Toolchain/Patches/llvm/0003-Driver-Add-support-for-SerenityOS.patch index 72a0652e55a..d80f24cbea0 100644 --- a/Toolchain/Patches/llvm/0003-Driver-Add-support-for-SerenityOS.patch +++ b/Toolchain/Patches/llvm/0003-Driver-Add-support-for-SerenityOS.patch @@ -2,25 +2,30 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Thu, 14 Apr 2022 10:09:50 +0200 Subject: [PATCH] [Driver] Add support for SerenityOS +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit Adds support for the `$arch-pc-serenity` target to the Clang front end. This makes the compiler look for libraries and headers in the right places, and enables some security mitigations like stack-smashing protection and position-independent code by default. + +Co-authored-by: kleines Filmröllchen --- - clang/lib/Basic/Targets.cpp | 6 + + clang/lib/Basic/Targets.cpp | 8 + clang/lib/Basic/Targets/OSTargets.h | 18 ++ clang/lib/Driver/CMakeLists.txt | 1 + clang/lib/Driver/Driver.cpp | 4 + clang/lib/Driver/ToolChain.cpp | 2 + clang/lib/Driver/ToolChains/Serenity.cpp | 336 +++++++++++++++++++++++ clang/lib/Driver/ToolChains/Serenity.h | 100 +++++++ - 7 files changed, 467 insertions(+) + 7 files changed, 469 insertions(+) create mode 100644 clang/lib/Driver/ToolChains/Serenity.cpp create mode 100644 clang/lib/Driver/ToolChains/Serenity.h diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp -index 8400774db..f105bed29 100644 +index 8400774db..f82618dc8 100644 --- a/clang/lib/Basic/Targets.cpp +++ b/clang/lib/Basic/Targets.cpp @@ -153,6 +153,8 @@ TargetInfo *AllocateTarget(const llvm::Triple &Triple, @@ -32,7 +37,16 @@ index 8400774db..f105bed29 100644 case llvm::Triple::Win32: switch (Triple.getEnvironment()) { case llvm::Triple::GNU: -@@ -542,6 +544,8 @@ TargetInfo *AllocateTarget(const llvm::Triple &Triple, +@@ -424,6 +426,8 @@ TargetInfo *AllocateTarget(const llvm::Triple &Triple, + return new FuchsiaTargetInfo(Triple, Opts); + case llvm::Triple::Linux: + return new LinuxTargetInfo(Triple, Opts); ++ case llvm::Triple::Serenity: ++ return new SerenityTargetInfo(Triple, Opts); + default: + return new RISCV64TargetInfo(Triple, Opts); + } +@@ -542,6 +546,8 @@ TargetInfo *AllocateTarget(const llvm::Triple &Triple, return new MCUX86_32TargetInfo(Triple, Opts); case llvm::Triple::Hurd: return new HurdTargetInfo(Triple, Opts); @@ -41,7 +55,7 @@ index 8400774db..f105bed29 100644 default: return new X86_32TargetInfo(Triple, Opts); } -@@ -596,6 +600,8 @@ TargetInfo *AllocateTarget(const llvm::Triple &Triple, +@@ -596,6 +602,8 @@ TargetInfo *AllocateTarget(const llvm::Triple &Triple, return new PS4OSTargetInfo(Triple, Opts); case llvm::Triple::PS5: return new PS5OSTargetInfo(Triple, Opts); diff --git a/Toolchain/Patches/llvm/0004-Driver-Default-to-ftls-model-initial-exec-on-Serenit.patch b/Toolchain/Patches/llvm/0004-Driver-Default-to-ftls-model-initial-exec-on-Serenit.patch index e8eda861ff0..b79ac975403 100644 --- a/Toolchain/Patches/llvm/0004-Driver-Default-to-ftls-model-initial-exec-on-Serenit.patch +++ b/Toolchain/Patches/llvm/0004-Driver-Default-to-ftls-model-initial-exec-on-Serenit.patch @@ -1,8 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Thu, 14 Apr 2022 10:12:54 +0200 -Subject: [PATCH] [Driver] Default to -ftls-model=initial-exec on - SerenityOS +Subject: [PATCH] [Driver] Default to -ftls-model=initial-exec on SerenityOS This is a hack to make Clang use the initial-exec TLS model instead of the default local-exec when building code for Serenity. @@ -13,7 +12,7 @@ This patch should be removed when we implement proper TLS support. 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp -index ec6860113..c5c07e5ee 100644 +index 77554aa2c..84eebee36 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6100,7 +6100,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, diff --git a/Toolchain/Patches/llvm/0005-libc-Add-support-for-SerenityOS.patch b/Toolchain/Patches/llvm/0005-libc-Add-support-for-SerenityOS.patch index 19138c5c4d3..bbecbd19010 100644 --- a/Toolchain/Patches/llvm/0005-libc-Add-support-for-SerenityOS.patch +++ b/Toolchain/Patches/llvm/0005-libc-Add-support-for-SerenityOS.patch @@ -36,7 +36,7 @@ index a12aa1de1..075ebbe81 100644 __support/solaris/wchar.h __support/solaris/xlocale.h diff --git a/libcxx/include/__config b/libcxx/include/__config -index 5e30a4217..5dea34814 100644 +index 108153883..e4434809c 100644 --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -930,7 +930,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD diff --git a/Toolchain/Patches/llvm/0008-cmake-Support-building-shared-libLLVM-and-libClang-f.patch b/Toolchain/Patches/llvm/0008-cmake-Support-building-shared-libLLVM-and-libClang-f.patch index dfe9ffe23c7..3a7b70adc81 100644 --- a/Toolchain/Patches/llvm/0008-cmake-Support-building-shared-libLLVM-and-libClang-f.patch +++ b/Toolchain/Patches/llvm/0008-cmake-Support-building-shared-libLLVM-and-libClang-f.patch @@ -1,8 +1,8 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Mon, 18 Apr 2022 22:32:29 +0200 -Subject: [PATCH] [cmake] Support building shared libLLVM and libClang - for SerenityOS +Subject: [PATCH] [cmake] Support building shared libLLVM and libClang for + SerenityOS This patch tells CMake that the --whole-archive linker option should be used for specifying the archives whose members will constitute these