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/" ...
This commit is contained in:
Pedro López-Cabanillas 2021-11-09 09:52:15 +00:00 committed by Jehan Pagès
parent 6f38ab95f5
commit d7dad549bd
4 changed files with 76 additions and 1 deletions

View File

@ -1,5 +1,5 @@
######## Project settings ######## Project settings
cmake_minimum_required(VERSION 2.8.5) cmake_minimum_required(VERSION 3.1)
include(CheckCCompilerFlag) include(CheckCCompilerFlag)
set (PACKAGE_NAME uchardet) set (PACKAGE_NAME uchardet)
project (${PACKAGE_NAME} CXX C) project (${PACKAGE_NAME} CXX C)
@ -72,3 +72,39 @@ install(
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(doc) add_subdirectory(doc)
add_subdirectory(test) 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}
)

View File

@ -84,6 +84,12 @@ if(BUILD_SHARED_LIBS)
target_compile_definitions("${UCHARDET_LIBRARY}" PUBLIC UCHARDET_SHARED) target_compile_definitions("${UCHARDET_LIBRARY}" PUBLIC UCHARDET_SHARED)
endif() endif()
target_include_directories(${UCHARDET_LIBRARY}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PACKAGE_NAME}>"
)
if (UCHARDET_STATIC_LIBRARY) if (UCHARDET_STATIC_LIBRARY)
add_library( add_library(
${UCHARDET_STATIC_LIBRARY} ${UCHARDET_STATIC_LIBRARY}
@ -91,6 +97,12 @@ if (UCHARDET_STATIC_LIBRARY)
${UCHARDET_SOURCES} ${UCHARDET_SOURCES}
) )
target_compile_definitions("${UCHARDET_STATIC_LIBRARY}" PRIVATE BUILDING_UCHARDET) target_compile_definitions("${UCHARDET_STATIC_LIBRARY}" PRIVATE BUILDING_UCHARDET)
target_include_directories(${UCHARDET_STATIC_LIBRARY}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PACKAGE_NAME}>"
)
endif (UCHARDET_STATIC_LIBRARY) endif (UCHARDET_STATIC_LIBRARY)
set_target_properties( set_target_properties(
@ -121,6 +133,8 @@ if (NOT WIN32)
install( install(
TARGETS TARGETS
${UCHARDET_LIBRARY} ${UCHARDET_LIBRARY}
EXPORT
UchardetTargets
LIBRARY DESTINATION LIBRARY DESTINATION
${CMAKE_INSTALL_LIBDIR} ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ARCHIVE DESTINATION
@ -130,6 +144,8 @@ else (NOT WIN32)
install( install(
TARGETS TARGETS
${UCHARDET_LIBRARY} ${UCHARDET_LIBRARY}
EXPORT
UchardetTargets
RUNTIME DESTINATION RUNTIME DESTINATION
${CMAKE_INSTALL_BINDIR} ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ARCHIVE DESTINATION
@ -141,6 +157,8 @@ if (UCHARDET_STATIC_LIBRARY)
install( install(
TARGETS TARGETS
${UCHARDET_STATIC_LIBRARY} ${UCHARDET_STATIC_LIBRARY}
EXPORT
UchardetTargets
ARCHIVE DESTINATION ARCHIVE DESTINATION
${CMAKE_INSTALL_LIBDIR} ${CMAKE_INSTALL_LIBDIR}
) )

View File

@ -35,6 +35,8 @@ target_link_libraries(
install( install(
TARGETS TARGETS
${UCHARDET_BINARY} ${UCHARDET_BINARY}
EXPORT
UchardetTargets
RUNTIME DESTINATION RUNTIME DESTINATION
${CMAKE_INSTALL_BINDIR} ${CMAKE_INSTALL_BINDIR}
) )

19
uchardet-config.cmake.in Normal file
View File

@ -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")