需要 python3.9 cmake3.21.1
注意这里 gcc-12 需要的 c 库版本是 2.33及更高. 如果不够的话也不行. 这里 c 库的位置也在 gcc-12 的 lib/ 里面
可能的 patch
///////// ./libcxx/include/__memory/aligned_alloc.h
/////////加上这个函数
inline void* __aligned_alloc(size_t __alignment, size_t __size) {
#if defined(_LIBCPP_HAS_ALIGNED_ALLOC)
return ::aligned_alloc(__alignment, __size);
#else
void* p = nullptr;
if (posix_memalign(&p, __alignment, __size) != 0) {
return nullptr;
}
return p;
#endif
}
/////然后改下原来的 aligned_alloc
// return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
return ::__aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
libcxx编译过程中会有 link 报错
[100%] Linking CXX shared library /home/zorch/tar_zip_pkg/llvm-main/llvm-project-21.1.1.src/build.clang/lib/x86_64-unknown-linux-gnu/libc++.so
/opt/compiler/gcc-12/bin/ld: CMakeFiles/cxx_shared.dir/chrono.cpp.o: in function `std::__1::chrono::system_clock::now()':
chrono.cpp:(.text._ZNSt3__16chrono12system_clock3nowEv+0xc): undefined reference to `clock_gettime'
/opt/compiler/gcc-12/bin/ld: CMakeFiles/cxx_shared.dir/chrono.cpp.o: in function `std::__1::chrono::steady_clock::now()':
chrono.cpp:(.text._ZNSt3__16chrono12steady_clock3nowEv+0xf): undefined reference to `clock_gettime'
/opt/compiler/gcc-12/bin/ld: CMakeFiles/cxx_shared.dir/filesystem/filesystem_clock.cpp.o: in function `std::__1::__fs::filesystem::_FilesystemClock::now()':
filesystem_clock.cpp:(.text._ZNSt3__14__fs10filesystem16_FilesystemClock3nowEv+0xc): undefined reference to `clock_gettime'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
需要手动增加一个链接库
vi build.clang/runtimes/runtimes-bins/libcxx/src/CMakeFiles/cxx_shared.dir/link.txt
## 最后加上-lrt
#再执行 cmake --build build.clang 进行编译.
$ cmake --build build.clang -j20
$ cmake --install build.clang
正式编译
llvm-core
export PATH=../python-3.9.16/bin/:$PATH
../cmake-3.21.1/bin/cmake \
-G "Unix Makefiles" \
-S llvm \
-B build.llvm \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DLLVM_PARALLEL_LINK_JOBS=2 \
-DPython3_EXECUTABLE=../python-3.9.16/bin/python3.9 \
-DPython3_INCLUDE_DIR=../python-3.9.16/include/python3.9 \
-DPython3_LIBRARY=../python-3.9.16/lib/libpython3.9.so \
-DLLVM_INSTALL_UTILS=ON \
-DCMAKE_INSTALL_PREFIX=./for-llvm/llvm-release \
-DCMAKE_PREFIX_PATH=/opt/compiler/gcc-12/ \
-DCMAKE_C_COMPILER=/opt/compiler/gcc-12/bin/gcc \
-DCMAKE_CXX_COMPILER=/opt/compiler/gcc-12/bin/g++ \
-DCMAKE_INCLUDE_PATH="/opt/compiler/gcc-12/include/" \
-DCMAKE_LIBRARY_PATH="/opt/compiler/gcc-12/lib/" \
-DCMAKE_EXE_LINKER_FLAGS="-Wl,-rpath,/opt/compiler/gcc-12/lib:/lib64" \
-DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,/opt/compiler/gcc-12/lib:/lib64 -L/opt/compiler/gcc-12/lib -L/lib64"
nohup ../cmake-3.21.1/bin/cmake --build build.llvm -j40 &> log.build &
../cmake-3.21.1/bin/cmake --install build.llvm
clang/clangd/other tools
../cmake-3.21.1/bin/cmake \
-G "Unix Makefiles" \
-S clang \
-B build.clang.extra \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_PARALLEL_LINK_JOBS=2 \
-DLLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR=./clang-tools-extra \
-DLLVM_EXTERNAL_LIT=build.llvm/utils/lit \
-DLLVM_ROOT=/home/for-llvm/llvm-debug \
-DCMAKE_INSTALL_PREFIX=./for-llvm/llvm-release \
-DCMAKE_PREFIX_PATH=/opt/compiler/gcc-12/ \
-DCMAKE_C_COMPILER=/opt/compiler/gcc-12/bin/gcc \
-DCMAKE_CXX_COMPILER=/opt/compiler/gcc-12/bin/g++ \
-DCMAKE_INCLUDE_PATH="/opt/compiler/gcc-12/include/" \
-DCMAKE_LIBRARY_PATH="/opt/compiler/gcc-12/lib/" \
-DCMAKE_EXE_LINKER_FLAGS="-Wl,-rpath,/opt/compiler/gcc-12/lib:/lib64" \
-DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,/opt/compiler/gcc-12/lib:/lib64 -L/opt/compiler/gcc-12/lib -L/lib64"
nohup ../cmake-3.21.1/bin/cmake --build build.clang.extra -j90 &> log.build &
../cmake-3.21.1/bin/cmake --install build.clang.extra
libcxx runtimes
../cmake-3.21.1/bin/cmake \
-G "Unix Makefiles" \
-S runtimes \
-B build.runtimes \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;" \
-DCMAKE_INSTALL_PREFIX=./for-llvm/llvm-release \
-DCMAKE_C_COMPILER=./for-llvm/llvm-release/bin/clang \
-DCMAKE_CXX_COMPILER=./for-llvm/llvm-release/bin/clang++ \
-DCMAKE_C_FLAGS="--gcc-toolchain=/opt/compiler/gcc-12" \
-DCMAKE_CXX_FLAGS="--gcc-toolchain=/opt/compiler/gcc-12" \
-DCMAKE_SHARED_LINKER_FLAGS="-lrt" \
-DCMAKE_EXE_LINKER_FLAGS="-Wl,-rpath,/opt/compiler/gcc-12/lib:/lib64" \
-DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,/opt/compiler/gcc-12/lib:/lib64 -L/opt/compiler/gcc-12/lib -L/lib64"
nohup ../cmake-3.21.1/bin/cmake --build build.runtimes -j40 &> log.build.rt &
../cmake-3.21.1/bin/cmake --install build.runtimes
最后如果有.so 找不到的问题的话, 需要 patchelf