1
0
Fork 0
mirror of https://github.com/TheLartians/ModernCppStarter.git synced 2025-08-30 21:51:12 +02:00

Merge pull request #1 from TheLartians/external-packaging

add PackageProject.cmake as a CPM dependency
This commit is contained in:
Lars Melchior 2020-04-14 12:52:35 +02:00 committed by GitHub
commit ad1f006762
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 67 deletions

View file

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# ---- Project ----
# Note: update this to your new project's name and version
project(Greeter
VERSION 1.0
LANGUAGES CXX
@ -20,23 +21,21 @@ endif()
FILE(GLOB_RECURSE headers "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
FILE(GLOB_RECURSE sources "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
# ---- Add dependencies via CPM (if required) ----
# ---- Add dependencies via CPM ----
# see https://github.com/TheLartians/CPM.cmake for more info
# include(cmake/CPM.cmake)
include(cmake/CPM.cmake)
# CPMAddPackage(
# NAME cxxopts
# GITHUB_REPOSITORY jarro2783/cxxopts
# VERSION 2.2.0
# OPTIONS
# "CXXOPTS_BUILD_EXAMPLES Off"
# "CXXOPTS_BUILD_TESTS Off"
# )
# PackageProject.cmake will be used to make our target installable
CPMAddPackage(
NAME PackageProject.cmake
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
VERSION 1.0
)
# ---- Create library ----
# Notes:
# Note:
# for single header libraries use `add_library(Greeter INTERFACE)` instead
# To create an executable use `add_executable(Greeter ${headers} ${sources})`
add_library(Greeter ${headers} ${sources})
@ -46,16 +45,22 @@ add_library(Greeter ${headers} ${sources})
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
# Link dependencies (if required)
# target_link_libraries(Greeter cxxopts)
# target_link_libraries(Greeter PUBLIC cxxopts)
# Node: change PUBLIC to INTERFACE for single header libraries
# Note: change PUBLIC to INTERFACE for single header libraries
target_include_directories(Greeter
PUBLIC
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)
# ---- Create an installable target ----
# this allows users install and find the library via `find_package(Greeter)`.
# this allows users to install and find the library via `find_package()`.
include(cmake/packaging.cmake)
packageProject(
NAME ${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
)

View file

@ -21,7 +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
- Installable target with versioning information via [PackageProject.cmake](https://github.com/TheLartians/PackageProject.cmake)
## Usage
@ -80,9 +80,16 @@ See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options
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?
- Can I configure and build my project offline?
As there are a lot of possible options and configurations, this is not in the scope of this template. See the [CPack documentation](https://cmake.org/cmake/help/latest/module/CPack.html) for more info.
Using CPM, all missing dependencies are downloaded at configure time.
To avoid redundant downloads, it's recommended to set a CPM cache directory, e.g.: `export CPM_SOURCE_CACHE=$HOME/.cache/CPM`.
This will also allow offline configurations if all dependencies are present.
No internet connection is required for building.
- Can I use CPack to create a package installer for my project?
As there are a lot of possible options and configurations, this is not (yet) in the scope of this template. See the [CPack documentation](https://cmake.org/cmake/help/latest/module/CPack.html) for more information on setting up CPack installers.
## Coming soon

View file

@ -1,5 +0,0 @@
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components("@PROJECT_NAME@")

View file

@ -1,43 +0,0 @@
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}
)