Skip to content

Commit

Permalink
(#18820) pagmo2: migrate to Conan v2
Browse files Browse the repository at this point in the history
* pagmo2: migrate to Conan v2

* pagmo2: require boost::boost as a fix for MSVC linker issues

* pagmo2: tweak compiler validation

* pagmo2: add v2.19.0, disable MSVC shared build

* pagmo2: MSVC shared builds are broken

* pagmo2: bump boost and onetbb versions
  • Loading branch information
valgur authored Jul 3, 2024
1 parent a4b4b08 commit ef2ae81
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 161 deletions.
7 changes: 0 additions & 7 deletions recipes/pagmo2/all/CMakeLists.txt

This file was deleted.

3 changes: 3 additions & 0 deletions recipes/pagmo2/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
sources:
"2.19.0":
url: "https://github.com/esa/pagmo2/archive/refs/tags/v2.19.0.tar.gz"
sha256: "701ada528de7d454201e92a5d88903dd1c22ea64f43861d9694195ddfef82a70"
"2.17.0":
url: "https://github.com/esa/pagmo2/archive/refs/tags/v2.17.0.tar.gz"
sha256: "1b95b036f75e6fa0b21082ab228dbd63cd18ca10d9622ac53629245e0f95c35c"
160 changes: 86 additions & 74 deletions recipes/pagmo2/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import functools
import os

required_conan_version = ">=1.43.0"
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, replace_in_file, rmdir
from conan.tools.microsoft import is_msvc
from conan.tools.scm import Version

required_conan_version = ">=1.53.0"


class Pagmo2Conan(ConanFile):
name = "pagmo2"
description = "pagmo is a C++ scientific library for massively parallel optimization."
license = ("LGPL-3.0-or-later", "GPL-3.0-or-later")
topics = ("pagmo", "optimization", "parallel-computing", "genetic-algorithm", "metaheuristics")
homepage = "https://esa.github.io/pagmo2"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://esa.github.io/pagmo2"
topics = ("pagmo", "optimization", "parallel-computing", "genetic-algorithm", "metaheuristics")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
Expand All @@ -30,124 +36,130 @@ class Pagmo2Conan(ConanFile):
"with_ipopt": False,
}

exports_sources = "CMakeLists.txt"
generators = "cmake", "cmake_find_package", "cmake_find_package_multi"

@property
def _source_subfolder(self):
return "source_subfolder"
def _compilers_minimum_version(self):
return {
"Visual Studio": "15.7",
"msvc": "191",
"gcc": "7",
"clang": "5.0",
"apple-clang": "9",
}

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
del self.options.fPIC
self.options.rm_safe("fPIC")

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
self.requires("boost/1.78.0")
self.requires("onetbb/2020.3")
self.requires("boost/1.85.0", transitive_headers=True)
self.requires("onetbb/2021.12.0")
if self.options.with_eigen:
self.requires("eigen/3.4.0")
self.requires("eigen/3.4.0", transitive_headers=True)
if self.options.with_nlopt:
self.requires("nlopt/2.7.1")

@property
def _compilers_minimum_version(self):
return {
"Visual Studio": "15.7",
"gcc": "7",
"clang": "5.0",
"apple-clang": "9.1"
}
self.requires("nlopt/2.7.1", transitive_headers=True, transitive_libs=True)

@property
def _required_boost_components(self):
return ["serialization"]

def validate(self):
if self.settings.compiler.get_safe("cppstd"):
tools.check_min_cppstd(self, 17)

def lazy_lt_semver(v1, v2):
lv1 = [int(v) for v in v1.split(".")]
lv2 = [int(v) for v in v2.split(".")]
min_length = min(len(lv1), len(lv2))
return lv1[:min_length] < lv2[:min_length]
check_min_cppstd(self, 17)

minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
if not minimum_version:
self.output.warn("{} {} requires C++17. Your compiler is unknown. Assuming it supports C++17.".format(self.name, self.version))
elif lazy_lt_semver(str(self.settings.compiler.version), minimum_version):
raise ConanInvalidConfiguration("{} {} requires C++17, which your compiler does not support.".format(self.name, self.version))
if minimum_version and Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration(
f"{self.name} {self.version} requires C++17, which your compiler does not support."
)

# TODO: add ipopt support
if self.options.with_ipopt:
raise ConanInvalidConfiguration("ipopt recipe not available yet in CCI")

miss_boost_required_comp = any(getattr(self.options["boost"], "without_{}".format(boost_comp), True) for boost_comp in self._required_boost_components)
if self.options["boost"].header_only or miss_boost_required_comp:
raise ConanInvalidConfiguration("{0} requires non header-only boost with these components: {1}".format(self.name, ", ".join(self._required_boost_components)))
miss_boost_required_comp = any(
self.dependencies["boost"].options.get_safe(f"without_{boost_comp}", True)
for boost_comp in self._required_boost_components
)
if self.dependencies["boost"].options.header_only or miss_boost_required_comp:
raise ConanInvalidConfiguration(
"{0} requires non header-only boost with these components: {1}".format(
self.name, ", ".join(self._required_boost_components)
)
)

if is_msvc(self) and self.options.shared:
# test_package.obj : error LNK2019: unresolved external symbol "public: __cdecl boost::archive::codecvt_null<wchar_t>::codecvt_null<wchar_t>(unsigned __int64)"
# https://github.com/boostorg/serialization/issues/232
# https://github.com/conda-forge/scipoptsuite-feedstock/pull/44
raise ConanInvalidConfiguration("Shared builds are currently broken on MSVC")

def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["PAGMO_BUILD_TESTS"] = False
tc.variables["PAGMO_BUILD_BENCHMARKS"] = False
tc.variables["PAGMO_BUILD_TUTORIALS"] = False
tc.variables["PAGMO_WITH_EIGEN3"] = self.options.with_eigen
tc.variables["PAGMO_WITH_NLOPT"] = self.options.with_nlopt
tc.variables["PAGMO_WITH_IPOPT"] = self.options.with_ipopt
tc.variables["PAGMO_ENABLE_IPO"] = False
tc.variables["PAGMO_BUILD_STATIC_LIBRARY"] = not self.options.shared
tc.generate()
tc = CMakeDeps(self)
tc.generate()

def _patch_sources(self):
# do not force MT runtime for static lib
tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"),
"if(YACMA_COMPILER_IS_MSVC AND PAGMO_BUILD_STATIC_LIBRARY)",
"if(0)")
if Version(self.version) < "2.18":
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
"if(YACMA_COMPILER_IS_MSVC AND PAGMO_BUILD_STATIC_LIBRARY)", "if(0)")
# No warnings as errors
yacma_cmake = os.path.join(self._source_subfolder, "cmake_modules", "yacma", "YACMACompilerLinkerSettings.cmake")
tools.replace_in_file(yacma_cmake, "list(APPEND _YACMA_CXX_FLAGS_DEBUG \"-Werror\")", "")
tools.replace_in_file(yacma_cmake, "_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(/W4)", "")
tools.replace_in_file(yacma_cmake, "_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(/WX)", "")

@functools.lru_cache(1)
def _configure_cmake(self):
cmake = CMake(self)
cmake.definitions["PAGMO_BUILD_TESTS"] = False
cmake.definitions["PAGMO_BUILD_BENCHMARKS"] = False
cmake.definitions["PAGMO_BUILD_TUTORIALS"] = False
cmake.definitions["PAGMO_WITH_EIGEN3"] = self.options.with_eigen
cmake.definitions["PAGMO_WITH_NLOPT"] = self.options.with_nlopt
cmake.definitions["PAGMO_WITH_IPOPT"] = self.options.with_ipopt
cmake.definitions["PAGMO_ENABLE_IPO"] = False
cmake.definitions["PAGMO_BUILD_STATIC_LIBRARY"] = not self.options.shared
cmake.configure()
return cmake
yacma_cmake = os.path.join(self.source_folder, "cmake_modules", "yacma", "YACMACompilerLinkerSettings.cmake")
replace_in_file(self, yacma_cmake, 'list(APPEND _YACMA_CXX_FLAGS_DEBUG "-Werror")', "")
replace_in_file(self, yacma_cmake, "_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(/W4)", "")
replace_in_file(self, yacma_cmake, "_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(/WX)", "")

def build(self):
self._patch_sources()
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
self.copy(pattern="COPYING.*", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
copy(self, "COPYING.*",
dst=os.path.join(self.package_folder, "licenses"),
src=self.source_folder)
cmake = CMake(self)
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "pagmo")
# https://esa.github.io/pagmo2/quickstart.html#using-pagmo-with-cmake
self.cpp_info.set_property("cmake_file_name", "Pagmo")
self.cpp_info.set_property("cmake_target_name", "Pagmo::pagmo")
# TODO: back to global scope in conan v2 once cmake_find_package_* generators removed

self.cpp_info.components["_pagmo"].libs = ["pagmo"]
self.cpp_info.components["_pagmo"].requires = ["boost::headers", "boost::serialization", "onetbb::onetbb"]
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.components["_pagmo"].system_libs.append("pthread")

# TODO: back to global scope in conan v2 once cmake_find_package_* generators removed
self.cpp_info.components["_pagmo"].requires = ["boost::boost", "onetbb::onetbb"]
if self.options.with_eigen:
self.cpp_info.components["_pagmo"].requires.append("eigen::eigen")
if self.options.with_nlopt:
self.cpp_info.components["_pagmo"].requires.append("nlopt::nlopt")
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.components["_pagmo"].system_libs.append("pthread")

# TODO: to remove in conan v2 once cmake_find_package_* generators removed
self.cpp_info.filenames["cmake_find_package"] = "pagmo"
self.cpp_info.filenames["cmake_find_package_multi"] = "pagmo"
self.cpp_info.names["cmake_find_package"] = "Pagmo"
self.cpp_info.names["cmake_find_package_multi"] = "Pagmo"
self.cpp_info.components["_pagmo"].names["cmake_find_package"] = "pagmo"
self.cpp_info.components["_pagmo"].names["cmake_find_package_multi"] = "pagmo"
self.cpp_info.components["_pagmo"].set_property("cmake_target_name", "Pagmo::pagmo")
9 changes: 3 additions & 6 deletions recipes/pagmo2/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
cmake_minimum_required(VERSION 3.8)
project(test_package)
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(pagmo REQUIRED CONFIG)
find_package(Pagmo REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} Pagmo::pagmo)
Expand Down
19 changes: 14 additions & 5 deletions recipes/pagmo2/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
8 changes: 8 additions & 0 deletions recipes/pagmo2/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/
${CMAKE_CURRENT_BINARY_DIR}/test_package/)
17 changes: 17 additions & 0 deletions recipes/pagmo2/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
2 changes: 2 additions & 0 deletions recipes/pagmo2/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
versions:
"2.19.0":
folder: all
"2.17.0":
folder: all
"2.10":
Expand Down
7 changes: 0 additions & 7 deletions recipes/pagmo2/pre_2.11/CMakeLists.txt

This file was deleted.

1 change: 0 additions & 1 deletion recipes/pagmo2/pre_2.11/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ sources:
patches:
"2.10":
- patch_file: "patches/0001-fix-cmake-module-path-2-10.patch"
base_path: "source_subfolder"
Loading

0 comments on commit ef2ae81

Please sign in to comment.