I am building a riscv application development environment based on llvm. Build clang using the following cmake command:
cmake -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${path}/llvm_host \
-DLLVM_TARGETS_TO_BUILD="RISCV" \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_USE_LINKER=gold \
-DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-linux-gnu" \
../llvm
Then compile 64-bit glibc using the following command based on the official riscv-gnu-toolchain source:
../configure --prefix=${path}/glibc64_host
make linux -j8
Finally, compile the âtest.cppâ using clang and glibc64:
// file test.cpp
#include<stdio.h>
int main(int argc, char *argv[]){
printf("hello\n");
return 0;
}
${path}/llvm_host/bin/clang --target=riscv64 -march=rv64imafcv \
--sysroot=${path}/glibc64_host/sysroot \
--gcc-toolchain=${path}/glibc64_host \
-g -fuse-ld=lld test.cpp -o test
However, clang complains that it canât find âstdio.hâ :
test.cpp:1:9: fatal error: 'stdio.h' file not found
Furthermore, when clang is given the path of âstdio.hâ using the -I
command, it further complains that the runtime library is not found:
ld.lld: error: cannot open crt0.o: No such file or directory
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lgloss
Notice that âcrt0.oâ is newlibâs launch library function, not glibcâs. If I compile test.cpp against newlib, everything is fine.
${path}/llvm_host/bin/clang --target=riscv64 -march=rv64imafcv \
--sysroot=${path}/newlib64_host/sysroot \
--gcc-toolchain=${path}/newlib64_host \
-g -fuse-ld=lld test.cpp -o test
It looks like the default runtime library for clang is newlib. What can I do to get clang to treat glibc as a runtime library , so that it looks for the relevant headers and links to the correct launcher functions, the way glibc works?
Specifically, Iâm not sure what cmake options should be configured to build clang, and just changing 'CMAKE_CXX_FLAGS 'and 'CMAKE_EXE_LINKER_FLAGS 'doesnât seem to fix the root cause of the problem.
Another problem was that I didnât know how to view the cmake options of the llvm source code. I tried to view the cmake options with the following command:
$user:/home/user/llvm-project/build$ cmake -LAH -DCMAKE_MODULE_PATH=../cmake/Modules ../
However, cmake keeps telling me that the cmakelists.txt file in the target directory doesnât exist!
CMake Error: The source directory "/home/user/work/llvm-project/" does not appear to contain CMakeLists.txt.
I know llvm uses cmake modules to perform cmake builds, but the argument âDCMAKE_MODULE_PATHâ doesnât seem to get this message to cmake.
I donât know how to get clang to compile with glibc as runtime library, and the clang version is 14.0.6. Any suggestions would be helpful, thanks!