Page 1 of 1

Compiling libc++ with clang

Posted: Fri Mar 11, 2022 10:45 am
by Crumble14
Hello!

I'm currently trying to cross-compile libc++ (the C++ standard library for clang) to be able to cross-compile compiler-rt, which in turn should make me able to cross compile programs to run in my kernel's userspace.

The commands I have been using are:

Code: Select all

cd llvm
mkdir -p build
cmake \
	-G Ninja \
	-S runtimes \
	-B build \
	-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
	-DCMAKE_INSTALL_PREFIX="$SYSROOT" \
	-DCMAKE_C_COMPILER="/bin/clang" \
	-DCMAKE_CXX_COMPILER="/bin/clang++" \
	-DCMAKE_C_COMPILER_TARGET="$TARGET" \
	-DCMAKE_CXX_COMPILER_TARGET="$TARGET"
ninja -C build cxx cxxabi unwind
ninja -C build install-cxx install-cxxabi install-unwind
cd ..
(The sysroot contains the compiled musl C library and the target is i686-unknown-linux-musl)

Code: Select all

-- The C compiler identification is Clang 10.0.0
-- The CXX compiler identification is Clang 10.0.0
-- The ASM compiler identification is Clang
-- Found assembler: /bin/clang
-- Check for working C compiler: /bin/clang
-- Check for working C compiler: /bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /bin/clang++
-- Check for working CXX compiler: /bin/clang++ -- broken
CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCXXCompiler.cmake:53 (message):
  The C++ compiler
 
    "/bin/clang++"
 
  is not able to compile a simple test program.
 
  It fails with the following output:
 
    Change Dir: /home/llenotre/Desktop/dev/gcc_maestro/llvm/build/CMakeFiles/CMakeTmp
 
    Run Build Command(s):/bin/ninja cmTC_1a17f && [1/2] Building CXX object CMakeFiles/cmTC_1a17f.dir/testCXXCompiler.cxx.o
    [2/2] Linking CXX executable cmTC_1a17f
    FAILED: cmTC_1a17f 
    : && /bin/clang++ --target=i686-unknown-linux-musl     CMakeFiles/cmTC_1a17f.dir/testCXXCompiler.cxx.o  -o cmTC_1a17f   && :
    /bin/ld: skipping incompatible /bin/../lib/gcc/x86_64-linux-gnu/9/libstdc++.so when searching for -lstdc++
    /bin/ld: skipping incompatible /bin/../lib/gcc/x86_64-linux-gnu/9/libstdc++.a when searching for -lstdc++
    /bin/ld: cannot find -lstdc++
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.
 
 
 
 
 
  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)
 
 
-- Configuring incomplete, errors occurred!
See also "/home/llenotre/Desktop/dev/gcc_maestro/llvm/build/CMakeFiles/CMakeOutput.log".
See also "/home/llenotre/Desktop/dev/gcc_maestro/llvm/build/CMakeFiles/CMakeError.log".
And I just can't figure out why compiling libc++ seems to require the standard C++ library :(

Thanks for your help :)

Re: Compiling libc++ with clang

Posted: Fri Mar 11, 2022 11:44 am
by nexos
Cross compiling LLVM is an absolute nightmare. The problem with what you're doing is that you're telling the build system to build libc++ with the host the same command line as the programs like clang. This won't work because we need to pass options such as -nostdlib in order to cross compile the support libraries. The solution? Look at this article on LLVM's docs. Please note that this is for compiler-rt, but the process for libc++ should be somewhat similar (just different option names. Use the ccmake configuration tool in the build directory of libc++ to look at the available options for libc++). Note that when it says /path/to/clang for instance, this is the compiler that was built, not /bin/clang.

https://llvm.org/docs/HowToCrossCompile ... OnArm.html

Also, here's some code I wrote in the past to do this. It's full of hacks because of how crazy cross compiling LLVM is:

https://github.com/nexos-dev/nexnix/blo ... in.sh#L301

Re: Compiling libc++ with clang

Posted: Sat Mar 12, 2022 11:50 am
by Crumble14
nexos wrote:Cross compiling LLVM is an absolute nightmare. The problem with what you're doing is that you're telling the build system to build libc++ with the host the same command line as the programs like clang. This won't work because we need to pass options such as -nostdlib in order to cross compile the support libraries. The solution? Look at this article on LLVM's docs. Please note that this is for compiler-rt, but the process for libc++ should be somewhat similar (just different option names. Use the ccmake configuration tool in the build directory of libc++ to look at the available options for libc++). Note that when it says /path/to/clang for instance, this is the compiler that was built, not /bin/clang.

https://llvm.org/docs/HowToCrossCompile ... OnArm.html

Also, here's some code I wrote in the past to do this. It's full of hacks because of how crazy cross compiling LLVM is:

https://github.com/nexos-dev/nexnix/blo ... in.sh#L301
Thanks for your help! Looking at the documentation and your script I was able to compile everything :)

Re: Compiling libc++ with clang

Posted: Sat Mar 12, 2022 2:20 pm
by nexos
Oh, one more thing. If your OS targets x86_64, then you should add -mno-red-zone to CMAKE_C_FLAGS and CMAKE_CXX_FLAGS.

If you haven't heard of the red zone, look here:

https://wiki.osdev.org/System_V_ABI#x86-64