From 7eda5e1152c0c2d3ffbb6091d7deee8a197e04ba Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Thu, 9 Feb 2023 17:09:41 +0100 Subject: [PATCH] conan v2 support --- recipes/coin-cbc/all/conandata.yml | 2 - recipes/coin-cbc/all/conanfile.py | 195 +++++++++--------- .../coin-cbc/all/test_package/CMakeLists.txt | 10 +- .../coin-cbc/all/test_package/conanfile.py | 24 ++- .../all/test_v1_package/CMakeLists.txt | 8 + .../coin-cbc/all/test_v1_package/conanfile.py | 20 ++ 6 files changed, 144 insertions(+), 115 deletions(-) create mode 100644 recipes/coin-cbc/all/test_v1_package/CMakeLists.txt create mode 100644 recipes/coin-cbc/all/test_v1_package/conanfile.py diff --git a/recipes/coin-cbc/all/conandata.yml b/recipes/coin-cbc/all/conandata.yml index 99a9ed92100b17..a1f2b3820b1ce6 100644 --- a/recipes/coin-cbc/all/conandata.yml +++ b/recipes/coin-cbc/all/conandata.yml @@ -5,6 +5,4 @@ sources: patches: "2.10.5": - patch_file: "patches/0001-no-pkg-config-check.patch" - base_path: "source_subfolder" - patch_file: "patches/0002-pthreads4w.patch" - base_path: "source_subfolder" diff --git a/recipes/coin-cbc/all/conanfile.py b/recipes/coin-cbc/all/conanfile.py index ffea985fa3a7d7..7f69d52709fb43 100644 --- a/recipes/coin-cbc/all/conanfile.py +++ b/recipes/coin-cbc/all/conanfile.py @@ -1,14 +1,16 @@ from conan import ConanFile from conan.errors import ConanInvalidConfiguration -from conan.tools.files import rename, get, apply_conandata_patches, rmdir, mkdir +from conan.tools.apple import fix_apple_shared_install_name from conan.tools.build import cross_building -from conan.tools.scm import Version -from conans import AutoToolsBuildEnvironment, tools -from contextlib import contextmanager +from conan.tools.env import VirtualBuildEnv +from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, mkdir, rename, rm, rmdir +from conan.tools.gnu import Autotools, AutotoolsToolchain, PkgConfigDeps +from conan.tools.layout import basic_layout +from conan.tools.microsoft import check_min_vs, is_msvc, msvc_runtime_flag, unix_path import os -import shutil -required_conan_version = ">=1.47.0" +required_conan_version = ">=1.57.0" + class CoinCbcConan(ConanFile): name = "coin-cbc" @@ -16,7 +18,7 @@ class CoinCbcConan(ConanFile): topics = ("clp", "simplex", "solver", "linear", "programming") url = "https://github.com/conan-io/conan-center-index" homepage = "https://github.com/coin-or/Clp" - license = ("EPL-2.0",) + license = "EPL-2.0" settings = "os", "arch", "build_type", "compiler" options = { "shared": [True, False], @@ -28,21 +30,13 @@ class CoinCbcConan(ConanFile): "fPIC": True, "parallel": False, } - generators = "pkg_config" - - _autotools = None - - @property - def _source_subfolder(self): - return "source_subfolder" @property - def _build_subfolder(self): - return "build_subfolder" + def _settings_build(self): + return getattr(self, "settings_build", self.settings) def export_sources(self): - for patch in self.conan_data.get("patches", {}).get(self.version, []): - self.copy(patch["patch_file"]) + export_conandata_patches(self) def config_options(self): if self.settings.os == "Windows": @@ -50,32 +44,30 @@ def config_options(self): def configure(self): if self.options.shared: - del self.options.fPIC + self.options.rm_safe("fPIC") + + def layout(self): + basic_layout(self, src_folder="src") def requirements(self): - self.requires("coin-utils/2.11.4") - self.requires("coin-osi/0.108.6") + self.requires("coin-utils/2.11.6") + self.requires("coin-osi/0.108.7") self.requires("coin-clp/1.17.6") self.requires("coin-cgl/0.60.3") - if self.settings.compiler == "Visual Studio" and self.options.parallel: - self.requires("pthreads4w/3.0.0") - - @property - def _settings_build(self): - return getattr(self, "settings_build", self.settings) - - @property - def _user_info_build(self): - return getattr(self, "user_info_build", self.deps_user_info) + if is_msvc(self) and self.options.parallel: + self.requires("pthreads4w/3.0.0") def build_requirements(self): - self.tool_requires("gnu-config/cci.20201022") - self.tool_requires("pkgconf/1.7.4") - if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"): - self.tool_requires("msys2/cci.latest") - if self.settings.compiler == "Visual Studio": + self.tool_requires("gnu-config/cci.20210814") + if not self.conf.get("tools.gnu:pkg_config", check_type=str): + self.tool_requires("pkgconf/1.9.3") + if self._settings_build.os == "Windows": + self.win_bash = True + if not self.conf.get("tools.microsoft.bash:path", check_type=str): + self.tool_requires("msys2/cci.latest") + if is_msvc(self): self.tool_requires("automake/1.16.5") - + def validate(self): if self.settings.os == "Windows" and self.options.shared: raise ConanInvalidConfiguration("coin-cbc does not support shared builds on Windows") @@ -84,89 +76,94 @@ def validate(self): raise ConanInvalidConfiguration("coin-cbc shared not supported yet when cross-building") def source(self): - get(self, **self.conan_data["sources"][self.version], - strip_root=True, destination=self._source_subfolder) - - @contextmanager - def _build_context(self): - if self.settings.compiler == "Visual Studio": - with tools.vcvars(self.settings): - env = { - "CC": "{} cl -nologo".format(tools.unix_path(self._user_info_build["automake"].compile)), - "CXX": "{} cl -nologo".format(tools.unix_path(self._user_info_build["automake"].compile)), - "LD": "{} link -nologo".format(tools.unix_path(self._user_info_build["automake"].compile)), - "AR": "{} lib".format(tools.unix_path(self._user_info_build["automake"].ar_lib)), - } - with tools.environment_append(env): - yield - else: - yield - - def _configure_autotools(self): - if self._autotools: - return self._autotools - self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows) - self._autotools.libs = [] + get(self, **self.conan_data["sources"][self.version], strip_root=True) + + def generate(self): + env = VirtualBuildEnv(self) + env.generate() + + tc = AutotoolsToolchain(self) yes_no = lambda v: "yes" if v else "no" - configure_args = [ - "--enable-shared={}".format(yes_no(self.options.shared)), - "--enable-cbc-parallel={}".format(yes_no(self.options.parallel)), + tc.configure_args.extend([ + f"--enable-cbc-parallel={yes_no(self.options.parallel)}", "--without-blas", "--without-lapack", - ] - if self.settings.compiler == "Visual Studio": - self._autotools.cxx_flags.append("-EHsc") - configure_args.append(f"--enable-msvc={self.settings.compiler.runtime}") - if Version(self.settings.compiler.version) >= 12: - self._autotools.flags.append("-FS") + ]) + if is_msvc(self): + tc.extra_cxxflags.append("-EHsc") + tc.configure_args.append(f"--enable-msvc={msvc_runtime_flag(self)}") + if check_min_vs(self, "180", raise_invalid=False): + tc.extra_cflags.append("-FS") + tc.extra_cxxflags.append("-FS") if self.options.parallel: - configure_args.append("--with-pthreadsw32-lib={}".format(tools.unix_path(os.path.join(self.deps_cpp_info["pthreads4w"].lib_paths[0], self.deps_cpp_info["pthreads4w"].libs[0] + ".lib")))) - configure_args.append("--with-pthreadsw32-incdir={}".format(tools.unix_path(self.deps_cpp_info["pthreads4w"].include_paths[0]))) - self._autotools.configure(configure_dir=os.path.join(self.source_folder, self._source_subfolder), args=configure_args) - return self._autotools + pthreads4w_cpp_info = self.dependencies["pthreads4w"].cpp_info.aggregated_components() + pthreads4w_lib = os.path.join(pthreads4w_cpp_info.libdirs[0], f"{pthreads4w_cpp_info.libs[0]}.lib") + pthreads4w_includedir = pthreads4w_cpp_info.includedirs[0] + tc.configure_args.extend([ + f"--with-pthreadsw32-lib={unix_path(self, pthreads4w_lib)}", + f"--with-pthreadsw32-incdir={unix_path(self, pthreads4w_includedir)}", + ]) + env = tc.environment() + if is_msvc(self): + compile_wrapper = unix_path(self, self.conf.get("user.automake:compile-wrapper", check_type=str)) + ar_wrapper = unix_path(self, self.conf.get("user.automake:lib-wrapper", check_type=str)) + env.define("CC", f"{compile_wrapper} cl -nologo") + env.define("CXX", f"{compile_wrapper} cl -nologo") + env.define("LD", f"{compile_wrapper} link -nologo") + env.define("AR", f"{ar_wrapper} \"lib -nologo\"") + env.define("NM", "dumpbin -symbols") + env.define("OBJDUMP", ":") + env.define("RANLIB", ":") + env.define("STRIP", ":") + tc.generate(env) + + deps = PkgConfigDeps(self) + deps.generate() def build(self): apply_conandata_patches(self) - shutil.copy(self._user_info_build["gnu-config"].CONFIG_SUB, - os.path.join(self._source_subfolder, "config.sub")) - shutil.copy(self._user_info_build["gnu-config"].CONFIG_GUESS, - os.path.join(self._source_subfolder, "config.guess")) - with self._build_context(): - autotools = self._configure_autotools() - autotools.make() + if not is_msvc(self): + for gnu_config in [ + self.conf.get("user.gnu-config:config_guess", check_type=str), + self.conf.get("user.gnu-config:config_sub", check_type=str), + ]: + if gnu_config: + copy(self, os.path.basename(gnu_config), src=os.path.dirname(gnu_config), dst=self.source_folder) + autotools = Autotools(self) + autotools.configure() + autotools.make() def package(self): - self.copy("LICENSE", src=self._source_subfolder, dst="licenses") + copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) # Installation script expects include/coin to already exist mkdir(self, os.path.join(self.package_folder, "include", "coin")) - with self._build_context(): - autotools = self._configure_autotools() - autotools.install() - - for l in ("CbcSolver", "Cbc", "OsiCbc"): - os.unlink(f"{self.package_folder}/lib/lib{l}.la") - if self.settings.compiler == "Visual Studio": - rename(self, - f"{self.package_folder}/lib/lib{l}.a", - f"{self.package_folder}/lib/{l}.lib") - + autotools = Autotools(self) + autotools.install(args=["-j1"]) + rm(self, "*.la", os.path.join(self.package_folder, "lib")) rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig")) rmdir(self, os.path.join(self.package_folder, "share")) + fix_apple_shared_install_name(self) + if is_msvc(self): + for l in ("CbcSolver", "Cbc", "OsiCbc"): + rename(self, os.path.join(self.package_folder, "lib", f"lib{l}.a"), + os.path.join(self.package_folder, "lib", f"{l}.lib")) def package_info(self): + self.cpp_info.components["libcbc"].set_property("pkg_config_name", "cbc") self.cpp_info.components["libcbc"].libs = ["CbcSolver", "Cbc"] self.cpp_info.components["libcbc"].includedirs.append(os.path.join("include", "coin")) - self.cpp_info.components["libcbc"].requires = ["coin-clp::osi-clp", "coin-utils::coin-utils", "coin-osi::coin-osi", "coin-cgl::coin-cgl"] - self.cpp_info.components["libcbc"].names["pkg_config"] = "cbc" + self.cpp_info.components["libcbc"].requires = [ + "coin-clp::osi-clp", "coin-utils::coin-utils", + "coin-osi::coin-osi", "coin-cgl::coin-cgl", + ] if self.settings.os in ["Linux", "FreeBSD"] and self.options.parallel: self.cpp_info.components["libcbc"].system_libs.append("pthread") - if self.settings.os in ["Windows"] and self.options.parallel: + if is_msvc(self) and self.options.parallel: self.cpp_info.components["libcbc"].requires.append("pthreads4w::pthreads4w") + self.cpp_info.components["osi-cbc"].set_property("pkg_config_name", "osi-cbc") self.cpp_info.components["osi-cbc"].libs = ["OsiCbc"] self.cpp_info.components["osi-cbc"].requires = ["libcbc"] - self.cpp_info.components["osi-cbc"].names["pkg_config"] = "osi-cbc" - bin_path = os.path.join(self.package_folder, "bin") - self.output.info("Appending PATH environment variable: {}".format(bin_path)) - self.env_info.PATH.append(bin_path) + # TODO: to remove in conan v2 + self.env_info.PATH.append(os.path.join(self.package_folder, "bin")) diff --git a/recipes/coin-cbc/all/test_package/CMakeLists.txt b/recipes/coin-cbc/all/test_package/CMakeLists.txt index 18c2470c6a65f3..1cc1524300aeee 100644 --- a/recipes/coin-cbc/all/test_package/CMakeLists.txt +++ b/recipes/coin-cbc/all/test_package/CMakeLists.txt @@ -1,11 +1,7 @@ -cmake_minimum_required(VERSION 3.1) -project(test_package) - -include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup() - -include(FindPkgConfig) +cmake_minimum_required(VERSION 3.6) +project(test_package LANGUAGES CXX) +find_package(PkgConfig REQUIRED) pkg_check_modules(OsiCbc REQUIRED IMPORTED_TARGET osi-cbc) add_executable(${PROJECT_NAME} test_package.cpp) diff --git a/recipes/coin-cbc/all/test_package/conanfile.py b/recipes/coin-cbc/all/test_package/conanfile.py index 9ade272b0a40d8..af1af0ebb3d7f1 100644 --- a/recipes/coin-cbc/all/test_package/conanfile.py +++ b/recipes/coin-cbc/all/test_package/conanfile.py @@ -1,13 +1,23 @@ -from conans import ConanFile, CMake, tools +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import CMake, cmake_layout import os class TestPackageConan(ConanFile): - settings = "os", "compiler", "build_type", "arch" - generators = "cmake", "pkg_config" + settings = "os", "arch", "compiler", "build_type" + generators = "CMakeToolchain", "PkgConfigDeps", "VirtualBuildEnv", "VirtualRunEnv" + test_type = "explicit" + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires(self.tested_reference_str) def build_requirements(self): - self.build_requires("pkgconf/1.7.4") + if not self.conf.get("tools.gnu:pkg_config", check_type=str): + self.tool_requires("pkgconf/1.9.3") def build(self): cmake = CMake(self) @@ -15,6 +25,6 @@ def build(self): cmake.build() def test(self): - if not tools.cross_building(self.settings): - 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.bindirs[0], "test_package") + self.run(bin_path, env="conanrun") diff --git a/recipes/coin-cbc/all/test_v1_package/CMakeLists.txt b/recipes/coin-cbc/all/test_v1_package/CMakeLists.txt new file mode 100644 index 00000000000000..0d20897301b68b --- /dev/null +++ b/recipes/coin-cbc/all/test_v1_package/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.1) +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) diff --git a/recipes/coin-cbc/all/test_v1_package/conanfile.py b/recipes/coin-cbc/all/test_v1_package/conanfile.py new file mode 100644 index 00000000000000..e6b0fdb8110e8d --- /dev/null +++ b/recipes/coin-cbc/all/test_v1_package/conanfile.py @@ -0,0 +1,20 @@ +from conans import ConanFile, CMake, tools +import os + + +class TestPackageConan(ConanFile): + settings = "os", "arch", "compiler", "build_type" + generators = "cmake", "pkg_config" + + def build_requirements(self): + self.build_requires("pkgconf/1.9.3") + + 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)