Skip to content

Commit

Permalink
conan v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceIm committed Mar 4, 2023
1 parent cbf992a commit 47efa24
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 105 deletions.
5 changes: 3 additions & 2 deletions recipes/readosm/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
160 changes: 81 additions & 79 deletions recipes/readosm/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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],
Expand All @@ -26,108 +33,103 @@ 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":
del self.options.fPIC

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}"]
Original file line number Diff line number Diff line change
@@ -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 @@
Expand All @@ -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

7 changes: 2 additions & 5 deletions recipes/readosm/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 13 additions & 13 deletions recipes/readosm/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,26 +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, 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)
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.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
8 changes: 8 additions & 0 deletions recipes/readosm/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.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)
17 changes: 17 additions & 0 deletions recipes/readosm/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)

0 comments on commit 47efa24

Please sign in to comment.