From 28f73ed406ec8b4a5d79ea486f92160f844dfb9b Mon Sep 17 00:00:00 2001 From: Robert Marki Date: Sun, 24 Apr 2016 16:55:32 +0200 Subject: [PATCH 1/4] Refactor CMake listfile to define an interface imported library Define the library as an interface imported library so other targets can use the library as a dependency and use the interface properties of the library. --- CMakeLists.txt | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16e9f960..3985171b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,24 +2,17 @@ cmake_minimum_required(VERSION 2.8) project(json CXX) -add_executable(json_unit - src/json.hpp test/catch.hpp test/unit.cpp -) +add_library(${PROJECT_NAME} INTERFACE IMPORTED GLOBAL) +set_target_properties(${PROJECT_NAME} PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/src") -if(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) -else(MSVC) - set(CMAKE_CXX_FLAGS - "-std=c++11" - ) -endif(MSVC) - -include_directories( - src test -) +set(UNIT_TEST_NAME "json_unit") +add_executable(${UNIT_TEST_NAME} + "test/catch.hpp" "test/unit.cpp") +set_target_properties(${UNIT_TEST_NAME} PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED ON + COMPILE_DEFINITIONS "$<$:_SCL_SECURE_NO_WARNINGS>" + COMPILE_OPTIONS "$<$:/EHsc;$<$:/Od>>") +target_include_directories(${UNIT_TEST_NAME} PRIVATE "test") +target_link_libraries(${UNIT_TEST_NAME} ${PROJECT_NAME}) From 30e396043aef78aae28a6785ab0253bc1df401d5 Mon Sep 17 00:00:00 2001 From: Robert Marki Date: Mon, 25 Apr 2016 09:36:05 +0200 Subject: [PATCH 2/4] Export library target and store build directory in package registry --- CMakeLists.txt | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3985171b..6cb02c09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,26 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 3.0) project(json CXX) -add_library(${PROJECT_NAME} INTERFACE IMPORTED GLOBAL) -set_target_properties(${PROJECT_NAME} PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/src") +set(JSON_TARGET_NAME ${PROJECT_NAME}) +set(JSON_UNITTEST_TARGET_NAME "json_unit") +set(JSON_PACKAGE_NAME ${JSON_TARGET_NAME}) +set(JSON_TARGETS_FILENAME "${JSON_PACKAGE_NAME}Targets.cmake") -set(UNIT_TEST_NAME "json_unit") -add_executable(${UNIT_TEST_NAME} +add_library(${JSON_TARGET_NAME} INTERFACE) +target_include_directories(${JSON_TARGET_NAME} INTERFACE + $) + +add_executable(${JSON_UNITTEST_TARGET_NAME} "test/catch.hpp" "test/unit.cpp") -set_target_properties(${UNIT_TEST_NAME} PROPERTIES +set_target_properties(${JSON_UNITTEST_TARGET_NAME} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON COMPILE_DEFINITIONS "$<$:_SCL_SECURE_NO_WARNINGS>" COMPILE_OPTIONS "$<$:/EHsc;$<$:/Od>>") -target_include_directories(${UNIT_TEST_NAME} PRIVATE "test") -target_link_libraries(${UNIT_TEST_NAME} ${PROJECT_NAME}) +target_include_directories(${JSON_UNITTEST_TARGET_NAME} PRIVATE "test") +target_link_libraries(${JSON_UNITTEST_TARGET_NAME} ${JSON_TARGET_NAME}) + +export(TARGETS ${JSON_TARGET_NAME} + FILE "${CMAKE_BINARY_DIR}/${JSON_TARGETS_FILENAME}") +export(PACKAGE ${JSON_PACKAGE_NAME}) From b1a2e9a7150fd7003c433c4e2693b8cebed2d8d9 Mon Sep 17 00:00:00 2001 From: Robert Marki Date: Mon, 25 Apr 2016 10:23:14 +0200 Subject: [PATCH 3/4] Create an install target for the project - define the project's version in the cmake listfile - create a config and config version file for the cmake package - install the json.hpp file and the package's targets, config and config version files --- CMakeLists.txt | 35 +++++++++++++++++++++++++++++++++-- cmake/config.cmake.in | 6 ++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 cmake/config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cb02c09..75caa9a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,25 @@ cmake_minimum_required(VERSION 3.0) -project(json CXX) +# define the project +project(json VERSION 2.0.0 LANGUAGES CXX) +# define project variables set(JSON_TARGET_NAME ${PROJECT_NAME}) set(JSON_UNITTEST_TARGET_NAME "json_unit") set(JSON_PACKAGE_NAME ${JSON_TARGET_NAME}) set(JSON_TARGETS_FILENAME "${JSON_PACKAGE_NAME}Targets.cmake") +set(JSON_CONFIG_FILENAME "${JSON_PACKAGE_NAME}Config.cmake") +set(JSON_CONFIGVERSION_FILENAME "${JSON_PACKAGE_NAME}ConfigVersion.cmake") +set(JSON_CONFIG_DESTINATION "cmake") +set(JSON_INCLUDE_DESTINATION "include/nlohmann") +# create and configure the library target add_library(${JSON_TARGET_NAME} INTERFACE) target_include_directories(${JSON_TARGET_NAME} INTERFACE - $) + $ + $) +# create and configure the unit test target add_executable(${JSON_UNITTEST_TARGET_NAME} "test/catch.hpp" "test/unit.cpp") set_target_properties(${JSON_UNITTEST_TARGET_NAME} PROPERTIES @@ -21,6 +30,28 @@ set_target_properties(${JSON_UNITTEST_TARGET_NAME} PROPERTIES target_include_directories(${JSON_UNITTEST_TARGET_NAME} PRIVATE "test") target_link_libraries(${JSON_UNITTEST_TARGET_NAME} ${JSON_TARGET_NAME}) +# generate a config and config version file for the package +include(CMakePackageConfigHelpers) +configure_package_config_file("cmake/config.cmake.in" + "${CMAKE_BINARY_DIR}/${JSON_CONFIG_FILENAME}" + INSTALL_DESTINATION ${JSON_CONFIG_DESTINATION}) +write_basic_package_version_file("${CMAKE_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion) + +# export the library target and store build directory in package registry export(TARGETS ${JSON_TARGET_NAME} FILE "${CMAKE_BINARY_DIR}/${JSON_TARGETS_FILENAME}") export(PACKAGE ${JSON_PACKAGE_NAME}) + +# install library target and config files +install(TARGETS ${JSON_TARGET_NAME} + EXPORT ${JSON_PACKAGE_NAME}) +install(FILES "src/json.hpp" + DESTINATION ${JSON_INCLUDE_DESTINATION}) +install(EXPORT ${JSON_PACKAGE_NAME} + FILE ${JSON_TARGETS_FILENAME} + DESTINATION ${JSON_CONFIG_DESTINATION}) +install(FILES "${CMAKE_BINARY_DIR}/${JSON_CONFIG_FILENAME}" + "${CMAKE_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" + DESTINATION ${JSON_CONFIG_DESTINATION}) diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in new file mode 100644 index 00000000..3ff97add --- /dev/null +++ b/cmake/config.cmake.in @@ -0,0 +1,6 @@ +@PACKAGE_INIT@ + +cmake_policy(PUSH) +cmake_policy(SET CMP0024 OLD) +include(${CMAKE_CURRENT_LIST_DIR}/@JSON_TARGETS_FILENAME@) +cmake_policy(POP) \ No newline at end of file From 192a6920342257d2feca3e35c9c4a8b44a792b32 Mon Sep 17 00:00:00 2001 From: Robert Marki Date: Mon, 25 Apr 2016 11:55:03 +0200 Subject: [PATCH 4/4] Generate package config files in cmake's current binary directory Generate the config files of the package in CMAKE_CURRENT_BINARY_DIR instead of CMAKE_BINARY_DIR because the later will refer to the main project's binary directory if the library is included in the project using the add_subdirectory command. --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75caa9a2..4ae1d8bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,15 +33,15 @@ target_link_libraries(${JSON_UNITTEST_TARGET_NAME} ${JSON_TARGET_NAME}) # generate a config and config version file for the package include(CMakePackageConfigHelpers) configure_package_config_file("cmake/config.cmake.in" - "${CMAKE_BINARY_DIR}/${JSON_CONFIG_FILENAME}" + "${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}" INSTALL_DESTINATION ${JSON_CONFIG_DESTINATION}) -write_basic_package_version_file("${CMAKE_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" +write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion) # export the library target and store build directory in package registry export(TARGETS ${JSON_TARGET_NAME} - FILE "${CMAKE_BINARY_DIR}/${JSON_TARGETS_FILENAME}") + FILE "${CMAKE_CURRENT_BINARY_DIR}/${JSON_TARGETS_FILENAME}") export(PACKAGE ${JSON_PACKAGE_NAME}) # install library target and config files @@ -52,6 +52,6 @@ install(FILES "src/json.hpp" install(EXPORT ${JSON_PACKAGE_NAME} FILE ${JSON_TARGETS_FILENAME} DESTINATION ${JSON_CONFIG_DESTINATION}) -install(FILES "${CMAKE_BINARY_DIR}/${JSON_CONFIG_FILENAME}" - "${CMAKE_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIG_FILENAME}" + "${CMAKE_CURRENT_BINARY_DIR}/${JSON_CONFIGVERSION_FILENAME}" DESTINATION ${JSON_CONFIG_DESTINATION})