Skip to content

Commit 538dd2b

Browse files
committed
feat: clang pre-commit hook, housekeeping
1 parent 0123cf9 commit 538dd2b

7 files changed

Lines changed: 170 additions & 68 deletions

File tree

.clang-tidy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
Checks: >
3+
clang-analyzer-*,
4+
cppcoreguidelines-*,
5+
modernize-*,
6+
performance-*,
7+
google-*,
8+
-google-runtime-int,
9+
-google-readability-braces-around-statements,
10+
-modernize-use-trailing-return-type
11+
12+
ExtraArgs: ['-std=c++20']

.githooks/pre-commit

Lines changed: 0 additions & 22 deletions
This file was deleted.

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ test:
4747
ctest --preset=debug
4848
lcov --gcov-tool gcov --capture --directory . --output-file lcov.info
4949

50+
## tidy: 🧹 tidy things up before committing code
51+
.PHONY: tidy
52+
tidy:
53+
find src/ tests/ -name '*.cpp' | xargs clang-tidy -p build/Debug --fix --format-style=.clang-format
54+
find src/ tests/ \( -name '*.cpp' -o -name '*.hpp' -o -name '*.c' -o -name '*.h' \) -exec clang-format -i {} +
55+
5056
## build-release: 🔨🔨 compile (prod)
5157
.PHONY: build-release
5258
build-release:

README.md

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,64 @@
99
# trader.cpp
1010
a proof-of-concept, showcasing some c++ coding combined with some fintech concepts
1111

12-
## RUN REQUIREMENTS
12+
## Run Requirements
1313
- a Binance account, with an Ed25519 token that has FIX read permissions enabled
14-
- `stunnel` or a local proxy, for TLS encryption
14+
- `stunnel` for TLS encryption (or a local proxy)
1515

16-
## BUILD REQUIREMENTS
16+
## Build Requirements
1717
- C++20
1818
- `Conan` (and a conan profile)
1919
- `CMake`
20+
- `ninja`
2021

21-
## DEV REQUIREMENTS
22+
## Dev Requirements
23+
- `make` (convenience)
2224
- `clang-tidy`
2325
- `clang-format`
2426
- `lcov`
2527

26-
## BUILD AND RUN
28+
## Build and Run
2729
(NB: this app uses `make` as a task runner, but it's not essential)
2830
1. copy `.env.example` to `.env`, and set your public/private keys
2931
2. run an SSL tunnel (e.g. `stunnel binance/stunnel_prod.conf`)
3032
3. `make init`
3133
4. `make build-debug`
3234
5. `make withenv RECIPE=run-debug`
3335

34-
## TEST
36+
## Test
3537
`make test`
3638

37-
## HELP
39+
## Debug
40+
- vscode
41+
- app and test debug profiles are pre-configured in the following files:
42+
- `.vscode/launch.json`
43+
- `.vscode/tasks.json`
44+
- intellij (clion)
45+
- enable the `debug` CMake profile
46+
47+
## Help
3848
`make`
3949

50+
## Cloud Config
51+
- Sonarcloud (click the badge)
52+
- Codecov (click the badge)
53+
4054

41-
# AIM
55+
# Aims
4256

43-
## FUNCTIONAL
57+
## Functional
4458
- ✅ create a FIX connection to Binance
4559
- ✅ subscribe to price updates
4660
- create a basic trading signal (e.g. standard deviations)
4761
- fire an order
4862
- test in the Binance test environment
4963

50-
51-
## NON-FUNCTIONAL
64+
## Non-functional
5265
- ✅ basic cpp app to start with
5366
- ✅ makefile and build chain
5467
- ✅ package management
5568
- ✅ debugging
69+
- ✅ single-threaded to start with, then re-architect (and mermaid diagram)
5670
- ✅ UI
5771
- ✅ publish messages to thread-safe queue
5872
- ✅ consume messages from thread-safe queue on a worker thread
@@ -62,47 +76,50 @@ a proof-of-concept, showcasing some c++ coding combined with some fintech concep
6276
- ✅ fast
6377
- structured
6478
- basic schema (severity, correlationId)
65-
- ✅ dependency injection
66-
- ✅ single-threaded to start with, then re-architect (and mermaid diagram)
67-
- ✅ code formatting / clang-format
79+
- code quality
80+
- ✅ clang-format
81+
- ✅ configure editor to auto-format
82+
- ✅ fail commits if not formatted
83+
- ✅ fail builds if not formatted
84+
- clang-tidy
85+
- all files tidied
86+
- configured clang-tidy => clang-format
87+
- fail commits/merges if not tidy
88+
- fail builds if not tidy
6889
- ✅ git hooks
6990
- ✅ integrated into build pipeline
70-
- ✅ coverage badge
71-
- ✅ custom docker build image with all dependencies (for faster pipelines)
72-
- ✅ static code analysis
91+
- ✅ badges
7392
- ✅ sonarcloud integrated into build pipeline
74-
- clang-tidy
75-
- decimal type
76-
- sparse arrays
77-
- release binaries on github
78-
- ccache.dev
79-
- zeromq + protobufs?
80-
- valgrind/cachegrind
81-
- github releases
82-
- local github action runner
83-
- sonarqube integration
84-
- nix virtual environment
85-
86-
# STANDARDS
87-
- high unit-test coverage
88-
- static code analysis
89-
- configure debugging
90-
- git use
91-
- integration test with mocked Binance
92-
- load test with mocked Binance (k6?)
93-
- profiling
94-
- github action
95-
- containerised integration tests / dind
93+
- sonarcloud coverage
94+
- pipeline
95+
- ✅ custom docker build image with all dependencies (hosted on GHCR for faster pipelines)
96+
- nix
97+
- ccache (faster pipelines)
98+
- github releases
99+
- local github action runner (`act`)
100+
- containerised integration tests / dind
101+
- testing
102+
- ✅ dependency injection
103+
- integration test with mocked Binance
104+
- performance
105+
- profiling (valgrind/cachegrind)
106+
- load test with mocked Binance server (k6?)
107+
- sparse arrays
108+
- memory-mapped files
96109
- observability
97-
- opentelemetry
98-
- grafana+tempo via docker-compose
110+
- opentelemetry
111+
- grafana+tempo via docker-compose
99112
- versioning
100-
- conventional commits
113+
- master branch merge check for conventional commit message (e.g. regex)
114+
- maybe a merge git gook check
101115
- automated semantic versioning
102116
- github-changelog-generator
103-
- master branch merge check for conventional commit message
104-
- maybe a git gook check for merges
105-
- memory-mapped files
117+
- other
118+
- decimal type
119+
- zeromq + protobufs?
120+
- shellcheck?
121+
- deployment
122+
- terraform
106123

107-
# CREDITS
124+
# Credits
108125
- https://github.com/binance/binance-fix-connector-python

hooks/check_clang_format.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
################################################################
4+
# check_clang_format.sh
5+
# checks for any formatting issues and errors out.
6+
# does not modify files in-place.
7+
################################################################
8+
9+
set -euo pipefail
10+
11+
echo ">> Checking for clang-format issues..."
12+
13+
CLANG_FORMAT_BIN="clang-format"
14+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15+
16+
files=$(find "$SCRIPT_DIR"/../src/ "$SCRIPT_DIR"/../tests/ \( -name '*.cpp' -o -name '*.hpp' -o -name '*.c' -o -name '*.h' \))
17+
format_errors=0
18+
for file in $files; do
19+
if [ -f "$file" ]; then
20+
if ! diff -q <($CLANG_FORMAT_BIN "$file") "$file" >/dev/null; then
21+
echo "clang-format issues found in file, exiting. file: [$file]"
22+
format_errors=1
23+
break
24+
fi
25+
fi
26+
done
27+
28+
if [ $format_errors -ne 0 ]; then
29+
echo "ERROR: clang-format found unformatted code. Try \`make tidy\`"
30+
exit 1
31+
fi

hooks/check_clang_tidy.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
################################################################
4+
# check_clang_tidy.sh
5+
# checks for any lint warnings and errors out.
6+
# does not modify files in-place.
7+
################################################################
8+
9+
set -euo pipefail
10+
11+
echo ">> Checking for clang-tidy warnings..."
12+
13+
CLANG_TIDY_BIN="clang-tidy"
14+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15+
BUILD_DIR="$SCRIPT_DIR/../build/Debug"
16+
17+
if [ ! -f "$BUILD_DIR/compile_commands.json" ]; then
18+
echo "ERROR: compile_commands.json not found in $BUILD_DIR"
19+
echo "Run: cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B $BUILD_DIR"
20+
exit 1
21+
fi
22+
23+
files=$(find "$SCRIPT_DIR"/../src/ "$SCRIPT_DIR"/../tests/ \( -name '*.cpp' -o -name '*.c' \))
24+
tidy_errors=0
25+
for file in $files; do
26+
echo "Running clang-tidy on $file"
27+
$CLANG_TIDY_BIN "$file" -p "$BUILD_DIR" > tidy_output.txt 2>&1 || true
28+
if grep -q "warning:" tidy_output.txt; then
29+
echo "clang-tidy warnings found in file, exiting. file: [$file] warnings:"
30+
cat tidy_output.txt
31+
tidy_errors=1
32+
break
33+
fi
34+
done
35+
rm tidy_output.txt
36+
37+
if [ $tidy_errors -ne 0 ]; then
38+
echo "ERROR: clang-tidy found warnings. Try \`make tidy\`"
39+
exit 1
40+
fi

hooks/pre-commit

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
################################################################
4+
# pre-commit.sh
5+
# Fails the commit on any linting/formatting errors.
6+
# NB: file paths are relative to the script location,
7+
# so the script can be executed from any path
8+
################################################################
9+
10+
set -euo pipefail
11+
12+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13+
14+
# Run subscripts
15+
echo "Running strict pre-commit checks on src/ and tests/"
16+
bash "$DIR/check_clang_tidy.sh"
17+
bash "$DIR/check_clang_format.sh"
18+
echo "✅ Pre-commit checks passed on full codebase."

0 commit comments

Comments
 (0)