Skip to content

Commit af0820c

Browse files
clang-format in CI (#116)
1 parent 0aa60a3 commit af0820c

114 files changed

Lines changed: 5571 additions & 6708 deletions

File tree

Some content is hidden

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

‎.clang-format‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
BasedOnStyle: Google
3+
Language: Cpp
4+
Standard: c++17
5+
6+
# LiveKit modifications to Google style below
7+
8+
ColumnLimit: 120 # more width on modern screens
9+
SpacesBeforeTrailingComments: 1 # one space for trailing namespace comments, e.g. `} // namespace foo` (Google uses 2)
10+
AccessModifierOffset: -2 # left-align public/protected/private with the class keyword (Google indents by 1)

‎.github/workflows/builds.yml‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ on:
1717
- docker/Dockerfile.base
1818
- docker/Dockerfile.sdk
1919
- .github/workflows/**
20+
- .clang-format
21+
- scripts/clang-format.sh
2022
pull_request:
2123
branches: ["main"]
2224
paths:
@@ -33,6 +35,8 @@ on:
3335
- docker/Dockerfile.base
3436
- docker/Dockerfile.sdk
3537
- .github/workflows/**
38+
- .clang-format
39+
- scripts/clang-format.sh
3640
workflow_dispatch:
3741

3842
permissions:
@@ -513,6 +517,42 @@ jobs:
513517
cmake --build build --parallel
514518
'
515519
520+
clang-format:
521+
name: clang-format
522+
runs-on: ubuntu-latest
523+
continue-on-error: false
524+
permissions:
525+
contents: read
526+
527+
steps:
528+
- name: Checkout
529+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
530+
with:
531+
# No submodules: scripts/clang-format.sh only walks our own src/ tree
532+
fetch-depth: 1
533+
534+
- name: Install clang-format 22
535+
run: |
536+
set -eux
537+
# Pin clang-format 22 to match the current macOS Homebrew LLVM
538+
# Ubuntu 24.04's default clang-format ships with LLVM 18
539+
sudo install -m 0755 -d /etc/apt/keyrings
540+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key \
541+
| sudo tee /etc/apt/keyrings/llvm.asc >/dev/null
542+
sudo chmod a+r /etc/apt/keyrings/llvm.asc
543+
codename=$(lsb_release -cs)
544+
echo "deb [signed-by=/etc/apt/keyrings/llvm.asc] http://apt.llvm.org/${codename}/ llvm-toolchain-${codename}-22 main" \
545+
| sudo tee /etc/apt/sources.list.d/llvm-22.list >/dev/null
546+
sudo apt-get update
547+
sudo apt-get install -y clang-format-22
548+
sudo ln -sf /usr/bin/clang-format-22 /usr/local/bin/clang-format
549+
clang-format --version
550+
551+
- name: Run clang-format
552+
env:
553+
FORMAT_BLOB_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
554+
run: ./scripts/clang-format.sh
555+
516556
clang-tidy:
517557
name: clang-tidy
518558
runs-on: ubuntu-latest

‎AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ All source files must have the LiveKit Apache 2.0 copyright header. Use the curr
169169
### Readability and Performance
170170
Code should be easy to read and understand. If a sacrifice is made for performance or readability, it should be documented.
171171

172+
Adhere to clang-format checks configured in `.clang-format`. Run `./scripts/clang-format.sh` if needed to confirm code styling.
173+
172174
### Static Analysis
173-
Adhere to clang-tidy checks configured in `.clang-tidy`.
175+
Adhere to clang-tidy checks configured in `.clang-tidy`. Run `./scripts/clang-tidy.sh` if needed to confirm code quality.
174176

175177
## Dependencies
176178

‎README.md‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ This SDK leverages various tools and checks to ensure the highest quality of the
521521
### Clang Tools
522522

523523
- `clang-tidy`: static analysis checks to catch common C++ pitfalls. See [.clang-tidy](./.clang-tidy) for the list of current checks (enforced in CI on PR)
524-
- `clang-format`: (coming soon) code formatting and style consistency
524+
- `clang-format`: code formatting and style consistency. See [.clang-format](./.clang-format) for the list of style configurations (enforced in CI on PR)
525525

526-
> **Note**: clang-tidy is not currently supported on Windows for this project because the Visual Studio CMake generator does not produce the compile_commands.json database that clang-tidy requires.
526+
> **Note**: For Windows, `clang-tidy` is not currently supported for this project because the Visual Studio CMake generator does not produce the compile_commands.json database that `clang-tidy` requires. Similarly, `clang-format` must be installed and run manually on Windows, referencing the root `.clang-format` file.
527527
528528
To run locally, first install the following:
529529

@@ -542,7 +542,13 @@ This installs `clang-format`, `clang-tidy`, and `run-clang-tidy`. Homebrew may a
542542
sudo apt-get install clang-format clang-tidy clang-tools
543543
```
544544

545-
To run:
545+
**Pre-commit hook**
546+
547+
```bash
548+
printf '#!/bin/sh\nFILES=$(git diff --cached --name-only --diff-filter=ACMR -- "*.c" "*.cc" "*.cpp" "*.cxx" "*.h" "*.hpp" "*.hxx")\n[ -z "$FILES" ] && exit 0\necho "$FILES" | xargs ./scripts/clang-format.sh --fix\necho "$FILES" | xargs git add\n' > .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
549+
```
550+
551+
To run `clang-tidy`:
546552

547553
1. Generate `compile_commands.json` and the protobuf headers via a release build:
548554

@@ -566,6 +572,24 @@ The wrapper forwards extra arguments to `run-clang-tidy`, examples below:
566572

567573
Output is captured to `clang-tidy.log` at the repo root. This is done as a convenience feature, as often times the terminal buffer is not large enough for all the output.
568574

575+
To run `clang-format`:
576+
577+
```bash
578+
./scripts/clang-format.sh
579+
```
580+
581+
With no arguments, runs `clang-format` against every relevant file in the repository against defined `.clang-format` rules.
582+
583+
Pass flags or paths as needed, examples below:
584+
585+
```bash
586+
./scripts/clang-format.sh --fix # Rewrite files in place
587+
./scripts/clang-format.sh src/room.cpp include/livekit/room.h # Check just these files
588+
./scripts/clang-format.sh --fix src/room.cpp # Fix just this file
589+
```
590+
591+
Output is captured to `clang-format.log` at the repo root.
592+
569593
### Memory Checks
570594

571595
Run valgrind on various examples or tests to check for memory leaks and other issues.

0 commit comments

Comments
 (0)