From a13df9bc8b1966ea48972c69bef743285c437a44 Mon Sep 17 00:00:00 2001 From: Luke Hutton Date: Wed, 1 Nov 2023 13:11:39 +0000 Subject: [PATCH 1/2] [CMake] Fallback for finding static zstd library from the system LLVM17 now depends on `-lzstd` when the `--link-static` option is used. I.e: ``` $ llvm-config-16 --link-static --system-libs -lrt -ldl -lm -lz -ltinfo -lxml2 $ llvm-config-17 --link-static --system-libs -lrt -ldl -lm -lz -lzstd -ltinfo -lxml2 ``` The current cmake rules try to find a "Findzstd.cmake" file located within the project, although this doesn't seem to exist, resulting in a compilation error. This commit adds a fallback to search the system for libzstd.a incase a "Findzstd.cmake" is not found. The zstd library is already installed as part of: https://github.com/apache/tvm/pull/15799/files#diff-c2c0605a8fdd4f5600690a8c7b1ec769882426af1b0ed01e8aaa0814e3a8f5dbR48 Change-Id: I19ab168f92d23e98809851f948e2122345ed01f1 --- cmake/utils/FindLLVM.cmake | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cmake/utils/FindLLVM.cmake b/cmake/utils/FindLLVM.cmake index f40d97d9ba7f..6ed7283f1877 100644 --- a/cmake/utils/FindLLVM.cmake +++ b/cmake/utils/FindLLVM.cmake @@ -165,13 +165,19 @@ macro(find_llvm use_llvm) 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") + find_package(zstd QUIET) + if(zstd_FOUND) + 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() else() - message(STATUS "LLVM links against shared zstd") - list(APPEND LLVM_LIBS "zstd::libzstd_shared") + find_library(zstd_LIBRARY NAMES zstd zstd_static REQUIRED) + message(STATUS "LLVM links against zstd") + list(APPEND LLVM_LIBS "${zstd_LIBRARY}") endif() elseif("${__flag}" STREQUAL "-lxml2") message(STATUS "LLVM links against xml2") From 760e96921739c164a094b8da3df9cf490f482e9c Mon Sep 17 00:00:00 2001 From: Luke Hutton Date: Thu, 2 Nov 2023 09:19:24 +0000 Subject: [PATCH 2/2] Use llvm-config to locate Findzstd.cmake Use llvm-config to find the location of the "Find*.cmake" files and add this location to the cmake `CMAKE_MODULE_PATH` variable. This allows the build to discover "Findzstd.cmake". Change-Id: I3d25074fad3b2b8fa4c3d47e0e4c0b27d8739c65 --- cmake/utils/FindLLVM.cmake | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/cmake/utils/FindLLVM.cmake b/cmake/utils/FindLLVM.cmake index 6ed7283f1877..fe236e299c92 100644 --- a/cmake/utils/FindLLVM.cmake +++ b/cmake/utils/FindLLVM.cmake @@ -111,6 +111,14 @@ macro(find_llvm use_llvm) message(FATAL_ERROR "Fatal error executing: ${LLVM_CONFIG} --libdir") endif() message(STATUS "LLVM libdir: ${__llvm_libdir}") + execute_process(COMMAND ${LLVM_CONFIG} --cmakedir + RESULT_VARIABLE __llvm_exit_code + OUTPUT_VARIABLE __llvm_cmakedir + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT "${__llvm_exit_code}" STREQUAL "0") + message(FATAL_ERROR "Fatal error executing: ${LLVM_CONFIG} --cmakedir") + endif() + message(STATUS "LLVM cmakedir: ${__llvm_cmakedir}") # map prefix => $ # to handle the case when the prefix contains space. string(REPLACE ${__llvm_prefix} "$" __llvm_cxxflags ${__llvm_cxxflags_space}) @@ -165,19 +173,14 @@ macro(find_llvm use_llvm) find_package(ZLIB REQUIRED) list(APPEND LLVM_LIBS "ZLIB::ZLIB") elseif("${__flag}" STREQUAL "-lzstd" OR ("${__flag}" STREQUAL "zstd.dll.lib")) - find_package(zstd QUIET) - if(zstd_FOUND) - 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() + list(APPEND CMAKE_MODULE_PATH "${__llvm_cmakedir}") + find_package(zstd REQUIRED) + if (TARGET "zstd::libzstd_static") + message(STATUS "LLVM links against static zstd") + list(APPEND LLVM_LIBS "zstd::libzstd_static") else() - find_library(zstd_LIBRARY NAMES zstd zstd_static REQUIRED) - message(STATUS "LLVM links against zstd") - list(APPEND LLVM_LIBS "${zstd_LIBRARY}") + 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")