From 68d27596e0f0dea6f09cb149f89010bab65a8dc1 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Mon, 28 Aug 2023 13:12:47 -0700 Subject: [PATCH] Do not link LLVM libraries into cpptest binary if they are visible in TVM They are already linked into libtvm.so, and cpptests links against libtvm.so, so if they are visible in the TVM library, they are not needed again. On the other hand, linking them twice can lead to the following error: ``` CommandLine Error: Option 'print-summary-global-ids' registered more than once! LLVM ERROR: inconsistency in registered CommandLine options ``` --- CMakeLists.txt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b69145806ad..75f80bfaac0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -713,7 +713,23 @@ if(GTEST_FOUND) # include runtime files for unit testing target_link_libraries(cpptest PRIVATE ${TVM_TEST_LIBRARY_NAME} GTest::GTest GTest::Main GTest::gmock pthread dl) if(DEFINED LLVM_LIBS) - target_link_libraries(cpptest PRIVATE ${LLVM_LIBS}) + # The TVM library is linked with LLVM libraries. If the LLVM libraries are + # static and the symbols are not hidden, then don't link them again into + # cpptest since cpptest is itself linked against the TVM library. If static + # LLVM libraries are linked in twice, it can cause issues with global + # variable initialization (cl::opt). + # If the LLVM libraries are dynamic, we have to link them again, since the + # TVM library will not contain any LLVM definitions. + unset(LLVM_SO) + foreach(L IN LISTS LLVM_LIBS) + if(L MATCHES "libLLVM.*\.so") + set(LLVM_SO TRUE) + break() + endif() + endforeach() + if(DEFINED LLVM_SO OR HIDE_PRIVATE_SYMBOLS) + target_link_libraries(cpptest PRIVATE ${LLVM_LIBS}) + endif() endif() if(DEFINED ETHOSN_RUNTIME_LIBRARY) target_link_libraries(cpptest PRIVATE ${ETHOSN_RUNTIME_LIBRARY})