From a01406f7c4262f576ccd52e8b1007a6eba6415af Mon Sep 17 00:00:00 2001 From: friendlyanon Date: Wed, 15 Dec 2021 17:02:19 +0100 Subject: [PATCH] Use presets version 2 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 --- README.md | 2 +- cmake-init/cmake_init.py | 11 +++--- cmake-init/templates/common/CMakePresets.json | 2 +- .../templates/common/CMakeUserPresets.json | 23 +++++++++++- cmake-init/templates/common/HACKING.md | 36 +++++++++++++------ 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c4cb114..f6752a3 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/cmake-init/cmake_init.py b/cmake-init/cmake_init.py index 861f5c7..864749c 100644 --- a/cmake-init/cmake_init.py +++ b/cmake-init/cmake_init.py @@ -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": @@ -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"]: @@ -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 + cmake --build --preset=dev -t """) @@ -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/ diff --git a/cmake-init/templates/common/CMakePresets.json b/cmake-init/templates/common/CMakePresets.json index 1a1a60c..0bf3a80 100644 --- a/cmake-init/templates/common/CMakePresets.json +++ b/cmake-init/templates/common/CMakePresets.json @@ -1,5 +1,5 @@ { - "version": 1, + "version": 2, "cmakeMinimumRequired": { "major": 3, "minor": 14, diff --git a/cmake-init/templates/common/CMakeUserPresets.json b/cmake-init/templates/common/CMakeUserPresets.json index ad0c9e2..f3d27cb 100644 --- a/cmake-init/templates/common/CMakeUserPresets.json +++ b/cmake-init/templates/common/CMakeUserPresets.json @@ -1,5 +1,5 @@ { - "version": 1, + "version": 2, "cmakeMinimumRequired": { "major": 3, "minor": 14, @@ -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 + } + } ] } diff --git a/cmake-init/templates/common/HACKING.md b/cmake-init/templates/common/HACKING.md index 48a63bd..342a9b6 100644 --- a/cmake-init/templates/common/HACKING.md +++ b/cmake-init/templates/common/HACKING.md @@ -31,7 +31,7 @@ the project: ```json { - "version": 1, + "version": 2, "cmakeMinimumRequired": { "major": 3, "minor": 14, @@ -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 + } + } ] } ``` @@ -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/