From 88a18835aa9c4570f0961e53f021aa0013c96c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Sowi=C5=84ski?= Date: Thu, 18 Jan 2024 14:23:13 +0100 Subject: [PATCH 1/2] [RISC-V] Build coredistools --- build-coredistools.sh | 6 ++++++ src/coredistools/coredistools.cpp | 6 ++++++ src/coredistools/coredistools.h | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/build-coredistools.sh b/build-coredistools.sh index 9dca3afc..306a6436 100755 --- a/build-coredistools.sh +++ b/build-coredistools.sh @@ -79,6 +79,12 @@ case "$TargetOSArchitecture" in LLVMTargetsToBuild="LoongArch" ;; + linux-riscv64) + CMakeCrossCompiling=ON + LLVMHostTriple=riscv64-linux-gnu + LLVMTargetsToBuild="RISCV" + ;; + osx-arm64) CMakeCrossCompiling=ON CMakeOSXArchitectures=arm64 diff --git a/src/coredistools/coredistools.cpp b/src/coredistools/coredistools.cpp index 0ff8232f..f4b0b964 100644 --- a/src/coredistools/coredistools.cpp +++ b/src/coredistools/coredistools.cpp @@ -286,6 +286,9 @@ bool CorDisasm::setTarget() { case Triple::loongarch64: TheTargetArch = Target_LoongArch64; break; + case Triple::riscv64: + TheTargetArch = Target_RiscV64; + break; default: Print->Error("Unsupported Architecture: %s\n", Triple::getArchTypeName(TheTriple->getArch())); @@ -309,6 +312,9 @@ bool CorDisasm::setTarget() { case Target_LoongArch64: TheTriple->setArch(Triple::loongarch64); break; + case Target_RiscV64: + TheTriple->setArch(Triple::riscv64); + break; default: Print->Error("Unsupported Architecture: %s\n", Triple::getArchTypeName(TheTriple->getArch())); diff --git a/src/coredistools/coredistools.h b/src/coredistools/coredistools.h index f26693f9..5bee8f22 100644 --- a/src/coredistools/coredistools.h +++ b/src/coredistools/coredistools.h @@ -42,7 +42,8 @@ enum TargetArch { Target_X64, Target_Thumb, Target_Arm64, - Target_LoongArch64 + Target_LoongArch64, + Target_RiscV64, }; struct CorDisasm; From 5b857a365266b62efb8b7fa8ec64989678b0ddcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Sowi=C5=84ski?= Date: Thu, 18 Jan 2024 15:15:31 +0100 Subject: [PATCH 2/2] [RISC-V] Adapt the build scripts for standard Ubuntu cross build container because there is no CBL Mariner container for RISC-V. Besides, it's more convenient to work in one docker only. --- build-coredistools.sh | 19 ++++++++++--------- build-tblgen.sh | 16 ++++++++++------ doc/building-coredistools.md | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/build-coredistools.sh b/build-coredistools.sh index 306a6436..a60600ab 100755 --- a/build-coredistools.sh +++ b/build-coredistools.sh @@ -17,14 +17,13 @@ CMakeOSXArchitectures= LLVMTargetsToBuild="AArch64;ARM;X86" # Figure out which `strip` to use. Prefer `llvm-strip` if it is available. -# `llvm-strip` is available in CBL-Mariner container; `strip` is available on macOS. -StripTool=$(command -v llvm-strip) +# `llvm-strip` is available in CBL-Mariner container, +# `llvm-strip-` is available on standard cross build Ubuntu container, +# `strip` is available on macOS. +StripTool=$(command -v llvm-strip{,-{20..15}} strip | head -n 1) if [ -z "$StripTool" ]; then - StripTool=$(command -v strip) - if [ -z "$StripTool" ]; then - echo "Strip tool not found" - exit 1 - fi + echo "Strip tool not found" + exit 1 fi TblGenTool=$(command -v llvm-tblgen) @@ -33,13 +32,14 @@ if [ -z "$TblGenTool" ]; then exit 1 fi -C_COMPILER=$(command -v clang) +# Take first match from: clang clang-20 clang-19 .. clang-15 +C_COMPILER=$(command -v clang{,-{20..15}} | head -n 1) if [ -z "$C_COMPILER" ]; then echo "C compiler not found" # Keep going in case cmake can find one? fi -CXX_COMPILER=$(command -v clang++) +CXX_COMPILER=$(command -v clang++{,-{20..15}} | head -n 1) if [ -z "$CXX_COMPILER" ]; then echo "C++ compiler not found" # Keep going in case cmake can find one? @@ -83,6 +83,7 @@ case "$TargetOSArchitecture" in CMakeCrossCompiling=ON LLVMHostTriple=riscv64-linux-gnu LLVMTargetsToBuild="RISCV" + EnsureCrossRootfsDirectoryExists ;; osx-arm64) diff --git a/build-tblgen.sh b/build-tblgen.sh index c616649a..7a2c3138 100755 --- a/build-tblgen.sh +++ b/build-tblgen.sh @@ -70,14 +70,18 @@ fi pushd "$BinariesDirectory" +# Take first match from: clang clang-20 clang-19 .. clang-15 +C_COMPILER=$(command -v clang{,-{20..15}} | head -n 1) +CXX_COMPILER=$(command -v clang++{,-{20..15}} | head -n 1) + if [ -z "$CrossRootfsDirectory" ]; then BUILD_FLAGS="" cmake \ -G "Unix Makefiles" \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \ - -DCMAKE_C_COMPILER=$(command -v clang) \ - -DCMAKE_CXX_COMPILER=$(command -v clang++) \ + -DCMAKE_C_COMPILER=$C_COMPILER \ + -DCMAKE_CXX_COMPILER=$CXX_COMPILER \ -DCMAKE_C_FLAGS="${BUILD_FLAGS}" \ -DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \ -DCMAKE_INSTALL_PREFIX=$RootDirectory \ @@ -92,8 +96,8 @@ elif [ $CrossBuildUsingMariner -eq 1 ]; then -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=$RootDirectory \ -DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \ - -DCMAKE_C_COMPILER=$(command -v clang) \ - -DCMAKE_CXX_COMPILER=$(command -v clang++) \ + -DCMAKE_C_COMPILER=$C_COMPILER \ + -DCMAKE_CXX_COMPILER=$CXX_COMPILER \ -DCMAKE_C_FLAGS="${BUILD_FLAGS}" \ -DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \ -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \ @@ -109,8 +113,8 @@ else -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=$RootDirectory \ -DCMAKE_CROSSCOMPILING=$CMakeCrossCompiling \ - -DCMAKE_C_COMPILER=$(command -v clang) \ - -DCMAKE_CXX_COMPILER=$(command -v clang++) \ + -DCMAKE_C_COMPILER=$C_COMPILER \ + -DCMAKE_CXX_COMPILER=$CXX_COMPILER \ -DCMAKE_C_FLAGS="${BUILD_FLAGS}" \ -DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \ -DCMAKE_INCLUDE_PATH=$CrossRootfsDirectory/usr/include \ diff --git a/doc/building-coredistools.md b/doc/building-coredistools.md index 9651e5f4..b1a0c08f 100644 --- a/doc/building-coredistools.md +++ b/doc/building-coredistools.md @@ -105,3 +105,17 @@ sudo tdnf install -y ncurses-compat export PATH=$(pwd)/bin:$PATH ./build-coredistools.sh linux-arm /crossrootfs/arm ``` + +8. Build `libcoredistools.so` for Linux riscv64 under Docker: +There is no CBL Mariner container for RISC-V so use the standard Ubuntu cross build container used for e.g. dotnet/runtime. +``` +docker run -it --rm --entrypoint /bin/bash -v ~/git/jitutils:/opt/code -w /opt/code mcr.microsoft.com/dotnet-buildtools/prereqs:ubuntu-22.04-cross-riscv64 +apt install libtinfo5 + +# If you haven't built llvm-tblgen in step 5, you can do so in the same docker (pass "/" as crossrootfs). +./build-tblgen.sh linux-x64 / + +# Now, the main course +export PATH=$(pwd)/bin:$PATH +./build-coredistools.sh linux-riscv64 /crossrootfs/riscv64 +``` \ No newline at end of file