FindZLIB: optimize zlib.h version parsing

Read zlib.h by using file(STRINGS.....REGEX) to limit the amount of text we apply the version regex on. This patch also addresses the fact that the version string may contain a tweak component.

Patch by Simone Rossetto
This commit is contained in:
Kovarththanan Rajaratnam 2010-07-31 07:24:47 +02:00
parent 41dbdb71cd
commit 6b9b9f44c8
1 changed files with 39 additions and 7 deletions

View File

@ -1,12 +1,20 @@
# - Find zlib # - 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_INCLUDE_DIRS - where to find zlib.h, etc.
# ZLIB_LIBRARIES - List of libraries when using zlib. # ZLIB_LIBRARIES - List of libraries when using zlib.
# ZLIB_FOUND - True if zlib found. # ZLIB_FOUND - True if zlib found.
# #
# ZLIB_VERSION_STRING - The version of zlib found (x.y.z) # 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_MINOR_VERSION - The minor version of zlib
# ZLIB_PATCH_VERSION - The patch version of zlib # ZLIB_PATCH_VERSION - The patch version of zlib
@ -36,9 +44,19 @@ FIND_LIBRARY(ZLIB_LIBRARY
) )
MARK_AS_ADVANCED(ZLIB_LIBRARY ZLIB_INCLUDE_DIR) MARK_AS_ADVANCED(ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
IF (ZLIB_INCLUDE_DIR AND EXISTS "${ZLIB_INCLUDE_DIR}/zlib.h") IF(ZLIB_INCLUDE_DIR AND EXISTS "${ZLIB_INCLUDE_DIR}/zlib.h")
FILE(READ "${ZLIB_INCLUDE_DIR}/zlib.h" ZLIB_H) FILE(STRINGS "${ZLIB_INCLUDE_DIR}/zlib.h" ZLIB_H REGEX "^#define ZLIB_VERSION \"[^\"]*\"$")
STRING(REGEX REPLACE ".*#define ZLIB_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+).*" "\\1.\\2.\\3" ZLIB_VERSION_STRING "${ZLIB_H}")
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() ENDIF()
# handle the QUIETLY and REQUIRED arguments and set ZLIB_FOUND to TRUE if # handle the QUIETLY and REQUIRED arguments and set ZLIB_FOUND to TRUE if
@ -46,8 +64,22 @@ ENDIF()
INCLUDE(FindPackageHandleStandardArgs) INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ZLIB DEFAULT_MSG ZLIB_INCLUDE_DIR ZLIB_LIBRARY) FIND_PACKAGE_HANDLE_STANDARD_ARGS(ZLIB DEFAULT_MSG ZLIB_INCLUDE_DIR ZLIB_LIBRARY)
IF (ZLIB_FOUND) # 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_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR})
SET(ZLIB_LIBRARIES ${ZLIB_LIBRARY}) SET(ZLIB_LIBRARIES ${ZLIB_LIBRARY})
ENDIF() ENDIF()