Skip to content

Commit 24ea5a0

Browse files
committedApr 15, 2020
Initial commit
0 parents  commit 24ea5a0

19 files changed

+907
-0
lines changed
 

‎.clang-format

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
BasedOnStyle: Google
3+
AccessModifierOffset: '-2'
4+
AlignTrailingComments: 'true'
5+
AllowAllParametersOfDeclarationOnNextLine: 'false'
6+
AlwaysBreakTemplateDeclarations: 'No'
7+
BreakBeforeBraces: Attach
8+
ColumnLimit: '100'
9+
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
10+
IncludeBlocks: Regroup
11+
IndentPPDirectives: AfterHash
12+
IndentWidth: '2'
13+
NamespaceIndentation: All
14+
BreakBeforeBinaryOperators: All
15+
BreakBeforeTernaryOperators: 'true'
16+
...

‎.github/workflows/install.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Install
2+
3+
on: [push]
4+
5+
env:
6+
CTEST_OUTPUT_ON_FAILURE: 1
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v1
15+
16+
- name: build and install library
17+
run: |
18+
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release
19+
sudo cmake --build build --target install
20+
rm -rf build
21+
22+
- name: configure
23+
run: cmake -Htest -Bbuild -DTEST_INSTALLED_VERSION=1
24+
25+
- name: build
26+
run: cmake --build build --config Debug -j4
27+
28+
- name: test
29+
run: |
30+
cd build
31+
ctest --build-config Debug

‎.github/workflows/macos.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: MacOS
2+
3+
on: [push]
4+
5+
env:
6+
CTEST_OUTPUT_ON_FAILURE: 1
7+
8+
jobs:
9+
build:
10+
11+
runs-on: macos-latest
12+
13+
steps:
14+
- uses: actions/checkout@v1
15+
16+
- name: configure
17+
run: cmake -Htest -Bbuild
18+
19+
- name: build
20+
run: cmake --build build --config Debug -j4
21+
22+
- name: test
23+
run: |
24+
cd build
25+
ctest --build-config Debug

‎.github/workflows/standalone.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Standalone
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
13+
- name: configure
14+
run: cmake -Hstandalone -Bbuild
15+
16+
- name: build
17+
run: cmake --build build -j4
18+
19+
- name: run
20+
run: ./build/Greeter

‎.github/workflows/style.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Style
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: macos-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
13+
- name: Install clang-format
14+
run: brew install clang-format
15+
16+
- name: configure
17+
run: cmake -Htest -Bbuild
18+
19+
- name: check style
20+
run: cmake --build build --target check-format

‎.github/workflows/ubuntu.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Ubuntu
2+
3+
on: [push]
4+
5+
env:
6+
CTEST_OUTPUT_ON_FAILURE: 1
7+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v1
16+
17+
- name: configure
18+
run: cmake -Htest -Bbuild -DENABLE_TEST_COVERAGE=1
19+
20+
- name: build
21+
run: cmake --build build --config Debug -j4
22+
23+
- name: test
24+
run: |
25+
cd build
26+
ctest --build-config Debug
27+
28+
- name: collect code coverage
29+
run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"

‎.github/workflows/windows.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Windows
2+
3+
on: [push]
4+
5+
env:
6+
CTEST_OUTPUT_ON_FAILURE: 1
7+
8+
jobs:
9+
build:
10+
11+
runs-on: windows-latest
12+
13+
steps:
14+
- uses: actions/checkout@v1
15+
16+
- name: configure
17+
run: cmake -Htest -Bbuild
18+
19+
- name: build
20+
run: cmake --build build --config Debug -j4
21+
22+
- name: test
23+
run: |
24+
cd build
25+
ctest --build-config Debug

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/build*
2+
/.vscode
3+
.DS_Store

‎CMakeLists.txt

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
# ---- Project ----
4+
5+
# Note: update this to your new project's name and version
6+
project(Greeter
7+
VERSION 1.0
8+
LANGUAGES CXX
9+
)
10+
11+
# ---- Include guards ----
12+
13+
if(${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR})
14+
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.")
15+
endif()
16+
17+
# ---- Add source files ----
18+
19+
# Note: globbing sources is considered bad practice as CMake's generators may not detect new files automatically.
20+
# Keep that in mind when changing files, or explicitly mention them here.
21+
FILE(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
22+
FILE(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
23+
24+
# ---- Add dependencies via CPM ----
25+
# see https://github.com/TheLartians/CPM.cmake for more info
26+
27+
include(cmake/CPM.cmake)
28+
29+
# PackageProject.cmake will be used to make our target installable
30+
CPMAddPackage(
31+
NAME PackageProject.cmake
32+
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
33+
VERSION 1.0
34+
)
35+
36+
# ---- Create library ----
37+
38+
# Note: for single header libraries create an interface target instead:
39+
# add_library(Greeter INTERFACE)
40+
# set_target_properties(Greeter PROPERTIES INTERFACE_COMPILE_FEATURES cxx_std_17)
41+
42+
add_library(Greeter ${headers} ${sources})
43+
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
44+
45+
# Link dependencies (if required)
46+
# target_link_libraries(Greeter PUBLIC cxxopts)
47+
48+
# Note: change PUBLIC to INTERFACE for single header libraries
49+
target_include_directories(Greeter
50+
PUBLIC
51+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
52+
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
53+
)
54+
55+
# ---- Create an installable target ----
56+
# this allows users to install and find the library via `find_package()`.
57+
58+
packageProject(
59+
NAME ${PROJECT_NAME}
60+
VERSION ${PROJECT_VERSION}
61+
BINARY_DIR ${PROJECT_BINARY_DIR}
62+
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
63+
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
64+
)

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Lars Melchior
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
[![Actions Status](https://github.com/TheLartians/ModernCppStarter/workflows/MacOS/badge.svg)](https://github.com/TheLartians/ModernCppStarter/actions)
2+
[![Actions Status](https://github.com/TheLartians/ModernCppStarter/workflows/Windows/badge.svg)](https://github.com/TheLartians/ModernCppStarter/actions)
3+
[![Actions Status](https://github.com/TheLartians/ModernCppStarter/workflows/Ubuntu/badge.svg)](https://github.com/TheLartians/ModernCppStarter/actions)
4+
[![Actions Status](https://github.com/TheLartians/ModernCppStarter/workflows/Style/badge.svg)](https://github.com/TheLartians/ModernCppStarter/actions)
5+
[![Actions Status](https://github.com/TheLartians/ModernCppStarter/workflows/Install/badge.svg)](https://github.com/TheLartians/ModernCppStarter/actions)
6+
[![codecov](https://codecov.io/gh/TheLartians/ModernCppStarter/branch/master/graph/badge.svg)](https://codecov.io/gh/TheLartians/ModernCppStarter)
7+
8+
# ModernCppStarter
9+
10+
Setting up a new C++ project usually requires a significant amount of preparation and boilerplate code, even more so for modern C++ projects with tests, executables and contiguous integration.
11+
This template is the result of learnings from many previous projects and should help reduce the work required to setup up a modern C++ project.
12+
13+
## Features
14+
15+
- Modern CMake practices
16+
- Suited for single header libraries and projects of any scale
17+
- Separation into library and executable code
18+
- Integrated test suite
19+
- Continuous integration via [GitHub Actions](https://help.github.com/en/actions/)
20+
- Code coverage via [codecov](https://codecov.io)
21+
- Code formatting enforced by [clang-format](https://clang.llvm.org/docs/ClangFormat.html) via [Format.cmake](https://github.com/TheLartians/Format.cmake)
22+
- Reproducible dependency management via [CPM.cmake](https://github.com/TheLartians/CPM.cmake)
23+
- Installable target with versioning information via [PackageProject.cmake](https://github.com/TheLartians/PackageProject.cmake)
24+
25+
## Usage
26+
27+
### Adjust the template to your needs
28+
29+
- Use this repo [as a template](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template) and replace all occurrences of "Greeter" in the relevant CMakeLists.txt with the name of your project
30+
- Replace the source files with your own
31+
- For single-header libraries: see the comments in [CMakeLists.txt](CMakeLists.txt)
32+
- Add your project's codecov token to your project's github secrets under `CODECOV_TOKEN`
33+
- Happy coding!
34+
35+
Eventually, you can remove any unused files, such as the standalone directory or irrelevant github workflows for your project.
36+
Feel free to replace the License with one suited for your project.
37+
38+
### Build and run the standalone target
39+
40+
Use the following command to build and run the executable target.
41+
42+
```bash
43+
cmake -Hstandalone -Bbuild/standalone
44+
cmake --build build/standalone
45+
./build/standalone/Greeter --help
46+
```
47+
48+
### Build and run test suite
49+
50+
Use the following commands from the project's root directory to run the test suite.
51+
52+
```bash
53+
cmake -Htest -Bbuild/test
54+
cmake --build build/test
55+
CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/test --target test
56+
57+
# or simply call the executable:
58+
./build/test/GreeterTests
59+
```
60+
61+
To collect code coverage information, run CMake with the `-DENABLE_TEST_COVERAGE=1` option.
62+
63+
### Run clang-format
64+
65+
Use the following commands from the project's root directory to run clang-format (must be installed on the host system).
66+
67+
```bash
68+
cmake -Htest -Bbuild/test
69+
70+
# view changes
71+
cmake --build build/test --target format
72+
73+
# apply changes
74+
cmake --build build/test --target fix-format
75+
```
76+
77+
See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options.
78+
79+
## FAQ
80+
81+
- Can I use this for header-only libraries?
82+
83+
Yes, however you will need to change the library type to an `INTERFACE` library as documented in the [CMakeLists.txt](CMakeLists.txt).
84+
85+
- I see you are using `GLOB` to add source files in CMakeLists.txt. Isn't that evil?
86+
87+
Glob is considered bad because any changes to the source file structure [might not be automatically caught](https://cmake.org/cmake/help/latest/command/file.html#filesystem) by CMake's builders and you will need to manually invoke CMake on changes.
88+
I personally prefer the `GLOB` solution for its simplicity, but feel free to change it to explicitly listing sources.
89+
90+
- I want to add additional targets to my project. Should I modify the main CMakeLists to conditionally include them?
91+
92+
If possible, avoid adding conditional includes to the CMakeLists (even though it is a common sight in the C++ world), as it makes the build system convoluted and hard to reason about.
93+
Instead, create a new directory with a CMakeLists that adds the main project as a dependency (e.g. just copy the [standalone](standalone) directory).
94+
Depending on the complexity of the project it might make sense move this to a separate repository and list a specific version or commit of the main project.
95+
96+
- You recommend to add external dependencies using CPM.cmake. Will this force users of my library to use CPM as well?
97+
98+
[CPM.cmake](https://github.com/TheLartians/CPM.cmake) should be invisible to library users as it's a self-contained CMake Script.
99+
If problems do arise, users can always opt-out by defining `CPM_USE_LOCAL_PACKAGES`, which will override all calls to `CPMAddPackage` with `find_package`.
100+
Alternatively, you could use `CPMFindPackage` instead of `CPMAddPackage`, which will try to use `find_package` before calling `CPMAddPackage` as a fallback.
101+
Both approaches should be compatible with common C++ package managers without modifications, however come with the cost of reproducible builds.
102+
103+
- Can I configure and build my project offline?
104+
105+
Using CPM, all missing dependencies are downloaded at configure time.
106+
To avoid redundant downloads, it's recommended to set a CPM cache directory, e.g.: `export CPM_SOURCE_CACHE=$HOME/.cache/CPM`.
107+
This will also allow offline configurations if all dependencies are present.
108+
No internet connection is required for building.
109+
110+
- Can I use CPack to create a package installer for my project?
111+
112+
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.
113+
114+
## Coming soon
115+
116+
- Script to automatically adjust the template for new projects

‎cmake/CPM.cmake

+329
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
# TheLartians/CPM - A simple Git dependency manager
2+
# =================================================
3+
# See https://github.com/TheLartians/CPM for usage and update instructions.
4+
#
5+
# MIT License
6+
# -----------
7+
#[[
8+
Copyright (c) 2019 Lars Melchior
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
]]
28+
29+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
30+
31+
set(CURRENT_CPM_VERSION 0.18)
32+
33+
if(CPM_DIRECTORY)
34+
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
35+
if (${CPM_VERSION} VERSION_LESS ${CURRENT_CPM_VERSION})
36+
message(AUTHOR_WARNING "${CPM_INDENT} \
37+
A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \
38+
It is recommended to upgrade CPM to the most recent version. \
39+
See https://github.com/TheLartians/CPM.cmake for more information."
40+
)
41+
endif()
42+
return()
43+
endif()
44+
endif()
45+
46+
option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" $ENV{CPM_USE_LOCAL_PACKAGES})
47+
option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" $ENV{CPM_LOCAL_PACKAGES_ONLY})
48+
option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL})
49+
50+
set(CPM_VERSION ${CURRENT_CPM_VERSION} CACHE INTERNAL "")
51+
set(CPM_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
52+
set(CPM_PACKAGES "" CACHE INTERNAL "")
53+
set(CPM_DRY_RUN OFF CACHE INTERNAL "Don't download or configure dependencies (for testing)")
54+
55+
if(DEFINED ENV{CPM_SOURCE_CACHE})
56+
set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE})
57+
else()
58+
set(CPM_SOURCE_CACHE_DEFAULT OFF)
59+
endif()
60+
61+
set(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE_DEFAULT} CACHE PATH "Directory to downlaod CPM dependencies")
62+
63+
include(FetchContent)
64+
include(CMakeParseArguments)
65+
66+
# Initialize logging prefix
67+
if(NOT CPM_INDENT)
68+
set(CPM_INDENT "CPM:")
69+
endif()
70+
71+
function(cpm_find_package NAME VERSION)
72+
string(REPLACE " " ";" EXTRA_ARGS "${ARGN}")
73+
find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET)
74+
if(${CPM_ARGS_NAME}_FOUND)
75+
message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${${CPM_ARGS_NAME}_VERSION}")
76+
CPMRegisterPackage(${CPM_ARGS_NAME} "${${CPM_ARGS_NAME}_VERSION}")
77+
set(CPM_PACKAGE_FOUND YES PARENT_SCOPE)
78+
else()
79+
set(CPM_PACKAGE_FOUND NO PARENT_SCOPE)
80+
endif()
81+
endfunction()
82+
83+
# Find a package locally or fallback to CPMAddPackage
84+
function(CPMFindPackage)
85+
set(oneValueArgs
86+
NAME
87+
VERSION
88+
FIND_PACKAGE_ARGUMENTS
89+
)
90+
91+
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN})
92+
93+
if (CPM_DOWNLOAD_ALL)
94+
CPMAddPackage(${ARGN})
95+
cpm_export_variables()
96+
return()
97+
endif()
98+
99+
cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS})
100+
101+
if(NOT CPM_PACKAGE_FOUND)
102+
CPMAddPackage(${ARGN})
103+
cpm_export_variables()
104+
endif()
105+
106+
endfunction()
107+
108+
# Download and add a package from source
109+
function(CPMAddPackage)
110+
111+
set(oneValueArgs
112+
NAME
113+
VERSION
114+
GIT_TAG
115+
DOWNLOAD_ONLY
116+
GITHUB_REPOSITORY
117+
GITLAB_REPOSITORY
118+
SOURCE_DIR
119+
DOWNLOAD_COMMAND
120+
FIND_PACKAGE_ARGUMENTS
121+
)
122+
123+
set(multiValueArgs
124+
OPTIONS
125+
)
126+
127+
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}")
128+
129+
if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY)
130+
cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS})
131+
132+
if(CPM_PACKAGE_FOUND)
133+
return()
134+
endif()
135+
136+
if(CPM_LOCAL_PACKAGES_ONLY)
137+
message(SEND_ERROR "CPM: ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})")
138+
endif()
139+
endif()
140+
141+
if (NOT DEFINED CPM_ARGS_VERSION)
142+
if (DEFINED CPM_ARGS_GIT_TAG)
143+
cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION)
144+
endif()
145+
if (NOT DEFINED CPM_ARGS_VERSION)
146+
set(CPM_ARGS_VERSION 0)
147+
endif()
148+
endif()
149+
150+
if (NOT DEFINED CPM_ARGS_GIT_TAG)
151+
set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION})
152+
endif()
153+
154+
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG})
155+
156+
if(CPM_ARGS_DOWNLOAD_ONLY)
157+
set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY})
158+
else()
159+
set(DOWNLOAD_ONLY NO)
160+
endif()
161+
162+
if (CPM_ARGS_GITHUB_REPOSITORY)
163+
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git")
164+
endif()
165+
166+
if (CPM_ARGS_GITLAB_REPOSITORY)
167+
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git")
168+
endif()
169+
170+
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES)
171+
CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION)
172+
if(${CPM_PACKAGE_VERSION} VERSION_LESS ${CPM_ARGS_VERSION})
173+
message(WARNING "${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION}).")
174+
endif()
175+
if (CPM_ARGS_OPTIONS)
176+
foreach(OPTION ${CPM_ARGS_OPTIONS})
177+
cpm_parse_option(${OPTION})
178+
if(NOT "${${OPTION_KEY}}" STREQUAL ${OPTION_VALUE})
179+
message(WARNING "${CPM_INDENT} ignoring package option for ${CPM_ARGS_NAME}: ${OPTION_KEY} = ${OPTION_VALUE} (${${OPTION_KEY}})")
180+
endif()
181+
endforeach()
182+
endif()
183+
cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
184+
cpm_get_fetch_properties(${CPM_ARGS_NAME})
185+
SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}")
186+
SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}")
187+
SET(${CPM_ARGS_NAME}_ADDED NO)
188+
cpm_export_variables()
189+
return()
190+
endif()
191+
192+
CPMRegisterPackage(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})
193+
194+
if (CPM_ARGS_OPTIONS)
195+
foreach(OPTION ${CPM_ARGS_OPTIONS})
196+
cpm_parse_option(${OPTION})
197+
set(${OPTION_KEY} ${OPTION_VALUE} CACHE INTERNAL "")
198+
endforeach()
199+
endif()
200+
201+
set(FETCH_CONTENT_DECLARE_EXTRA_OPTS "")
202+
203+
if (DEFINED CPM_ARGS_GIT_TAG)
204+
set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}")
205+
else()
206+
set(PACKAGE_INFO "${CPM_ARGS_VERSION}")
207+
endif()
208+
209+
if (DEFINED CPM_ARGS_DOWNLOAD_COMMAND)
210+
set(FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND})
211+
elseif(DEFINED CPM_ARGS_SOURCE_DIR)
212+
set(FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR})
213+
elseif (CPM_SOURCE_CACHE)
214+
string(TOLOWER ${CPM_ARGS_NAME} lower_case_name)
215+
set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS})
216+
list(SORT origin_parameters)
217+
string(SHA1 origin_hash "${origin_parameters}")
218+
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash})
219+
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory})
220+
if (EXISTS ${download_directory})
221+
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ":")
222+
set(PACKAGE_INFO "${download_directory}")
223+
else()
224+
# remove timestamps so CMake will re-download the dependency
225+
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild)
226+
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}")
227+
endif()
228+
endif()
229+
230+
cpm_declare_fetch(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${PACKAGE_INFO} "${CPM_ARGS_UNPARSED_ARGUMENTS}" ${FETCH_CONTENT_DECLARE_EXTRA_OPTS})
231+
cpm_fetch_package(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
232+
cpm_get_fetch_properties(${CPM_ARGS_NAME})
233+
SET(${CPM_ARGS_NAME}_ADDED YES)
234+
cpm_export_variables()
235+
endfunction()
236+
237+
# export variables available to the caller to the parent scope
238+
# expects ${CPM_ARGS_NAME} to be set
239+
macro(cpm_export_variables)
240+
SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}" PARENT_SCOPE)
241+
SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" PARENT_SCOPE)
242+
SET(${CPM_ARGS_NAME}_ADDED "${${CPM_ARGS_NAME}_ADDED}" PARENT_SCOPE)
243+
endmacro()
244+
245+
# declares that a package has been added to CPM
246+
function(CPMRegisterPackage PACKAGE VERSION)
247+
list(APPEND CPM_PACKAGES ${PACKAGE})
248+
set(CPM_PACKAGES ${CPM_PACKAGES} CACHE INTERNAL "")
249+
set("CPM_PACKAGE_${PACKAGE}_VERSION" ${VERSION} CACHE INTERNAL "")
250+
endfunction()
251+
252+
# retrieve the current version of the package to ${OUTPUT}
253+
function(CPMGetPackageVersion PACKAGE OUTPUT)
254+
set(${OUTPUT} "${CPM_PACKAGE_${PACKAGE}_VERSION}" PARENT_SCOPE)
255+
endfunction()
256+
257+
# declares a package in FetchContent_Declare
258+
function (cpm_declare_fetch PACKAGE VERSION INFO)
259+
message(STATUS "${CPM_INDENT} adding package ${PACKAGE}@${VERSION} (${INFO})")
260+
261+
if (${CPM_DRY_RUN})
262+
message(STATUS "${CPM_INDENT} package not declared (dry run)")
263+
return()
264+
endif()
265+
266+
FetchContent_Declare(
267+
${PACKAGE}
268+
${ARGN}
269+
)
270+
endfunction()
271+
272+
# returns properties for a package previously defined by cpm_declare_fetch
273+
function (cpm_get_fetch_properties PACKAGE)
274+
if (${CPM_DRY_RUN})
275+
return()
276+
endif()
277+
FetchContent_GetProperties(${PACKAGE})
278+
string(TOLOWER ${PACKAGE} lpackage)
279+
SET(${PACKAGE}_SOURCE_DIR "${${lpackage}_SOURCE_DIR}" PARENT_SCOPE)
280+
SET(${PACKAGE}_BINARY_DIR "${${lpackage}_BINARY_DIR}" PARENT_SCOPE)
281+
endfunction()
282+
283+
# downloads a previously declared package via FetchContent
284+
function (cpm_fetch_package PACKAGE DOWNLOAD_ONLY)
285+
286+
if (${CPM_DRY_RUN})
287+
message(STATUS "${CPM_INDENT} package ${PACKAGE} not fetched (dry run)")
288+
return()
289+
endif()
290+
291+
set(CPM_OLD_INDENT "${CPM_INDENT}")
292+
set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:")
293+
if(${DOWNLOAD_ONLY})
294+
if(NOT "${PACKAGE}_POPULATED")
295+
FetchContent_Populate(${PACKAGE})
296+
endif()
297+
else()
298+
FetchContent_MakeAvailable(${PACKAGE})
299+
endif()
300+
set(CPM_INDENT "${CPM_OLD_INDENT}")
301+
endfunction()
302+
303+
# splits a package option
304+
function(cpm_parse_option OPTION)
305+
string(REGEX MATCH "^[^ ]+" OPTION_KEY ${OPTION})
306+
string(LENGTH ${OPTION} OPTION_LENGTH)
307+
string(LENGTH ${OPTION_KEY} OPTION_KEY_LENGTH)
308+
if (OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH)
309+
# no value for key provided, assume user wants to set option to "ON"
310+
set(OPTION_VALUE "ON")
311+
else()
312+
math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1")
313+
string(SUBSTRING ${OPTION} "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE)
314+
endif()
315+
set(OPTION_KEY "${OPTION_KEY}" PARENT_SCOPE)
316+
set(OPTION_VALUE "${OPTION_VALUE}" PARENT_SCOPE)
317+
endfunction()
318+
319+
# guesses the package version from a git tag
320+
function(cpm_get_version_from_git_tag GIT_TAG RESULT)
321+
string(LENGTH ${GIT_TAG} length)
322+
if (length EQUAL 40)
323+
# GIT_TAG is probably a git hash
324+
SET(${RESULT} 0 PARENT_SCOPE)
325+
else()
326+
string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG})
327+
SET(${RESULT} ${CMAKE_MATCH_1} PARENT_SCOPE)
328+
endif()
329+
endfunction()

‎include/greeter.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace greeter {
6+
7+
enum class LanguageCode { EN, DE, ES, FR };
8+
9+
class Greeter {
10+
std::string name;
11+
12+
public:
13+
Greeter(std::string name);
14+
std::string greet(LanguageCode lang = LanguageCode::EN) const;
15+
};
16+
17+
} // namespace greeter

‎source/greeter.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <greeter.h>
2+
3+
using namespace greeter;
4+
5+
Greeter::Greeter(std::string _name) : name(_name) {}
6+
7+
std::string Greeter::greet(LanguageCode lang) const {
8+
switch (lang) {
9+
#if defined(_WIN32) || defined(WIN32)
10+
// this silences a MSVC warning as it does not seem to understand strongly-typed enums
11+
default:
12+
#endif
13+
case LanguageCode::EN:
14+
return "Hello, " + name + "!";
15+
case LanguageCode::DE:
16+
return "Hallo " + name + "!";
17+
case LanguageCode::ES:
18+
return "¡Hola " + name + "!";
19+
case LanguageCode::FR:
20+
return "Bonjour " + name + "!";
21+
}
22+
}

‎standalone/CMakeLists.txt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2+
3+
project(GreeterStandalone
4+
LANGUAGES CXX
5+
)
6+
7+
# ---- Dependencies ----
8+
9+
include(../cmake/CPM.cmake)
10+
11+
CPMAddPackage(
12+
NAME cxxopts
13+
GITHUB_REPOSITORY jarro2783/cxxopts
14+
VERSION 2.2.0
15+
OPTIONS
16+
"CXXOPTS_BUILD_EXAMPLES Off"
17+
"CXXOPTS_BUILD_TESTS Off"
18+
)
19+
20+
CPMAddPackage(
21+
NAME Greeter
22+
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..
23+
)
24+
25+
# ---- Create standalone executable ----
26+
27+
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
28+
29+
add_executable(GreeterStandalone ${sources})
30+
31+
set_target_properties(GreeterStandalone PROPERTIES
32+
CXX_STANDARD 17
33+
COMPILE_FLAGS "-Wall -pedantic -Wextra"
34+
OUTPUT_NAME "Greeter"
35+
)
36+
37+
target_link_libraries(GreeterStandalone Greeter cxxopts)

‎standalone/source/main.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <greeter.h>
2+
3+
#include <cxxopts.hpp>
4+
#include <iostream>
5+
#include <string>
6+
#include <unordered_map>
7+
8+
const std::unordered_map<std::string, greeter::LanguageCode> languages{
9+
{"en", greeter::LanguageCode::EN},
10+
{"de", greeter::LanguageCode::DE},
11+
{"es", greeter::LanguageCode::ES},
12+
{"fr", greeter::LanguageCode::FR},
13+
};
14+
15+
int main(int argc, char** argv) {
16+
cxxopts::Options options(argv[0], "A program to welcome the world!");
17+
18+
std::string language;
19+
std::string name;
20+
21+
// clang-format off
22+
options.add_options()
23+
("h,help", "Show help")
24+
("n,name", "Name to greet", cxxopts::value(name)->default_value("World"))
25+
("l,lang", "Language code to use", cxxopts::value(language)->default_value("en"))
26+
;
27+
// clang-format on
28+
29+
auto result = options.parse(argc, argv);
30+
31+
if (result["help"].as<bool>()) {
32+
std::cout << options.help() << std::endl;
33+
return 0;
34+
}
35+
36+
auto langIt = languages.find(language);
37+
if (langIt == languages.end()) {
38+
std::cout << "unknown language code: " << language << std::endl;
39+
return 1;
40+
}
41+
42+
greeter::Greeter greeter(name);
43+
std::cout << greeter.greet(langIt->second) << std::endl;
44+
45+
return 0;
46+
}

‎test/CMakeLists.txt

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2+
3+
project(GreeterTests
4+
LANGUAGES CXX
5+
)
6+
7+
# ---- Options ----
8+
9+
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
10+
option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF)
11+
12+
# ---- Dependencies ----
13+
14+
include(../cmake/CPM.cmake)
15+
16+
CPMAddPackage(
17+
NAME doctest
18+
GITHUB_REPOSITORY onqtam/doctest
19+
GIT_TAG 2.3.7
20+
)
21+
22+
if (TEST_INSTALLED_VERSION)
23+
find_package(Greeter REQUIRED)
24+
else()
25+
CPMAddPackage(
26+
NAME Greeter
27+
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..
28+
)
29+
endif()
30+
31+
CPMAddPackage(
32+
NAME Format.cmake
33+
GITHUB_REPOSITORY TheLartians/Format.cmake
34+
VERSION 1.0
35+
)
36+
37+
# ---- Create binary ----
38+
39+
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
40+
add_executable(GreeterTests ${sources})
41+
target_link_libraries(GreeterTests doctest Greeter)
42+
43+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
44+
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
45+
else()
46+
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
47+
endif()
48+
49+
# ---- Add GreeterTests ----
50+
51+
ENABLE_TESTING()
52+
53+
# use doctest_discover_tests for better CTest support
54+
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
55+
doctest_discover_tests(GreeterTests)
56+
57+
# Note: for general testing frameworks use:
58+
# ADD_TEST(GreeterTests GreeterTests)
59+
60+
# ---- code coverage ----
61+
62+
if (ENABLE_TEST_COVERAGE)
63+
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
64+
target_link_options(Greeter PUBLIC "--coverage")
65+
endif()

‎test/source/greeter.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <doctest/doctest.h>
2+
#include <greeter.h>
3+
4+
#if defined(_WIN32) || defined(WIN32)
5+
// apparently this is required to compile in MSVC++
6+
# include <sstream>
7+
#endif
8+
9+
TEST_CASE("Greeter") {
10+
using namespace greeter;
11+
12+
Greeter greeter("World");
13+
14+
CHECK(greeter.greet(LanguageCode::EN) == "Hello, World!");
15+
CHECK(greeter.greet(LanguageCode::DE) == "Hallo World!");
16+
CHECK(greeter.greet(LanguageCode::ES) == "¡Hola World!");
17+
CHECK(greeter.greet(LanguageCode::FR) == "Bonjour World!");
18+
}

‎test/source/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2+
3+
#include <doctest/doctest.h>

0 commit comments

Comments
 (0)
Please sign in to comment.