From 4084b6da8052f0a07e8a240fcbb4a1555ef14c5b Mon Sep 17 00:00:00 2001 From: Martin Lambertsen Date: Sat, 20 Jul 2024 22:23:39 +0200 Subject: [PATCH 1/3] Add IMPORTED_CONFIGURATIONS property for CMake targets Some frameworks use the property in order to find libraries, e.g. like https://github.com/ros/catkin/blob/noetic-devel/cmake/catkin_libraries.cmake#L150 In order to support these kind of usages, this commit adds the property to all targets. This should solve #14606 and #16688. --- conan/tools/cmake/cmakedeps/templates/macros.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/conan/tools/cmake/cmakedeps/templates/macros.py b/conan/tools/cmake/cmakedeps/templates/macros.py index b5c0e185b8a..6ae7b33b9eb 100644 --- a/conan/tools/cmake/cmakedeps/templates/macros.py +++ b/conan/tools/cmake/cmakedeps/templates/macros.py @@ -88,6 +88,9 @@ def template(self): message(DEBUG "Created target ${_LIB_NAME} ${library_type} IMPORTED") set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION${config_suffix} ${CONAN_FOUND_LIBRARY} IMPORTED_NO_SONAME ${no_soname_mode}) endif() + string(REGEX REPLACE "^_" "" _LOWER_CASE_CONFIG ${config_suffix}) + string(TOUPPER ${_LOWER_CASE_CONFIG} _CONFIG) + set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_CONFIGURATIONS ${_CONFIG}) list(APPEND _out_libraries_target ${_LIB_NAME}) message(VERBOSE "Conan: Found: ${CONAN_FOUND_LIBRARY}") else() From 8bb68d651b747cb307f3bad750ddd2cb337ed440 Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 2 Sep 2024 23:54:47 +0200 Subject: [PATCH 2/3] added test --- .../tools/cmake/cmakedeps/templates/macros.py | 3 +- .../cmake/cmakedeps/test_cmakedeps.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/conan/tools/cmake/cmakedeps/templates/macros.py b/conan/tools/cmake/cmakedeps/templates/macros.py index 6ae7b33b9eb..9e29b74efb1 100644 --- a/conan/tools/cmake/cmakedeps/templates/macros.py +++ b/conan/tools/cmake/cmakedeps/templates/macros.py @@ -88,8 +88,7 @@ def template(self): message(DEBUG "Created target ${_LIB_NAME} ${library_type} IMPORTED") set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION${config_suffix} ${CONAN_FOUND_LIBRARY} IMPORTED_NO_SONAME ${no_soname_mode}) endif() - string(REGEX REPLACE "^_" "" _LOWER_CASE_CONFIG ${config_suffix}) - string(TOUPPER ${_LOWER_CASE_CONFIG} _CONFIG) + string(REGEX REPLACE "^_" "" _CONFIG ${config_suffix}) set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_CONFIGURATIONS ${_CONFIG}) list(APPEND _out_libraries_target ${_LIB_NAME}) message(VERBOSE "Conan: Found: ${CONAN_FOUND_LIBRARY}") diff --git a/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index 68a0a86556b..aad075796e7 100644 --- a/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -620,6 +620,45 @@ def test_error_missing_build_type(matrix_client): assert "matrix/1.0: Hello World Release!" in client.out +@pytest.mark.tool("cmake", "3.23") +def test_imported_configurations(matrix_client): + # https://github.com/conan-io/conan/issues/11168 + client = matrix_client + + conanfile = textwrap.dedent(""" + [requires] + matrix/1.0 + [generators] + CMakeDeps + CMakeToolchain + """) + + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.15) + project(app) + find_package(matrix REQUIRED) + # The matrix::matrix is not the target defining the data, it is a package interface + get_target_property(configs matrix::matrix IMPORTED_CONFIGURATIONS) + foreach(cfg ${configs}) + message(STATUS "matrix::matrix configuration found: ${cfg}!!") + endforeach() + get_target_property(configs CONAN_LIB::matrix_matrix_RELEASE IMPORTED_CONFIGURATIONS) + foreach(cfg ${configs}) + message(STATUS "CONAN_LIB::matrix configuration found: ${cfg}!!") + endforeach() + """) + + client.save({"conanfile.txt": conanfile, + "CMakeLists.txt": cmakelists}, clean_first=True) + + client.run("install .") + client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake") + # TODO: The matrix::matrix target does not contain the information + # this will require the new CMakeDeps generator + assert "matrix::matrix configuration found: configs-NOTFOUND!!" in client.out + assert "CONAN_LIB::matrix configuration found: RELEASE!!" in client.out + + @pytest.mark.tool("cmake") def test_map_imported_config(matrix_client): # https://github.com/conan-io/conan/issues/12041 From 35215a2f2ef1ae2722ec01961a426260cd0fc37a Mon Sep 17 00:00:00 2001 From: memsharded Date: Tue, 3 Sep 2024 01:03:52 +0200 Subject: [PATCH 3/3] add build-type --- test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index aad075796e7..419393bd7c7 100644 --- a/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -652,7 +652,8 @@ def test_imported_configurations(matrix_client): "CMakeLists.txt": cmakelists}, clean_first=True) client.run("install .") - client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake") + client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake " + "-DCMAKE_BUILD_TYPE=Release") # TODO: The matrix::matrix target does not contain the information # this will require the new CMakeDeps generator assert "matrix::matrix configuration found: configs-NOTFOUND!!" in client.out