kraj / meta-clang

Clang C/C++ cross compiler and runtime for OpenEmbedded/Yocto Project
MIT License
151 stars 192 forks source link

nativesdk-clang build fails with DEBUG_BUILD enabled #932

Closed lms-ts closed 2 weeks ago

lms-ts commented 3 months ago

We are using the nanbield branch of meta-clang. The following behaviour can also be observed with the newest scarthgap commit.

We are building nativesdk-clang. The build is successful, however, with DEBUG_BUILD = "1" set in local.conf the build fails with following error:

2024-03-21 12:38:46 - INFO     - | FAILED: lib/libLLVM-17.so
[...]
2024-03-21 12:38:46 - INFO     - | /home/lms-yocto-worker-2/LY_project-lms-icecake_build_production/build/build/tmp/work/x86_64-nativesdk-oesdk-linux/nativesdk-clang/17.0.4/recipe-sysroot-native/usr/bin/x86_64-oesdk-linux/x86_64-oesdk-linux-ld: lib/libLLVMSupport.a(CrashRecoveryContext.cpp.o): relocation R_X86_64_TPOFF32 against `_ZN12_GLOBAL__N_121IsRecoveringFromCrashE' can not be used when making a shared object; recompile with -fPIC
2024-03-21 12:38:46 - INFO     - | /home/lms-yocto-worker-2/LY_project-lms-icecake_build_production/build/build/tmp/work/x86_64-nativesdk-oesdk-linux/nativesdk-clang/17.0.4/recipe-sysroot-native/usr/bin/x86_64-oesdk-linux/x86_64-oesdk-linux-ld: failed to set dynamic section sizes: bad value
2024-03-21 12:38:46 - INFO     - | x86_64-oesdk-linux-clang++: error: linker command failed with exit code 1 (use -v to see invocation)

This error can be fixed by setting

CXXFLAGS:append:class-nativesdk = " -fPIC"
CFLAGS:append:class-nativesdk = " -fPIC"

in a clang_%.bbappend.

The question: Would it be useful to always set the -fPIC flag for nativesdk in the clang_git.bb recipe? Or maybe conditionally, depending on the DEBUG_BUILD flag?

kraj commented 3 months ago

hmm it seems to be a bug in LLVM build. Can you see why it happens with nativesdk alone ?

lms-ts commented 3 months ago

I just verified that clang builds perfectly fine with DEBUG_BUILD enabled.

I don't see an obvious reason, why nativesdk specifically fails. I may need to take a closer look into the do_compile logs.

With mickledore the issue with nativesdk-clang wasn't there.

lms-ts commented 3 months ago

log.do_compile-nativesdkclang.txt log.do_compile-clang.txt

Here are the logs with DEBUG_BUILD = "1" for bitbake clang and the failing bitbake nativesdk-clang (without -fPIC added to the CXXFLAGS).

kraj commented 3 months ago

if -DLLVM_ENABLE_PIC=ON -DCLANG_DEFAULT_PIE_ON_LINUX=ON is making into config step that should have done it

lms-ts commented 3 months ago

Yeah, these 2 options are set in both the nativesdk-clang and clang when taking a look at the EXTRA_OECMAKE variable, but only clang itself seems to set the -fPIC option without explicitly adding it to the CXXFLAGS.

lms-ts commented 3 months ago
-- LLVM default target triple: x86_64-unknown-linux-gnu
-- Performing Test C_SUPPORTS_FPIC
-- Performing Test C_SUPPORTS_FPIC - Failed
-- Performing Test CXX_SUPPORTS_FPIC
-- Performing Test CXX_SUPPORTS_FPIC - Failed
CMake Warning at cmake/modules/HandleLLVMOptions.cmake:320 (message):
  -fPIC is not supported.
Call Stack (most recent call first):
  cmake/modules/HandleLLVMOptions.cmake:380 (add_flag_or_print_warning)
  CMakeLists.txt:936 (include)

That is in the log.do_configure of nativesdk-clang

kraj commented 3 months ago
-- LLVM default target triple: x86_64-unknown-linux-gnu
-- Performing Test C_SUPPORTS_FPIC
-- Performing Test C_SUPPORTS_FPIC - Failed
-- Performing Test CXX_SUPPORTS_FPIC
-- Performing Test CXX_SUPPORTS_FPIC - Failed
CMake Warning at cmake/modules/HandleLLVMOptions.cmake:320 (message):
  -fPIC is not supported.
Call Stack (most recent call first):
  cmake/modules/HandleLLVMOptions.cmake:380 (add_flag_or_print_warning)
  CMakeLists.txt:936 (include)

That is in the log.do_configure of nativesdk-clang

that might be the right direction to look into. Find out why cmake test for fPIC support detection is failing ? also see if this test fails for native or target clang builds too.

lms-ts commented 3 months ago

The test only fails for nativesdk with debug enabled.

Thanks for your help so far, I will take a closer look next week why cmake thinks that the compiler does not support the -fPIC option even though I can set it manually via CXXFLAGS and build just fine.

lms-ts commented 3 months ago

I got further into it.

The problem are the

x86_64-oesdk-linux-clang++: warning: argument unused during compilation: '-feliminate-unused-debug-types' [-Wunused-command-line-argument]

warnings in the nativesdk-clang log (but strangely they appear only with DEBUG_BUILD enabled). This warning is not present in the clang log even though the same argument is used.

Taking a look at

https://github.com/llvm/llvm-project/blob/main/llvm/cmake/modules/HandleLLVMOptions.cmake#L336

one can see that the -WError flag is set during the check for -fPIC. So, due to the above warning, the check for the flag fails.

I have no idea yet why clang does not use the compilation flag under those specific circumstances.

Taking a look into poky -feliminate-unused-debug-types is set here per default: https://github.com/yoctoproject/poky/blob/ae7056844aa05a239384335a66684394e10290a6/meta/conf/bitbake.conf#L666

kraj commented 3 months ago

Perhaps try adding DEBUG_FLAGS:append:class-nativesdk = " -Wno-error=unused-command-line-argument" but I think DEBUG_FLAGS should only be used for target packages, it does not make sense to use it for native, nativesdk, cross packages.

lms-ts commented 3 months ago

Yes, BUILDSDK_CPPFLAGS seems to be the better variable which is meant for the nativesdk context specifically: https://docs.yoctoproject.org/ref-manual/variables.html#term-BUILDSDK_CPPFLAGS

I added

BUILDSDK_CPPFLAGS:append:class-nativesdk = " -Wno-error=unused-command-line-argument"

and the nativesdk can be built successfully. BUILDSDK_CXXFLAGS could probably also be used instead.