Merge pull request #669 from dan-42/change_installed_cmake_include_path

REFACTOR: rewrite CMakeLists.txt for better inlcude and reuse
This commit is contained in:
Niels Lohmann 2017-07-30 20:01:03 +02:00 committed by GitHub
commit 67fb517ccd
2 changed files with 68 additions and 50 deletions

View file

@ -1,56 +1,78 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0.0)
# define the project ##
project(nlohmann_json VERSION 2.1.1 LANGUAGES CXX) ## PROJECT
## name and version
##
project(nlohmann_json VERSION 2.1.1)
##
## OPTIONS
##
option(JSON_BuildTests "Build the unit tests" ON) option(JSON_BuildTests "Build the unit tests" ON)
# define project variables ##
set(JSON_TARGET_NAME ${PROJECT_NAME}) ## CONFIGURATION
set(JSON_PACKAGE_NAME ${JSON_TARGET_NAME}) ##
set(JSON_TARGETS_FILENAME "${JSON_PACKAGE_NAME}Targets.cmake") set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME})
set(JSON_CONFIG_FILENAME "${JSON_PACKAGE_NAME}Config.cmake") set(NLOHMANN_JSON_SOURCE_DIR "src/")
set(JSON_CONFIGVERSION_FILENAME "${JSON_PACKAGE_NAME}ConfigVersion.cmake") set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "lib/cmake/${PROJECT_NAME}")
set(JSON_CONFIG_DESTINATION "cmake") set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "include")
set(JSON_INCLUDE_DESTINATION "include/nlohmann") set(NLOHMANN_JSON_HEADER_INSTALL_DIR "${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}/nlohmann")
set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in")
set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/cmake_config")
set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") ##
## TARGET
## create target and add include path
##
add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE)
# create and configure the library target target_include_directories(
add_library(${JSON_TARGET_NAME} INTERFACE) ${NLOHMANN_JSON_TARGET_NAME}
target_include_directories(${JSON_TARGET_NAME} INTERFACE INTERFACE $<INSTALL_INTERFACE:include/>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src> )
$<INSTALL_INTERFACE:${JSON_INCLUDE_DESTINATION}>)
##
# create and configure the unit test target ## TESTS
## create and configure the unit test target
##
if(JSON_BuildTests) if(JSON_BuildTests)
enable_testing() enable_testing()
include_directories(${NLOHMANN_JSON_SOURCE_DIR})
add_subdirectory(test) add_subdirectory(test)
endif() endif()
# generate a config and config version file for the package ##
## INSTALL
## install header files, generate and install cmake config files for find_package()
##
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
configure_package_config_file("cmake/config.cmake.in" write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}" ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} COMPATIBILITY SameMajorVersion
INSTALL_DESTINATION ${JSON_CONFIG_DESTINATION} )
PATH_VARS JSON_INCLUDE_DESTINATION) configure_package_config_file(
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" ${NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE}
VERSION ${PROJECT_VERSION} ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE}
COMPATIBILITY SameMajorVersion) INSTALL_DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
)
# export the library target and store build directory in package registry install(
export(TARGETS ${JSON_TARGET_NAME} DIRECTORY ${NLOHMANN_JSON_SOURCE_DIR}
FILE "${CMAKE_CURRENT_BINARY_DIR}/${JSON_TARGETS_FILENAME}") DESTINATION ${NLOHMANN_JSON_HEADER_INSTALL_DIR}
export(PACKAGE ${JSON_PACKAGE_NAME}) )
install(
# install library target and config files FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE}
install(TARGETS ${JSON_TARGET_NAME} DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
EXPORT ${JSON_PACKAGE_NAME}) )
install(FILES "src/json.hpp" install(
DESTINATION ${JSON_INCLUDE_DESTINATION}) TARGETS ${NLOHMANN_JSON_TARGET_NAME}
install(EXPORT ${JSON_PACKAGE_NAME} EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
FILE ${JSON_TARGETS_FILENAME} INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}
DESTINATION ${JSON_CONFIG_DESTINATION}) )
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}" install(
"${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
DESTINATION ${JSON_CONFIG_DESTINATION}) DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
)

View file

@ -1,7 +1,3 @@
@PACKAGE_INIT@ @PACKAGE_INIT@
set_and_check(JSON_INCLUDE_DIR "@PACKAGE_JSON_INCLUDE_DESTINATION@") include("${CMAKE_CURRENT_LIST_DIR}/@NLOHMANN_JSON_TARGETS_EXPORT_NAME@.cmake")
check_required_components("@PROJECT_NAME@")
cmake_policy(PUSH)
cmake_policy(SET CMP0024 OLD)
include(${CMAKE_CURRENT_LIST_DIR}/@JSON_TARGETS_FILENAME@)
cmake_policy(POP)