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 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
6 changes: 6 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,12 @@ 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 additional_variables_prefixes(self):
prefix_list = (
self.cmakedeps.get_property("cmake_additional_variables_prefixes", self.conanfile) or [])
return list(set([self.file_name] + prefix_list))

@property
def suffix(self):
if not self.require.build:
Expand Down
16 changes: 10 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,
"additional_variables_prefixes": self.additional_variables_prefixes,
"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,14 @@ 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) }} )
{% for prefix in additional_variables_prefixes %}
set({{ prefix }}_VERSION_STRING "{{ version }}")
set({{ prefix }}_INCLUDE_DIRS {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ prefix }}_INCLUDE_DIR {{ pkg_var(pkg_name, 'INCLUDE_DIRS', config_suffix) }} )
set({{ prefix }}_LIBRARIES {{ pkg_var(pkg_name, 'LIBRARIES', config_suffix) }} )
set({{ prefix }}_DEFINITIONS {{ pkg_var(pkg_name, 'DEFINITIONS', config_suffix) }} )

{% endfor %}

# 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,38 @@ 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()
base_conanfile = str(GenConanfile("dep", "1.0"))
conanfile = base_conanfile + """
def package_info(self):
self.cpp_info.set_property("cmake_file_name", "CMakeFileName")
"""
client.save({"dep/conanfile.py": conanfile})
client.run("create dep")
client.run("install --requires=dep/1.0 -g CMakeDeps")

# Check that all the CMake variables are generated with the file_name
dep_config = client.load("CMakeFileNameConfig.cmake")
cmake_variables = ["VERSION_STRING", "INCLUDE_DIRS", "INCLUDE_DIR", "LIBRARIES", "DEFINITIONS"]
for variable in cmake_variables:
assert f"CMakeFileName_{variable}" in dep_config

conanfile = base_conanfile + """
def package_info(self):
self.cpp_info.set_property("cmake_file_name", "NewCMakeFileName")
self.cpp_info.set_property("cmake_additional_variables_prefixes", ["PREFIX", "prefix", "PREFIX"])
"""
client.save({"dep/conanfile.py": conanfile})
client.run("create dep")
client.run("install --requires=dep/1.0 -g CMakeDeps")

# Check that all the CMake variables are generated with the file_name and both prefixes
dep_config = client.load("NewCMakeFileNameConfig.cmake")
for variable in cmake_variables:
assert f"NewCMakeFileName_{variable}" in dep_config
assert f"PREFIX_{variable}" in dep_config
assert f"prefix_{variable}" in dep_config
# Check that variables are not duplicated
assert dep_config.count("PREFIX_VERSION") == 1