From dcfab29e70af058cb514f1b21343b396e0cfdfad Mon Sep 17 00:00:00 2001 From: Claus Klein Date: Tue, 2 Feb 2021 17:45:59 +0100 Subject: [PATCH] add cmake files to build and install header only library (#85) * add cmake build and install support export cmake config package too * enable bootstap build without cmake config package cleanup cmake list file --- CMakeLists.txt | 54 ++++++++++++++++++++++++++++++++++ cmake/tomlplusplusConfig.cmake | 1 + examples/CMakeLists.txt | 15 ++++++++++ 3 files changed, 70 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/tomlplusplusConfig.cmake create mode 100644 examples/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..945717b2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,54 @@ +cmake_minimum_required(VERSION 3.13...3.19) + +project(tomlplusplus LANGUAGES CXX VERSION 2.3.1) + +# Determine if this project is built as a subproject (using +# add_subdirectory) or if it is the master project. +set(MASTER_PROJECT OFF) +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(MASTER_PROJECT ON) + message(STATUS "CMake version: ${CMAKE_VERSION}") +endif() + + +add_library(tomlplusplus INTERFACE) +add_library(tomlplusplus::tomlplusplus ALIAS tomlplusplus) +target_compile_features(tomlplusplus INTERFACE cxx_std_17) +target_include_directories(tomlplusplus INTERFACE + $ + $ +) + + +option(TOMLPP_BUILD_EXAMPLES "Build examples." ${MASTER_PROJECT}) +if(TOMLPP_BUILD_EXAMPLES) + add_subdirectory(examples) +endif() + + +option(TOMLPP_INSTALL "Generate the install target" ${MASTER_PROJECT}) +if(TOMLPP_INSTALL) + include(CMakePackageConfigHelpers) + include(GNUInstallDirs) + install(TARGETS tomlplusplus EXPORT ${PROJECT_NAME}Targets) + + install(DIRECTORY include/toml++ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + + write_basic_package_version_file( + ${PROJECT_NAME}ConfigVersion.cmake + COMPATIBILITY SameMajorVersion + ) + + install( + FILES cmake/tomlplusplusConfig.cmake # ----------> + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + ) + + install( + EXPORT ${PROJECT_NAME}Targets + NAMESPACE ${PROJECT_NAME}:: + FILE ${PROJECT_NAME}Targets.cmake # <---------- + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + ) +endif() diff --git a/cmake/tomlplusplusConfig.cmake b/cmake/tomlplusplusConfig.cmake new file mode 100644 index 00000000..ec9f7a00 --- /dev/null +++ b/cmake/tomlplusplusConfig.cmake @@ -0,0 +1 @@ +include(${CMAKE_CURRENT_LIST_DIR}/tomlplusplusTargets.cmake) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..a1cf87f3 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.13...3.19) + +project(Examples LANGUAGES CXX) + +if(NOT TARGET tomlplusplus::tomlplusplus) + find_package(tomlplusplus REQUIRED) +endif() + +add_executable(toml_to_json_transcoder toml_to_json_transcoder.cpp) +target_link_libraries(toml_to_json_transcoder PRIVATE tomlplusplus::tomlplusplus) + +enable_testing() +add_test(NAME toml_to_json_transcoder + COMMAND toml_to_json_transcoder ${CMAKE_CURRENT_LIST_DIR}/example.toml +)