Skip to content

Commit

Permalink
Add <package>_VERSION output variable to cmake_find_package generator (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
Manu343726 authored and lasote committed Feb 26, 2019
1 parent 0660e94 commit 9a1d780
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 9 additions & 2 deletions conans/client/generators/cmake_find_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@


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})
SET({name}_LIBRARIES "") # Will be filled later
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})
Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion conans/test/functional/generators/cmake_find_package_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

0 comments on commit 9a1d780

Please sign in to comment.