Skip to content

Commit 9bb73ce

Browse files
committed
feat: clang-format and github action
1 parent d325ed7 commit 9bb73ce

33 files changed

Lines changed: 1096 additions & 989 deletions

.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BasedOnStyle: Google
2+
IndentWidth: 2
3+
ColumnLimit: 90

.githooks/pre-commit

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Find all staged C++ source and header files (you can modify this pattern)
4+
files=$(git diff --cached --name-only --diff-filter=AM | grep -E '\.cpp$|\.h$')
5+
6+
# If there are no relevant files, exit early
7+
if [ -z "$files" ]; then
8+
exit 0
9+
fi
10+
11+
# Run clang-format on all staged files
12+
echo "Running clang-format on staged files..."
13+
for file in $files; do
14+
clang-format -i "$file" # Format the file in place
15+
git add "$file" # Re-add the formatted file to the staging area
16+
done
17+
18+
echo "clang-format done."
19+
20+
# Optionally: Run tests or other checks here, like 'make lint'
21+
22+
exit 0

.github/workflows/build.yml

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,50 @@ on:
88
branches: [ master ]
99

1010
jobs:
11-
build:
11+
format:
1212
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up clang-format
18+
run: sudo apt update && sudo apt install -y clang-format
19+
20+
- name: Check code formatting
21+
run: |
22+
set -e
23+
CHANGED_FILES=$(find src tests -name '*.cpp' -o -name '*.h')
24+
FORMAT_ERRORS=0
25+
for file in $CHANGED_FILES; do
26+
clang-format --dry-run --Werror "$file" || FORMAT_ERRORS=1
27+
done
28+
if [ "$FORMAT_ERRORS" -ne 0 ]; then
29+
echo "Code is not properly formatted. Please run clang-format."
30+
exit 1
31+
fi
1332
33+
build_and_test:
34+
needs: format
35+
runs-on: ubuntu-latest
1436
steps:
1537
- name: Checkout repository
1638
uses: actions/checkout@v4
1739

18-
- name: Cache Conan
40+
- name: Restore Conan cache
1941
uses: actions/cache@v4
2042
with:
2143
path: ~/.conan2
2244
key: ${{ runner.os }}-conan-${{ hashFiles('conanfile.*') }}
2345
restore-keys: |
2446
${{ runner.os }}-conan-
25-
26-
- name: Set up build tools
47+
48+
- name: Install dependencies and build
2749
run: |
50+
set -e
2851
sudo apt update && sudo apt install -y g++
2952
pip install conan
3053
conan profile detect --force
31-
32-
- name: Build
33-
run: |
3454
conan install . --build=missing -s build_type=Debug
3555
cmake --preset=conan-debug
3656
cmake --build --preset=conan-debug
37-
38-
- name: Test
39-
run: |
4057
ctest --test-dir build/Debug --output-on-failure

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
.idea/
22
.venv/
33
.trash/
4-
54
build/
65
binance/keys/
76
qf_files/
87
qf_logs/
98
logs/
109

10+
# ENVIRONMENT
1111
.env
1212
.env.*
1313

14+
# COVERAGE
15+
lcov.info
16+
1417
# TODO: Understand the auto-generation of this file
1518
CMakeUserPresets.json
1619

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
3-
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/Debug/compile_commands.json"
4-
}
3+
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/Debug/compile_commands.json",
4+
"editor.formatOnSave": true
5+
}

CMakeLists.txt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_CXX_STANDARD_REQUIRED YES)
66

77
############################################
8-
# Conan-generated toolchain setup
98

9+
# Conan-generated toolchain setup
1010
include(${CMAKE_BINARY_DIR}/conan_toolchain.cmake OPTIONAL RESULT_VARIABLE _found_toolchain)
1111
if(_found_toolchain)
1212
message(STATUS "Conan toolchain included")
@@ -22,14 +22,13 @@ find_package(libsodium REQUIRED)
2222
find_package(spdlog REQUIRED)
2323

2424
############################################
25-
# Collect source files
2625

26+
# Collect source files
2727
file(GLOB_RECURSE ALL_SOURCES "src/*.cpp")
2828
# Separate main.cpp from the rest of the sources
2929
list(FILTER ALL_SOURCES EXCLUDE REGEX ".*main\\.cpp$")
3030
set(SOURCES ${ALL_SOURCES})
3131

32-
############################################
3332
# Create core logic library
3433
add_library(traderlib STATIC ${SOURCES})
3534

@@ -40,20 +39,33 @@ target_link_libraries(traderlib PRIVATE --coverage)
4039

4140
# Link dependencies to the library (so both exe and tests get them)
4241
target_link_libraries(traderlib PUBLIC
43-
concurrentqueue::concurrentqueue
44-
ftxui::ftxui
45-
OpenSSL::SSL
46-
OpenSSL::Crypto
47-
quickfix::quickfix
48-
libsodium::libsodium
49-
spdlog::spdlog
42+
concurrentqueue::concurrentqueue
43+
ftxui::ftxui
44+
OpenSSL::SSL
45+
OpenSSL::Crypto
46+
quickfix::quickfix
47+
libsodium::libsodium
48+
spdlog::spdlog
5049
)
5150

5251
############################################
5352
# Create main executable
5453
add_executable(tradercpp "src/main.cpp")
5554
target_link_libraries(tradercpp PRIVATE traderlib)
5655

56+
############################################
57+
58+
# Run clang-format on all source files before building
59+
add_custom_command(
60+
TARGET tradercpp
61+
PRE_BUILD
62+
COMMAND clang-format -i ${CMAKE_SOURCE_DIR}/src/**/*.cpp ${CMAKE_SOURCE_DIR}/src/**/*.h
63+
&& clang-format -i ${CMAKE_SOURCE_DIR}/tests/**/*.cpp ${CMAKE_SOURCE_DIR}/tests/**/*.h
64+
COMMENT "Running clang-format on all source and header files"
65+
)
66+
67+
# set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-config-file=.clang-tidy;-checks=*")
68+
5769
############################################
5870
# Enable `ctest`
5971
enable_testing()

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ withenv:
2222
## init: 🏌️ initialize the project, fetch dependencies
2323
.PHONY: init
2424
init:
25+
test -e .git/hooks/pre-commit || ln -s ../../.githooks/pre-commit .git/hooks/pre-commit
2526
rm -rf build && mkdir build
2627
python3 -m venv .venv
2728
source .venv/bin/activate && \
@@ -49,7 +50,7 @@ build-release:
4950
cmake --preset=conan-release
5051
cmake --build --preset=conan-release
5152

52-
## run-debug: 🏃‍♂️ run the app (debug) (don't forget withenv)
53+
## run-debug: 🏃‍♂️ run the app (debug) (don't forget `withenv`)
5354
.PHONY: run-debug
5455
run-debug:
5556
build/Debug/tradercpp

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ a proof-of-concept, showcasing some c++ coding combined with some fintech concep
66
width="250" />
77

88
## REQUIREMENTS
9+
- a Binance account, with an Ed25519 token that has FIX read permissions enabled
910
- C++20
1011
- `Conan` (and a conan profile)
1112
- `CMake`
12-
- a local proxy for TLS encryption (e.g. `stunnel`)
13-
- a Binance account, with an Ed25519 token that has FIX read permissions enabled
13+
- `stunnel` or a local proxy, for TLS encryption
14+
15+
## DEV REQUIREMENTS
16+
- `clang-tidy`
17+
- `clang-format`
1418
- `lcov`
1519

1620
## BUILD AND RUN
1721
(NB: this app uses `make` as a task runner, but it's not essential)
1822
1. copy `.env.example` to `.env`, and set your public/private keys
19-
2. run an SSL tunnel (`stunnel binance/stunnel_prod.conf`)
23+
2. run an SSL tunnel (e.g. `stunnel binance/stunnel_prod.conf`)
2024
3. `make init`
2125
4. `make build-debug`
2226
5. `make withenv RECIPE=run-debug`
@@ -61,11 +65,16 @@ a proof-of-concept, showcasing some c++ coding combined with some fintech concep
6165
- ccache.dev
6266
- zeromq + protobufs?
6367
- valgrind/cachegrind
68+
- code formatting / auto-formatter
69+
- ✅ clang-format
70+
- ✅ git hooks
71+
- ✅integrated into build pipeline
72+
- clang-tidy
73+
- github releases
74+
- custom docker build image with all dependencies
6475

6576
# STANDARDS
6677
- high unit-test coverage + badge
67-
- code formatting / auto-formatter
68-
- clang?
6978
- static code analysis
7079
- configure debugging
7180
- git use
@@ -77,8 +86,10 @@ a proof-of-concept, showcasing some c++ coding combined with some fintech concep
7786
- observability
7887
- opentelemetry
7988
- grafana+tempo via docker-compose
80-
- conventional commits
81-
- automated semantic versioning
89+
- versioning
90+
- conventional commits
91+
- automated semantic versioning
92+
- github-changelog-generator
8293
- memory-mapped files
8394

8495
# CREDITS

docs/DESIGN.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# DESIGN
2+
```mermaid
3+
sequenceDiagram
4+
participant T1 as Thread 1<br>(Main + UI)
5+
participant T2 as Thread 2<br>(FIX Worker)
6+
participant T3 as Thread 3<br>(UI Worker)
7+
8+
T1->>T2: Start FIX Worker
9+
T1->>T3: Start UI Worker
10+
T2-->>T2: connect+subscribe<br>+push to <queue>
11+
T2->>T3: pull from <queue>
12+
T3->>T1: request render
13+
T2-->>T1: Thread 1 done
14+
T3-->>T1: Thread 2 done
15+
```

0 commit comments

Comments
 (0)