From 4ece73cd76016c86cde9071dded432f65cce8978 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 23 Sep 2022 13:57:44 +0200 Subject: [PATCH] Clean up installation and inline variables --- CMakeLists.txt | 106 ++++++++++++++++++++++----------------- cmake/json_opts.cmake | 18 +++---- cmake/json_summary.cmake | 2 +- tests/CMakeLists.txt | 2 +- tests/abi/CMakeLists.txt | 2 +- 5 files changed, 68 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ba817ab6..a83bca5b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,16 +32,16 @@ include(json_opts) # add library targets ############################################################################# -add_library(${JSON_TARGET_NAME} INTERFACE) -add_library(${PROJECT_NAME}::${JSON_TARGET_NAME} ALIAS ${JSON_TARGET_NAME}) +add_library(nlohmann_json INTERFACE) +add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json) if (${CMAKE_VERSION} VERSION_LESS "3.8.0") - target_compile_features(${JSON_TARGET_NAME} INTERFACE cxx_range_for) + target_compile_features(nlohmann_json INTERFACE cxx_range_for) else() - target_compile_features(${JSON_TARGET_NAME} INTERFACE cxx_std_11) + target_compile_features(nlohmann_json INTERFACE cxx_std_11) endif() target_compile_definitions( - ${JSON_TARGET_NAME} + nlohmann_json INTERFACE $<$>:JSON_USE_GLOBAL_UDLS=0> $<$>:JSON_USE_IMPLICIT_CONVERSIONS=0> @@ -51,21 +51,19 @@ target_compile_definitions( ) target_include_directories( - ${JSON_TARGET_NAME} + nlohmann_json ${JSON_SYSTEM_INCLUDE} INTERFACE - $ - $ + $ + $ ) -## add debug view definition file for msvc (natvis) +# add Natvis debug view definition file for MSVC if (MSVC) - set(NLOHMANN_ADD_NATVIS TRUE) - set(NLOHMANN_NATVIS_FILE "nlohmann_json.natvis") target_sources( - ${JSON_TARGET_NAME} + nlohmann_json INTERFACE - $ - $ + $ + $ ) endif() @@ -91,23 +89,23 @@ endif() # generate pkg-config file configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json.pc" ) include(CMakePackageConfigHelpers) # generate CMake module configuration file configure_package_config_file( - ${JSON_CMAKE_CONFIG_TEMPLATE} - ${JSON_CMAKE_PROJECT_CONFIG_FILE} - INSTALL_DESTINATION ${JSON_CONFIG_INSTALL_DIR} + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/nlohmann_jsonConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfig.cmake" + INSTALL_DESTINATION "${JSON_INSTALL_CONFIG_DIR}/cmake/nlohmann_json" NO_SET_AND_CHECK_MACRO ) # generate CMake module version file if(CMAKE_VERSION VERSION_EQUAL "3.14" OR CMAKE_VERSION VERSION_GREATER "3.14") write_basic_package_version_file( - ${JSON_CMAKE_VERSION_CONFIG_FILE} + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT ) @@ -116,50 +114,64 @@ else() # write_basic_package_version_file to ensure that it's architecture-independent # https://github.com/nlohmann/json/issues/1697 configure_file( - "cmake/nlohmann_jsonConfigVersion.cmake.in" - ${JSON_CMAKE_VERSION_CONFIG_FILE} + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/nlohmann_jsonConfigVersion.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" @ONLY ) endif() +# generate CMake module targets file for use without installation +export( + TARGETS nlohmann_json + NAMESPACE nlohmann_json:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonTargets.cmake" +) + ############################################################################# # install files and targets ############################################################################# if(JSON_Install) + # install pkg-config file install( - DIRECTORY ${JSON_INCLUDE_BUILD_DIR} - DESTINATION ${JSON_INCLUDE_INSTALL_DIR} + FILES "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json.pc" + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/pkgconfig" ) + + # install CMake module configuration and version files install( - FILES ${JSON_CMAKE_PROJECT_CONFIG_FILE} ${JSON_CMAKE_VERSION_CONFIG_FILE} - DESTINATION ${JSON_CONFIG_INSTALL_DIR} + FILES "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/cmake/nlohmann_json" ) - if (NLOHMANN_ADD_NATVIS) + + # install targets + install( + TARGETS nlohmann_json + EXPORT nlohmann_jsonTargets + INCLUDES DESTINATION "${JSON_INSTALL_INCLUDE_DIR}" + ) + + # generate and install CMake module targets file(s) + install( + EXPORT nlohmann_jsonTargets + NAMESPACE nlohmann_json:: + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/cmake/nlohmann_json" + ) + + # install header files + install( + DIRECTORY "${JSON_BUILD_INCLUDE_DIR}" + DESTINATION "${JSON_INSTALL_INCLUDE_DIR}" + ) + + # install Natvis debug view definition file for MSVC + if (MSVC) install( - FILES ${NLOHMANN_NATVIS_FILE} + FILES "nlohmann_json.natvis" DESTINATION . - ) + ) endif() - export( - TARGETS ${JSON_TARGET_NAME} - NAMESPACE ${PROJECT_NAME}:: - FILE ${JSON_CMAKE_PROJECT_TARGETS_FILE} - ) - install( - TARGETS ${JSON_TARGET_NAME} - EXPORT ${JSON_TARGETS_EXPORT_NAME} - INCLUDES DESTINATION ${JSON_INCLUDE_INSTALL_DIR} - ) - install( - EXPORT ${JSON_TARGETS_EXPORT_NAME} - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${JSON_CONFIG_INSTALL_DIR} - ) - install( - FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" - DESTINATION ${JSON_PKGCONFIG_INSTALL_DIR} - ) endif() ############################################################################# diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index 1e6f55445..ac0b2043b 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -43,21 +43,15 @@ option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF # configuration ############################################################################# -set(JSON_TARGET_NAME ${PROJECT_NAME}) -set(JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") -set(JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") -set(JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") -set(JSON_CMAKE_CONFIG_TEMPLATE "cmake/nlohmann_jsonConfig.cmake.in") -set(JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") -set(JSON_CMAKE_VERSION_CONFIG_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake") -set(JSON_CMAKE_PROJECT_CONFIG_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake") -set(JSON_CMAKE_PROJECT_TARGETS_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake") -set(JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig") +# package configuration install base directory +set(JSON_INSTALL_CONFIG_DIR "${CMAKE_INSTALL_DATADIR}") +# include directories +set(JSON_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}") if (JSON_MultipleHeaders) - set(JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") + set(JSON_BUILD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/") else() - set(JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") + set(JSON_BUILD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/single_include/") endif() if (JSON_SystemInclude) diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index 4cdaed8e4..758dda535 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -88,5 +88,5 @@ json_feature(JSON_LegacyDiscardedValueComparison "Legacy discarded value compari message("") json_feature(JSON_MultipleHeaders "Use the multi-header code?") -message(" Include directory: ${JSON_INCLUDE_BUILD_DIR}") +message(" Include directory: ${JSON_BUILD_INCLUDE_DIR}") json_feature(JSON_SystemInclude "Include as system headers?") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 58f840e62..c6ee45c69 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,7 +54,7 @@ target_include_directories(test_main PUBLIC thirdparty/doctest thirdparty/fifo_map ${PROJECT_BINARY_DIR}/include) -target_link_libraries(test_main PUBLIC ${JSON_TARGET_NAME}) +target_link_libraries(test_main PUBLIC nlohmann_json) ############################################################################# # define test- and standard-specific build settings diff --git a/tests/abi/CMakeLists.txt b/tests/abi/CMakeLists.txt index 964b19776..26bfd6e7d 100644 --- a/tests/abi/CMakeLists.txt +++ b/tests/abi/CMakeLists.txt @@ -18,7 +18,7 @@ target_compile_options(abi_compat_common INTERFACE target_include_directories(abi_compat_common SYSTEM INTERFACE ../thirdparty/doctest include) -target_link_libraries(abi_compat_common INTERFACE ${JSON_TARGET_NAME}) +target_link_libraries(abi_compat_common INTERFACE nlohmann_json) # shared main() add_library(abi_compat_main STATIC main.cpp)