Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe/20067 armadillo fix cmake targets #20068

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 @@ -134,7 +135,7 @@
]
if options_without_value and (len(options) != len(options_without_value)):
raise ConanInvalidConfiguration(
"Options {} must all be set to '{}' to use this feature. To fix this, set option {} to '{}'.".format(

Check warning on line 138 in recipes/armadillo/all/conanfile.py

View workflow job for this annotation

GitHub Actions / Lint changed conanfile.py (v2 migration)

Duplicate string formatting argument 'value', consider passing as named argument
", ".join(options),
value,
", ".join(options_without_value),
Expand Down Expand Up @@ -263,6 +264,40 @@
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:

Check warning on line 270 in recipes/armadillo/all/conanfile.py

View workflow job for this annotation

GitHub Actions / Lint changed conanfile.py (v2 migration)

Using open without explicitly specifying an encoding
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 @@ -271,10 +306,25 @@
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
22 changes: 21 additions & 1 deletion recipes/armadillo/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.15)
project(PackageTest CXX)

find_package(armadillo CONFIG REQUIRED)
find_package(Armadillo CONFIG REQUIRED)
samuel-emrys marked this conversation as resolved.
Show resolved Hide resolved
if (LINK_HDF5)
find_package(HDF5)
set(HDF5_TARGETS HDF5::HDF5)
Expand All @@ -10,3 +10,23 @@ endif()
add_executable(example src/example.cpp)
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()
22 changes: 21 additions & 1 deletion 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)
samuel-emrys marked this conversation as resolved.
Show resolved Hide resolved

add_executable(example ../test_package/src/example.cpp)
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()