From 2a0cc76cbb58ce23f44e6ba3a457c060b9783f73 Mon Sep 17 00:00:00 2001 From: Guillaume Racicot Date: Fri, 25 Jan 2019 15:29:45 -0500 Subject: [PATCH] Added install target and cmake package exports --- CMakeLists.txt | 109 +++++++++++++++++- cmake/config/injaBuildConfig.cmake.in | 11 ++ cmake/config/injaConfig.cmake.in | 12 ++ meson.build | 2 +- .../include}/nlohmann/LICENSE.MIT | 0 .../include}/nlohmann/json.hpp | 0 6 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 cmake/config/injaBuildConfig.cmake.in create mode 100644 cmake/config/injaConfig.cmake.in rename {include => third_party/include}/nlohmann/LICENSE.MIT (100%) rename {include => third_party/include}/nlohmann/json.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f4bc0024..fe605bf4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,14 @@ cmake_minimum_required(VERSION 3.5) project(inja LANGUAGES CXX VERSION 2.0.0) +option(INJA_USE_EMBEDDED_JSON "Use the shipped json header if not available on the system" ON) +option(INJA_INSTALL "Generate install targets for inja" ON) +option(INJA_EXPORT "Export the current build tree to the package registry" ON) option(BUILD_TESTS "Build the inja unit tests" ON) option(BUILD_BENCHMARK "Build the inja benchmark" ON) option(COVERALLS "Generate coveralls data" OFF) +set(INJA_INSTALL_INCLUDE_DIR "include") set(CMAKE_CXX_STANDARD 17) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake) @@ -25,8 +29,43 @@ endif() add_library(inja INTERFACE) -target_include_directories(inja INTERFACE include) +add_library(pantor::inja ALIAS inja) +target_include_directories(inja INTERFACE + $ + $ +) + +target_compile_features(inja INTERFACE cxx_std_11) + +set(INJA_PACKAGE_USE_EMBEDDED_JSON OFF) + +if(INJA_USE_EMBEDDED_JSON) + find_package(nlohmann_json QUIET) + if (NOT nlohmann_json_FOUND) + set(INJA_PACKAGE_USE_EMBEDDED_JSON ON) + add_library(nlohmann_json INTERFACE) + add_library(pantor::nlohmann_json ALIAS nlohmann_json) + + target_include_directories(nlohmann_json INTERFACE + $ + $ + ) + + target_compile_features(nlohmann_json INTERFACE cxx_std_11) + + install(TARGETS nlohmann_json EXPORT injaTargets) + + set(INJA_SELECTED_JSON_LIBRARY "pantor::nlohmann_json") + else() + set(INJA_SELECTED_JSON_LIBRARY "nlohmann_json::nlohmann_json") + endif() +else() + find_package(nlohmann_json REQUIRED) + set(INJA_SELECTED_JSON_LIBRARY "nlohmann_json::nlohmann_json") +endif() + +target_link_libraries(inja INTERFACE ${INJA_SELECTED_JSON_LIBRARY}) execute_process(COMMAND python3 amalgamate/amalgamate.py -c amalgamate/config.json -s include WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) @@ -57,7 +96,7 @@ if(BUILD_TESTS) add_library(single_inja INTERFACE) - target_include_directories(single_inja INTERFACE single_include include) + target_include_directories(single_inja INTERFACE single_include include third_party/include) add_executable(single_inja_test test/unit.cpp @@ -74,3 +113,69 @@ if(BUILD_BENCHMARK) add_executable(inja_benchmark test/benchmark.cpp) target_link_libraries(inja_benchmark PRIVATE inja) endif() + +include(CMakePackageConfigHelpers) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/injaConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion +) + +# build tree package config +configure_file( + cmake/config/injaBuildConfig.cmake.in + injaConfig.cmake + @ONLY +) + +install(TARGETS inja EXPORT injaTargets) + +export( + EXPORT injaTargets + NAMESPACE pantor:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/injaTargets.cmake" +) + +# build tree package config +configure_file( + cmake/config/injaBuildConfig.cmake.in + injaConfig.cmake + @ONLY +) + +if (INJA_INSTALL) + set(INJA_CONFIG_PATH "lib/cmake/inja") + + # install tree package config + configure_package_config_file( + cmake/config/injaConfig.cmake.in + ${INJA_CONFIG_PATH}/injaConfig.cmake + INSTALL_DESTINATION ${INJA_CONFIG_PATH} + NO_CHECK_REQUIRED_COMPONENTS_MACRO + ) + + install(DIRECTORY include/inja DESTINATION ${INJA_INSTALL_INCLUDE_DIR} FILES_MATCHING PATTERN "*.hpp") + install(FILES + "${CMAKE_CURRENT_SOURCE_DIR}/third_party/include/nlohmann/json.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/third_party/include/nlohmann/LICENSE.MIT" + DESTINATION "${INJA_INSTALL_INCLUDE_DIR}/inja/json/include/nlohmann" + ) + + install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/${INJA_CONFIG_PATH}/injaConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/injaConfigVersion.cmake" + DESTINATION ${INJA_CONFIG_PATH} + ) + + install( + EXPORT injaTargets FILE injaTargets.cmake + NAMESPACE pantor:: + DESTINATION ${INJA_CONFIG_PATH} + ) +endif() + +if (INJA_EXPORT) + export(PACKAGE inja) +endif() diff --git a/cmake/config/injaBuildConfig.cmake.in b/cmake/config/injaBuildConfig.cmake.in new file mode 100644 index 00000000..ccbc2ae4 --- /dev/null +++ b/cmake/config/injaBuildConfig.cmake.in @@ -0,0 +1,11 @@ +set(INJA_VERSION "@PROJECT_VERSION@") + +set(INJA_PACKAGE_USE_EMBEDDED_JSON "@INJA_PACKAGE_USE_EMBEDDED_JSON@") + +include(CMakeFindDependencyMacro) + +if(NOT INJA_PACKAGE_USE_EMBEDDED_JSON) + find_dependency(nlohmann_json REQUIRED) +endif() + +include("${CMAKE_CURRENT_LIST_DIR}/injaTargets.cmake") diff --git a/cmake/config/injaConfig.cmake.in b/cmake/config/injaConfig.cmake.in new file mode 100644 index 00000000..ea81ef6a --- /dev/null +++ b/cmake/config/injaConfig.cmake.in @@ -0,0 +1,12 @@ +@PACKAGE_INIT@ +set(INJA_VERSION "@PROJECT_VERSION@") + +set(INJA_PACKAGE_USE_EMBEDDED_JSON "@INJA_PACKAGE_USE_EMBEDDED_JSON@") + +include(CMakeFindDependencyMacro) + +if(NOT INJA_PACKAGE_USE_EMBEDDED_JSON) + find_dependency(nlohmann_json REQUIRED) +endif() + +include("${CMAKE_CURRENT_LIST_DIR}/injaTargets.cmake") diff --git a/meson.build b/meson.build index e823b7ad..666e2d88 100644 --- a/meson.build +++ b/meson.build @@ -6,7 +6,7 @@ project('inja', 'cpp', default_options: ['cpp_std=c++17']) inja_dep = declare_dependency( - include_directories: include_directories('include') + include_directories: include_directories('include', 'third_party/include') ) diff --git a/include/nlohmann/LICENSE.MIT b/third_party/include/nlohmann/LICENSE.MIT similarity index 100% rename from include/nlohmann/LICENSE.MIT rename to third_party/include/nlohmann/LICENSE.MIT diff --git a/include/nlohmann/json.hpp b/third_party/include/nlohmann/json.hpp similarity index 100% rename from include/nlohmann/json.hpp rename to third_party/include/nlohmann/json.hpp