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 property to change the CMake variable names generated with CMakeDeps #16231

Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions conan/tools/cmake/cmakedeps/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def root_target_name(self):
def file_name(self):
return self.cmakedeps.get_cmake_package_name(self.conanfile, module_mode=self.generating_module) + self.suffix

@property
def legacy_variable_prefix(self):
return self.cmakedeps.get_property("cmake_legacy_variable_prefix", self.conanfile)
juansblanco marked this conversation as resolved.
Show resolved Hide resolved

@property
def suffix(self):
if not self.require.build:
Expand Down
19 changes: 13 additions & 6 deletions conan/tools/cmake/cmakedeps/templates/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def context(self):
targets_include += "{}Targets.cmake".format(self.file_name)
return {"is_module": self.generating_module,
"version": self.conanfile.ref.version,
"file_name": self.file_name,
"file_name": self.file_name,
"legacy_variable_prefix": self.legacy_variable_prefix,
"pkg_name": self.pkg_name,
"config_suffix": self.config_suffix,
"check_components_exist": self.cmakedeps.check_components_exist,
Expand Down Expand Up @@ -67,11 +68,17 @@ def template(self):
endif()
endforeach()

set({{ file_name }}_VERSION_STRING "{{ version }}")
set({{ file_name }}_INCLUDE_DIRS {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ file_name }}_INCLUDE_DIR {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ file_name }}_LIBRARIES {{ pkg_var(pkg_name, 'LIBRARIES', config_suffix) }} )
set({{ file_name }}_DEFINITIONS {{ pkg_var(pkg_name, 'DEFINITIONS', config_suffix) }} )
{% if legacy_variable_prefix %}
{% set prefix_name = legacy_variable_prefix %}
{% else %}
{% set prefix_name = file_name %}
{% endif %}

set({{ prefix_name }}_VERSION_STRING "{{ version }}")
set({{ prefix_name }}_INCLUDE_DIRS {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ prefix_name }}_INCLUDE_DIR {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ prefix_name }}_LIBRARIES {{ pkg_var(pkg_name, 'LIBRARIES', config_suffix) }} )
set({{ prefix_name }}_DEFINITIONS {{ pkg_var(pkg_name, 'DEFINITIONS', config_suffix) }} )
juansblanco marked this conversation as resolved.
Show resolved Hide resolved

# Only the first installed configuration is included to avoid the collision
foreach(_BUILD_MODULE {{ pkg_var(pkg_name, 'BUILD_MODULES_PATHS', config_suffix) }} )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,39 @@ def generate(self):
assert 'set(dep_NO_SONAME_MODE_RELEASE TRUE)' in dep
other = c.load("app/other-release-data.cmake")
assert 'set(other_other_mycomp1_NO_SONAME_MODE_RELEASE TRUE)' in other

def test_cmakedeps_set_legacy_variable_name():
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
client = TestClient()
conanfile = textwrap.dedent("""\
from conan import ConanFile

class libRecipe(ConanFile):
name = "dep"
version = "1.0"

def package_info(self):
self.cpp_info.set_property("cmake_legacy_variable_prefix", "DEP")
""")
client.save({"conanfile.py": conanfile})
client.run("create .")
conanfile = textwrap.dedent("""\
from conan import ConanFile
from conan.tools.cmake import CMakeDeps

class libRecipe(ConanFile):
name = "lib"
version = "1.0"

settings = "os", "compiler", "build_type", "arch"

def requirements(self):
self.requires("dep/1.0")

def generate(self):
deps = CMakeDeps(self)
deps.generate()
""")
client.save({"conanfile.py": conanfile})
client.run("install .")
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
dep_config = client.load("dep-config.cmake")
assert "DEP_VERSION" in dep_config