From d7dad549bd5a3442b92e861bcd2c5cda2adeea27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez-Cabanillas?= Date: Tue, 9 Nov 2021 09:52:15 +0000 Subject: [PATCH] cmake exported targets The minimum required cmake version is raised to 3.1, because the exported targets started at that version. The build system creates the exported targets: - The executable uchardet::uchardet - The library uchardet::libuchardet - The static library uchardet::libuchardet_static A downstream project using CMake can find and link the library target directly with cmake (without needing pkg-config) this way: ~~~ project(sample LANGUAGES C) find_package ( uchardet ) if (uchardet_FOUND) add_executable( sample sample.c ) target_link_libraries ( sample PRIVATE uchardet::libuchardet ) endif () ~~~ After installing uchardet in a prefix like "$HOME/uchardet/": cmake -DCMAKE_PREFIX_PATH="$HOME/uchardet/;..." Instead installing, the build directory can be used directly, for instance: cmake -Duchardet_DIR="$HOME/uchardet-0.1.0/build/" ... --- CMakeLists.txt | 38 +++++++++++++++++++++++++++++++++++++- src/CMakeLists.txt | 18 ++++++++++++++++++ src/tools/CMakeLists.txt | 2 ++ uchardet-config.cmake.in | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 uchardet-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 50a11e8..8dece97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ ######## Project settings -cmake_minimum_required(VERSION 2.8.5) +cmake_minimum_required(VERSION 3.1) include(CheckCCompilerFlag) set (PACKAGE_NAME uchardet) project (${PACKAGE_NAME} CXX C) @@ -72,3 +72,39 @@ install( add_subdirectory(src) add_subdirectory(doc) add_subdirectory(test) + +######## Exported targets + +install( + EXPORT UchardetTargets + FILE ${PACKAGE_NAME}-targets.cmake + NAMESPACE ${PACKAGE_NAME}:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PACKAGE_NAME} +) + +export( + EXPORT UchardetTargets + FILE "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-targets.cmake" + NAMESPACE ${PACKAGE_NAME}:: +) + +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + ${PACKAGE_NAME}-config-version.cmake + VERSION ${UCHARDET_VERSION} + COMPATIBILITY AnyNewerVersion +) + +configure_file( + ${PACKAGE_NAME}-config.cmake.in + ${PACKAGE_NAME}-config.cmake + @ONLY +) + +install ( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" + DESTINATION + ${CMAKE_INSTALL_LIBDIR}/cmake/${PACKAGE_NAME} +) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 61e315f..4f6afa6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -84,6 +84,12 @@ if(BUILD_SHARED_LIBS) target_compile_definitions("${UCHARDET_LIBRARY}" PUBLIC UCHARDET_SHARED) endif() +target_include_directories(${UCHARDET_LIBRARY} + PUBLIC + "$" + "$" +) + if (UCHARDET_STATIC_LIBRARY) add_library( ${UCHARDET_STATIC_LIBRARY} @@ -91,6 +97,12 @@ if (UCHARDET_STATIC_LIBRARY) ${UCHARDET_SOURCES} ) target_compile_definitions("${UCHARDET_STATIC_LIBRARY}" PRIVATE BUILDING_UCHARDET) + + target_include_directories(${UCHARDET_STATIC_LIBRARY} + PUBLIC + "$" + "$" + ) endif (UCHARDET_STATIC_LIBRARY) set_target_properties( @@ -121,6 +133,8 @@ if (NOT WIN32) install( TARGETS ${UCHARDET_LIBRARY} + EXPORT + UchardetTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION @@ -130,6 +144,8 @@ else (NOT WIN32) install( TARGETS ${UCHARDET_LIBRARY} + EXPORT + UchardetTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ARCHIVE DESTINATION @@ -141,6 +157,8 @@ if (UCHARDET_STATIC_LIBRARY) install( TARGETS ${UCHARDET_STATIC_LIBRARY} + EXPORT + UchardetTargets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index bee2e51..7afad1d 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -35,6 +35,8 @@ target_link_libraries( install( TARGETS ${UCHARDET_BINARY} + EXPORT + UchardetTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) diff --git a/uchardet-config.cmake.in b/uchardet-config.cmake.in new file mode 100644 index 0000000..b6759b4 --- /dev/null +++ b/uchardet-config.cmake.in @@ -0,0 +1,19 @@ +# This file may optionally do: +# +# 1. Check for dependencies of exported targets. Example: +# +# include(CMakeFindDependencyMacro) +# find_dependency(MYDEP REQUIRED) +# +# find_dependency() has the same syntax as find_package() +# +# 2. Capture values from configuration. Example: +# +# set(my-config-var @my-config-var@) +# +# 3. Other required setup when importing targets from another project +# +# See also: +# https://cliutils.gitlab.io/modern-cmake/chapters/install.html +# +include("${CMAKE_CURRENT_LIST_DIR}/uchardet-targets.cmake")