From 47efa240d39a5e74b7b02cbe15702ea85215145d Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Mon, 20 Feb 2023 22:31:36 +0100 Subject: [PATCH] conan v2 support --- recipes/readosm/all/conandata.yml | 5 +- recipes/readosm/all/conanfile.py | 160 +++++++++--------- ...honor-flags.patch => 0001-fix-nmake.patch} | 7 +- .../readosm/all/test_package/CMakeLists.txt | 7 +- recipes/readosm/all/test_package/conanfile.py | 26 +-- .../all/test_v1_package/CMakeLists.txt | 8 + .../readosm/all/test_v1_package/conanfile.py | 17 ++ 7 files changed, 125 insertions(+), 105 deletions(-) rename recipes/readosm/all/patches/{0001-nmake-honor-flags.patch => 0001-fix-nmake.patch} (71%) create mode 100644 recipes/readosm/all/test_v1_package/CMakeLists.txt create mode 100644 recipes/readosm/all/test_v1_package/conanfile.py diff --git a/recipes/readosm/all/conandata.yml b/recipes/readosm/all/conandata.yml index db13b0c83d55b..c2fb67bfed2fc 100644 --- a/recipes/readosm/all/conandata.yml +++ b/recipes/readosm/all/conandata.yml @@ -4,5 +4,6 @@ sources: sha256: "db7c051d256cec7ecd4c3775ab9bc820da5a4bf72ffd4e9f40b911d79770f145" patches: "1.1.0a": - - patch_file: "patches/0001-nmake-honor-flags.patch" - base_path: "source_subfolder" + - patch_file: "patches/0001-fix-nmake.patch" + patch_description: "Remove hardcoded paths in NMake" + patch_type: "conan" diff --git a/recipes/readosm/all/conanfile.py b/recipes/readosm/all/conanfile.py index b804820f05054..b9c04661624b4 100644 --- a/recipes/readosm/all/conanfile.py +++ b/recipes/readosm/all/conanfile.py @@ -1,8 +1,14 @@ -from conans import ConanFile, AutoToolsBuildEnvironment, VisualStudioBuildEnvironment, tools -import functools +from conan import ConanFile +from conan.tools.apple import fix_apple_shared_install_name +from conan.tools.build import cross_building +from conan.tools.env import VirtualBuildEnv, VirtualRunEnv +from conan.tools.files import apply_conandata_patches, chdir, copy, export_conandata_patches, get, replace_in_file, rm, rmdir +from conan.tools.gnu import Autotools, AutotoolsDeps, AutotoolsToolchain +from conan.tools.layout import basic_layout +from conan.tools.microsoft import is_msvc, NMakeDeps, NMakeToolchain import os -required_conan_version = ">=1.36.0" +required_conan_version = ">=1.58.0" class ReadosmConan(ConanFile): @@ -12,10 +18,11 @@ class ReadosmConan(ConanFile): "an Open Street Map input file." ) license = ("MPL-1.1", "GPL-2.0-or-later", "LGPL-2.1-or-later") - topics = ("readosm", "osm", "open-street-map", "xml", "protobuf") + topics = ("osm", "open-street-map", "xml", "protobuf") homepage = "https://www.gaia-gis.it/fossil/readosm" url = "https://github.com/conan-io/conan-center-index" + package_type = "library" settings = "os", "arch", "compiler", "build_type" options = { "shared": [True, False], @@ -26,21 +33,12 @@ class ReadosmConan(ConanFile): "fPIC": True, } - @property - def _source_subfolder(self): - return "source_subfolder" - - @property - def _is_msvc(self): - return str(self.settings.compiler) in ["Visual Studio", "msvc"] - @property 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": @@ -48,86 +46,90 @@ def config_options(self): def configure(self): if self.options.shared: - del self.options.fPIC - del self.settings.compiler.libcxx - del self.settings.compiler.cppstd + self.options.rm_safe("fPIC") + self.settings.rm_safe("compiler.cppstd") + self.settings.rm_safe("compiler.libcxx") + + def layout(self): + basic_layout(self, src_folder="src") def requirements(self): - self.requires("expat/2.4.8") - self.requires("zlib/1.2.12") + self.requires("expat/2.5.0") + self.requires("zlib/1.2.13") def build_requirements(self): - if not self._is_msvc: - self.build_requires("libtool/2.4.6") - if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"): - self.build_requires("msys2/cci.latest") + if not is_msvc(self): + self.tool_requires("libtool/2.4.7") + 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") def source(self): - tools.get(**self.conan_data["sources"][self.version], - destination=self._source_subfolder, strip_root=True) - - def _build_msvc(self): - target = "readosm_i.lib" if self.options.shared else "readosm.lib" - optflags = ["-DDLL_EXPORT"] if self.options.shared else [] - system_libs = [lib + ".lib" for lib in self.deps_cpp_info.system_libs] - with tools.chdir(self._source_subfolder): - with tools.vcvars(self): - with tools.environment_append(VisualStudioBuildEnvironment(self).vars): - self.run("nmake -f makefile.vc {} OPTFLAGS=\"{}\" SYSTEM_LIBS=\"{}\"".format(target, - " ".join(optflags), - " ".join(system_libs))) - - def _build_autotools(self): + get(self, **self.conan_data["sources"][self.version], strip_root=True) + + def generate(self): + if is_msvc(self): + tc = NMakeToolchain(self) + if self.options.shared: + tc.extra_defines.append("DLL_EXPORT") + tc.generate() + deps = NMakeDeps(self) + deps.generate() + else: + env = VirtualBuildEnv(self) + env.generate() + if not cross_building(self): + env = VirtualRunEnv(self) + env.generate(scope="build") + tc = AutotoolsToolchain(self) + tc.configure_args.append("--disable-gcov") + tc.generate() + deps = AutotoolsDeps(self) + deps.generate() + + def _patch_sources(self): + apply_conandata_patches(self) # fix MinGW - tools.replace_in_file(os.path.join(self._source_subfolder, "configure.ac"), - "AC_CHECK_LIB(z,", - "AC_CHECK_LIB({},".format(self.deps_cpp_info["zlib"].libs[0])) + zlib_lib = self.dependencies["zlib"].cpp_info.aggregated_components().libs[0] + replace_in_file( + self, os.path.join(self.source_folder, "configure.ac"), + "AC_CHECK_LIB(z,", f"AC_CHECK_LIB({zlib_lib},", + ) # Disable tests & examples - tools.replace_in_file(os.path.join(self._source_subfolder, "Makefile.am"), - "SUBDIRS = headers src tests examples", - "SUBDIRS = headers src") - - with tools.chdir(self._source_subfolder): - self.run("{} -fiv".format(tools.get_env("AUTORECONF")), win_bash=tools.os_info.is_windows) - # Relocatable shared lib for Apple platforms - tools.replace_in_file("configure", "-install_name \\$rpath/", "-install_name @rpath/") - autotools = self._configure_autotools() - autotools.make() - - @functools.lru_cache(1) - def _configure_autotools(self): - yes_no = lambda v: "yes" if v else "no" - args = [ - "--enable-static={}".format(yes_no(not self.options.shared)), - "--enable-shared={}".format(yes_no(self.options.shared)), - "--disable-gcov", - ] - autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows) - autotools.configure(args=args) - return autotools + replace_in_file( + self, os.path.join(self.source_folder, "Makefile.am"), + "SUBDIRS = headers src tests examples", "SUBDIRS = headers src", + ) def build(self): - for patch in self.conan_data.get("patches", {}).get(self.version, []): - tools.patch(**patch) - if self._is_msvc: - self._build_msvc() + self._patch_sources() + if is_msvc(self): + with chdir(self, self.source_folder): + target = "readosm_i.lib" if self.options.shared else "readosm.lib" + self.run(f"nmake -f makefile.vc {target}") else: - self._build_autotools() + with chdir(self, self.source_folder): + autotools = Autotools(self) + autotools.autoreconf() + autotools.configure() + autotools.make() def package(self): - self.copy("COPYING", dst="licenses", src=self._source_subfolder) - if self._is_msvc: - self.copy("readosm.h", dst="include", src=os.path.join(self._source_subfolder, "headers")) - self.copy("*.lib", dst="lib", src=self._source_subfolder) - self.copy("*.dll", dst="bin", src=self._source_subfolder) + copy(self, "COPYING", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) + if is_msvc(self): + copy(self, "readosm.h", src=os.path.join(self.source_folder, "headers"), dst=os.path.join(self.package_folder, "include")) + copy(self, "*.lib", src=self.source_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False) + copy(self, "*.dll", src=self.source_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False) else: - with tools.chdir(self._source_subfolder): - autotools = self._configure_autotools() + with chdir(self, self.source_folder): + autotools = Autotools(self) autotools.install() - tools.remove_files_by_mask(os.path.join(self.package_folder, "lib"), "*.la") - tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig")) + rm(self, "*.la", os.path.join(self.package_folder, "lib")) + rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig")) + fix_apple_shared_install_name(self) def package_info(self): self.cpp_info.set_property("pkg_config_name", "readosm") - suffix = "_i" if self._is_msvc and self.options.shared else "" - self.cpp_info.libs = ["readosm{}".format(suffix)] + suffix = "_i" if is_msvc(self) and self.options.shared else "" + self.cpp_info.libs = [f"readosm{suffix}"] diff --git a/recipes/readosm/all/patches/0001-nmake-honor-flags.patch b/recipes/readosm/all/patches/0001-fix-nmake.patch similarity index 71% rename from recipes/readosm/all/patches/0001-nmake-honor-flags.patch rename to recipes/readosm/all/patches/0001-fix-nmake.patch index f8f590069d552..43d9d7d1957a1 100644 --- a/recipes/readosm/all/patches/0001-nmake-honor-flags.patch +++ b/recipes/readosm/all/patches/0001-fix-nmake.patch @@ -1,8 +1,3 @@ -This patch for msvc build allows to: -* define OPTFLAG ourself from conanfile (allow to honor profile) -* inject a custom SYSTEM_LIBS variable from conanfile (allow robust shared build) -* not hardcode this very specific C:\OSGeo4W environment - --- a/makefile.vc +++ b/makefile.vc @@ -2,13 +2,12 @@ @@ -26,7 +21,7 @@ This patch for msvc build allows to: link /dll /out:$(READOSM_DLL) \ - /implib:readosm_i.lib $(LIBOBJ) \ - C:\OSGeo4w\lib\libexpat.lib C:\OSGeo4w\lib\zlib.lib -+ /implib:readosm_i.lib $(LIBOBJ) $(SYSTEM_LIBS) ++ /implib:readosm_i.lib $(LIBOBJ) if exist $(READOSM_DLL).manifest mt -manifest \ $(READOSM_DLL).manifest -outputresource:$(READOSM_DLL);2 diff --git a/recipes/readosm/all/test_package/CMakeLists.txt b/recipes/readosm/all/test_package/CMakeLists.txt index ac8d4c253bde9..8ea28640523dd 100644 --- a/recipes/readosm/all/test_package/CMakeLists.txt +++ b/recipes/readosm/all/test_package/CMakeLists.txt @@ -1,10 +1,7 @@ cmake_minimum_required(VERSION 3.1) -project(test_package C) - -include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup(TARGETS) +project(test_package LANGUAGES C) find_package(readosm REQUIRED CONFIG) add_executable(${PROJECT_NAME} test_package.c) -target_link_libraries(${PROJECT_NAME} readosm::readosm) +target_link_libraries(${PROJECT_NAME} PRIVATE readosm::readosm) diff --git a/recipes/readosm/all/test_package/conanfile.py b/recipes/readosm/all/test_package/conanfile.py index 697dfef261b53..0a6bc68712d90 100644 --- a/recipes/readosm/all/test_package/conanfile.py +++ b/recipes/readosm/all/test_package/conanfile.py @@ -1,19 +1,19 @@ -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", "arch", "compiler", "build_type" - generators = "cmake", "cmake_find_package_multi" + generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv" + test_type = "explicit" - def build_requirements(self): - if self.settings.os == "Macos" and self.settings.arch == "armv8": - # Workaround for CMake bug with error message: - # Attempting to use @rpath without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being - # set. This could be because you are using a Mac OS X version less than 10.5 - # or because CMake's platform configuration is corrupt. - # FIXME: Remove once CMake on macOS/M1 CI runners is upgraded. - self.build_requires("cmake/3.22.0") + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires(self.tested_reference_str) def build(self): cmake = CMake(self) @@ -21,6 +21,6 @@ def build(self): 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.bindirs[0], "test_package") + self.run(bin_path, env="conanrun") diff --git a/recipes/readosm/all/test_v1_package/CMakeLists.txt b/recipes/readosm/all/test_v1_package/CMakeLists.txt new file mode 100644 index 0000000000000..0d20897301b68 --- /dev/null +++ b/recipes/readosm/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/readosm/all/test_v1_package/conanfile.py b/recipes/readosm/all/test_v1_package/conanfile.py new file mode 100644 index 0000000000000..38f4483872d47 --- /dev/null +++ b/recipes/readosm/all/test_v1_package/conanfile.py @@ -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)