support cmake package config file to make the library searchable with find_package.

This commit is contained in:
Damien Buhl (alias daminetreg) 2015-09-20 19:12:27 +02:00
parent 0a81353989
commit 0b231f45fc
2 changed files with 59 additions and 12 deletions

View File

@ -1,25 +1,49 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 3.0.0)
project(json CXX) project(json CXX)
set (json_VERSION "1.0.0-rc1")
add_executable(json_unit add_executable(json_unit
src/json.hpp test/catch.hpp test/unit.cpp src/json.hpp test/catch.hpp test/unit.cpp
) )
if(MSVC) if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
"/EHsc"
)
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) elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
else(MSVC) set(CMAKE_CXX_FLAGS "/EHsc")
set(CMAKE_CXX_FLAGS
"-std=c++11 -stdlib=libc++" STRING(REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
)
endif(MSVC) add_definitions(-D_SCL_SECURE_NO_WARNINGS)
endif()
include_directories( include_directories(
src test 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})

View File

@ -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()