From 9a1d780e16e06871fe0490a2fa051759adcf3444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manu=20S=C3=A1nchez?= Date: Tue, 26 Feb 2019 13:44:08 +0100 Subject: [PATCH] Add _VERSION output variable to cmake_find_package generator (#4257) This standard find_package() output variable allows users to query the found library version. Note this variable is defined for requested packages only, not _VERSION variables are defined for the package dependencies. --- conans/client/generators/cmake_find_package.py | 11 +++++++++-- .../functional/generators/cmake_find_package_test.py | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py index 5f792fb2255..8edfd1816ae 100644 --- a/conans/client/generators/cmake_find_package.py +++ b/conans/client/generators/cmake_find_package.py @@ -3,9 +3,12 @@ generic_find_package_template = """ +include(FindPackageHandleStandardArgs) + message(STATUS "Conan: Using autogenerated Find{name}.cmake") # Global approach SET({name}_FOUND 1) +SET({name}_VERSION "{version}") SET({name}_INCLUDE_DIRS {deps.include_paths}) SET({name}_INCLUDES {deps.include_paths}) SET({name}_DEFINITIONS {deps.defines}) @@ -13,9 +16,12 @@ SET({name}_LIBRARIES_TARGETS "") # Will be filled later, if CMake 3 SET({name}_LIBS "") # Same as {name}_LIBRARIES -mark_as_advanced({name}_FOUND {name}_INCLUDE_DIRS {name}_INCLUDES - {name}_DEFINITIONS {name}_LIBRARIES {name}_LIBS) +find_package_handle_standard_args({name} + REQUIRED_VARS {name}_VERSION + VERSION_VAR {name}_VERSION) +mark_as_advanced({name}_FOUND {name}_INCLUDE_DIRS {name}_INCLUDES + {name}_DEFINITIONS {name}_LIBRARIES {name}_LIBS {name}_VERSION) # Find the real .lib/.a and add them to {name}_LIBS and {name}_LIBRARY_LIST SET({name}_LIBRARY_LIST {deps.libs}) @@ -84,6 +90,7 @@ def _single_find_package(name, cpp_info): if cpp_info.public_deps: lines = CMakeFindPackageGenerator._transitive_lines(name, cpp_info) tmp = generic_find_package_template.format(name=name, deps=deps, + version=cpp_info.version, find_dependencies="\n".join(lines)) return tmp diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 3fdb8a006ee..414e66ad20c 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -44,6 +44,7 @@ def build(self): cmake_minimum_required(VERSION 3.1) find_package(Test) message("Libraries to Link: ${Test_LIBS}") +message("Version: ${Test_VERSION}") get_target_property(tmp Test::Test INTERFACE_LINK_LIBRARIES) message("Target libs: ${tmp}") @@ -55,6 +56,7 @@ def build(self): client.run("create . user/channel --build missing") self.assertIn("Library fake_lib not found in package, might be system one", client.out) self.assertIn("Libraries to Link: fake_lib", client.out) + self.assertIn("Version: 0.1", client.out) self.assertIn("Target libs: fake_lib;shared_link_flag", client.out) self.assertIn("Compile options: a_cxx_flag;a_flag", client.out) @@ -125,6 +127,7 @@ def cmake_find_package_test(self): if(Hello0_LIBRARIES) MESSAGE("Hello0_LIBRARIES set") endif() +message("Version: ${Hello0_VERSION}") add_executable(say_hello main.cpp) target_link_libraries(say_hello helloHello1) @@ -133,6 +136,7 @@ def cmake_find_package_test(self): client.run("create . user/channel -s build_type=Release") self.assertIn("Conan: Using autogenerated FindHello0.cmake", client.out) self.assertIn("Hello0_LIBRARIES set", client.out) + self.assertIn("Version: 0.1", client.out) self.assertNotIn("Skipping already existing target", client.out) # Now link with old cmake @@ -173,7 +177,7 @@ def cmake_find_package_test(self): self.assertIn("Conan: Using autogenerated FindHello0.cmake", client.out) # Now a transitive consumer, but the consumer only find_package the first level Hello1 - files = cpp_hello_conan_files(name="Hello2", deps=["Hello1/0.1@user/channel"], + files = cpp_hello_conan_files(name="Hello2", version="0.2", deps=["Hello1/0.1@user/channel"], settings='"os", "compiler", "arch", "build_type"') files["CMakeLists.txt"] = """ set(CMAKE_CXX_COMPILER_WORKS 1) @@ -182,6 +186,7 @@ def cmake_find_package_test(self): cmake_minimum_required(VERSION 2.8) set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH}) find_package(Hello1 REQUIRED) # We don't need to find Hello0, it is transitive +message("Version1: ${Hello1_VERSION}") add_library(helloHello2 hello.cpp) target_link_libraries(helloHello2 PUBLIC Hello1::Hello1) @@ -196,3 +201,4 @@ def cmake_find_package_test(self): client.run("create . user/channel -s build_type=Release") self.assertIn("Conan: Using autogenerated FindHello0.cmake", client.out) self.assertIn("Conan: Using autogenerated FindHello1.cmake", client.out) + self.assertIn("Version1: 0.1", client.out)