From 4efceac27ba196eecfdc46a2c33f160e4ae9c65e Mon Sep 17 00:00:00 2001 From: Samuel Dowling Date: Wed, 8 May 2024 04:13:35 +0930 Subject: [PATCH] (#20068) Recipe/20067 armadillo fix cmake targets * [armadillo] Fix cmake target names to match upstream These target names are defined by the public CMake module published by Kitware at https://cmake.org/cmake/help/latest/module/FindArmadillo.html * [armadillo] Add legacy CMake variables Add the following legacy CMake variables defined by the upstream FindArmadillo.cmake module published by Kitware - https://cmake.org/cmake/help/latest/module/FindArmadillo.html: * ARMADILLO_FOUND * ARMADILLO_INCLUDE_DIRS * ARMADILLO_LIBRARIES * ARMADILLO_VERSION_MAJOR * ARMADILLO_VERSION_MINOR * ARMADILLO_VERSION_PATCH * ARMADILLO_VERSION_STRING * ARMADILLO_VERSION_NAME * [armadillo] Remove debug messages * [armadillo] Update find_package to use new Armadillo target * [armadillo] Add conan 1.x compatibility for legacy cmake variables * [armadillo] Add alias targets for wide compatibility Add alias targets for wide compatibility with existing CMake scripts. The upstream CMake find module doesn't define what the name of the targets should be, so this provides coverage for the wide usage of different possible combinations found in the wild. This is based on inspection of target_link_library patterns for armadillo usage found on github. * [armadillo] Make primary CMake target armadillo::armadillo and reduce aliases * Make armadillo::armadillo the primary CMake target because the upstream CMake module doesn't enforce this behaviour, and the policy of CCI is to use packagename::packagename when no target is enforced * Make Armadillo::Armadillo and armadillo CMake target aliases as these are common in the wild, and this will facilitate conan package compatibility with existing build scripts. * Remove Armadillo::armadillo and Armadillo as CMake targets as these have not been observed in the wild. * Revert "[armadillo] Make primary CMake target armadillo::armadillo and reduce aliases" This reverts commit 64016f570774514b49c5d35ec2d0afb9a3e52f29. * [armadillo] Remove unused cmake target aliases * Remove Armadillo::armadillo and Armadillo as CMake targets as these have not been observed in the wild. --- recipes/armadillo/all/conanfile.py | 54 ++++++++++++++++++- .../armadillo/all/test_package/CMakeLists.txt | 24 ++++++++- .../all/test_v1_package/CMakeLists.txt | 24 ++++++++- 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/recipes/armadillo/all/conanfile.py b/recipes/armadillo/all/conanfile.py index d37ca846e702e..4735e4fcda303 100644 --- a/recipes/armadillo/all/conanfile.py +++ b/recipes/armadillo/all/conanfile.py @@ -1,13 +1,14 @@ from conan import ConanFile from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout -from conan.tools.files import copy, get, rmdir, apply_conandata_patches, export_conandata_patches +from conan.tools.files import copy, get, rmdir, apply_conandata_patches, export_conandata_patches, save from conan.tools.build import check_min_cppstd from conan.tools.scm import Version from conan.tools.build import cross_building from conan.errors import ConanInvalidConfiguration import os +import textwrap -required_conan_version = ">=1.53.0" +required_conan_version = ">=1.55.0" class ArmadilloConan(ConanFile): @@ -258,6 +259,40 @@ def build(self): cmake.configure() cmake.build() + @property + def _get_arma_version_name(self): + version_file = os.path.join(self.source_folder, "include", "armadillo_bits", "arma_version.hpp") + with open(version_file, "r") as f: + for line in f: + if "ARMA_VERSION_NAME" in line: + return line.split("\"")[-2].strip() + return "" + + @property + def _module_vars_rel_path(self): + return os.path.join("lib", "cmake", f"conan-official-{self.name}-variables.cmake") + + def _create_cmake_module_variables(self, module_file): + content = textwrap.dedent(f"""\ + set(ARMADILLO_FOUND TRUE) + if(DEFINED Armadillo_INCLUDE_DIRS) + set(ARMADILLO_INCLUDE_DIRS ${{Armadillo_INCLUDE_DIRS}}) + endif() + if(DEFINED Armadillo_LIBRARIES) + set(ARMADILLO_LIBRARIES ${{Armadillo_LIBRARIES}}) + endif() + set(ARMADILLO_VERSION_MAJOR "{Version(self.version).major}") + set(ARMADILLO_VERSION_MINOR "{Version(self.version).minor}") + set(ARMADILLO_VERSION_PATCH "{Version(self.version).patch}") + if(DEFINED Armadillo_VERSION_STRING) + set(ARMADILLO_VERSION_STRING ${{Armadillo_VERSION_STRING}}) + else() + set(ARMADILLO_VERSION_STRING "${{ARMADILLO_VERSION_MAJOR}}.${{ARMADILLO_VERSION_MINOR}}.${{ARMADILLO_VERSION_PATCH}}") + endif() + set(ARMADILLO_VERSION_NAME "{self._get_arma_version_name}") + """) + save(self, module_file, content) + def package(self): cmake = CMake(self) cmake.install() @@ -266,10 +301,25 @@ def package(self): copy(self, "NOTICE.txt", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder) rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig")) rmdir(self, os.path.join(self.package_folder, "share")) + self._create_cmake_module_variables(os.path.join(self.package_folder, self._module_vars_rel_path)) + def package_info(self): self.cpp_info.libs = ["armadillo"] self.cpp_info.set_property("pkg_config_name", "armadillo") + self.cpp_info.set_property("cmake_find_mode", "both") + self.cpp_info.set_property("cmake_file_name", "Armadillo") + self.cpp_info.set_property("cmake_target_name", "Armadillo::Armadillo") + self.cpp_info.set_property("cmake_target_aliases", ["armadillo", "armadillo::armadillo"]) + self.cpp_info.set_property("cmake_build_modules", [self._module_vars_rel_path]) + + # Remove when cmake_find_package and pkg_config generators are no + # longer supported + self.cpp_info.names["pkg_config"] = "armadillo" + self.cpp_info.names["cmake_find_package"] = "Armadillo" + self.cpp_info.names["cmake_find_package_multi"] = "Armadillo" + self.cpp_info.build_modules["cmake_find_package"] = [self._module_vars_rel_path] + self.cpp_info.build_modules["cmake_find_package_multi"] = [self._module_vars_rel_path] if self.options.get_safe("use_extern_rng"): self.cpp_info.defines.append("ARMA_USE_EXTERN_RNG") diff --git a/recipes/armadillo/all/test_package/CMakeLists.txt b/recipes/armadillo/all/test_package/CMakeLists.txt index da38c2d4cf348..7f0a114a2e9e8 100644 --- a/recipes/armadillo/all/test_package/CMakeLists.txt +++ b/recipes/armadillo/all/test_package/CMakeLists.txt @@ -1,12 +1,32 @@ cmake_minimum_required(VERSION 3.15) project(PackageTest CXX) -find_package(armadillo CONFIG REQUIRED) +find_package(Armadillo CONFIG REQUIRED) if (LINK_HDF5) find_package(HDF5) set(HDF5_TARGETS HDF5::HDF5) endif() add_executable(example src/example.cpp) -target_link_libraries(example armadillo::armadillo ${HDF5_TARGETS}) +target_link_libraries(example Armadillo::Armadillo ${HDF5_TARGETS}) set_property(TARGET example PROPERTY CXX_STANDARD 11) + +# Test whether variables from https://cmake.org/cmake/help/latest/module/FindArmadillo.html are properly defined + +set(_custom_vars + ARMADILLO_FOUND + ARMADILLO_INCLUDE_DIRS + ARMADILLO_LIBRARIES + ARMADILLO_VERSION_MAJOR + ARMADILLO_VERSION_MINOR + ARMADILLO_VERSION_PATCH + ARMADILLO_VERSION_STRING + ARMADILLO_VERSION_NAME +) +foreach(_custom_var ${_custom_vars}) + if(DEFINED ${_custom_var}) + message(STATUS "${_custom_var}: ${${_custom_var}}") + else() + message(FATAL_ERROR "${_custom_var} not defined") + endif() +endforeach() diff --git a/recipes/armadillo/all/test_v1_package/CMakeLists.txt b/recipes/armadillo/all/test_v1_package/CMakeLists.txt index 3ac69a9731274..30cc31a24a3d1 100644 --- a/recipes/armadillo/all/test_v1_package/CMakeLists.txt +++ b/recipes/armadillo/all/test_v1_package/CMakeLists.txt @@ -4,8 +4,28 @@ project(PackageTest CXX) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup(TARGETS) -find_package(armadillo REQUIRED) +find_package(Armadillo REQUIRED) add_executable(example ../test_package/src/example.cpp) -target_link_libraries(example armadillo::armadillo) +target_link_libraries(example Armadillo::Armadillo) set_property(TARGET example PROPERTY CXX_STANDARD 11) + +# Test whether variables from https://cmake.org/cmake/help/latest/module/FindArmadillo.html are properly defined + +set(_custom_vars + ARMADILLO_FOUND + ARMADILLO_INCLUDE_DIRS + ARMADILLO_LIBRARIES + ARMADILLO_VERSION_MAJOR + ARMADILLO_VERSION_MINOR + ARMADILLO_VERSION_PATCH + ARMADILLO_VERSION_STRING + ARMADILLO_VERSION_NAME +) +foreach(_custom_var ${_custom_vars}) + if(DEFINED ${_custom_var}) + message(STATUS "${_custom_var}: ${${_custom_var}}") + else() + message(FATAL_ERROR "${_custom_var} not defined") + endif() +endforeach()