Skip to content

Commit

Permalink
Use presets version 2
Browse files Browse the repository at this point in the history
Presets version 2 requires CMake 3.20.

This commit allows users with VSCode to comfortably use the generated
projects with the "CMake Tools" extension out of the box.

Hints printed after project generation and generated documentation have
been adjusted to use the new capabilities of the presets, which results
in simpler workflow for developers regardless of what build system or
operating system they use.

The CI workflows or the checked in presets file have not been further
adjusted, because the additional build and test presets aren't as useful
there. The workflows are already deduplicated and serve as a sort of
preset on their own. Users may add their own if that better suits their
tastes.

Fixes #34
  • Loading branch information
friendlyanon committed Dec 15, 2021
1 parent ba0076b commit a01406f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ libraries! (Part 2)](https://www.youtube.com/watch?v=_5weX5mx8hc)
Make sure you have these programs installed:

* Python 3.8 or newer
* CMake 3.19 or newer
* CMake 3.20 or newer
* git
* [clang-tidy](#clang-tidy) (optional)
* [cppcheck](#cppcheck) (optional)
Expand Down
11 changes: 5 additions & 6 deletions cmake-init/cmake_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def ask(*args, **kwargs):
"c_header": False,
"include_source": False,
"has_source": True,
"cpus": os.cpu_count(),
}
d["uc_name"] = d["name"].upper().replace("-", "_")
if d["type_id"] != "e":
Expand Down Expand Up @@ -268,17 +269,15 @@ def git_init(cwd):

def print_tips(d):
config = " --config Debug" if is_windows else ""
test_cfg = " -C Debug" if is_windows else ""
cpus = os.cpu_count()
print(f"""\
To get you started with the project in developer mode, you may configure,
build, install and test with the following commands from the project directory,
in that order:
cmake --preset=dev
cmake --build build/dev{config} -j {cpus}
cmake --build --preset=dev
cmake --install build/dev{config} --prefix prefix
cd build/dev && ctest{test_cfg} -j {cpus} --output-on-failure
ctest --preset=dev
""")
extra = [" docs - build the documentation using Doxygen and m.css"]
if d["c_examples"] or d["cpp_examples"]:
Expand All @@ -296,7 +295,7 @@ def print_tips(d):
These targets are only available in developer mode, because they are generally
not useful for consumers. You can run these targets with the following command:
cmake --build build/dev{config} -t <target>
cmake --build --preset=dev -t <target>
""")


Expand Down Expand Up @@ -330,7 +329,7 @@ def create(args, zip):
git_init(path)
print_tips(d)
print("""\
Now make sure you have at least CMake 3.19 installed for local development, to
Now make sure you have at least CMake 3.20 installed for local development, to
make use of all the nice Quality-of-Life improvements in newer releases:
https://cmake.org/download/
Expand Down
2 changes: 1 addition & 1 deletion cmake-init/templates/common/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1,
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 14,
Expand Down
23 changes: 22 additions & 1 deletion cmake-init/templates/common/CMakeUserPresets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1,
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 14,
Expand Down Expand Up @@ -37,5 +37,26 @@
"binaryDir": "${sourceDir}/build/coverage",
"inherits": ["dev-mode", "coverage-unix"]
}
],
"buildPresets": [
{
"name": "dev",
"configurePreset": "dev",
"configuration": "Debug",
"jobs": %(cpus)s
}
],
"testPresets": [
{
"name": "dev",
"configurePreset": "dev",
"configuration": "Debug",
"output": {
"outputOnFailure": true
},
"execution": {
"jobs": %(cpus)s
}
}
]
}
36 changes: 25 additions & 11 deletions cmake-init/templates/common/HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ the project:

```json
{
"version": 1,
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 14,
Expand All @@ -46,6 +46,23 @@ the project:
"CMAKE_BUILD_TYPE": "Debug"
}
}
],
"buildPresets": [
{
"name": "dev",
"configurePreset": "dev",
"configuration": "Debug"
}
],
"testPresets": [
{
"name": "dev",
"configurePreset": "dev",
"configuration": "Debug",
"output": {
"outputOnFailure": true
}
}
]
}
```
Expand All @@ -62,21 +79,18 @@ in the terminal.

If you followed the above instructions, then you can configure, build and test
the project respectively with the following commands from the project root on
Windows:
any operating system with any build system:

```sh
cmake --preset=dev
cmake --build build/dev --config Debug
cd build/dev && ctest -C Debug
cmake --build --preset=dev
ctest --preset=dev
```

And here is the same on a Unix based system (Linux, macOS):

```sh
cmake --preset=dev
cmake --build build/dev
cd build/dev && ctest
```
Please note that both the build and test command accepts a `-j` flag to specify
the number of jobs to use, which should ideally be specified to the number of
threads your CPU has. You may also want to add that to your preset using the
`jobs` property, see the [presets documentation][1] for more details.

[1]: https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html
[2]: https://cmake.org/download/

0 comments on commit a01406f

Please sign in to comment.