Anatoly Trosinenko 4da745a0f4
[BOLT] Fix unit test failures with LLVM_LINK_LLVM_DYLIB=ON (#152190)
When LLVM_LINK_LLVM_DYLIB is ON, `check-bolt` target reports unit test
failures:

    BOLT-Unit :: Core/./CoreTests/failed_to_discover_tests_from_gtest
    BOLT-Unit :: Profile/./ProfileTests/failed_to_discover_tests_from_gtest

The reason is that when llvm-lit runs a unit-test executable:

    /path/to/CoreTests --gtest_list_tests '--gtest_filter=-*DISABLED_*'

an assertion is triggered with the following message:

    LLVM ERROR: Option 'default' already exists!

This assertion triggers when the initializer of defaultListDAGScheduler
defined at SelectionDAGISel.cpp:219 is called as a statically-linked
function after already being called during the initialization of
libLLVM.

The issue can be traced down to LLVMTestingSupport library which depends
on libLLVM as neither COMPONENT_LIB nor DISABLE_LLVM_LINK_LLVM_DYLIB is
specified in a call to `add_llvm_library(LLVMTestingSupport ...)`.

Specifying DISABLE_LLVM_LINK_LLVM_DYLIB for LLVMTestingSupport makes
Clang unit test fail and COMPONENT_LIB is probably inappropriate for a
testing-specific library, thus as a workaround, added Error.cpp source
from LLVMTestingSupport directly to the list of source files of
CoreTests target (as it depends on
`llvm::detail::TakeError(llvm::Error)`) and removed LLVMTestingSupport
from the list of dependencies of ProfileTests.
2025-08-07 11:27:15 +03:00

38 lines
874 B
CMake

set(LLVM_LINK_COMPONENTS
DebugInfoDWARF
Object
MC
${BOLT_TARGETS_TO_BUILD}
)
add_bolt_unittest(CoreTests
BinaryContext.cpp
MCPlusBuilder.cpp
MemoryMaps.cpp
DynoStats.cpp
# FIXME CoreTests uses `llvm::detail::TakeError(llvm::Error)`, but linking
# to LLVMTestingSupport introduces a transitive dependency on the
# dynamic LLVM library when LLVM_LINK_LLVM_DYLIB is ON.
${LLVM_MAIN_SRC_DIR}/lib/Testing/Support/Error.cpp
DISABLE_LLVM_LINK_LLVM_DYLIB
)
target_link_libraries(CoreTests
PRIVATE
LLVMBOLTCore
LLVMBOLTRewrite
LLVMBOLTProfile
LLVMBOLTUtils
)
foreach (tgt ${BOLT_TARGETS_TO_BUILD})
include_directories(
${LLVM_MAIN_SRC_DIR}/lib/Target/${tgt}
${LLVM_BINARY_DIR}/lib/Target/${tgt}
)
string(TOUPPER "${tgt}" upper)
target_compile_definitions(CoreTests PRIVATE "${upper}_AVAILABLE")
endforeach()