From 0b231f45fcb402e62bf0695a676f80019a815183 Mon Sep 17 00:00:00 2001 From: "Damien Buhl (alias daminetreg)" Date: Sun, 20 Sep 2015 19:12:27 +0200 Subject: [PATCH] support cmake package config file to make the library searchable with find_package. --- CMakeLists.txt | 48 +++++++++++++++------ cmake/modules/nlohmann-json-config.cmake.in | 23 ++++++++++ 2 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 cmake/modules/nlohmann-json-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index a1fb47e8f..c4f3ec783 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,25 +1,49 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 3.0.0) project(json CXX) +set (json_VERSION "1.0.0-rc1") + add_executable(json_unit src/json.hpp test/catch.hpp test/unit.cpp ) -if(MSVC) - set(CMAKE_CXX_FLAGS - "/EHsc" - ) +if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") + set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++") - STRING(REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) +elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set(CMAKE_CXX_FLAGS "-std=c++11") - add_definitions(-D_SCL_SECURE_NO_WARNINGS) -else(MSVC) - set(CMAKE_CXX_FLAGS - "-std=c++11 -stdlib=libc++" - ) -endif(MSVC) +elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set(CMAKE_CXX_FLAGS "/EHsc") + + STRING(REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) + + add_definitions(-D_SCL_SECURE_NO_WARNINGS) +endif() include_directories( src test ) + +install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/src/ DESTINATION include/nlohmann + FILES_MATCHING PATTERN "*.[ih]pp") + +include(CMakePackageConfigHelpers) +set(INCLUDE_INSTALL_DIR include/) +set(DEFINITIONS "${CMAKE_CXX_FLAGS}") +configure_package_config_file( + ${CMAKE_CURRENT_LIST_DIR}/cmake/modules/nlohmann-json-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/nlohmann-json-config.cmake + INSTALL_DESTINATION lib/cmake/nlohmann-json-${json_VERSION} + PATH_VARS INCLUDE_INSTALL_DIR) + +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/nlohmann-json-config-version.cmake + VERSION ${json_VERSION} + COMPATIBILITY AnyNewerVersion) + +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/nlohmann-json-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/nlohmann-json-config-version.cmake + DESTINATION lib/cmake/nlohmann-json-${json_VERSION}) diff --git a/cmake/modules/nlohmann-json-config.cmake.in b/cmake/modules/nlohmann-json-config.cmake.in new file mode 100644 index 000000000..073f6de11 --- /dev/null +++ b/cmake/modules/nlohmann-json-config.cmake.in @@ -0,0 +1,23 @@ +# It can be used as : +# +# find_package(nlohmann-json) +# target_link_libraries(program nlohmann-json::nlohmann-json) +# +# It also defines +# - ${NLOHMANN_JSON_INCLUDE_DIRS} +# - ${NLOHMANN_JSON_DEFINITIONS} +# +set(NLOHMANN_JSON_VERSION @json_VERSION@) + +@PACKAGE_INIT@ + +set_and_check(NLOHMANN_JSON_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIR@") +set(NLOHMANN_JSON_DEFINITIONS "@CMAKE_CXX_FLAGS@") +check_required_components(nlohmann_json) + +if (NOT TARGET "nlohmann-json::nlohmann-json") + add_library("nlohmann-json::nlohmann-json" INTERFACE IMPORTED) + set_target_properties("nlohmann-json::nlohmann-json" + PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${NLOHMANN_JSON_INCLUDE_DIRS}") +endif()