Hi,

The find_dependency() macro defined in CMakeFindDependencyMacro.cmake has a 
bug which makes it reuse the version number used in a previous call in 
certain situation.

Here is my test case:

cmake_minimum_required(VERSION 3.0)

include(CMakeFindDependencyMacro)
find_dependency(Qt5Script 5.2)
find_dependency(KF5Pty 4.96.0)

This fails here (with CMake 3.0.0rc1 from the release branch): the second 
call to find_dependency fails, complaining it cannot find KF5Pty 5.2.

It turns out the code checks if the version number is passed to 
find_dependency() with: if (${ARGV1}) which is FALSE if ARGV1 is 4.96.0 (!).

Since find_dependency() is a macro, the previous call to it set the 
"version" variable, causing it to be reused in the second call. Attached 
patch sets the version variable unconditionally so that it is not skipped if 
the version ends with ".0" and turns the macro into a function so that it 
does not leak local variables.

Aurélien
>From d2b01f16f6487c5dddc2ac1eb47872d3db5d543e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= <[email protected]>
Date: Mon, 24 Feb 2014 18:16:09 +0100
Subject: [PATCH] Fix version handling in find_dependency()

Turn find_dependency() into a function so that it does not overwrite and
leak variables like `version`. Set `version` unconditionally so that it is
always set, even if ${ARGV1} value evaluates to FALSE (4.96.0 evaluates to
FALSE for example)
---
 Modules/CMakeFindDependencyMacro.cmake | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/Modules/CMakeFindDependencyMacro.cmake b/Modules/CMakeFindDependencyMacro.cmake
index 0f1f56d..4d6c08f 100644
--- a/Modules/CMakeFindDependencyMacro.cmake
+++ b/Modules/CMakeFindDependencyMacro.cmake
@@ -27,11 +27,9 @@
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 
-macro(find_dependency dep)
+function(find_dependency dep)
   if (NOT ${dep}_FOUND)
-    if (${ARGV1})
-      set(version ${ARGV1})
-    endif()
+    set(version "${ARGV1}")
     set(exact_arg)
     if(${CMAKE_FIND_PACKAGE_NAME}_FIND_VERSION_EXACT)
       set(exact_arg EXACT)
@@ -64,4 +62,4 @@ macro(find_dependency dep)
     set(quiet_arg)
     set(exact_arg)
   endif()
-endmacro()
+endfunction()
-- 
1.8.3.2


-- 

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://www.cmake.org/mailman/listinfo/cmake

Reply via email to