From 067c95efbcd2b3940e241e1406f10caec7c6ecb6 Mon Sep 17 00:00:00 2001 From: friendlyanon Date: Sun, 2 May 2021 12:34:20 +0200 Subject: [PATCH] Add cppcheck integration --- .github/workflows/ci.yml | 4 ++-- README.md | 20 ++++++++++++++++++- cmake-init/__main__.py | 16 ++++++++++++++- .../templates/common/.github/workflows/ci.yml | 4 ++-- cmake-init/templates/common/CMakePresets.json | 9 ++++++++- .../templates/common/CMakeUserPresets.json | 2 +- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bcac09c..78b49cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,9 +41,9 @@ jobs: steps: - uses: actions/checkout@v1 - - name: Install clang-tidy + - name: Install static analyzers if: matrix.os == 'ubuntu' - run: sudo apt-get install clang-tidy -y -q + run: sudo apt-get install clang-tidy cppcheck -y -q - uses: actions/setup-python@v2 with: { python-version: "3.8" } diff --git a/README.md b/README.md index b230555..765d03d 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ Make sure you have these programs installed: * Python 3.8 or newer * CMake 3.19 or newer * git -* clang-tidy (optional, should be available in PATH as `clang-tidy`) +* [clang-tidy](#clang-tidy) (optional) +* [cppcheck](#cppcheck) (optional) `cmake-init` consists of a single file that can be run using Python. Python was chosen for this, because it is cross-platform, convenient for this use-case and @@ -91,6 +92,21 @@ and use it locally, but it is recommended. reason for this is that only [Makefiles and Ninja][7] are supported with CMake for use with clang-tidy. For other generators, this feature is a no-op. +### cppcheck + +[cppcheck][8] is a static analysis tool similar to clang-tidy, however the +overlap in what they detect is minimal, so it's beneficial to use both of them. +This script gives you the option to inherit the `cppcheck` preset in your `dev` +preset, enabling the CMake integration for this tool. + +CI will always run cppcheck for you, so it is entirely optional to install and +use it locally, but it is recommended. + +**For Windows users**, if you wish to use cppcheck, then you must install +[Ninja][6] and set the `generator` field in your `dev` preset to `Ninja`. The +reason for this is that only [Makefiles and Ninja][9] are supported with CMake +for use with cppcheck. For other generators, this feature is a no-op. + ## Usage * `cmake-init ` @@ -124,3 +140,5 @@ indirectly from the use or non-use of these files. [5]: https://clang.llvm.org/extra/clang-tidy/ [6]: https://github.com/ninja-build/ninja [7]: https://cmake.org/cmake/help/latest/prop_tgt/LANG_CLANG_TIDY.html +[8]: http://cppcheck.sourceforge.net/ +[9]: https://cmake.org/cmake/help/latest/prop_tgt/LANG_CPPCHECK.html diff --git a/cmake-init/__main__.py b/cmake-init/__main__.py index 3226a2c..3b598f7 100644 --- a/cmake-init/__main__.py +++ b/cmake-init/__main__.py @@ -124,6 +124,13 @@ def ask(*args, **kwargs): predicate=lambda v: v in ["y", "n"], header="This will require you to download clang-tidy locally.", ) == "y", + "use_cppcheck": ask( + "Add cppcheck to local dev preset ([Y]es/[n]o)", + cli_args.use_cppcheck or "y", + mapper=lambda v: v[0:1].lower(), + predicate=lambda v: v in ["y", "n"], + header="This will require you to download cppcheck locally.", + ) == "y", "examples": False, "os": "win64" if is_windows else "unix", } @@ -280,7 +287,7 @@ def main(): type=os.path.realpath, help="path to generate to, the name is also derived from this", ) - create_flags = ["type_id", "std", "use_clang_tidy"] + create_flags = ["type_id", "std", "use_clang_tidy", "use_cppcheck"] p.set_defaults(**{k: "" for k in create_flags}) type_g = p.add_mutually_exclusive_group() mapping = { @@ -308,6 +315,13 @@ def main(): const="n", help="omit the clang-tidy preset from the dev preset", ) + p.add_argument( + "--no-cppcheck", + action="store_const", + dest="use_cppcheck", + const="n", + help="omit the cppcheck preset from the dev preset", + ) args = p.parse_args() flags_used = any(getattr(args, k) != "" for k in create_flags) setattr(args, "flags_used", flags_used) diff --git a/cmake-init/templates/common/.github/workflows/ci.yml b/cmake-init/templates/common/.github/workflows/ci.yml index cd46f9c..dbc0b57 100644 --- a/cmake-init/templates/common/.github/workflows/ci.yml +++ b/cmake-init/templates/common/.github/workflows/ci.yml @@ -61,9 +61,9 @@ jobs: steps: - uses: actions/checkout@v1 - - name: Install clang-tidy + - name: Install static analyzers if: matrix.os == 'ubuntu' - run: sudo apt-get install clang-tidy -y -q + run: sudo apt-get install clang-tidy cppcheck -y -q - name: Configure run: cmake --preset=ci-${{ matrix.os }}{type shared} diff --git a/cmake-init/templates/common/CMakePresets.json b/cmake-init/templates/common/CMakePresets.json index ac8e0b7..00c9b4d 100644 --- a/cmake-init/templates/common/CMakePresets.json +++ b/cmake-init/templates/common/CMakePresets.json @@ -6,6 +6,13 @@ "patch": 0 }, "configurePresets": [ + { + "name": "cppcheck", + "hidden": true, + "cacheVariables": { + "CMAKE_CXX_CPPCHECK": "cppcheck" + } + }, { "name": "clang-tidy", "hidden": true, @@ -65,7 +72,7 @@ }, { "name": "ci-ubuntu", - "inherits": ["ci-unix", "clang-tidy"] + "inherits": ["ci-unix", "clang-tidy", "cppcheck"] }, { "name": "ci-windows", diff --git a/cmake-init/templates/common/CMakeUserPresets.json b/cmake-init/templates/common/CMakeUserPresets.json index ad2bfd7..3c77163 100644 --- a/cmake-init/templates/common/CMakeUserPresets.json +++ b/cmake-init/templates/common/CMakeUserPresets.json @@ -8,7 +8,7 @@ "configurePresets": [ { "name": "dev", - "inherits": ["ci-%(os)s"{if use_clang_tidy}, "clang-tidy"{end}], + "inherits": ["ci-%(os)s"{if use_clang_tidy}, "clang-tidy"{end}{if use_cppcheck}, "cppcheck"{end}], "cacheVariables": { "%(name)s_DEVELOPER_MODE": "ON" }