Repository: conan-io/conan-center-index

Issue:
[package] armadillo/12.2.0 Doesn't use upstream cmake target
### Description

CMake has a published [`FindArmadillo.cmake`](https://cmake.org/cmake/help/latest/module/FindArmadillo.html) module that defines the filename to be `Armadillo` and the target to be `Armadillo::Armadillo`. This is in contrast to the default recipe `armadillo::armadillo` currently used.

This should be updated to reflect the upstream convention.

### Package and Environment Details

N/A


### Conan profile

N/A


### Steps to reproduce

N/A

### Logs

<details><summary>Click to expand log</summary>

```
Put your log output here
```

</details>



Discussion:

Resolving patch:
diff --git a/recipes/armadillo/all/conanfile.py b/recipes/armadillo/all/conanfile.py
--- 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):
@@ -263,6 +264,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()
@@ -271,10 +306,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")


Test patch:
diff --git a/recipes/armadillo/all/test_package/CMakeLists.txt b/recipes/armadillo/all/test_package/CMakeLists.txt
--- 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
--- 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()