A fix for https://gitlab.kitware.com/cmake/cmake/issues/16196

This is my first attempt at doing anything with CMake, so I'd appreciate any feedback on my patch! In particular, the pairs of file()/string() commands seem a bit convoluted for extracting strings out of the header file - is there a more idiomatic approach? Also, I'm a bit concerned that they are polluting scope by leaking out the GIFMAJ/GIFMIN/GIFREL working variables... how would I improve this?


---
 Modules/FindGIF.cmake | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/Modules/FindGIF.cmake b/Modules/FindGIF.cmake
index 7bbb8cf..283a299 100644
--- a/Modules/FindGIF.cmake
+++ b/Modules/FindGIF.cmake
@@ -61,7 +61,8 @@ set(GIF_LIBRARIES ${GIF_LIBRARY})
 # to be always " Version 2.0, " in versions 3.x of giflib.
# In version 4 the member UserData was added to GifFileType, so we check for this
 # one.
-# http://giflib.sourcearchive.com/documentation/4.1.4/files.html
+# Versions after 4.1.6 define GIFLIB_MAJOR, GIFLIB_MINOR, and GIFLIB_RELEASE
+# see http://giflib.sourceforge.net/gif_lib.html#compatibility
 if(GIF_INCLUDE_DIR)
   include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
   include(${CMAKE_CURRENT_LIST_DIR}/CheckStructHasMember.cmake)
@@ -71,7 +72,22 @@ if(GIF_INCLUDE_DIR)
   set(CMAKE_REQUIRED_INCLUDES "${GIF_INCLUDE_DIR}")
CHECK_STRUCT_HAS_MEMBER(GifFileType UserData gif_lib.h GIF_GifFileType_UserData )
   if(GIF_GifFileType_UserData)
-    set(GIF_VERSION 4)
+    # OK. So we're version 4 or higher. Check for specific version defines
+ file(STRINGS ${GIF_INCLUDE_DIR}/gif_lib.h GIFMAJ REGEX "^[ \t]*#define[ \t]+GIFLIB_MAJOR") + string(REGEX REPLACE "^.*GIFLIB_MAJOR ([0-9]+).*$" "\\1" GIFMAJ ${GIFMAJ})
+    # extract minor version
+ file(STRINGS ${GIF_INCLUDE_DIR}/gif_lib.h GIFMIN REGEX "^[ \t]*#define[ \t]+GIFLIB_MINOR") + string(REGEX REPLACE "^.*GIFLIB_MINOR ([0-9]+).*$" "\\1" GIFMIN ${GIFMIN})
+    # point release
+ file(STRINGS ${GIF_INCLUDE_DIR}/gif_lib.h GIFREL REGEX "^[ \t]*#define[ \t]+GIFLIB_RELEASE") + string(REGEX REPLACE "^.*GIFLIB_RELEASE ([0-9]+).*$" "\\1" GIFREL ${GIFREL})
+    if(GIFMAJ)
+      # yay - got exact version info
+      set(GIF_VERSION ${GIFMAJ}.${GIFMIN}.${GIFREL})
+    else(GIFMAJ)
+      # couldn't find the defines - assume version 4
+      set(GIF_VERSION 4)
+    endif(GIFMAJ)
   endif()
   CMAKE_POP_CHECK_STATE()
 endif()
--
2.7.4
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers

Reply via email to