Skip to content

Commit e43022c

Browse files
liushuyuviktormalik
authored andcommitted
cmake: fix the LLVM linking logic ...
... when system contains both shared and static libraries and shared libraries are preferred over the static one. The current behavior is to link static libraries even if shared ones are preferred. `llvm_config` macro does the component to library resolution automatically and will consider if any library contains more than one components. `ast` CMake target now uses the new style `target_link_libraries` to match `llvm_config` macro's behavior to avoid CMake complaining that mixing both styles is not allowed.
1 parent 0f6214d commit e43022c

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/ast/CMakeLists.txt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ add_library(ast STATIC
2424
)
2525

2626
target_compile_definitions(ast PRIVATE ${BPFTRACE_FLAGS})
27-
target_link_libraries(ast ast_defs arch parser)
27+
target_link_libraries(ast PUBLIC ast_defs arch parser)
2828

2929
if(STATIC_LINKING)
3030
include(Util)
@@ -66,23 +66,19 @@ if(STATIC_LINKING)
6666
unlink_transitive_dependency("${CLANG_EXPORTED_TARGETS}" "$<LINK_ONLY:clang-cpp>")
6767

6868
if(TARGET libclang_static)
69-
target_link_libraries(ast libclang_static)
69+
target_link_libraries(ast PUBLIC libclang_static)
7070
else()
7171
# old LLVM versions don't export libclang_static in ClangTargets.cmake; fall back to
7272
# libclang.a in that case
73-
target_link_libraries(ast libclang.a)
73+
target_link_libraries(ast PUBLIC libclang.a)
7474
endif()
7575

76-
target_link_libraries(ast ${clang_libs})
77-
target_link_libraries(ast ${llvm_libs})
76+
target_link_libraries(ast PUBLIC ${clang_libs})
77+
target_link_libraries(ast PUBLIC ${llvm_libs})
7878
else()
79-
find_library(found_LLVM LLVM HINTS ${LLVM_LIBRARY_DIRS})
80-
if(found_LLVM)
81-
target_link_libraries(ast LLVM)
82-
else()
83-
llvm_map_components_to_libnames(_llvm_libs bpfcodegen ipo irreader mcjit orcjit)
84-
llvm_expand_dependencies(llvm_libs ${_llvm_libs})
85-
target_link_libraries(ast ${llvm_libs})
86-
endif()
87-
target_link_libraries(ast libclang)
79+
# llvm_config macro comes from the LLVM toolchain and will auto-resolve component
80+
# names to library names. USE_SHARED option will tell llvm_config macro to prefer
81+
# shared library / DLL on the system over the static libraries
82+
llvm_config(ast USE_SHARED bpfcodegen ipo irreader mcjit orcjit)
83+
target_link_libraries(ast PUBLIC libclang)
8884
endif()

0 commit comments

Comments
 (0)