--- llvm/cmake/modules/Findzstd.cmake.orig	2024-09-17 13:26:36.000000000 +0200
+++ llvm/cmake/modules/Findzstd.cmake	2024-09-29 13:19:16.980962300 +0200
@@ -16,6 +16,8 @@
   set(zstd_STATIC_LIBRARY_SUFFIX "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$")
 endif()
 
+find_package(zstd CONFIG QUIET)
+if(NOT zstd_FOUND)
 find_path(zstd_INCLUDE_DIR NAMES zstd.h)
 find_library(zstd_LIBRARY NAMES zstd zstd_static)
 find_library(zstd_STATIC_LIBRARY NAMES
@@ -27,17 +29,18 @@
     zstd DEFAULT_MSG
     zstd_LIBRARY zstd_INCLUDE_DIR
 )
+endif()
 
 if(zstd_FOUND)
-  if(zstd_LIBRARY MATCHES "${zstd_STATIC_LIBRARY_SUFFIX}$")
+  if(zstd_LIBRARY MATCHES "${zstd_STATIC_LIBRARY_SUFFIX}$" AND NOT MINGW)
     set(zstd_STATIC_LIBRARY "${zstd_LIBRARY}")
   elseif (NOT TARGET zstd::libzstd_shared)
     add_library(zstd::libzstd_shared SHARED IMPORTED)
-    if(MSVC OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
+    if(WIN32)
       include(GNUInstallDirs) # For CMAKE_INSTALL_LIBDIR and friends.
       # IMPORTED_LOCATION is the path to the DLL and IMPORTED_IMPLIB is the "library".
       get_filename_component(zstd_DIRNAME "${zstd_LIBRARY}" DIRECTORY)
       if(NOT "${CMAKE_INSTALL_LIBDIR}" STREQUAL "" AND NOT "${CMAKE_INSTALL_BINDIR}" STREQUAL "")
-        string(REGEX REPLACE "${CMAKE_INSTALL_LIBDIR}$" "${CMAKE_INSTALL_BINDIR}" zstd_DIRNAME "${zstd_DIRNAME}")
+        string(REGEX REPLACE "\\${CMAKE_INSTALL_LIBDIR}$" "${CMAKE_INSTALL_BINDIR}" zstd_DIRNAME "${zstd_DIRNAME}")
       endif()
       get_filename_component(zstd_BASENAME "${zstd_LIBRARY}" NAME)
       string(REGEX REPLACE "\\${CMAKE_LINK_LIBRARY_SUFFIX}$" "${CMAKE_SHARED_LIBRARY_SUFFIX}" zstd_BASENAME "${zstd_BASENAME}")
