cmake-pre-commit-hooks 1.10.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cmake_pc_hooks/__init__.py +15 -0
- cmake_pc_hooks/_argparse.py +332 -0
- cmake_pc_hooks/_call_process.py +74 -0
- cmake_pc_hooks/_cmake.py +659 -0
- cmake_pc_hooks/_utils.py +242 -0
- cmake_pc_hooks/clang_format.py +59 -0
- cmake_pc_hooks/clang_tidy.py +82 -0
- cmake_pc_hooks/cppcheck.py +87 -0
- cmake_pc_hooks/cpplint.py +20 -0
- cmake_pc_hooks/include_what_you_use.py +141 -0
- cmake_pc_hooks/lizard.py +72 -0
- cmake_pre_commit_hooks-1.10.0.dist-info/METADATA +396 -0
- cmake_pre_commit_hooks-1.10.0.dist-info/RECORD +17 -0
- cmake_pre_commit_hooks-1.10.0.dist-info/WHEEL +5 -0
- cmake_pre_commit_hooks-1.10.0.dist-info/entry_points.txt +7 -0
- cmake_pre_commit_hooks-1.10.0.dist-info/licenses/LICENSE +201 -0
- cmake_pre_commit_hooks-1.10.0.dist-info/top_level.txt +1 -0
cmake_pc_hooks/lizard.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Copyright 2021 Damien Nguyen
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
"""Wrapper script for lizard."""
|
|
17
|
+
|
|
18
|
+
import sys
|
|
19
|
+
|
|
20
|
+
from ._utils import StaticAnalyzerCmd, _read_compile_commands_json
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class LizardCmd(StaticAnalyzerCmd):
|
|
24
|
+
"""Class for the lizard command."""
|
|
25
|
+
|
|
26
|
+
command = 'lizard'
|
|
27
|
+
lookbehind = ''
|
|
28
|
+
|
|
29
|
+
def __init__(self, args):
|
|
30
|
+
"""Initialize a LizardCmd object."""
|
|
31
|
+
super().__init__(self.command, self.lookbehind, args)
|
|
32
|
+
self.parse_args(args)
|
|
33
|
+
|
|
34
|
+
def run(self):
|
|
35
|
+
"""Run lizard."""
|
|
36
|
+
if self.read_json_db:
|
|
37
|
+
if not self.cmake.no_cmake_configure:
|
|
38
|
+
self.cmake.configure(self.command, clean_build=self.clean_build)
|
|
39
|
+
|
|
40
|
+
compile_db = self._resolve_compilation_database(self.cmake.build_dir, self.build_dir_list)
|
|
41
|
+
if compile_db:
|
|
42
|
+
self.files.extend(set(_read_compile_commands_json(compile_db)) - set(self.files))
|
|
43
|
+
|
|
44
|
+
if self.all_at_once:
|
|
45
|
+
self.run_command(self.files)
|
|
46
|
+
else:
|
|
47
|
+
for filename in self.files:
|
|
48
|
+
self.run_command([filename])
|
|
49
|
+
|
|
50
|
+
# NB: do not rely on exit_on_error() since it only considers the outcome of the very last call to lizard
|
|
51
|
+
failures = [result for result in self.history if result.returncode != 0]
|
|
52
|
+
if failures:
|
|
53
|
+
for result in failures:
|
|
54
|
+
sys.stderr.write(result.stdout + result.stderr)
|
|
55
|
+
sys.exit(failures[0].returncode)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def main(argv=None):
|
|
59
|
+
"""
|
|
60
|
+
Run command.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
argv (:obj:`list` of :obj:`str`): list of arguments
|
|
64
|
+
"""
|
|
65
|
+
if argv is None:
|
|
66
|
+
argv = sys.argv
|
|
67
|
+
cmd = LizardCmd(argv)
|
|
68
|
+
cmd.run()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if __name__ == '__main__': # pragma: nocover
|
|
72
|
+
main()
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cmake_pre_commit_hooks
|
|
3
|
+
Version: 1.10.0
|
|
4
|
+
Summary: pre-commit hooks for CMake-based projects
|
|
5
|
+
Author-email: Damien Nguyen <ngn.damien@gmail.com>
|
|
6
|
+
License: Apache2
|
|
7
|
+
Project-URL: Homepage, https://github.com/Takishima/cmake-pre-commit-hooks
|
|
8
|
+
Project-URL: Pypi, https://pypi.org/project/cmake-pre-commit-hooks/
|
|
9
|
+
Project-URL: Repository, https://github.com/Takishima/cmake-pre-commit-hooks
|
|
10
|
+
Project-URL: Issues, https://github.com/Takishima/cmake-pre-commit-hooks/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/Takishima/cmake-pre-commit-hooks/blob/main/CHANGELOG.md
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: toml
|
|
25
|
+
Requires-Dist: CLinters>=1.3.0
|
|
26
|
+
Requires-Dist: fasteners
|
|
27
|
+
Requires-Dist: attrs>=23
|
|
28
|
+
Requires-Dist: lizard
|
|
29
|
+
Provides-Extra: clang-format
|
|
30
|
+
Requires-Dist: clang-format; extra == "clang-format"
|
|
31
|
+
Provides-Extra: clang-tidy
|
|
32
|
+
Requires-Dist: clang-tidy; extra == "clang-tidy"
|
|
33
|
+
Provides-Extra: lizard
|
|
34
|
+
Requires-Dist: lizard; extra == "lizard"
|
|
35
|
+
Provides-Extra: test
|
|
36
|
+
Requires-Dist: pytest; extra == "test"
|
|
37
|
+
Requires-Dist: pytest-cov; extra == "test"
|
|
38
|
+
Requires-Dist: pytest-mock; extra == "test"
|
|
39
|
+
Requires-Dist: pytest-xdist; extra == "test"
|
|
40
|
+
Requires-Dist: mock; extra == "test"
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
Dynamic: requires-dist
|
|
43
|
+
|
|
44
|
+
# pre-commit hooks
|
|
45
|
+
|
|
46
|
+
 [](https://badge.fury.io/py/cmake-pre-commit-hooks) [](https://github.com/Takishima/cmake-pre-commit-hooks/actions/workflows/ci.yml) [](https://github.com/Takishima/cmake-pre-commit-hooks/actions/workflows/codeql-analysis.yml) [](https://results.pre-commit.ci/latest/github/Takishima/cmake-pre-commit-hooks/main) [](https://www.codefactor.io/repository/github/takishima/cmake-pre-commit-hooks) [](https://sonarcloud.io/summary/new_code?id=Takishima_cmake-pre-commit-hooks)[](https://app.codacy.com/gh/Takishima/cmake-pre-commit-hooks?utm_source=github.com\&utm_medium=referral\&utm_content=Takishima/cmake-pre-commit-hooks\&utm_campaign=Badge_Grade)
|
|
47
|
+
|
|
48
|
+
This is a [pre-commit](https://pre-commit.com) hooks repo that integrates C/C++ linters/formatters to work with
|
|
49
|
+
CMake-based projects.
|
|
50
|
+
|
|
51
|
+
> [clang-format](https://clang.llvm.org/docs/ClangFormatStyleOptions.html),
|
|
52
|
+
> [clang-tidy](https://clang.llvm.org/extra/clang-tidy/),
|
|
53
|
+
> [cppcheck](http://cppcheck.sourceforge.net/),
|
|
54
|
+
> [cpplint](https://github.com/cpplint/cpplint),
|
|
55
|
+
> [lizard](http://www.lizard.ws) and
|
|
56
|
+
> [iwyu](https://include-what-you-use.org/)
|
|
57
|
+
|
|
58
|
+
It is largely based on the work found [here](https://github.com/pocc/pre-commit-hooks). The main difference with POCC's
|
|
59
|
+
pre-commit hooks is that the ones from this repository will do a CMake configuration step prior to running any
|
|
60
|
+
pre-commit hooks. This is done in order to have CMake generate the compilation database file that can then be used by
|
|
61
|
+
the various hooks (using the `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON` CMake option).
|
|
62
|
+
|
|
63
|
+
This repository is only has Python-based pre-commit hooks.
|
|
64
|
+
|
|
65
|
+
## Current known issues
|
|
66
|
+
|
|
67
|
+
1. Currently, the hooks that depend on having a compilation database generated by CMake (e.g. `clang-tidy`, `cppcheck`)
|
|
68
|
+
are not working on Windows if you are not using the `Ninja` or `Makefile` generators.
|
|
69
|
+
|
|
70
|
+
2. Currently, arguments set in a TOML configuration file (`pyproject.toml`, `cmake_pc_hooks.toml` or else) are applied
|
|
71
|
+
to all hooks. Future improvements may allow to customize arguments on a per-hook basis.
|
|
72
|
+
|
|
73
|
+
## Example usage
|
|
74
|
+
|
|
75
|
+
Assuming that you have the following directory structure for your projects
|
|
76
|
+
|
|
77
|
+
```text
|
|
78
|
+
root
|
|
79
|
+
├── .pre-commit-config.yaml
|
|
80
|
+
├── CMakeLists.txt
|
|
81
|
+
└── src
|
|
82
|
+
└── err.cpp
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
with the following file contents:
|
|
86
|
+
|
|
87
|
+
**.pre-commit-config.yaml**
|
|
88
|
+
|
|
89
|
+
```yaml
|
|
90
|
+
repos:
|
|
91
|
+
- repo: https://github.com/Takishima/cmake-pre-commit-hooks
|
|
92
|
+
rev: v1.8.0
|
|
93
|
+
hooks:
|
|
94
|
+
- id: clang-format
|
|
95
|
+
- id: clang-tidy
|
|
96
|
+
args: [--checks=readability-magic-numbers,--warnings-as-errors=*]
|
|
97
|
+
- id: cppcheck
|
|
98
|
+
- id: include-what-you-use
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**CMakeLists.txt**
|
|
102
|
+
|
|
103
|
+
```cmake
|
|
104
|
+
cmake_minimum_required(VERSION 3.15)
|
|
105
|
+
project(LANGUAGE CXX)
|
|
106
|
+
add_library(mylib STATIC src/err.cpp)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**src/err.cpp**
|
|
110
|
+
|
|
111
|
+
```cpp
|
|
112
|
+
#include <string>
|
|
113
|
+
int main() { int i; return 10; }
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Running pre-commit on the above project will lead to an output similar to this one:
|
|
117
|
+
|
|
118
|
+
```text
|
|
119
|
+
$ pre-commit run --all-files
|
|
120
|
+
clang-format.............................................................Failed
|
|
121
|
+
- hook id: clang-format
|
|
122
|
+
- exit code: 1
|
|
123
|
+
|
|
124
|
+
src/err.cpp
|
|
125
|
+
====================
|
|
126
|
+
< int main() { int i; return 10; }
|
|
127
|
+
---
|
|
128
|
+
> int main() {
|
|
129
|
+
> int i;
|
|
130
|
+
> return 10;
|
|
131
|
+
> }
|
|
132
|
+
|
|
133
|
+
clang-tidy...............................................................Failed
|
|
134
|
+
- hook id: clang-tidy
|
|
135
|
+
- exit code: 1
|
|
136
|
+
|
|
137
|
+
/tmp/temp/src/err.cpp:2:28: error: 10 is a magic number; consider replacing it with a named constant [readability-magic-numbers,-warnings-as-errors]
|
|
138
|
+
int main() { int i; return 10; }
|
|
139
|
+
^
|
|
140
|
+
|
|
141
|
+
cppcheck.................................................................Failed
|
|
142
|
+
- hook id: cppcheck
|
|
143
|
+
- exit code: 1
|
|
144
|
+
|
|
145
|
+
/tmp/temp/src/err.cpp:2:18: style: Unused variable: i [unusedVariable]
|
|
146
|
+
int main() { int i; return 10; }
|
|
147
|
+
^
|
|
148
|
+
include-what-you-use.....................................................Failed
|
|
149
|
+
- hook id: include-what-you-use
|
|
150
|
+
- exit code: 1
|
|
151
|
+
|
|
152
|
+
Problem with /usr/local/bin/iwyu_tool.py: Include-What-You-Use violations found
|
|
153
|
+
|
|
154
|
+
/tmp/temp/src/err.cpp should add these lines:
|
|
155
|
+
/tmp/temp/src/err.cpp should remove these lines:
|
|
156
|
+
- #include <string> // lines 1-1
|
|
157
|
+
The full include-list for /tmp/temp/src/err.cpp:
|
|
158
|
+
---
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Note that your mileage may vary depending on the version of the tools. The example above was generated using
|
|
162
|
+
`clang-format` 12.0.0, `clang-tidy` 12.0.0, `cppcheck` 2.4.1 and `include-what-you-use` 0.16.
|
|
163
|
+
|
|
164
|
+
## Using the Hooks
|
|
165
|
+
|
|
166
|
+
Python 3.8+ is required to use these hooks as all 3 invoking scripts are written in it. As this is also the minimum
|
|
167
|
+
version of pre-commit, this should not be an issue.
|
|
168
|
+
|
|
169
|
+
Running multiple hooks in parallel is currently supported by using the `fastener` Python package. If the hooks are run
|
|
170
|
+
in parallel, only one of the hooks will run the CMake configure step while the others will simply wait until the call to
|
|
171
|
+
CMake ends to continue. In the case where the hooks are run serially, all the hooks will be running the CMake configure
|
|
172
|
+
step. However, if nothing changed in your CMake configuration, this should not cost too much time.
|
|
173
|
+
|
|
174
|
+
### Installation
|
|
175
|
+
|
|
176
|
+
For installing the various utilities, refer to your package manager documentation. Some guidance can also be found
|
|
177
|
+
[here](https://github.com/pocc/pre-commit-hooks#installation).
|
|
178
|
+
|
|
179
|
+
### Hook Info
|
|
180
|
+
|
|
181
|
+
| Hook Info | Type | Languages |
|
|
182
|
+
|--------------------------|--------------------------|------------------|
|
|
183
|
+
| [clang-format][] | Formatter | C, C++, ObjC |
|
|
184
|
+
| [clang-tidy][] | Static code analyzer | C, C++, ObjC |
|
|
185
|
+
| [cppcheck][] | Static code analyzer | C, C++ |
|
|
186
|
+
| [cpplint][] | Static code analyzer | C, C++ |
|
|
187
|
+
| [include-what-you-use][] | Static code analyzer | C, C++ |
|
|
188
|
+
| [lizard][] | Code complexity analyzer | C/C++, ObjC, ... |
|
|
189
|
+
|
|
190
|
+
[clang-format]: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
|
|
191
|
+
|
|
192
|
+
[clang-tidy]: https://clang.llvm.org/extra/clang-tidy/
|
|
193
|
+
|
|
194
|
+
[cppcheck]: http://cppcheck.sourceforge.net/
|
|
195
|
+
|
|
196
|
+
[cpplint]: https://github.com/cpplint/cpplint
|
|
197
|
+
|
|
198
|
+
[include-what-you-use]: https://include-what-you-use.org/
|
|
199
|
+
|
|
200
|
+
[lizard]: http://www.lizard.ws
|
|
201
|
+
|
|
202
|
+
### Hook options
|
|
203
|
+
|
|
204
|
+
Since v1.1.0 all hooks that depend on a compilation database (e.g. `clang-tidy`, `cppcheck`, `include-what-you-use`)
|
|
205
|
+
will attempt to generate a CMake build directory before running the actual command.
|
|
206
|
+
|
|
207
|
+
These hooks accept all the most common CMake options:
|
|
208
|
+
|
|
209
|
+
| CMake options | Description |
|
|
210
|
+
|------------------------------|--------------------------------------------------|
|
|
211
|
+
| `-S <path-to-source>` | Explicitly specify a source directory. |
|
|
212
|
+
| `-B <path-to-build>` | Explicitly specify a build directory. |
|
|
213
|
+
| `-D <var>[:<type>]=<value>` | Create or update a cmake cache entry. |
|
|
214
|
+
| `-U <globbing_expr>` | Remove matching entries from CMake cache. |
|
|
215
|
+
| `-G <generator-name>` | Specify a build system generator. |
|
|
216
|
+
| `-T <toolset-name>` | Specify toolset name if supported by generator. |
|
|
217
|
+
| `-A <platform-name>` | Specify platform name if supported by generator. |
|
|
218
|
+
| `--preset <preset>` | Specify a configure preset. |
|
|
219
|
+
| `-Wdev` | Enable developer warnings. |
|
|
220
|
+
| `-Wno-dev` | Suppress developer warnings. |
|
|
221
|
+
| `-Werror=dev` | Make developer warnings errors. |
|
|
222
|
+
| `-Wno-error=dev` | Make developer warnings not errors. |
|
|
223
|
+
|
|
224
|
+
One important thing to note (particularly for those that intend to use this on CIs), you may specify the build directory
|
|
225
|
+
argument (`-B`) multiple times. The hooks will then simply cycle through all of the values provided and choose the first
|
|
226
|
+
directory that contains a configured CMake project (by looking at the presence of the `CMakeCache.txt` file). This may
|
|
227
|
+
be useful if you already have a build directory available somewhere that you would like to reuse. In the case where
|
|
228
|
+
none of the provided options is viable, the first one will automatically be selected as the build directory.
|
|
229
|
+
|
|
230
|
+
In addition to the above CMake options, the hooks also accept the following:
|
|
231
|
+
|
|
232
|
+
| Other hook options | Description | Note |
|
|
233
|
+
|------------------------------|--------------------------------------------------------|--------------|
|
|
234
|
+
| `--all-at-once` | Pass all filenames to the command at once | Since v1.4.0 |
|
|
235
|
+
| `--clean` | Perform a clean CMake build | Since v1.4.0 |
|
|
236
|
+
| `--cmake` | Specify path to CMake executable | Since v1.4.0 |
|
|
237
|
+
| `--cmake-build-target` | Build a CMake target after the configure step | Unreleased |
|
|
238
|
+
| `--codegen` | Build the CMake `codegen` target (CMake >= 3.31) | Unreleased |
|
|
239
|
+
| `--detect-configured-files` | Enable cmake tracing and detection of configured files | Since v1.9.0 |
|
|
240
|
+
| `--dump-toml` | Dump the current configuration as TOML on stdout | Since v1.9.0 |
|
|
241
|
+
| `--no-automatic-discovery` | Disable automatic build directory discovery | Since v1.9.0 |
|
|
242
|
+
| `--no-cmake-configure` | Do not call CMake configure | Since v1.9.2 |
|
|
243
|
+
| `--read-json-db` | Append file list from compile database | Since v1.7.0 |
|
|
244
|
+
| `--linux` | Linux-only CMake options | Since v1.3.0 |
|
|
245
|
+
| `--mac` | MacOS-only CMake options | Since v1.3.0 |
|
|
246
|
+
| `--win` | Windows-only CMake options | Since v1.3.0 |
|
|
247
|
+
|
|
248
|
+
NB: by specifying `--all-at-once` the linter/formatter command will only be called once for all the files instead of
|
|
249
|
+
calling the command once per file.
|
|
250
|
+
|
|
251
|
+
NB: Since v1.6.0, the `--debug` command line argument has been removed. Use the `LOGLEVEL` environment variable instead
|
|
252
|
+
to control the level of verbosity of each of the commands. To show all debug messages, set `LOGLEVEL=DEBUG` in your
|
|
253
|
+
environment variables when running the hooks.
|
|
254
|
+
|
|
255
|
+
NB: by specifying `--read-json-db` the hook will read the list of files from the `compile_commands.json` generated by
|
|
256
|
+
CMake and will append those files to the list of files to process regardless of the list of files otherwise passed on
|
|
257
|
+
the command line.
|
|
258
|
+
|
|
259
|
+
NB: by specifying `--no-cmake-configure` the hook will not attempt to create a compilation database by using
|
|
260
|
+
CMake. However, if one is present, then it will be used by the relevant hooks.
|
|
261
|
+
|
|
262
|
+
NB: by specifying `--cmake-build-target <name>` (may be repeated) the hook will build the given CMake target(s) right
|
|
263
|
+
after the CMake configure step and before running the actual command. This is useful if some of your source files are
|
|
264
|
+
generated at build time (e.g. using `add_custom_command(...)`) and need to exist for the hooks to work properly.
|
|
265
|
+
|
|
266
|
+
NB: `--codegen` is a shorthand for `--cmake-build-target codegen`, which builds the `codegen` target provided by CMake
|
|
267
|
+
in order to run all the custom commands marked with the `CODEGEN` keyword. This requires CMake 3.31 or newer, a Ninja or
|
|
268
|
+
Makefile generator and the policy `CMP0171` set to `NEW` in your project (e.g. using
|
|
269
|
+
`cmake_minimum_required(VERSION 3.31)`). The hooks will check the CMake version and the generator and exit with an error
|
|
270
|
+
if they do not support the `codegen` target. With older versions of CMake, use `--cmake-build-target` with one of your
|
|
271
|
+
own targets instead.
|
|
272
|
+
|
|
273
|
+
NB: the hooks will use the CMake found on your system if there is one. If no CMake executable can be found when the
|
|
274
|
+
hooks are installed, CMake (>= 3.31) will be installed automatically from [PyPI](https://pypi.org/project/cmake/)
|
|
275
|
+
inside the environment of the hooks.
|
|
276
|
+
|
|
277
|
+
Usage example:
|
|
278
|
+
|
|
279
|
+
```yaml
|
|
280
|
+
repos:
|
|
281
|
+
- repo: https://github.com/Takishima/cmake-pre-commit-hooks
|
|
282
|
+
rev: v1.8.0
|
|
283
|
+
hooks:
|
|
284
|
+
- id: cppcheck
|
|
285
|
+
args: [-DBUILD_TESTING=ON,
|
|
286
|
+
--unix="-DCMAKE_CXX_COMPILER=g++-10",
|
|
287
|
+
--win="-DCMAKE_CXX_COMPILER=cl.exe"
|
|
288
|
+
-Bpath/to/build_dir,
|
|
289
|
+
-Bpath/to/other_build_dir,
|
|
290
|
+
-Spath/to/src_dir
|
|
291
|
+
]
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
In the example above, the any hooks requiring a compilation database will first search for a build directory
|
|
295
|
+
`path/to/build_dir` and then `path/to/other_build_dir`. If any of those is deemed valid (ie. is a CMake build directory
|
|
296
|
+
that contains some CMake cache files), then it will be used. If none qualify, the hooks will default to using
|
|
297
|
+
`path/to/build_dir` as a build directory, creating it as necessary.
|
|
298
|
+
|
|
299
|
+
Also, builds on Linux and MacOS will set the C++ compiler to `g++-10`, while builds on Windows will be using
|
|
300
|
+
`cl.exe`. This is done by looking at the value returned by
|
|
301
|
+
[`platform.system()`](https://docs.python.org/3/library/platform.html#platform.system).
|
|
302
|
+
|
|
303
|
+
### TOML support
|
|
304
|
+
|
|
305
|
+
Since v1.9.0, the hooks support loading the CLI arguments from TOML files. This can be used to configure all the hooks
|
|
306
|
+
for a particular repository using either of:
|
|
307
|
+
|
|
308
|
+
1. `pyproject.toml` (under `tool.cmake_pc_hooks` section)
|
|
309
|
+
2. `cmake_pc_hooks.toml`
|
|
310
|
+
3. TOML file specified using `--config=/path/to/file.toml`
|
|
311
|
+
4. Command line arguments
|
|
312
|
+
|
|
313
|
+
Note that each step in the above list is overridden by the steps that happen **after** it. For example, CLI arguments
|
|
314
|
+
will always override any arguments read from any TOML file.
|
|
315
|
+
|
|
316
|
+
A good place to start if you plan on creating a TOML configuration file is to use the hooks using the CLI arguments as
|
|
317
|
+
you would normally and then run the hook manually and with the addition of `--dump-toml`. This will output a
|
|
318
|
+
TOML-formatted configuration on the standard output for all the parameters that *diverge* from their default values.
|
|
319
|
+
|
|
320
|
+
For example, running the following command (assuming that no valid TOML configuration exists):
|
|
321
|
+
|
|
322
|
+
```bash
|
|
323
|
+
cmake-pc-clang-format-hook tests/cmake_bad/bad.cpp --dump-toml -B /tmp/build -S source -Wdev --no-automatic-discovery
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
will result in the following output:
|
|
327
|
+
|
|
328
|
+
```toml
|
|
329
|
+
automatic_discovery = false
|
|
330
|
+
source_dir = "source"
|
|
331
|
+
build_dir = [ "/tmp/build",]
|
|
332
|
+
dev_warnings = true
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
### CMake configured file detection
|
|
336
|
+
|
|
337
|
+
Since v1.9.0, the hooks support the use of CMake with trace mode enabled in order to keep track of files that are
|
|
338
|
+
generated using calls to the `configure_file(...)` CMake function. If `--detect-configured-files` is specified on the
|
|
339
|
+
command line (or in some TOML configuration file), the hooks will attempt to locate those generated files and
|
|
340
|
+
automatically add them to the list of processed files for any hook invocation.
|
|
341
|
+
|
|
342
|
+
### Hook Option Comparison
|
|
343
|
+
|
|
344
|
+
| Hook Options | Fix In Place | Enable all Checks | Set key/value |
|
|
345
|
+
|--------------------------|---------------------|------------------------------------------|---------------|
|
|
346
|
+
| [clang-format][] | `-i` | | |
|
|
347
|
+
| [clang-tidy][] | `--fix-errors` [^1] | `-checks=*` `-warnings-as-errors=*` [^2] | |
|
|
348
|
+
| [cppcheck][] | | `-enable=all` | |
|
|
349
|
+
| [include-what-you-use][] | | | |
|
|
350
|
+
|
|
351
|
+
[^1]: `-fix` will fail if there are compiler errors. `-fix-errors` will `-fix` and fix compiler errors if it can, like missing semicolons.
|
|
352
|
+
|
|
353
|
+
[^2]: Be careful with `-checks=*`; some checks can have self-contradictory rules in newer versions of LLVM (9+). For
|
|
354
|
+
example, modernize wants to use [trailing return type][] but Fuchsia [disallows it][].
|
|
355
|
+
|
|
356
|
+
[trailing return type]: https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-trailing-return-type.html
|
|
357
|
+
|
|
358
|
+
[disallows it]: https://clang.llvm.org/extra/clang-tidy/checks/fuchsia-trailing-return.html
|
|
359
|
+
|
|
360
|
+
### The '--' doubledash option
|
|
361
|
+
|
|
362
|
+
Options after `--` like `-std=c++11` will be interpreted correctly for `clang-tidy`. Make sure they sequentially follow
|
|
363
|
+
the `--` argument in the hook's args list.
|
|
364
|
+
|
|
365
|
+
### Standalone Hooks
|
|
366
|
+
|
|
367
|
+
If you want to have predictable return codes for your C linters outside of pre-commit, these hooks are available via
|
|
368
|
+
[PyPI](https://pypi.org/project/cmake-pre-commit-hooks/). Install it with `pip install cmake-pre-commit-hooks`. They
|
|
369
|
+
are named as `cmake-pc-$cmd-hook`, so `clang-format` becomes `cmake-pc-clang-format-hook`.
|
|
370
|
+
|
|
371
|
+
## Running the tests
|
|
372
|
+
|
|
373
|
+
The unit tests only require the Python test dependencies:
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
python -m pip install -e .[test]
|
|
377
|
+
python -m pytest
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
The integration tests run the hooks for real, on copies of the projects found in `tests/cmake_good` and
|
|
381
|
+
`tests/cmake_bad`. They require CMake as well as the linters (and `pre-commit` for the end-to-end tests):
|
|
382
|
+
|
|
383
|
+
```bash
|
|
384
|
+
python -m pytest tests/integration
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
Each test works on its own temporary copy of the projects, so they can safely be run in parallel using
|
|
388
|
+
[pytest-xdist](https://pytest-xdist.readthedocs.io) (installed as part of the `test` extra), which is about 2.5 times
|
|
389
|
+
faster on a 4-core machine:
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
python -m pytest tests/integration -n auto
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Tests that require a tool that is not installed are skipped. Use `--required-tools` to make them fail instead, e.g.
|
|
396
|
+
`--required-tools=all` or `--required-tools=cmake,clang-format,clang-tidy`.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
cmake_pc_hooks/__init__.py,sha256=Dp6fiklVTMOlgHQf-jJSaiiynO1pMYUREdGJ0OnZan8,624
|
|
2
|
+
cmake_pc_hooks/_argparse.py,sha256=CGPewWPr5Kxl046GsxWpyAbIoyKAbWvWk0JSnEcUOt8,11938
|
|
3
|
+
cmake_pc_hooks/_call_process.py,sha256=3-t3UZY1th1ogrO-UBO0rV2tPo8bqsflrjWGO6NWA0U,2282
|
|
4
|
+
cmake_pc_hooks/_cmake.py,sha256=vE7qpJRwGZo9L8fXZp2AaACrUF_jI8i4lZCkvT20Ong,25131
|
|
5
|
+
cmake_pc_hooks/_utils.py,sha256=zJuCJzkNRoXMT96Ln3ReLADuULpL2XqyQRabewBaqVY,9433
|
|
6
|
+
cmake_pc_hooks/clang_format.py,sha256=u2OYwopg1cqC2b6rJuHcwaThW7eJKqbQjyQ4lxJHwis,1657
|
|
7
|
+
cmake_pc_hooks/clang_tidy.py,sha256=T2tBCGHFfp6oailmCS9ML6-nhJ7yFjLmo2jvt4bXLfE,2520
|
|
8
|
+
cmake_pc_hooks/cppcheck.py,sha256=P1MtbGlCLw5MHkH9GemZYfpErvAAET6ICb8jhS8gqZY,2899
|
|
9
|
+
cmake_pc_hooks/cpplint.py,sha256=fyhGif97_CbLmQZSAFW29GfyCEB4KvsFEuQ7GGnZqjw,702
|
|
10
|
+
cmake_pc_hooks/include_what_you_use.py,sha256=OHSEgFPcuDEQPzPHId7_6_mFaQj04PWzQnV6_uWqAgM,4450
|
|
11
|
+
cmake_pc_hooks/lizard.py,sha256=wz5Uawc2CFfG2hs0JeghQ8IeOjmDKPVd7mVidIkQwlw,2242
|
|
12
|
+
cmake_pre_commit_hooks-1.10.0.dist-info/licenses/LICENSE,sha256=f_82pNieh4KVqI3swn0ruiI3ojBhFHrZ3hChGobOXzA,11341
|
|
13
|
+
cmake_pre_commit_hooks-1.10.0.dist-info/METADATA,sha256=1m4X-nVrxk2PRiBcduzEGZzui3qiSHk1Kb3wbRxMkho,19953
|
|
14
|
+
cmake_pre_commit_hooks-1.10.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
15
|
+
cmake_pre_commit_hooks-1.10.0.dist-info/entry_points.txt,sha256=0nRNugWGOLSndubZEExAaCoxm-KweuKiYHjE7SS4Dmk,372
|
|
16
|
+
cmake_pre_commit_hooks-1.10.0.dist-info/top_level.txt,sha256=3fc7-o2L606c6YIkZuE9vG3ZZWQarynuVWm4_1vqGA4,15
|
|
17
|
+
cmake_pre_commit_hooks-1.10.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
[console_scripts]
|
|
2
|
+
cmake-pc-clang-format-hook = cmake_pc_hooks.clang_format:main
|
|
3
|
+
cmake-pc-clang-tidy-hook = cmake_pc_hooks.clang_tidy:main
|
|
4
|
+
cmake-pc-cppcheck-hook = cmake_pc_hooks.cppcheck:main
|
|
5
|
+
cmake-pc-cpplint-hook = cmake_pc_hooks.cpplint:main
|
|
6
|
+
cmake-pc-include-what-you-use-hook = cmake_pc_hooks.include_what_you_use:main
|
|
7
|
+
cmake-pc-lizard-hook = cmake_pc_hooks.lizard:main
|