Skip to content

Commit df0a6d0

Browse files
authored
Merge pull request bemanproject#52 from steve-downey/precommit
Precommit
2 parents 158b8c6 + 5019306 commit df0a6d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+604
-185
lines changed

.cmake-format.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"parse": {
3+
"additional_commands": {
4+
"foo": {
5+
"flags": [
6+
"BAR",
7+
"BAZ"
8+
],
9+
"kwargs": {
10+
"HEADERS": "*",
11+
"SOURCES": "*",
12+
"DEPENDS": "*"
13+
}
14+
}
15+
},
16+
"override_spec": {},
17+
"vartags": [],
18+
"proptags": []
19+
},
20+
"format": {
21+
"disable": false,
22+
"line_width": 80,
23+
"tab_size": 2,
24+
"use_tabchars": false,
25+
"fractional_tab_policy": "use-space",
26+
"max_subgroups_hwrap": 2,
27+
"max_pargs_hwrap": 6,
28+
"max_rows_cmdline": 2,
29+
"separate_ctrl_name_with_space": false,
30+
"separate_fn_name_with_space": false,
31+
"dangle_parens": false,
32+
"dangle_align": "prefix",
33+
"min_prefix_chars": 4,
34+
"max_prefix_chars": 10,
35+
"max_lines_hwrap": 2,
36+
"line_ending": "unix",
37+
"command_case": "canonical",
38+
"keyword_case": "unchanged",
39+
"always_wrap": [],
40+
"enable_sort": true,
41+
"autosort": false,
42+
"require_valid_layout": false,
43+
"layout_passes": {}
44+
},
45+
"markup": {
46+
"bullet_char": "*",
47+
"enum_char": ".",
48+
"first_comment_is_literal": false,
49+
"literal_comment_pattern": null,
50+
"fence_pattern": "^\\s*([`~]{3}[`~]*)(.*)$",
51+
"ruler_pattern": "^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$",
52+
"explicit_trailing_pattern": "#<",
53+
"hashruler_min_length": 10,
54+
"canonicalize_hashrulers": true,
55+
"enable_markup": true
56+
},
57+
"lint": {
58+
"disabled_codes": [],
59+
"function_pattern": "[0-9a-z_]+",
60+
"macro_pattern": "[0-9A-Z_]+",
61+
"global_var_pattern": "[A-Z][0-9A-Z_]+",
62+
"internal_var_pattern": "_[A-Z][0-9A-Z_]+",
63+
"local_var_pattern": "[a-z][a-z0-9_]+",
64+
"private_var_pattern": "_[0-9a-z_]+",
65+
"public_var_pattern": "[A-Z][0-9A-Z_]+",
66+
"argument_var_pattern": "[a-z][a-z0-9_]+",
67+
"keyword_pattern": "[A-Z][0-9A-Z_]+",
68+
"max_conditionals_custom_parser": 2,
69+
"min_statement_spacing": 1,
70+
"max_statement_spacing": 2,
71+
"max_returns": 6,
72+
"max_branches": 12,
73+
"max_arguments": 5,
74+
"max_localvars": 15,
75+
"max_statements": 50
76+
},
77+
"encode": {
78+
"emit_byteorder_mark": false,
79+
"input_encoding": "utf-8",
80+
"output_encoding": "utf-8"
81+
},
82+
"misc": {
83+
"per_command": {}
84+
}
85+
}

.codespell_ignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
copyable
2+
falsy
3+
pullrequest
4+
unexpect

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
907e040

.github/workflows/pre-commit.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: pre-commit
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
pre-commit:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-python@v3
14+
- uses: pre-commit/[email protected]

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ compile_commands.json
2525
/papers/**/_minted-optional_ref/
2626
/papers/**/_minted-optional_ref_wording/
2727
.update-submodules
28+
/.install/
29+
/.venv/

.pre-commit-config.yaml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v4.6.0
6+
hooks:
7+
- id: check-added-large-files
8+
- id: check-case-conflict
9+
- id: check-executables-have-shebangs
10+
- id: check-json
11+
- id: check-merge-conflict
12+
- id: check-symlinks
13+
- id: check-toml
14+
- id: check-xml
15+
- id: check-yaml
16+
- id: detect-private-key
17+
- id: end-of-file-fixer
18+
exclude: '^(.*\.svg)$'
19+
- id: mixed-line-ending
20+
args: ['--fix=lf']
21+
exclude: '^(.*\.svg)$'
22+
- id: trailing-whitespace
23+
exclude: '^(|.*\.svg)$'
24+
- id: fix-byte-order-marker
25+
- id: no-commit-to-branch
26+
- id: requirements-txt-fixer
27+
28+
- repo: local
29+
hooks:
30+
- id: clang-format-fix
31+
name: clang-format-fix
32+
entry: clang-format-18
33+
files: ^(src/|include/)
34+
types_or: [c++, c]
35+
language: system
36+
args: ['-i']
37+
stages: [manual]
38+
39+
- repo: https://github.com/pocc/pre-commit-hooks
40+
rev: v1.3.5
41+
hooks:
42+
- id: clang-format
43+
additional_dependencies: ['clang-format==18.1.8']
44+
# - id: oclint
45+
# - id: cppcheck
46+
# - id: cpplint
47+
48+
- repo: https://github.com/pocc/pre-commit-hooks
49+
rev: v1.3.5
50+
hooks:
51+
- id: clang-tidy
52+
stages: [manual]
53+
additional_dependencies: ['clang-tidy==18.1.8']
54+
55+
- repo: https://github.com/codespell-project/codespell
56+
rev: v2.2.6
57+
hooks:
58+
- id: codespell
59+
args: ['-I', '.codespell_ignore', '--uri-ignore-words-list']
60+
exclude: |
61+
(?x)^(
62+
papers/.*
63+
)$
64+
65+
# CMake formatting
66+
- repo: https://github.com/cheshirekow/cmake-format-precommit
67+
rev: "v0.6.13"
68+
hooks:
69+
- id: cmake-format
70+
additional_dependencies: [pyyaml]
71+
- id: cmake-lint
72+
73+
- repo: https://github.com/jorisroovers/gitlint
74+
rev: v0.19.1
75+
hooks:
76+
- id: gitlint
77+
- id: gitlint-ci
78+
79+
- repo: https://github.com/shellcheck-py/shellcheck-py
80+
rev: v0.10.0.1
81+
hooks:
82+
- id: shellcheck
83+
84+
exclude: 'vendor'

.shellcheckrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# shellcheck
22
# allow for non-alphanumeric filenames
3-
disable=SC2038
3+
disable=SC2038
4+
external-sources=true

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ install(
5454
FILES ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake
5555
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}ConfigVersion.cmake
5656
DESTINATION ${INSTALL_CONFIGDIR})
57+
58+
# Coverage
59+
configure_file("cmake/gcovr.cfg.in" gcovr.cfg @ONLY)
60+
61+
add_custom_target(
62+
process_coverage
63+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
64+
COMMENT "Running gcovr to process coverage results"
65+
COMMAND mkdir -p coverage
66+
COMMAND gcovr --config gcovr.cfg .)

Makefile

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export
2727
ifeq ($(strip $(TOOLCHAIN)),)
2828
_build_name?=build-system/
2929
_build_dir?=.build/
30-
_configuration_types?="RelWithDebInfo;Debug;Tsan;Asan"
30+
_configuration_types?="RelWithDebInfo;Debug;Tsan;Asan;Gcov"
3131
_cmake_args=-DCMAKE_TOOLCHAIN_FILE=$(CURDIR)/etc/toolchain.cmake
3232
else
3333
_build_name?=build-$(TOOLCHAIN)
3434
_build_dir?=.build/
35-
_configuration_types?="RelWithDebInfo;Debug;Tsan;Asan"
35+
_configuration_types?="RelWithDebInfo;Debug;Tsan;Asan;Gcov"
3636
_cmake_args=-DCMAKE_TOOLCHAIN_FILE=$(CURDIR)/etc/$(TOOLCHAIN)-toolchain.cmake
3737
endif
3838

@@ -66,10 +66,10 @@ install: $(_build_path)/CMakeCache.txt ## Install the project
6666
DESTDIR=$(abspath $(DEST)) ninja -C $(_build_path) -k 0 install
6767

6868
ctest: $(_build_path)/CMakeCache.txt ## Run CTest on current build
69-
cd $(_build_path) && ctest --output-on-failure
69+
cd $(_build_path) && ctest --output-on-failure -C $(CONFIG)
7070

7171
ctest_ : compile
72-
cd $(_build_path) && ctest -C $(CONFIG) --output-on-failure
72+
cd $(_build_path) && ctest --output-on-failure -C $(CONFIG)
7373

7474
test: ctest_ ## Rebuild and run tests
7575

@@ -89,9 +89,82 @@ env:
8989

9090
.PHONY: papers
9191
papers:
92-
$(MAKE) -C papers papers
92+
$(MAKE) -C papers/P2988 papers
93+
94+
PYEXECPATH ?= $(shell which python3.12 || which python3.11 || which python3.10 || which python3.9 || which python3.8 || which python3.7 || which python3)
95+
PYTHON ?= $(shell basename $(PYEXECPATH))
96+
VENV := .venv
97+
ACTIVATE := . $(VENV)/bin/activate &&
98+
PYEXEC := $(ACTIVATE) $(PYTHON)
99+
MARKER=.initialized.venv.stamp
100+
101+
PIP := $(PYEXEC) -m pip
102+
PIP_SYNC := $(PYEXEC) -m piptools sync
103+
PIPTOOLS_COMPILE := $(PYEXEC) -m piptools compile --no-header --strip-extras
104+
105+
PRE_COMMIT := $(ACTIVATE) pre-commit
106+
107+
PHONY: venv
108+
venv: ## Create python virtual env
109+
venv: $(VENV)/$(MARKER)
110+
111+
.PHONY: clean-venv
112+
clean-venv:
113+
clean-venv: ## Delete python virtual env
114+
-rm -rf $(VENV)
115+
116+
realclean: clean-venv
117+
118+
.PHONY: show-venv
119+
show-venv: venv
120+
show-venv: ## Debugging target - show venv details
121+
$(PYEXEC) -c "import sys; print('Python ' + sys.version.replace('\n',''))"
122+
$(PIP) --version
123+
@echo venv: $(VENV)
124+
125+
requirements.txt: requirements.in
126+
$(PIPTOOLS_COMPILE) --output-file=$@ $<
127+
128+
requirements-dev.txt: requirements-dev.in
129+
$(PIPTOOLS_COMPILE) --output-file=$@ $<
130+
131+
$(VENV):
132+
$(PYEXECPATH) -m venv $(VENV)
133+
$(PIP) install --upgrade pip setuptools wheel
134+
$(PIP) install pip-tools
135+
136+
$(VENV)/$(MARKER): requirements.txt requirements-dev.txt | $(VENV)
137+
$(PIP_SYNC) requirements.txt
138+
$(PIP_SYNC) requirements-dev.txt
139+
touch $(VENV)/$(MARKER)
140+
141+
.PHONY: dev-shell
142+
dev-shell: venv
143+
dev-shell: ## Shell with the venv activated
144+
$(ACTIVATE) $(notdir $(SHELL))
145+
146+
.PHONY: bash zsh
147+
bash zsh: venv
148+
bash zsh: ## Run bash or zsh with the venv activated
149+
$(ACTIVATE) exec $@
150+
151+
.PHONY: lint
152+
lint: venv
153+
lint: ## Run all configured tools in pre-commit
154+
$(PRE_COMMIT) run -a
155+
156+
.PHONY: lint-manual
157+
lint-manual: venv
158+
lint-manual: ## Run all manual tools in pre-commit
159+
$(PRE_COMMIT) run --hook-stage manual -a
160+
161+
.PHONY: coverage
162+
coverage: ## Build and run the tests with the GCOV profile and process the results
163+
coverage: venv
164+
$(MAKE) CONFIG=Gcov test
165+
$(ACTIVATE) cmake --build $(_build_path) --config Gcov --target process_coverage
93166

94167
# Help target
95168
.PHONY: help
96169
help: ## Show this help.
97-
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) targets.mk | sort
170+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Documentation and associated papers are licensed with the Creative Commons Attri
4343

4444
The intent is that the source and documentation are available for use by people implementing their own optional types as well as people using the optional presented here as-is.
4545

46-
The README itself is licesed with CC0 1.0 Universal. Copy the contents and incorporate in your own work as you see fit.
46+
The README itself is licensed with CC0 1.0 Universal. Copy the contents and incorporate in your own work as you see fit.
4747

4848
// SPDX-License-Identifier: CC0-1.0
4949

@@ -237,6 +237,36 @@ Test project /path/to/Beman.Optional26/.build
237237
No tests were found!!!
238238
```
239239

240+
#### Pre-Commit for Linting
241+
Various linting tools are configured and installed via the [pre-commit](https://pre-commit.com/) framework. This requires a working python environment, but also allows the tools, such as clang-format and cmake-lint, to be versioned on a per project basis rather than being installed globally. Version changes in lint checks often means differences in success or failure between the versions in CI and the versions used by a developer. By using the same configurations, this problem is avoided.
242+
243+
In order to set up a python environment, using a python virtual environment can simplify maintaining different configurations between projects. There is no particular dependency on a particular python3 version.
244+
245+
##### Creating and configuring a venv
246+
```shell
247+
python3 -m venv .venv
248+
. .venv/bin/activate && python3 -m pip install --upgrade pip setuptools wheel
249+
. .venv/bin/activate && python3 -m pip install pip-tools
250+
. .venv/bin/activate && python3 -m piptools sync requirements.txt
251+
. .venv/bin/activate && python3 -m piptools sync requirements-dev.txt
252+
. .venv/bin/activate && exec bash
253+
```
254+
255+
This will create the venv, install the python and python development tools, and run bash with the PATH and other environment variables set to use the venv preferentially.
256+
257+
##### Running the linting tools
258+
```shell
259+
pre-commit run -a
260+
```
261+
262+
This will download and configure the lint tools specified in .pre-commit-config.yaml.
263+
264+
There is also a Makefile that will automate this process and keep everything up to date.
265+
266+
```shell
267+
make lint
268+
```
269+
240270
## Papers
241271

242272
Latest revision(s) of the papers can be built / found at:

0 commit comments

Comments
 (0)