Merge topic 'findzlib_fix_version'

6b9b9f4 FindZLIB: optimize zlib.h version parsing
This commit is contained in:
Brad King 2010-08-03 16:18:42 -04:00 committed by CMake Topic Stage
commit 976aaf45de
1 changed files with 39 additions and 7 deletions

View File

@ -1,12 +1,20 @@
# - Find zlib
# Find the native ZLIB includes and library
# Find the native ZLIB includes and library.
# Once done this will define
#
# ZLIB_INCLUDE_DIRS - where to find zlib.h, etc.
# ZLIB_LIBRARIES - List of libraries when using zlib.
# ZLIB_FOUND - True if zlib found.
#
# ZLIB_VERSION_STRING - The version of zlib found (x.y.z)
# ZLIB_MAJOR_VERSION - the major version of zlib
# ZLIB_VERSION_MAJOR - The major version of zlib
# ZLIB_VERSION_MINOR - The minor version of zlib
# ZLIB_VERSION_PATCH - The patch version of zlib
# ZLIB_VERSION_TWEAK - The tweak version of zlib
#
# The following variable are provided for backward compatibility
#
# ZLIB_MAJOR_VERSION - The major version of zlib
# ZLIB_MINOR_VERSION - The minor version of zlib
# ZLIB_PATCH_VERSION - The patch version of zlib
@ -37,8 +45,18 @@ FIND_LIBRARY(ZLIB_LIBRARY
MARK_AS_ADVANCED(ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
IF(ZLIB_INCLUDE_DIR AND EXISTS "${ZLIB_INCLUDE_DIR}/zlib.h")
FILE(READ "${ZLIB_INCLUDE_DIR}/zlib.h" ZLIB_H)
STRING(REGEX REPLACE ".*#define ZLIB_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+).*" "\\1.\\2.\\3" ZLIB_VERSION_STRING "${ZLIB_H}")
FILE(STRINGS "${ZLIB_INCLUDE_DIR}/zlib.h" ZLIB_H REGEX "^#define ZLIB_VERSION \"[^\"]*\"$")
STRING(REGEX REPLACE "^.*ZLIB_VERSION \"([0-9]+).*$" "\\1" ZLIB_VERSION_MAJOR "${ZLIB_H}")
STRING(REGEX REPLACE "^.*ZLIB_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" ZLIB_VERSION_MINOR "${ZLIB_H}")
STRING(REGEX REPLACE "^.*ZLIB_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" ZLIB_VERSION_PATCH "${ZLIB_H}")
STRING(REGEX REPLACE "^.*ZLIB_VERSION \"[0-9]+\\.[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" ZLIB_VERSION_TWEAK "${ZLIB_H}")
SET(ZLIB_VERSION_STRING "${ZLIB_VERSION_MAJOR}.${ZLIB_VERSION_MINOR}.${ZLIB_VERSION_PATCH}.${ZLIB_VERSION_TWEAK}")
SET(ZLIB_MAJOR_VERSION "${ZLIB_VERSION_MAJOR}")
SET(ZLIB_MINOR_VERSION "${ZLIB_VERSION_MINOR}")
SET(ZLIB_PATCH_VERSION "${ZLIB_VERSION_PATCH}")
ENDIF()
# handle the QUIETLY and REQUIRED arguments and set ZLIB_FOUND to TRUE if
@ -46,6 +64,20 @@ ENDIF()
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ZLIB DEFAULT_MSG ZLIB_INCLUDE_DIR ZLIB_LIBRARY)
# handle the VERSION provided in find_package() command
if(ZLIB_FIND_VERSION)
if(ZLIB_FIND_VERSION_EXACT AND NOT ${ZLIB_VERSION_STRING} VERSION_EQUAL ${ZLIB_FIND_VERSION})
message(FATAL_ERROR "ZLIB version found (${ZLIB_VERSION_STRING}) does not match the required one (${ZLIB_FIND_VERSION}), aborting.")
elseif(${ZLIB_VERSION_STRING} VERSION_LESS ${ZLIB_FIND_VERSION})
if(ZLIB_FIND_REQUIRED)
message(FATAL_ERROR "ZLIB version found (${ZLIB_VERSION_STRING}) is less then the minimum required (${ZLIB_FIND_VERSION}), aborting.")
else(ZLIB_FIND_REQUIRED)
message("ZLIB version found (${ZLIB_VERSION_STRING}) is less then the minimum required (${ZLIB_FIND_VERSION}), continue without ZLIB support.")
set(ZLIB_FOUND FALSE)
endif(ZLIB_FIND_REQUIRED)
endif()
endif(ZLIB_FIND_VERSION)
IF(ZLIB_FOUND)
SET(ZLIB_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR})
SET(ZLIB_LIBRARIES ${ZLIB_LIBRARY})