Skip to content

Commit e27cc04

Browse files
committed
Initial Setup for Testing
Support for * Windows Visual Studio 2019 / 2022 * Linux GCC and * Docker Container with GCC-11
0 parents  commit e27cc04

22 files changed

Lines changed: 469 additions & 0 deletions

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
ColumnLimit: 120
5+
BreakConstructorInitializersBeforeComma: true

.editorconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
###############################
2+
# Core EditorConfig Options #
3+
###############################
4+
root = true
5+
# All files
6+
[*]
7+
indent_style = space
8+
9+
# XML project files
10+
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
11+
indent_size = 2
12+
13+
# XML config files
14+
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
15+
indent_size = 2
16+
17+
# Code files
18+
[*.{cpp,json,cmake}]
19+
indent_size = 2
20+
insert_final_newline = true
21+
trim_trailing_whitespace = true
22+
23+
[CMakeLists.txt]
24+
indent_size = 2
25+
insert_final_newline = true
26+
trim_trailing_whitespace = true

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
out/
2+
.vscode/
3+
.vs/
4+
*.user
5+
CMakeUserPresets.json

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "third_party/googletest"]
2+
path = third_party/googletest
3+
url = https://github.com/google/googletest.git

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
project(BaseSetup
3+
VERSION 1.0.0
4+
DESCRIPTION "Basic Setup with Googletest"
5+
LANGUAGES CXX
6+
)
7+
8+
enable_testing()
9+
set(CMAKE_CXX_STANDARD 17)
10+
11+
add_subdirectory(third_party)
12+
add_subdirectory(src)

CMakePresets.json

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 20,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "multi-config",
11+
"hidden": true,
12+
"generator": "Ninja Multi-Config",
13+
"binaryDir": "${sourceDir}/out/build/${presetName}",
14+
"cacheVariables": {
15+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
16+
"CMAKE_CONFIGURATION_TYPES": "Debug;Release"
17+
}
18+
},
19+
{
20+
"name": "msvc-cl16",
21+
"inherits": "multi-config",
22+
"displayName": "Ninja Visual C++ 16",
23+
"description": "Recommended configuration for windows builds",
24+
"cacheVariables": {
25+
"CMAKE_CXX_FLAGS": "/EHsc /permissive- /Zc:__cplusplus /Zc:externConstexpr /Zc:inline /Zc:preprocessor /Zc:throwingNew /diagnostics:caret /experimental:external /external:anglebrackets /external:W0 /D_ENABLE_EXTENDED_ALIGNED_STORAGE"
26+
}
27+
},
28+
{
29+
"name": "gcc",
30+
"inherits": "multi-config",
31+
"displayName": "G++",
32+
"description": "Recommended configuration for GNU GCC",
33+
"cacheVariables": {
34+
"CMAKE_CXX_FLAGS": "-fsized-deallocation -Wall -Wextra -Werror --pedantic -ftemplate-backtrace-limit=0"
35+
}
36+
},
37+
{
38+
"name": "msbuild-vs16-x64",
39+
"displayName": "MSBuild cl16 x64",
40+
"description": "Fallback solution for Visual Studio 16 2019 (x64)",
41+
"generator": "Visual Studio 16 2019",
42+
"toolset": "host=x64",
43+
"architecture": "x64",
44+
"binaryDir": "${sourceDir}/out/build/${presetName}",
45+
"cacheVariables": {
46+
"CMAKE_CXX_FLAGS": "/EHsc /permissive- /Zc:__cplusplus /Zc:externConstexpr /Zc:inline /Zc:preprocessor /Zc:throwingNew /diagnostics:caret /experimental:external /external:anglebrackets /external:W0 /D_ENABLE_EXTENDED_ALIGNED_STORAGE"
47+
}
48+
}
49+
],
50+
"buildPresets": [
51+
{
52+
"name": "msvc-cl16-debug",
53+
"displayName": "Debug",
54+
"configurePreset": "msvc-cl16",
55+
"configuration": "Debug"
56+
},
57+
{
58+
"name": "msvc-cl16-release",
59+
"displayName": "Release",
60+
"configurePreset": "msvc-cl16",
61+
"configuration": "Release"
62+
},
63+
{
64+
"name": "gcc-debug",
65+
"displayName": "Debug",
66+
"configurePreset": "gcc",
67+
"configuration": "Debug"
68+
},
69+
{
70+
"name": "gcc-release",
71+
"displayName": "Release",
72+
"configurePreset": "gcc",
73+
"configuration": "Release"
74+
},
75+
{
76+
"name": "msbuild-vs16-x64-debug",
77+
"displayName": "Debug",
78+
"configurePreset": "msbuild-vs16-x64",
79+
"configuration": "Debug"
80+
},
81+
{
82+
"name": "msbuild-vs16-x64-release",
83+
"displayName": "Release",
84+
"configurePreset": "msbuild-vs16-x64",
85+
"configuration": "Release"
86+
}
87+
],
88+
"testPresets": [
89+
{
90+
"name": "test-base",
91+
"hidden": true,
92+
"output": {
93+
"outputOnFailure": true,
94+
"debug": false
95+
},
96+
"execution": {
97+
"noTestsAction": "error",
98+
"stopOnFailure": true
99+
}
100+
},
101+
{
102+
"name": "test-msvc-cl16-debug",
103+
"inherits": "test-base",
104+
"displayName": "Test Debug",
105+
"configurePreset": "msvc-cl16",
106+
"configuration": "Debug"
107+
},
108+
{
109+
"name": "test-msvc-cl16-release",
110+
"inherits": "test-base",
111+
"displayName": "Test Release",
112+
"configurePreset": "msvc-cl16",
113+
"configuration": "Release"
114+
},
115+
{
116+
"name": "test-gcc-debug",
117+
"inherits": "test-base",
118+
"displayName": "Test Debug",
119+
"configurePreset": "gcc",
120+
"configuration": "Debug"
121+
},
122+
{
123+
"name": "test-gcc-release",
124+
"inherits": "test-base",
125+
"displayName": "Test Release",
126+
"configurePreset": "gcc",
127+
"configuration": "Release"
128+
}
129+
]
130+
}

Readme.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# CMake Test Setup
2+
3+
Basic project setup for C++17 with CMake and googletest.
4+
5+
You should successfully checkout, setup and test before the training.
6+
7+
## Checkout with submodules
8+
9+
```
10+
git clone --recurse-submodules https://github.com/cleancode-cpp/cmake-testsetup.git
11+
```
12+
13+
If you forgot to use the submodules option on clone you can do
14+
15+
```
16+
git submodules --init
17+
git submodules --update
18+
```
19+
20+
## Validated build and test environments
21+
22+
### Windows Visual Studio 2019 / 2022
23+
24+
* Please update to the latest version (16.11.7)
25+
* Make sure "Desktop development with C++" is selected in the installer
26+
27+
First build:
28+
* Open the folder in Visual Studio
29+
* Select the "msvc-cl16" preset. The "gcc" default will not work
30+
* CMake generator should run fine
31+
* Build the "CMakeLists.txt" on the top folder
32+
33+
Notes:
34+
* The Visual Studio Test Explorer only works in Visual Studio 2022
35+
* You can only run debug builds from Visual Studio
36+
* The "msbuild-vs16-x64" preset also works but compiles slower.
37+
38+
### Windows VSCode
39+
40+
* Install Visual Studio like above
41+
* Install CMake (>= v3.20)
42+
* Install VSCode CMake Tools extension
43+
* Configure the VSCode extension to use the installed CMake tool
44+
45+
First build:
46+
* Open the folder in VSCode
47+
* Select Configuraton "msbuild-vs16-x64"
48+
* Build
49+
50+
Notes:
51+
* Ninja preset "msvc-cl16" only work if Windows SDK is in PATH, otherwise CMake will complain that kernel32 could not be linked.
52+
53+
### Linux GCC
54+
55+
* Install a recent GCC with libstdc++
56+
* Install CMake (>= v3.20)
57+
* Use the editor of your choice (none were validated)
58+
59+
First build:
60+
* run build `script/build_cmake.sh`
61+
* run tests `script/test_cmake.sh`
62+
63+
Notes:
64+
* GCC version 10 or 11 should work fine
65+
66+
### Docker Container with GCC
67+
68+
* Install a Docker / Container runner for your system
69+
* Docker Desktop and CLI work great (check license)
70+
* Open source alternatives like `nerdctl` or `podman` also work fine
71+
* Use the editor of your choice (none were validated)
72+
73+
First build:
74+
* run shell in container `script/docker_run_cmake_gcc.sh` or `script\docker_run_cmake_gcc.bat`
75+
* run build `script/build_cmake.sh`
76+
* run tests `script/test_cmake.sh`
77+
78+
## Contributions are welcome
79+
80+
If you like the project feel free to leave a star.
81+
If you have issues with your setup, try to find a colleage to help you out.
82+
If something is wrong with this project or you think I might be able to help, feel free to open an issue.

script/build_cmake.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
set -e # stop on first error
3+
4+
# default arguments:
5+
# script/test_cmake.sh gcc debug
6+
ConfigPreset=${1:-gcc}
7+
Config=${2:-debug}
8+
BuildPreset="${ConfigPreset}-${Config}"
9+
TestPreset="test-${BuildPreset}"
10+
11+
BUILD_PATH="out/build/${ConfigPreset}"
12+
13+
pushd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null
14+
15+
if [ ! -f "${BUILD_PATH}/compile_commands.json" ] ; then
16+
cmake -S . --preset "${ConfigPreset}"
17+
fi
18+
cmake --build --preset "${BuildPreset}"
19+
20+
popd >/dev/null

script/docker_run_cmake_gcc.bat

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@echo off
2+
3+
set "PROJECT=basesetup"
4+
set "IMAGE=arbmind/cmake-gcc11:latest"
5+
set "BUILD_VOLUME=%PROJECT%-cmake-build"
6+
7+
pushd "%~dp0.."
8+
docker run -it --rm --mount src="%cd%",target=/project,type=bind -w /project --mount src=%BUILD_VOLUME%,target=/project/out/build,type=volume %IMAGE% %*
9+
popd

script/docker_run_cmake_gcc.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@echo off
2+
3+
PROJECT=basesetup
4+
IMAGE="arbmind/cmake-gcc11:latest"
5+
BUILD_VOLUME="${PROJECT}-cmake-build"
6+
7+
pushd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null
8+
9+
docker run -it --rm --mount src="$(pwd)",target=/project,type=bind -w /project --mount src=${BUILD_VOLUME},target=/project/out/build,type=volume ${IMAGE} $*
10+
11+
popd >/dev/null

0 commit comments

Comments
 (0)