Skip to content

Commit

Permalink
- generate ConfigVersion.cmake file
Browse files Browse the repository at this point in the history
Signed-off-by: SSE4 <tomskside@gmail.com>
  • Loading branch information
SSE4 committed Nov 12, 2019
1 parent 14f8441 commit 02c0dcf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
29 changes: 29 additions & 0 deletions conans/client/generators/cmake_find_package_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ class CMakeFindPackageMultiGenerator(Generator):
$<$<CONFIG:Debug>:${{{name}_COMPILE_OPTIONS_DEBUG_LIST}}>)
"""

# https://gitlab.kitware.com/cmake/cmake/blob/master/Modules/BasicConfigVersion-SameMajorVersion.cmake.in
version_template = """
set(PACKAGE_VERSION "{version}")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("{version}" MATCHES "^([0-9]+)\\\\.")
set(CVF_VERSION_MAJOR "${{CMAKE_MATCH_1}}")
else()
set(CVF_VERSION_MAJOR "{version}")
endif()
if(PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR)
set(PACKAGE_VERSION_COMPATIBLE TRUE)
else()
set(PACKAGE_VERSION_COMPATIBLE FALSE)
endif()
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
"""

@property
def filename(self):
pass
Expand All @@ -79,6 +106,8 @@ def content(self):
find_lib = target_template.format(name=depname, deps=deps,
build_type_suffix=build_type_suffix)
ret["{}Target-{}.cmake".format(depname, build_type.lower())] = find_lib
ret["{}ConfigVersion.cmake".format(depname)] = self.version_template.\
format(version=cpp_info.version)
return ret

def _build_type_suffix(self, build_type):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest

from nose.plugins.attrib import attr
from parameterized import parameterized

from conans.model.ref import ConanFileReference, PackageReference
from conans.test.utils.tools import TestClient, NO_SETTINGS_PACKAGE_ID, replace_in_file
Expand Down Expand Up @@ -229,8 +230,8 @@ def cpp_info_name_test(self):
output=client.out)
client.run("create .")
cmakelists = """
project(consumer)
cmake_minimum_required(VERSION 3.1)
project(consumer)
find_package(MYHELLO2)
get_target_property(tmp MYHELLO2::MYHELLO2 INTERFACE_LINK_LIBRARIES)
Expand Down Expand Up @@ -261,3 +262,47 @@ def build(self):
"$<$<CONFIG:MinSizeRel>:;>;"
"$<$<CONFIG:Debug>:;>",
client.out)

@parameterized.expand([
("find_package(hello 1.0)", True),
("find_package(hello 1.1)", True),
("find_package(hello 1.2)", False),
("find_package(hello 1.0 EXACT)", False),
("find_package(hello 1.1 EXACT)", True),
("find_package(hello 1.2 EXACT)", False),
("find_package(hello 0.1)", False),
("find_package(hello 2.0)", False)
])
def version_test(self, find_package_string, expected_result):
client = TestClient()
client.run("new hello/1.1 -s")
client.run("create .")

cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.1)
project(consumer)
{find_package_string}
message(STATUS "hello found: ${{hello_FOUND}}")
""").format(find_package_string=find_package_string)

conanfile = textwrap.dedent("""
from conans import ConanFile, CMake
class Conan(ConanFile):
settings = "build_type"
requires = "hello/1.1"
generators = "cmake_find_package_multi"
def build(self):
cmake = CMake(self)
cmake.configure()
""")

client.save({"conanfile.py": conanfile, "CMakeLists.txt": cmakelists})
client.run("install .")
client.run("build .")
if expected_result:
self.assertIn("hello found: 1", client.out)
else:
self.assertIn("hello found: 0", client.out)
3 changes: 2 additions & 1 deletion conans/test/unittests/client/generators/cmake_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ def cmake_find_package_multi_test(self):
content = generator.content
six.assertCountEqual(self, ['MyPkG2Targets.cmake', 'MyPkGConfig.cmake', 'MyPkG2Config.cmake',
'MyPkGTargets.cmake', 'MyPkGTarget-debug.cmake',
'MyPkG2Target-debug.cmake'], content.keys())
'MyPkG2Target-debug.cmake', 'MyPkGConfigVersion.cmake',
'MyPkG2ConfigVersion.cmake'], content.keys())
self.assertNotIn("my_pkg", content["MyPkGConfig.cmake"])
self.assertNotIn("MY_PKG", content["MyPkGConfig.cmake"])
self.assertNotIn("my_pkg", content["MyPkG2Config.cmake"])
Expand Down

0 comments on commit 02c0dcf

Please sign in to comment.