[llvm] [cmake] Support finding both static and shared zstd via FindZstd
authorMichał Górny <mgorny@gentoo.org>
Fri, 7 Oct 2022 16:42:27 +0000 (18:42 +0200)
committerMichał Górny <mgorny@gentoo.org>
Wed, 12 Oct 2022 05:14:28 +0000 (07:14 +0200)
Improve the logic in FindZstd to support finding both shared and static
variants of the zstd library simultaneously.  Otherwise, if the shared
library is installed, zstd::libzstd_static is not declared at all
and CMake fails if LLVM_USE_STATIC_ZSTD is used.

Differential Revision: https://reviews.llvm.org/D135457

llvm/cmake/modules/Findzstd.cmake

index fab9ea8..2994eaf 100644 (file)
@@ -3,6 +3,7 @@
 # If successful, the following variables will be defined:
 # zstd_INCLUDE_DIR
 # zstd_LIBRARY
+# zstd_STATIC_LIBRARY
 # zstd_FOUND
 #
 # Additionally, one of the following import targets will be defined:
@@ -19,6 +20,9 @@ endif()
 
 find_path(zstd_INCLUDE_DIR NAMES zstd.h)
 find_library(zstd_LIBRARY NAMES zstd zstd_static)
+find_library(zstd_STATIC_LIBRARY NAMES
+  zstd_static
+  "${CMAKE_STATIC_LIBRARY_PREFIX}zstd${CMAKE_STATIC_LIBRARY_SUFFIX}")
 
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(
@@ -33,17 +37,19 @@ if(zstd_FOUND)
     set_target_properties(zstd::libzstd_shared PROPERTIES
           INTERFACE_INCLUDE_DIRECTORIES "${zstd_INCLUDE_DIR}"
           IMPORTED_LOCATION "${zstd_LIBRARY}")
+  else()
+    set(zstd_STATIC_LIBRARY "${zstd_LIBRARY}")
   endif()
-  if(zstd_LIBRARY MATCHES "${zstd_STATIC_LIBRARY_SUFFIX}$" AND
+  if(zstd_STATIC_LIBRARY MATCHES "${zstd_STATIC_LIBRARY_SUFFIX}$" AND
      NOT TARGET zstd::libzstd_static)
     add_library(zstd::libzstd_static STATIC IMPORTED)
     set_target_properties(zstd::libzstd_static PROPERTIES
         INTERFACE_INCLUDE_DIRECTORIES "${zstd_INCLUDE_DIR}"
-        IMPORTED_LOCATION "${zstd_LIBRARY}")
+        IMPORTED_LOCATION "${zstd_STATIC_LIBRARY}")
   endif()
 endif()
 
 unset(zstd_SHARED_LIBRARY_SUFFIX)
 unset(zstd_STATIC_LIBRARY_SUFFIX)
 
-mark_as_advanced(zstd_INCLUDE_DIR zstd_LIBRARY)
+mark_as_advanced(zstd_INCLUDE_DIR zstd_LIBRARY zstd_STATIC_LIBRARY)