<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/121345>121345</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [cmake] Findzstd.cmake cannot find shared zstd library in win-64 conda environment when using Clang
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            cmake,
            platform:windows
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mgorny
      </td>
    </tr>
</table>

<pre>
    Also reported for the feedstock but I'm pretty sure the problem is in the CMake file: https://github.com/conda-forge/llvmdev-feedstock/issues/306

Example Azure log from downstream package indirectly broken by this: [azure-log.txt](https://github.com/user-attachments/files/18275830/azure-log.txt)

I don't have a Windows setup to test this properly, but my reading of the code leads to the following conclusion (that I've originally stated in https://github.com/conda-forge/triton-feedstock/pull/29#issuecomment-2565614877):

```
  │ │   -- Found zstd: %PREFIX%/Library/lib/libzstd.lib
```

https://github.com/llvm/llvm-project/blob/e21dc4bd5474d04b8e62d7331362edcc5648d7e5/llvm/cmake/modules/Findzstd.cmake

When MSVC is used, `zstd_STATIC_LIBRARY_SUFFIX` is `_static.lib$`, otherwise it's just `.lib$`. So when using MSVC, `zstd.lib` (and `libzstd.lib` would too) triggers the second clause in the `zstd_FOUND` condition, causing it to perform explicit lookup for `bin/zstd.dll`. On the other hand, when using Clang, it just treats `libzstd.lib` as a static library (the first clause) and doesn't bother beyond that. So effectively, with MSVC it finds both static and shared libraries, and with Clang only the former.

------

Now, this is particularly problematic, when LLVM itself is built with MSVC (as it is in conda-forge). It uses the shared zstd library and includes references to the dependency on this library in the installed CMake files. However, when reverse dependencies use Clang instead, and try to `find_package(LLVM)`, the lookup is performed again. Since it is now performed with Clang and it doesn't find the shared library, the dependencies are unsatisfied and CMake errors out:

```
 │ │   CMake Error at %BUILD_PREFIX%/Library/lib/cmake/llvm/LLVMExports.cmake:64 (set_target_properties):
 │ │     The link interface of target "LLVMSupport" contains:
 │ │       zstd::libzstd_shared
 │ │     but the target was not found.  Possible reasons include:
 │ │       * There is a typo in the target name.
 │ │       * A find_package call is missing for an IMPORTED target.
 │ │       * An ALIAS target is missing.
 │ │   Call Stack (most recent call first):
 │ │     %BUILD_PREFIX%/Library/lib/cmake/llvm/LLVMConfig.cmake:349 (include)
 │ │     %BUILD_PREFIX%/Library/lib/cmake/mlir/MLIRConfig.cmake:10 (find_package)
 │ │     CMakeLists.txt:61 (find_package)
```

------

And perhaps the worst part, it is possible that I've actually broken that code before. I specifically see the shared library logic for MSVC has been added in e7fc7540daa9333f0be4f380fc9c619236d17f57 — perhaps the condition there should check for Windows rather than MSVC. But it's outside my expertise to change that.

CC @nga888 
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysV01v4zgS_TX0pWBDpj5sH3xQOzHWQHq6kXTP7J4CiizZnFCkQVJxPL9-UZScdmaz6dnFBIFtSWR9vHp8VRIh6L1FXLPyEytvJqKPB-fX3d55e540Tp3XtQkOPB6dj6igdR7iAaFFVCE6-QRNH2HH-KKDo8cYzxB6j2nN0bvGYAc6gLbpzuazeEJotUGW13CI8RhYXjO-ZXy71_HQNzPpOsa30lklpq3ze2R8a8xzp_B5-uqU8a0OocfA-DbPKpbVLKtvX0R3NAj1HxSAcXtovetAuZMN0aPo4Cjkk9gjaKu0RxnNGRrvntBCc4Z40BQMsPKTIAtT4_az-BJZecP48oNY-4B-KmIU8tChjRQTZUjf8yVflMs8Y3z71iZfDTHvQDnL-CLCQTwjCPhNW-VOAQLG_gjRQcQQU2wE5xG9OTO-SaB3Z_AolLZ7cG2CVzqFYFCokHZSmZwx7kRLpLPS9EE7C4wv40EMVXtGcF7vtRXGnCFEQUXW9q_XJnodnX1TmmNvDONbvmI8T2WSriNkprysympeLBcLAiCvBwxYlY3_WQ3AbjlbFWzJr34BTKewdb1V8EeIKlWJl1_vb7e7fzJeMr69040X_kxc0c3wSStndPXWA8vqD3Ijpo1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