Browse Source

FindZLIB: more library names + option to prefer static library

Adds additional library names `zlibstat[d]` and `zlibvc[d]` for Windows,
when zlib is built using one of the Visual Studio solutions under
`zlib/contrib/vstudio`.

Adds a `ZLIB_USE_STATIC_LIBS` option that is equivilent to similar
settings in other modules such as FindProtobuf, FindOpenSSL, etc.

Implements #18029 and #23140
Peter Würth 3 years ago
parent
commit
185723461f
2 changed files with 27 additions and 2 deletions
  1. 5 0
      Help/release/dev/findzlib-static.rst
  2. 22 2
      Modules/FindZLIB.cmake

+ 5 - 0
Help/release/dev/findzlib-static.rst

@@ -0,0 +1,5 @@
+findzlib-static
+---------------
+
+* The :module:`FindZLIB` learned a new ``ZLIB_USE_STATIC_LIBS`` variable to
+  search only for static libraries.

+ 22 - 2
Modules/FindZLIB.cmake

@@ -53,6 +53,11 @@ Hints
 
 A user may set ``ZLIB_ROOT`` to a zlib installation root to tell this
 module where to look.
+
+.. versionadded:: 3.24
+  Set ``ZLIB_USE_STATIC_LIBS`` to ``ON`` to look for static libraries.
+  Default is ``OFF``.
+
 #]=======================================================================]
 
 set(_ZLIB_SEARCHES)
@@ -72,8 +77,8 @@ set(_ZLIB_SEARCH_NORMAL
 unset(_ZLIB_x86)
 list(APPEND _ZLIB_SEARCHES _ZLIB_SEARCH_NORMAL)
 
-set(ZLIB_NAMES z zlib zdll zlib1 zlibstatic)
-set(ZLIB_NAMES_DEBUG zd zlibd zdlld zlibd1 zlib1d zlibstaticd)
+set(ZLIB_NAMES z zlib zdll zlib1 zlibstatic zlibstat zlibvc)
+set(ZLIB_NAMES_DEBUG zd zlibd zdlld zlibd1 zlib1d zlibstaticd zlibstatd zlibvcd)
 
 # Try each search configuration.
 foreach(search ${_ZLIB_SEARCHES})
@@ -82,11 +87,26 @@ endforeach()
 
 # Allow ZLIB_LIBRARY to be set manually, as the location of the zlib library
 if(NOT ZLIB_LIBRARY)
+  # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
+  if(ZLIB_USE_STATIC_LIBS)
+    set(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
+    if(WIN32)
+      set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
+    else()
+      set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
+    endif()
+  endif()
+
   foreach(search ${_ZLIB_SEARCHES})
     find_library(ZLIB_LIBRARY_RELEASE NAMES ${ZLIB_NAMES} NAMES_PER_DIR ${${search}} PATH_SUFFIXES lib)
     find_library(ZLIB_LIBRARY_DEBUG NAMES ${ZLIB_NAMES_DEBUG} NAMES_PER_DIR ${${search}} PATH_SUFFIXES lib)
   endforeach()
 
+  # Restore the original find library ordering
+  if(ZLIB_USE_STATIC_LIBS)
+    set(CMAKE_FIND_LIBRARY_SUFFIXES ${_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
+  endif()
+
   include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
   select_library_configurations(ZLIB)
 endif()