replace example dependency with PackageProject.cmake

This commit is contained in:
Lars Melchior 2020-04-14 12:49:34 +02:00
parent 5f3c39b657
commit df111c1104
2 changed files with 21 additions and 21 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)
# 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,11 +45,11 @@ 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)
# 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}>
)
@ -58,12 +57,6 @@ target_include_directories(Greeter
# ---- Create an installable target ----
# this allows users to install and find the library via `find_package()`.
CPMAddPackage(
NAME PackageProject.cmake
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
VERSION 1.0
)
packageProject(
NAME ${PROJECT_NAME}
VERSION ${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