Skip to content

Commit

Permalink
provide more official CMake imported targets
Browse files Browse the repository at this point in the history
add non namespaced imported lib target
add namespaced and non-namespaced imported executable targets
  • Loading branch information
SpaceIm committed Jan 21, 2021
1 parent ac6a683 commit ec87edc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
67 changes: 67 additions & 0 deletions recipes/geographiclib/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import glob
import os

required_conan_version = ">=1.29.1"
Expand Down Expand Up @@ -99,6 +100,70 @@ def package(self):
os.path.join("lib", "cmake"), "sbin", "python", "matlab", "doc", "cmake"]:
tools.rmdir(os.path.join(os.path.join(self.package_folder, folder)))
tools.remove_files_by_mask(os.path.join(self.package_folder, "bin"), "*.pdb")
self._create_cmake_module_targets(
os.path.join(self.package_folder, self._module_subfolder, self._module_file),
libs={"GeographicLib": "GeographicLib::GeographicLib"}, # "GeographicLib and GeographicLib::GeographicLib are both official targets
executables=self._executables
)

@staticmethod
def _create_cmake_module_targets(module_file, libs={}, executables={}):
content = ""
for alias, aliased in libs.items():
content += (
"if(TARGET {aliased} AND NOT TARGET {alias})\n"
" add_library({alias} INTERFACE IMPORTED)\n"
" target_link_libraries({alias} INTERFACE {aliased})\n"
"endif()\n"
).format(alias=alias, aliased=aliased)
for target, executable in executables.items():
# Geographic lib provides both namespaced and non-namespaced imported targets
content += (
"if(NOT TARGET {target} OR NOT TARGET GeographicLib::{target})\n"
" get_filename_component(GeographicLib_{target}_IMPORTED_LOCATION \"${{CMAKE_CURRENT_LIST_DIR}}/../../bin/{exec}\" ABSOLUTE)\n"
" message(STATUS \"GeographicLib component {target} found: ${{GeographicLib_{target}_IMPORTED_LOCATION}}\")\n"
" set(GeographicLib_{target}_FOUND ON)\n"
" if(NOT TARGET {target})\n"
" add_executable({target} IMPORTED)\n"
" set_property(TARGET {target} PROPERTY IMPORTED_LOCATION ${{GeographicLib_{target}_IMPORTED_LOCATION}})\n"
" endif()\n"
" if(NOT TARGET GeographicLib::{target})\n"
" add_executable(GeographicLib::{target} IMPORTED)\n"
" set_property(TARGET GeographicLib::{target} PROPERTY IMPORTED_LOCATION ${{GeographicLib_{target}_IMPORTED_LOCATION}})\n"
" endif()\n"
"endif()\n"
).format(target=target, exec=executable)
tools.save(module_file, content)

@property
def _module_subfolder(self):
return os.path.join("lib", "cmake")

@property
def _module_file(self):
return "conan-official-{}-targets.cmake".format(self.name)

@property
def _executables(self):
def exec_name(name):
return os.path.basename(glob.glob(os.path.join(self.package_folder, "bin", name + "*"))[0])

executables = {}
if self.options.tools:
executables.update({
"CartConvert": exec_name("CartConvert"),
"ConicProj": exec_name("ConicProj"),
"GeodesicProj": exec_name("GeodesicProj"),
"GeoConvert": exec_name("GeoConvert"),
"GeodSolve": exec_name("GeodSolve"),
"GeoidEval": exec_name("GeoidEval"),
"Gravity": exec_name("Gravity"),
"MagneticField": exec_name("MagneticField"),
"Planimeter": exec_name("Planimeter"),
"RhumbSolve": exec_name("RhumbSolve"),
"TransverseMercatorProj": exec_name("TransverseMercatorProj"),
})
return executables

def package_info(self):
self.cpp_info.filenames["cmake_find_package"] = "geographiclib"
Expand All @@ -107,6 +172,8 @@ def package_info(self):
self.cpp_info.names["cmake_find_package_multi"] = "GeographicLib"
self.cpp_info.libs = tools.collect_libs(self)
self.cpp_info.defines.append("GEOGRAPHICLIB_SHARED_LIB={}".format("1" if self.options.shared else "0"))
self.cpp_info.builddirs = [self._module_subfolder]
self.cpp_info.build_modules = [os.path.join(self._module_subfolder, self._module_file)]

if self.options.tools:
bin_path = os.path.join(self.package_folder, "bin")
Expand Down
17 changes: 15 additions & 2 deletions recipes/geographiclib/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ cmake_minimum_required(VERSION 3.1)
project(test_package CXX)

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

find_package(geographiclib CONFIG REQUIRED)
find_package(geographiclib COMPONENTS GeographicLib CartConvert)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} GeographicLib::GeographicLib)

# Test CMake imported executable target
if(GeographicLib_CartConvert_FOUND)
set(CARTCONVERT_RESULT ${CMAKE_CURRENT_BINARY_DIR}/cartconvert_result.txt)
add_custom_target(CartConvert-test DEPENDS ${CARTCONVERT_RESULT})
add_custom_command(
COMMENT "Test GeographicLib::CartConvert imported executable target"
OUTPUT ${CARTCONVERT_RESULT}
COMMAND GeographicLib::CartConvert
ARGS --input-string "33.3 44.4 6000" -l 33 44 20 --output-file ${CARTCONVERT_RESULT}
)
add_dependencies(${PROJECT_NAME} CartConvert-test)
endif()

0 comments on commit ec87edc

Please sign in to comment.