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 1/2] 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() From f9f0c3641d70fd5284b7a6ed1a9afc0824c7c401 Mon Sep 17 00:00:00 2001 From: "Damien Buhl (alias daminetreg)" Date: Tue, 22 Sep 2015 21:18:06 +0200 Subject: [PATCH 2/2] Fixes after review performed by @ruslo for cmake file in https://github.com/ruslo/hunter/pull/239 --- CMakeLists.txt | 13 +++++++------ cmake/modules/nlohmann-json-config.cmake.in | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4f3ec783..e10f83f1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,24 +1,26 @@ cmake_minimum_required(VERSION 3.0.0) -project(json CXX) - -set (json_VERSION "1.0.0-rc1") +project(json + LANGUAGES CXX + VERSION "1.0.0") add_executable(json_unit src/json.hpp test/catch.hpp test/unit.cpp ) if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") - set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "-std=c++11") + set(CMAKE_CXX_FLAGS "-std=c++11") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set(CMAKE_CXX_FLAGS "/EHsc") STRING(REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) + # See https://msdn.microsoft.com/fr-fr/library/aa985974.aspx?f=255&MSPPError=-2147217396 + # Avoid warnings for unsafe STL methods call add_definitions(-D_SCL_SECURE_NO_WARNINGS) endif() @@ -40,7 +42,6 @@ configure_package_config_file( write_basic_package_version_file( ${CMAKE_CURRENT_BINARY_DIR}/nlohmann-json-config-version.cmake - VERSION ${json_VERSION} COMPATIBILITY AnyNewerVersion) install(FILES diff --git a/cmake/modules/nlohmann-json-config.cmake.in b/cmake/modules/nlohmann-json-config.cmake.in index 073f6de11..7235c5671 100644 --- a/cmake/modules/nlohmann-json-config.cmake.in +++ b/cmake/modules/nlohmann-json-config.cmake.in @@ -19,5 +19,6 @@ 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}") + INTERFACE_INCLUDE_DIRECTORIES "${NLOHMANN_JSON_INCLUDE_DIRS}" + INTERFACE_COMPILE_OPTIONS ${NLOHMANN_JSON_DEFINITIONS}) endif()