Skip to content

Commit

Permalink
(#14317) geotrans: conan v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceIm authored Dec 1, 2022
1 parent acfc250 commit b3107ef
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 76 deletions.
14 changes: 4 additions & 10 deletions recipes/geotrans/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
cmake_minimum_required(VERSION 3.8)
project(geotrans LANGUAGES CXX)
####
# Conan
####
include(${CMAKE_SOURCE_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

####
# Directories
####

set(SRC_SUBFOLDER "${CMAKE_SOURCE_DIR}/source_subfolder")
set(DTCCDIR "${SRC_SUBFOLDER}/CCS/src/dtcc/CoordinateSystems")
set(CCSERVICEDIR "${SRC_SUBFOLDER}/CCS/src")
set(SRCDIR "${SRC_SUBFOLDER}/GEOTRANS3/java_gui/geotrans3/jni")
set(DTCCDIR "${GEOTRANS_SRC_DIR}/CCS/src/dtcc/CoordinateSystems")
set(CCSERVICEDIR "${GEOTRANS_SRC_DIR}/CCS/src")
set(SRCDIR "${GEOTRANS_SRC_DIR}/GEOTRANS3/java_gui/geotrans3/jni")

####
# Sources
Expand Down Expand Up @@ -79,4 +73,4 @@ install(TARGETS MSPdtcc MSPCoordinateConversionService
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY "${SRC_SUBFOLDER}/data/" DESTINATION res)
install(DIRECTORY "${GEOTRANS_SRC_DIR}/data/" DESTINATION res)
3 changes: 1 addition & 2 deletions recipes/geotrans/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
sources:
"3.8":
url: "https://c3i.jfrog.io/artifactory/cci-sources-backup/sources/geotrans/geotrans-3.8.tgz"
sha256: "BAA72D3B1AE12F237A8AD30F2DEB3FED2B80FEB759528EA0A72B4B42CB77C565"
sha256: "baa72d3b1ae12f237a8ad30f2deb3fed2b80feb759528ea0a72b4b42cb77c565"
patches:
"3.8":
- patch_file: "patches/3.8-fix-for-cxx20.patch"
base_path: "source_subfolder"
78 changes: 35 additions & 43 deletions recipes/geotrans/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get
import os

required_conan_version = ">=1.35.0"
required_conan_version = ">=1.53.0"


class GeotransConan(ConanFile):
name = "geotrans"
Expand Down Expand Up @@ -32,65 +36,53 @@ class GeotransConan(ConanFile):
"fPIC": True,
}

generators = "cmake"
_cmake = None

@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _build_subfolder(self):
return "build_subfolder"

def export_sources(self):
self.copy("CMakeLists.txt")
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])
copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder)
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
self.options.rm_safe("fPIC")

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

def validate(self):
if self.info.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, 11)

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

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["GEOTRANS_SRC_DIR"] = self.source_folder.replace("\\", "/")
tc.generate()

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
apply_conandata_patches(self)
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir))
cmake.build()

def package(self):
self.copy(
"*.txt",
dst="licenses",
src=os.path.join(self._source_subfolder, "GEOTRANS3", "docs"),
)
cmake = self._configure_cmake()
copy(self, "*.txt",
src=os.path.join(self.source_folder, "GEOTRANS3", "docs"),
dst=os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()

def package_info(self):
self.cpp_info.components["dtcc"].libs = ["MSPdtcc"]
self.cpp_info.components["dtcc"].includedirs = [
path[0] for path in os.walk("include")
]
self.cpp_info.components["dtcc"].res = ["res"]
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.components["dtcc"].system_libs.append("pthread")
self.cpp_info.components["dtcc"].system_libs.append("m")
Expand All @@ -100,9 +92,9 @@ def package_info(self):
self.cpp_info.components["ccs"].includedirs = [
path[0] for path in os.walk("include")
]
self.cpp_info.components["ccs"].res = ["res"]

mpccs_data_path = os.path.join(self.package_folder, "res")
self.output.info("Prepending to MPCCS_DATA runtime environment variable: {}".format(mpccs_data_path))
self.runenv_info.prepend_path("MPCCS_DATA", mpccs_data_path)
mspccs_data_path = os.path.join(self.package_folder, "res")
self.runenv_info.define_path("MSPCCS_DATA", mspccs_data_path)
# TODO: to remove after conan v2, it allows to not break consumers still relying on virtualenv generator
self.env_info.MPCCS_DATA.append(mpccs_data_path)
self.env_info.MSPCCS_DATA = mspccs_data_path
8 changes: 3 additions & 5 deletions recipes/geotrans/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
project(test_package LANGUAGES CXX)

find_package(geotrans REQUIRED dtcc ccs CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} geotrans::dtcc geotrans::ccs)
target_link_libraries(${PROJECT_NAME} PRIVATE geotrans::dtcc geotrans::ccs)
31 changes: 15 additions & 16 deletions recipes/geotrans/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,27 +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 GeotransTestConan(ConanFile):
class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"

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):
# NOTE: In order to use this library, the MPCCS_DATA environment variable *must* be set.
# The path to the appropriate data directory is available in the env_info variable. This can be
# accessed from a consumer package using `self.deps_env_info["geotrans"].MPCCS_DATA.
# Alternatively, this data directory can be moved to a location of your choice from its location
# in `res`, using the `imports()` method.
# This new location can then be used as the value for the MPCCS_DATA environment variable.
# TODO: should be automatically injected in self.run in conan v2
with tools.environment_append(
{"MSPCCS_DATA": self.deps_env_info["geotrans"].MPCCS_DATA}
):
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/geotrans/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)
29 changes: 29 additions & 0 deletions recipes/geotrans/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from conans import ConanFile, CMake, tools
from contextlib import contextmanager
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()

@contextmanager
def _workaround_2profiles(self):
if hasattr(self, "settings_build"):
with tools.environment_append(
{"MSPCCS_DATA": os.path.join(self.deps_cpp_info["geotrans"].rootpath, "res")}
):
yield
else:
yield

def test(self):
if not tools.cross_building(self):
with self._workaround_2profiles():
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit b3107ef

Please sign in to comment.