libriscv icon indicating copy to clipboard operation
libriscv copied to clipboard

clang toolchain compilation for newbies?

Open ninnghazad opened this issue 4 years ago • 3 comments

i am playing around with the examples, and using the toolchain linked in the readme works. now i want a toolchain i could redistribute, so i am trying to build a llvm toolchain that can target riscv32 and comes with a non-gpl-ed static libc/cxx. i would like to play around with c++ including stl but (mostly) my own syscalls in the vm. but i am having a hard time getting the llvm toolchain compiled, especially the llvm-libc. i tried using a riscv-musl instead, but that collides horribly over std::size_t being 32 or 64 bit. seeing clang/llvm mentioned, are you able to provide the options you used to compile a working llvm toolchain that goes nicely with the vm?

i am currently using this for llvm: cmake ../llvm-project/llvm -DLLVM_TARGETS_TO_BUILD="RISCV" -DCMAKE_INSTALL_PREFIX=${TOOLCHAIN_DIR} -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;libc;libcxx;libcxxabi" -DLLVM_ENABLE_THREADS=on -DLLVM_STATIC_LINK_CXX_STDLIB=on but as i said the libc part will fail, complaining about a missing syscall.h. this is with the latest git llvm, same with release 10.

ninnghazad avatar Mar 30 '20 02:03 ninnghazad

I compiled with clang in freestanding mode and with a dash of a tiny libc + malloc implemented using syscalls.

https://github.com/fwsGonzo/libriscv/blob/master/binaries/barebones/libc/heap.hpp https://github.com/fwsGonzo/libriscv/blob/master/emulator/src/native_libc.cpp Here, the system calls are only used to accelerate the heap chunk operations, as the memory itself will always be inside the virtual memory of the guest. I am using this method in the barebones binary example as you can see, and it already supports Clang. You just have to set Clang as your compiler and implement any missing library functions yourself. I am re-using the system headers from the embedded RISC-V pack: https://github.com/fwsGonzo/libriscv/blob/master/binaries/barebones/toolchain.cmake Maybe you can replace the toolchain itself with your own headers? And go from there.

For a full system like what you suggest I would have to sit down and try it myself. musl will not work on riscv32 because it's still on their TODO list, but I will for sure add a musl example binary once that is possible.

As far as performance goes, I have had better performance with RISC-V on GCC, while the emuator itself has something like 20-25% better performance with Clang-11. I'll update you once I have tried to build llvm-libc for rv32g/rv32gc.

fwsGonzo avatar Mar 30 '20 11:03 fwsGonzo

It looks like libc does not build at all, for any target. Perhaps it's best to just wait until it grows up and becomes buildable. I'm using clang-11 and cmake-3.17 and I was not able to build it at all.

make[2]: *** No rule to make target 'projects/libc/src/math/round.o', needed by 'projects/libc/lib/libllvmlibm.a'.  Stop.
CMakeFiles/Makefile2:40806: recipe for target 'projects/libc/lib/CMakeFiles/llvmlibm.dir/all' failed
make[1]: *** [projects/libc/lib/CMakeFiles/llvmlibm.dir/all] Error 2

fwsGonzo avatar Mar 30 '20 15:03 fwsGonzo

Thank you for checking. Even the binary packages provided by llvm don't seem to include their libc - and apparently never have. It seems llvm-libc, while existing in the main branch, isn't actually ready and not mentioned as an available project.

ninnghazad avatar Mar 30 '20 16:03 ninnghazad