diff --git a/CMakeLists.txt b/CMakeLists.txt index b345c77a56..8d584c59c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,17 +26,17 @@ set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "lib/cmake/${PROJECT_NAME}" CACHE INTERNAL "") set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "include") set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") -set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in") +set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "${CMAKE_CURRENT_LIST_DIR}/cmake/config.cmake.in") set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") 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(NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake") if (JSON_MultipleHeaders) - set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") + set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${CMAKE_CURRENT_LIST_DIR}/include/") message(STATUS "Using the multi-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") else() - set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") + set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${CMAKE_CURRENT_LIST_DIR}/single_include/") message(STATUS "Using the single-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") endif() @@ -58,12 +58,12 @@ target_include_directories( ## add debug view definition file for msvc (natvis) if (MSVC) set(NLOHMANN_ADD_NATVIS TRUE) - set(NLOHMANN_NATVIS_FILE "nlohmann_json.natvis") + set(NLOHMANN_NATVIS_FILE "${CMAKE_CURRENT_LIST_DIR}/nlohmann_json.natvis") target_sources( ${NLOHMANN_JSON_TARGET_NAME} INTERFACE $ - $ + $ ) endif() @@ -75,12 +75,13 @@ include(CTest) #adds option BUILD_TESTING (default ON) if(BUILD_TESTING AND JSON_BuildTests) enable_testing() - add_subdirectory(test) + add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/test" "${CMAKE_BINARY_DIR}/json-tests") endif() ## ## INSTALL -## install header files, generate and install cmake config files for find_package() +## install header files, generate and install cmake config files for find_package(); +## but only if Nlohmann JSON is the main project being built. ## include(CMakePackageConfigHelpers) write_basic_package_version_file( @@ -91,33 +92,35 @@ configure_package_config_file( ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} INSTALL_DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} ) - -install( - DIRECTORY ${NLOHMANN_JSON_INCLUDE_BUILD_DIR} - DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} -) -install( - FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} - DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} -) -if (NLOHMANN_ADD_NATVIS) - install( - FILES ${NLOHMANN_NATVIS_FILE} - DESTINATION . -) -endif() export( TARGETS ${NLOHMANN_JSON_TARGET_NAME} NAMESPACE ${PROJECT_NAME}:: FILE ${NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE} ) -install( - TARGETS ${NLOHMANN_JSON_TARGET_NAME} - EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} - INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} -) -install( - EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} -) + +if($PROJECT_SOURCE_DIR STREQUAL $CMAKE_CURRENT_LIST_DIR) + install( + DIRECTORY ${NLOHMANN_JSON_INCLUDE_BUILD_DIR} + DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} + ) + install( + FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} + DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} + ) + if (NLOHMANN_ADD_NATVIS) + install( + FILES ${NLOHMANN_NATVIS_FILE} + DESTINATION . + ) + endif() + install( + TARGETS ${NLOHMANN_JSON_TARGET_NAME} + EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} + INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} + ) + install( + EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} + NAMESPACE ${PROJECT_NAME}:: + DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} + ) +endif() \ No newline at end of file