Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <package>_VERSION output variable to cmake_find_package generator #4257

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this (https://cmake.org/cmake/help/v3.0/module/FindPackageHandleStandardArgs.html) additionally help with the casing of the package names when using this generator or nothing to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that more recent versions of cmake mark that FOUND_VARIABLE parameter as obsolete. See for example https://cmake.org/cmake/help/v3.13/module/FindPackageHandleStandardArgs.html which states "This exists only for compatibility with older versions of CMake and is now ignored. Result variables of both names are always set for compatibility."

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)