Rework the find_package() support

* It is now possible to include the repository correctly
  through add_subdirectory
* find_package(continuable) was improved to work when
  only the path of the repository was added to the module path.
This commit is contained in:
Denis Blank 2018-12-10 17:37:25 +01:00
parent 65916e29a8
commit 49e3a659ad
4 changed files with 134 additions and 59 deletions

View File

@ -22,57 +22,86 @@
cmake_minimum_required(VERSION 3.2) cmake_minimum_required(VERSION 3.2)
project(continuable VERSION 3.0.0 LANGUAGES C CXX) project(continuable VERSION 3.0.0 LANGUAGES C CXX)
string(COMPARE EQUAL ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} if (NOT CTI_CONTINUABLE_IS_FIND_INCLUDED)
IS_TOP_LEVEL_PROJECT) string(COMPARE EQUAL ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}
CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
endif()
option(CTI_CONTINUABLE_WITH_TESTS if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
option(CTI_CONTINUABLE_WITH_INSTALL
"Add the continuable install targets"
${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_TESTS
"Build the continuable unit tests" "Build the continuable unit tests"
${IS_TOP_LEVEL_PROJECT}) ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_EXAMPLES option(CTI_CONTINUABLE_WITH_EXAMPLES
"Build the continuable examples" "Build the continuable examples"
${IS_TOP_LEVEL_PROJECT}) ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_BENCHMARKS option(CTI_CONTINUABLE_WITH_BENCHMARKS
"Build the continuable benchmarks" "Build the continuable benchmarks"
OFF) OFF)
option(CTI_CONTINUABLE_WITH_NO_EXCEPTIONS option(CTI_CONTINUABLE_WITH_NO_EXCEPTIONS
"Disable exception support" "Disable exception support"
OFF) OFF)
option(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS option(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS
"Enable unhandled asynchronous exceptions" "Enable unhandled asynchronous exceptions"
OFF) OFF)
option(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE option(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE
"Enable co_await support" "Enable co_await support"
OFF) OFF)
option(CTI_CONTINUABLE_WITH_CPP_LATEST option(CTI_CONTINUABLE_WITH_CPP_LATEST
"Enable the highest C++ standard available for testing polyfills" "Enable the highest C++ standard available for testing polyfills"
OFF) OFF)
option(CTI_CONTINUABLE_WITH_LIGHT_TESTS option(CTI_CONTINUABLE_WITH_LIGHT_TESTS
"Disable some template heavy unit tests (for CI usage)" "Disable some template heavy unit tests (for CI usage)"
OFF) OFF)
# Top level project settings only # Top level project settings only
if (IS_TOP_LEVEL_PROJECT)
set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS
"0" CACHE STRING "0" CACHE STRING
"Set the number of concurrent compilation jobs (0 = unlimited, for CI usage)") "Set the number of concurrent compilation jobs (0 = unlimited, for CI usage)")
else()
set(CTI_CONTINUABLE_WITH_INSTALL ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
set(CTI_CONTINUABLE_WITH_TESTS ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
set(CTI_CONTINUABLE_WITH_EXAMPLES ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
set(CTI_CONTINUABLE_WITH_BENCHMARKS OFF)
set(CTI_CONTINUABLE_WITH_NO_EXCEPTIONS OFF)
set(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS OFF)
set(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE OFF)
set(CTI_CONTINUABLE_WITH_CPP_LATEST OFF)
set(CTI_CONTINUABLE_WITH_LIGHT_TESTS OFF)
set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS "0")
endif() endif()
include(cmake/CMakeLists.txt) if(NOT TARGET Threads::Threads)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
endif()
set(THREADS_PREFER_PTHREAD_FLAG ON) if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
find_package(Threads REQUIRED) include(cmake/CMakeLists.txt)
add_subdirectory(dep)
add_subdirectory(dep) else()
if(NOT TARGET function2::function2)
find_package(function2 2 REQUIRED)
endif()
endif()
# continuable-base # continuable-base
add_library(continuable-base INTERFACE) if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
add_library(continuable-base INTERFACE)
else()
add_library(continuable-base INTERFACE IMPORTED GLOBAL)
endif()
add_library(continuable::continuable-base ALIAS continuable-base) add_library(continuable::continuable-base ALIAS continuable-base)
target_include_directories(continuable-base target_include_directories(continuable-base
@ -100,7 +129,6 @@ target_compile_features(continuable-base
cxx_trailing_return_types cxx_trailing_return_types
cxx_return_type_deduction) cxx_return_type_deduction)
if (CTI_CONTINUABLE_WITH_CPP_LATEST) if (CTI_CONTINUABLE_WITH_CPP_LATEST)
target_compile_features(continuable-base target_compile_features(continuable-base
INTERFACE INTERFACE
@ -115,46 +143,76 @@ if (CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
target_compile_definitions(continuable-base target_compile_definitions(continuable-base
INTERFACE INTERFACE
-DCONTINUABLE_WITH_EXPERIMENTAL_COROUTINE) CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
endif() endif()
if (CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS) if (CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
target_compile_definitions(continuable-base target_compile_definitions(continuable-base
INTERFACE INTERFACE
-DCONTINUABLE_WITH_UNHANDLED_EXCEPTIONS) CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
endif()
if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
add_library(continuable INTERFACE)
else()
add_library(continuable INTERFACE IMPORTED GLOBAL)
endif() endif()
add_library(continuable INTERFACE)
add_library(continuable::continuable ALIAS continuable) add_library(continuable::continuable ALIAS continuable)
target_link_libraries(continuable target_link_libraries(continuable
INTERFACE INTERFACE
continuable-base continuable::continuable-base
function2) function2::function2)
# Create an install target if (CTI_CONTINUABLE_WITH_INSTALL)
install(TARGETS continuable-base continuable include(ExternalProject)
EXPORT continuable-config include(GNUInstallDirs)
INCLUDES DESTINATION include) include(CMakePackageConfigHelpers)
install(EXPORT continuable-config # Create an install target:
FILE continuable-config.cmake # Headers and license files
NAMESPACE continuable:: install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/continuable"
DESTINATION share/continuable/cmake) DESTINATION "include")
install(FILES "LICENSE.txt" DESTINATION .)
install(FILES "Readme.md" DESTINATION .)
install(DIRECTORY include/continuable # Config.cmake
DESTINATION include FILES_MATCHING PATTERN "*.hpp") write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
install(FILES LICENSE.txt DESTINATION . RENAME continuable-LICENSE.txt) # ConfigVersion.cmake
install(FILES Readme.md DESTINATION . RENAME continuable-Readme.md) configure_package_config_file("cmake/config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
# PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
# Setup CPack for bundling # Targets.cmake
set(CPACK_GENERATOR "ZIP") export(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) NAMESPACE ${PROJECT_NAME}::
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
EXPORT "${PROJECT_NAME}Targets"
INCLUDES DESTINATION "include")
install(EXPORT "${PROJECT_NAME}Targets"
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
include(CPack) # Setup CPack for bundling
set(CPACK_GENERATOR "ZIP")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
include(CPack)
endif()
# Testing and examples # Testing and examples
if (CTI_CONTINUABLE_WITH_TESTS OR CTI_CONTINUABLE_WITH_EXAMPLES) if (CTI_CONTINUABLE_WITH_TESTS OR CTI_CONTINUABLE_WITH_EXAMPLES)

10
Findcontinuable.cmake Normal file
View File

@ -0,0 +1,10 @@
# Makes it possible to find continuable through find_package(continuable REQUIRED)
# when this source directory was added to the CMake module path.
# For instance it could be done through adding this to the CMakeLists.txt
# file in the parent directory:
# ```cmake
# list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/continuable")
# ```
set(CTI_CONTINUABLE_IS_FIND_INCLUDED ON)
include("${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt")

7
cmake/config.cmake.in Normal file
View File

@ -0,0 +1,7 @@
set(@PROJECT_NAME@_VERSION @PROJECT_VERSION@)
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components(@PROJECT_NAME@)

View File

@ -1,4 +1,4 @@
if(NOT TARGET function2) if(NOT TARGET function2::function2)
add_subdirectory(function2) add_subdirectory(function2)
endif() endif()