diff --git a/CMakeLists.txt b/CMakeLists.txt index 959499f..477b06a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,46 +58,4 @@ target_include_directories(Greeter # ---- Create an installable target ---- # this allows users install and find the library via `find_package(Greeter)`. -include(CMakePackageConfigHelpers) - -write_basic_package_version_file( - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion -) - -install( - TARGETS ${PROJECT_NAME} - EXPORT ${PROJECT_NAME}Targets - LIBRARY DESTINATION lib/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Runtime - ARCHIVE DESTINATION lib/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Development - RUNTIME DESTINATION bin/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Runtime - PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Development - BUNDLE DESTINATION bin COMPONENT Runtime -) - -include(CMakePackageConfigHelpers) - -configure_package_config_file( - "${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in" - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} -) - -install( - EXPORT ${PROJECT_NAME}Targets - DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} -) - -install( - FILES - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - DESTINATION - lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} -) - -install( - DIRECTORY ${PROJECT_SOURCE_DIR}/include/ - DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} -) +include(cmake/packaging.cmake) diff --git a/README.md b/README.md index e07d208..41002f9 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ This template is a collection from learnings of previous projects and should all - Code coverage via [codecov](https://codecov.io) - Code formatting enforced by [clang-format](https://clang.llvm.org/docs/ClangFormat.html) via [Format.cmake](https://github.com/TheLartians/Format.cmake) - Reproducible dependency management via [CPM.cmake](https://github.com/TheLartians/CPM.cmake) +- Installable target with versioning information ## Usage @@ -77,6 +78,7 @@ See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options If problems do arise, they can always opt-out by defining `CPM_USE_LOCAL_PACKAGES`, which will override all calls to `CPMAddPackage` with `find_package`. If you are using `CPMFindPackage` instead of `CPMAddPackage`, CPM will always try to use `find_package` to add packages. This approach should be compatible with any common C++ package manager without any user intervention, however at the cost of reproducible builds. + For more info, see the [CPM.cmake documentation](https://github.com/TheLartians/CPM.cmake). - How do I package my library / executable into an installer? @@ -84,4 +86,4 @@ See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options ## Coming soon -- Script to automatically adjust this template for new projects +- Script to automatically adjust the template for new projects diff --git a/cmake/packaging.cmake b/cmake/packaging.cmake new file mode 100644 index 0000000..bd0fdc7 --- /dev/null +++ b/cmake/packaging.cmake @@ -0,0 +1,43 @@ +include(CMakePackageConfigHelpers) + +write_basic_package_version_file( + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion +) + +install( + TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}Targets + LIBRARY DESTINATION lib/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Runtime + ARCHIVE DESTINATION lib/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Development + RUNTIME DESTINATION bin/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Runtime + PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} COMPONENT Development + BUNDLE DESTINATION bin COMPONENT Runtime +) + +include(CMakePackageConfigHelpers) + +configure_package_config_file( + "${CMAKE_CURRENT_LIST_DIR}/Config.cmake.in" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} +) + +install( + EXPORT ${PROJECT_NAME}Targets + DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} +) + +install( + FILES + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + DESTINATION + lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} +) + +install( + DIRECTORY ${PROJECT_SOURCE_DIR}/include/ + DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} +)