OS:Ubuntu 22.04 x86_64
从https://github.com/llvm/llvm-project 下最新的release
版本,这里下载的是llvm17.0.0-rc4
解压源码后,在源码目录执行编译脚本 build.sh
$ cat build.sh
#!/bin/bash
set -e
set -x
CONFIG_AND_BUILD=true
INSTALL_DIR="$PWD/llvm_install"
while $CONFIG_AND_BUILD ; do
rm -rf _build
mkdir _build
break
done
cd _build
while $CONFIG_AND_BUILD; do
#cmake --log-level=VERBOSE --debug-output \
cmake \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
\
../llvm -G Ninja \
\
-DLLVM_TARGETS_TO_BUILD="all" \
-DLLVM_ENABLE_PROJECTS="clang;lld;compiler-rt" \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
\
-DLLVM_INSTALL_UTILS=ON \
-DLLVM_INSTALL_TOOLCHAIN_ONLY=OFF \
-DBUILD_SHARED_LIBS=OFF \
\
-DLLVM_INCLUDE_TOOLS=ON \
-DLLVM_BUILD_TOOLS=ON \
\
-DLLVM_INCLUDE_UTILS=ON \
-DLLVM_BUILD_UTILS=ON \
\
-DLLVM_INCLUDE_RUNTIMES=ON \
-DLLVM_BUILD_RUNTIME=ON \
\
-DLLVM_INCLUDE_EXAMPLES=ON \
-DLLVM_BUILD_EXAMPLES=OFF \
\
-DLLVM_INCLUDE_TESTS=ON \
-DLLVM_BUILD_TESTS=OFF \
\
-DLLVM_INCLUDE_DOCS=ON \
-DLLVM_BUILD_DOCS=OFF \
-DLLVM_ENABLE_DOXYGEN=OFF \
-DLLVM_ENABLE_SPHINX=OFF \
-DLLVM_ENABLE_OCAMLDOC=OFF \
-DLLVM_ENABLE_BINDINGS=OFF \
\
-DLLVM_BUILD_LLVM_DYLIB=ON \
-DLLVM_LINK_LLVM_DYLIB=ON \
\
-DLLVM_ENABLE_LIBCXX=OFF \
-DLLVM_ENABLE_ZLIB=ON \
-DLLVM_ENABLE_FFI=ON \
-DLLVM_ENABLE_RTTI=ON \
-DCOMPILER_RT_BUILD_LIBRUZZER=OFF
break
done
ninja -v -j6
ninja -v install
下载gcc交叉工具链https://github.com/sunhaiyong1978/CLFS-for-LoongArch/releases/download/8.1/CLFS-loongarch64-8.1.1-x86_64-cross-tools-gcc-libc.tar.xz,并解压到/opt
执行交叉编译脚本 cross_build.sh
$ cat cross_build.sh
#!/bin/bash
set -e
set -x
CONFIG_AND_BUILD=true
#CONFIG_AND_BUILD=false
CONFIG_DIR="$PWD/llvm_install/bin"
LA_TOOLCHAIN=/opt/cross-tools/loongarch64-unknown-linux-gnu
LA_FLAGS="-B$LA_TOOLCHAIN"
while $CONFIG_AND_BUILD ; do
rm -rf _build_cross
mkdir _build_cross
break
done
cd _build_cross
while $CONFIG_AND_BUILD; do
cmake \
-DCMAKE_CROSSCOMPILING=True \
-DLLVM_CONFIG_PATH=$CONFIG_DIR/llvm-config \
-DCMAKE_C_COMPILER=/opt/cross-tools/bin/loongarch64-unknown-linux-gnu-gcc \
-DCMAKE_CXX_COMPILER=/opt/cross-tools/bin/loongarch64-unknown-linux-gnu-g++ \
-DCMAKE_SYSROOT="/opt/cross-tools/target" \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
-DCMAKE_C_COMPILER_TARGET="loongarch64" \
-DCMAKE_CXX_COMPILER_TARGET="loongarch64" \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
-DCMAKE_C_FLAGS="$LA_FLAGS" \
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_INSTALL_PREFIX="`$CONFIG_DIR/clang -print-resource-dir`" \
\
../compiler-rt -G Ninja
break
done
ninja -v -j6
ninja -v install
编译完成之后会安装到当前目录下的 llvm_install
目录。
$ cd llvm_install/bin
$ ./llc -version
LLVM (http://llvm.org/):
LLVM version 17.0.0rc
Optimized build.
Default target: x86_64-unknown-linux-gnu
Host CPU: haswell
Registered Targets:
aarch64 - AArch64 (little endian)
aarch64_32 - AArch64 (little endian ILP32)
aarch64_be - AArch64 (big endian)
amdgcn - AMD GCN GPUs
arm - ARM
arm64 - ARM64 (little endian)
arm64_32 - ARM64 (little endian ILP32)
armeb - ARM (big endian)
avr - Atmel AVR Microcontroller
bpf - BPF (host endian)
bpfeb - BPF (big endian)
bpfel - BPF (little endian)
hexagon - Hexagon
lanai - Lanai
loongarch32 - 32-bit LoongArch
loongarch64 - 64-bit LoongArch
mips - MIPS (32-bit big endian)
mips64 - MIPS (64-bit big endian)
mips64el - MIPS (64-bit little endian)
mipsel - MIPS (32-bit little endian)
msp430 - MSP430 [experimental]
nvptx - NVIDIA PTX 32-bit
nvptx64 - NVIDIA PTX 64-bit
ppc32 - PowerPC 32
ppc32le - PowerPC 32 LE
ppc64 - PowerPC 64
ppc64le - PowerPC 64 LE
r600 - AMD GPUs HD2XXX-HD6XXX
riscv32 - 32-bit RISC-V
riscv64 - 64-bit RISC-V
sparc - Sparc
sparcel - Sparc LE
sparcv9 - Sparc V9
systemz - SystemZ
thumb - Thumb
thumbeb - Thumb (big endian)
ve - VE
wasm32 - WebAssembly 32-bit
wasm64 - WebAssembly 64-bit
x86 - 32-bit X86: Pentium-Pro and above
x86-64 - 64-bit X86: EM64T and AMD64
xcore - XCore