Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISC-V] Build coredistools #387

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions build-coredistools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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-<version>` 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)
Expand All @@ -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?
Expand Down Expand Up @@ -79,6 +79,13 @@ case "$TargetOSArchitecture" in
LLVMTargetsToBuild="LoongArch"
;;

linux-riscv64)
CMakeCrossCompiling=ON
LLVMHostTriple=riscv64-linux-gnu
LLVMTargetsToBuild="RISCV"
EnsureCrossRootfsDirectoryExists
;;

osx-arm64)
CMakeCrossCompiling=ON
CMakeOSXArchitectures=arm64
Expand Down
16 changes: 10 additions & 6 deletions build-tblgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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" \
Expand All @@ -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 \
Expand Down
14 changes: 14 additions & 0 deletions doc/building-coredistools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
6 changes: 6 additions & 0 deletions src/coredistools/coredistools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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()));
Expand Down
3 changes: 2 additions & 1 deletion src/coredistools/coredistools.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enum TargetArch {
Target_X64,
Target_Thumb,
Target_Arm64,
Target_LoongArch64
Target_LoongArch64,
Target_RiscV64,
};

struct CorDisasm;
Expand Down
Loading