Skip to content

Commit

Permalink
[CMake] Support LLVM-16 static linking
Browse files Browse the repository at this point in the history
LLVM static linking in TVM relies on default flags provided by
llvm-config, which however does not depend on modern CMake features,
namely, imported targets, leading to a series of issues on builds
particularly on macOS and Windows. This PR strengthens CMake
configuration process to avoid such behavior.
  • Loading branch information
junrushao committed Jun 30, 2023
1 parent 28aead9 commit c5bec87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
32 changes: 26 additions & 6 deletions cmake/utils/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ macro(find_llvm use_llvm)
if(NOT "${__llvm_exit_code}" STREQUAL "0")
message(FATAL_ERROR "Fatal error executing: ${LLVM_CONFIG} --libdir")
endif()
message(STATUS "LLVM libdir: ${__llvm_libdir}")
# map prefix => $
# to handle the case when the prefix contains space.
string(REPLACE ${__llvm_prefix} "$" __llvm_cxxflags ${__llvm_cxxflags_space})
Expand Down Expand Up @@ -144,14 +145,33 @@ macro(find_llvm use_llvm)
endforeach()
separate_arguments(__llvm_system_libs)
foreach(__flag IN ITEMS ${__llvm_system_libs})
# If the library file ends in .lib try to
# also search the llvm_libdir
if(__flag MATCHES ".lib$")
if(EXISTS "${__llvm_libdir}/${__flag}")
set(__flag "${__llvm_libdir}/${__flag}")
if("${__flag}" STREQUAL "-lm")
message(STATUS "LLVM links against math")
list(APPEND LLVM_LIBS "m")
elseif(("${__flag}" STREQUAL "-lz") OR ("${__flag}" STREQUAL "z.lib"))
message(STATUS "LLVM links against zlib")
find_package(ZLIB REQUIRED)
list(APPEND LLVM_LIBS "ZLIB::ZLIB")
elseif("${__flag}" STREQUAL "-lzstd" OR ("${__flag}" STREQUAL "zstd.dll.lib"))
find_package(zstd REQUIRED)
if (TARGET "zstd::libzstd_static")
message(STATUS "LLVM links against static zstd")
list(APPEND LLVM_LIBS "zstd::libzstd_static")
else()
message(STATUS "LLVM links against shared zstd")
list(APPEND LLVM_LIBS "zstd::libzstd_shared")
endif()
elseif("${__flag}" STREQUAL "-lxml2")
message(STATUS "LLVM links against xml2")
list(APPEND LLVM_LIBS "-lxml2")
elseif((__flag MATCHES ".lib$") AND (EXISTS "${__llvm_libdir}/${__flag}"))
# If the library file ends in .lib try to also search the llvm_libdir
message(STATUS "LLVM linker flag under LLVM libdir: ${__llvm_libdir}/${__flag}")
list(APPEND LLVM_LIBS "${__llvm_libdir}/${__flag}")
else()
message(STATUS "LLVM linker flag: ${__flag}")
list(APPEND LLVM_LIBS "${__flag}")
endif()
list(APPEND LLVM_LIBS "${__flag}")
endforeach()
endif()
message(STATUS "Found LLVM_INCLUDE_DIRS=" "${LLVM_INCLUDE_DIRS}")
Expand Down
1 change: 1 addition & 0 deletions tests/scripts/task_config_build_minimal_cross_isa.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ if [ "$architecture_type" != "aarch64" ]; then
echo set\(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu\) >> config.cmake
echo set\(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER\) >> config.cmake
echo set\(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY\) >> config.cmake
echo set\(ZLIB_LIBRARY /lib/aarch64-linux-gnu/libz.a\) >> config.cmake
else
# This usually runs in the ci_arm docker image.
echo -e 'find_program(LLVM_CONFIG "llvm-config")\nif (LLVM_CONFIG) \n\tset(USE_LLVM llvm-config) \nelse() \n\tset(USE_LLVM llvm-config-15)\nendif()' >> config.cmake
Expand Down

0 comments on commit c5bec87

Please sign in to comment.