Skip to content

Commit 2cccfba

Browse files
committed
libLLVM.a doesn't exist, so fix how enzyme is linking LLVM
1 parent 4bcf46f commit 2cccfba

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

enzyme/Enzyme/CMakeLists.txt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ endif()
88

99
get_target_property(TBL_LINKED_LIBS LLVMSupport INTERFACE_LINK_LIBRARIES)
1010
if (NOT TBL_LINKED_LIBS)
11+
message(STATUS "No TBL_LINKED_LIBS found")
1112
else()
12-
list(REMOVE_ITEM TBL_LINKED_LIBS "ZLIB::ZLIB")
13+
message(STATUS "TBL_LINKED_LIBS (test): ${TBL_LINKED_LIBS}")
14+
#list(REMOVE_ITEM TBL_LINKED_LIBS "ZLIB::ZLIB")
1315
set_property(TARGET LLVMSupport PROPERTY INTERFACE_LINK_LIBRARIES ${TBL_LINKED_LIBS})
1416
endif()
1517

@@ -145,7 +147,36 @@ if (${ENZYME_EXTERNAL_SHARED_LIB})
145147
add_dependencies(Enzyme-${LLVM_VERSION_MAJOR} BlasDeclarationsIncGen)
146148
add_dependencies(Enzyme-${LLVM_VERSION_MAJOR} BlasTAIncGen)
147149
add_dependencies(Enzyme-${LLVM_VERSION_MAJOR} BlasDiffUseIncGen)
148-
target_link_libraries(Enzyme-${LLVM_VERSION_MAJOR} LLVM)
150+
151+
if (${LLVM_BUILD_LLVM_DYLIB})
152+
target_link_libraries(Enzyme-${LLVM_VERSION_MAJOR} LLVM)
153+
else()
154+
# This would be the desired way to link against LLVM components,
155+
# however this function is bugged and does not work with `all`, see:
156+
# https://github.com/llvm/llvm-project/issues/46347
157+
#llvm_map_components_to_libnames(llvm_libraries Passes)
158+
# Therefore, manually invoke llvm-config
159+
if (EXISTS "${LLVM_TOOLS_BINARY_DIR}/llvm-config")
160+
message(STATUS "Using llvm-config from ${LLVM_TOOLS_BINARY_DIR}")
161+
else()
162+
message(SEND_ERROR "llvm-config not found in ${LLVM_TOOLS_BINARY_DIR}")
163+
endif()
164+
execute_process(COMMAND ${LLVM_TOOLS_BINARY_DIR}/llvm-config --libs all
165+
OUTPUT_VARIABLE llvm_libraries)
166+
string(STRIP "${llvm_libraries}" llvm_libraries)
167+
message(STATUS "Linking against LLVM libraries: ${llvm_libraries}")
168+
# In theory, adding --libs should also add all the -l flags,
169+
# but it isn't picked up correctly by clang, so we call target_link_libraries
170+
execute_process(COMMAND ${LLVM_TOOLS_BINARY_DIR}/llvm-config --ldflags
171+
OUTPUT_VARIABLE llvm_ldflags)
172+
string(STRIP "${llvm_ldflags}" llvm_ldflags)
173+
message(STATUS "Linking against LLVM ldflags: ${llvm_ldflags}")
174+
set_target_properties(Enzyme-${LLVM_VERSION_MAJOR} PROPERTIES LINK_FLAGS ${llvm_ldflags})
175+
target_link_libraries(Enzyme-${LLVM_VERSION_MAJOR} ${llvm_libraries})
176+
177+
llvm_map_components_to_libnames(llvm_librariess Passes Support)
178+
target_link_libraries(Enzyme-${LLVM_VERSION_MAJOR} ${llvm_librariess})
179+
endif()
149180
install(TARGETS Enzyme-${LLVM_VERSION_MAJOR}
150181
EXPORT EnzymeTargets
151182
LIBRARY DESTINATION lib COMPONENT shlib

0 commit comments

Comments
 (0)