Skip to content

Commit

Permalink
(#20068) Recipe/20067 armadillo fix cmake targets
Browse files Browse the repository at this point in the history
* [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 64016f5.

* [armadillo] Remove unused cmake target aliases

* Remove Armadillo::armadillo and Armadillo as CMake targets as these
  have not been observed in the wild.
  • Loading branch information
samuel-emrys authored May 7, 2024
1 parent 0243035 commit 4efceac
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
54 changes: 52 additions & 2 deletions recipes/armadillo/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand Down
24 changes: 22 additions & 2 deletions recipes/armadillo/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
24 changes: 22 additions & 2 deletions recipes/armadillo/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 4efceac

Please sign in to comment.