Skip to content

Commit

Permalink
Add property to change the CMake variable names generated with CMakeD…
Browse files Browse the repository at this point in the history
…eps (conan-io#16231)

* add legacy variable prefix for cmakedeps

* fix implementation

* improve template variables

* simplify code

* improve test

* add support for multiple cmake variable names and simplify test

* improve code
  • Loading branch information
juansblanco authored and memsharded committed May 14, 2024
1 parent ed26d4d commit f855e95
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
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():
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

0 comments on commit f855e95

Please sign in to comment.