[llvm] [cmake] Fix compatibility between Findzstd.cmake and zstd's own zstdConfig.cmake (PR #160576)

Ruoyu Zhong via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 24 11:08:11 PDT 2025


https://github.com/ZhongRuoyu created https://github.com/llvm/llvm-project/pull/160576

When importing LLVM before zstd in CMake config mode, CMake throws errors about incomplete target definitions:

    CMake Error at /usr/lib/x86_64-linux-gnu/cmake/zstd/zstdTargets.cmake:42 (message):
    Some (but not all) targets in this export set were already defined.

    Targets Defined: zstd::libzstd_shared, zstd::libzstd_static

    Targets not yet defined: zstd::libzstd

    Call Stack (most recent call first):
    /usr/lib/x86_64-linux-gnu/cmake/zstd/zstdConfig.cmake:42 (include)
    CMakeLists.txt:7 (find_package)

This occurs because LLVM uses its own `Findzstd.cmake module` which creates `zstd::libzstd_shared` and `zstd::libzstd_static` targets, while zstd's own `zstdConfig.cmake` also creates an additional `zstd::libzstd` interface target since v1.5.6 [^1].

Fix by adding creation of the `zstd::libzstd` interface target in LLVM's `Findzstd.cmake` module expected by newer zstd versions. It links to `zstd::libzstd_shared` if available, or `zstd::libzstd_shared` as fallback. This can be controlled by `LLVM_USE_STATIC_ZSTD`.

I choose not to defer entirely to zstd's own config module because it does not define variables like `zstd_INCLUDE_DIR` and `zstd_LIBRARY` already expected by existing LLVM CMake code (see e.g. `llvm/cmake/modules/LLVMExternalProjectUtils.cmake`). It is possible but can be more complicated to extract these variables from imported targets to match what is already being done here.

Fixes https://github.com/llvm/llvm-project/issues/139666.
See also https://bugreports.qt.io/browse/QTBUG-139199.

[^1]: https://github.com/facebook/zstd/commit/c53d650d9a047ab12b2c7e5808878aff37d3cfc5


>From a7e7c8b86576fb35af49f1c184d36189b534589f Mon Sep 17 00:00:00 2001
From: Ruoyu Zhong <zhongruoyu at outlook.com>
Date: Thu, 25 Sep 2025 02:02:46 +0800
Subject: [PATCH] [cmake] Fix compatibility between Findzstd.cmake and zstd's
 own zstdConfig.cmake

When importing LLVM before zstd in CMake config mode, CMake throws errors
about incomplete target definitions:

    CMake Error at /usr/lib/x86_64-linux-gnu/cmake/zstd/zstdTargets.cmake:42 (message):
    Some (but not all) targets in this export set were already defined.

    Targets Defined: zstd::libzstd_shared, zstd::libzstd_static

    Targets not yet defined: zstd::libzstd

    Call Stack (most recent call first):
    /usr/lib/x86_64-linux-gnu/cmake/zstd/zstdConfig.cmake:42 (include)
    CMakeLists.txt:7 (find_package)

This occurs because LLVM uses its own Findzstd.cmake module which
creates zstd::libzstd_shared and zstd::libzstd_static targets, while
zstd's own zstdConfig.cmake also creates an additional zstd::libzstd
interface target since v1.5.6 [1].

Fix by adding creation of the zstd::libzstd interface target in LLVM's
Findzstd.cmake module expected by newer zstd versions. It links to
zstd::libzstd_shared if available, or zstd::libzstd_shared as fallback.
This can be controlled by LLVM_USE_STATIC_ZSTD.

I choose not to defer entirely to zstd's own config module because it
does not define variables like zstd_INCLUDE_DIR and zstd_LIBRARY already
expected by existing LLVM CMake code (see e.g.
llvm/cmake/modules/LLVMExternalProjectUtils.cmake). It is possible but
can be more complicated to extract these variables from imported targets
to match what is already being done here.

Fixes https://github.com/llvm/llvm-project/issues/139666.
See also https://bugreports.qt.io/browse/QTBUG-139199.

[1]: https://github.com/facebook/zstd/commit/c53d650d9a047ab12b2c7e5808878aff37d3cfc5

Signed-off-by: Ruoyu Zhong <zhongruoyu at outlook.com>
---
 llvm/cmake/modules/Findzstd.cmake | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/llvm/cmake/modules/Findzstd.cmake b/llvm/cmake/modules/Findzstd.cmake
index dbfaadb5de2b8..7c2f9fff6934b 100644
--- a/llvm/cmake/modules/Findzstd.cmake
+++ b/llvm/cmake/modules/Findzstd.cmake
@@ -6,9 +6,13 @@
 # zstd_STATIC_LIBRARY
 # zstd_FOUND
 #
-# Additionally, one of the following import targets will be defined:
+# Additionally, at least one of the following import targets will be defined:
 # zstd::libzstd_shared
 # zstd::libzstd_static
+#
+# The zstd::libzstd interface target will also be defined if either of the above
+# targets are created, for compatibility with zstd 1.5.6 and newer. It prefers
+# the shared library unless LLVM_USE_STATIC_ZSTD is set.
 
 if(MSVC OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
   set(zstd_STATIC_LIBRARY_SUFFIX "_static\\${CMAKE_STATIC_LIBRARY_SUFFIX}$")
@@ -61,6 +65,19 @@ if(zstd_FOUND)
         INTERFACE_INCLUDE_DIRECTORIES "${zstd_INCLUDE_DIR}"
         IMPORTED_LOCATION "${zstd_STATIC_LIBRARY}")
   endif()
+
+  if(NOT TARGET zstd::libzstd)
+    add_library(zstd::libzstd INTERFACE IMPORTED)
+    set_target_properties(zstd::libzstd PROPERTIES
+        INTERFACE_INCLUDE_DIRECTORIES "${zstd_INCLUDE_DIR}")
+    if(TARGET zstd::libzstd_shared AND NOT LLVM_USE_STATIC_ZSTD)
+      set_target_properties(zstd::libzstd PROPERTIES
+          INTERFACE_LINK_LIBRARIES zstd::libzstd_shared)
+    elseif(TARGET zstd::libzstd_static)
+      set_target_properties(zstd::libzstd PROPERTIES
+          INTERFACE_LINK_LIBRARIES zstd::libzstd_static)
+    endif()
+  endif()
 endif()
 
 unset(zstd_STATIC_LIBRARY_SUFFIX)



More information about the llvm-commits mailing list