Skip to content

Commit

Permalink
Merge pull request #29 from stuarthayhurst/clang-linting
Browse files Browse the repository at this point in the history
Support clang-tidy, add a linting target and workflow
  • Loading branch information
stuarthayhurst authored Sep 3, 2024
2 parents be8416a + 5810768 commit f4975f9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
Checks: '-clang-diagnostic-#pragma-messages'
WarningsAsErrors: 'true,*'
---
24 changes: 24 additions & 0 deletions .github/workflows/run-clang-tidy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This workflow will lint the project using clang-tidy
name: Clang-tidy linting

on:
push:
branches: '**'
pull_request:
branches: '**'

jobs:
lint:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install build dependencies
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends make pkg-config build-essential
sudo apt-get install --no-install-recommends clang-tidy-18
sudo apt-get install --no-install-recommends libglm-dev libglfw3-dev libglew-dev libstb-dev libassimp-dev
- name: Run the linting target
run: |
make lint -j$(nproc)
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ HELPER_HEADER_SOURCE = $(shell ls ./src/helper/**/*.hpp)
DEMO_OBJECTS_SOURCE = $(shell ls ./src/demos/**/*.cpp)
DEMO_HEADER_SOURCE = $(shell ls ./src/demos/**/*.hpp)

ROOT_OBJECTS_SOURCE = $(shell ls ./src/*.cpp)

ALL_OBJECTS_SOURCE = $(AMMONITE_OBJECTS_SOURCE) $(HELPER_OBJECTS_SOURCE) $(DEMO_OBJECTS_SOURCE) \
$(ROOT_OBJECTS_SOURCE)
LINT_OBJECTS = $(subst ./src,$(OBJECT_DIR),$(subst .cpp,.linted,$(ALL_OBJECTS_SOURCE)))

OBJECT_DIR = $(BUILD_DIR)/objects
AMMONITE_OBJECTS = $(subst ./src,$(OBJECT_DIR),$(subst .cpp,.o,$(AMMONITE_OBJECTS_SOURCE)))
HELPER_OBJECTS = $(subst ./src,$(OBJECT_DIR),$(subst .cpp,.o,$(HELPER_OBJECTS_SOURCE)))
Expand Down Expand Up @@ -74,7 +80,18 @@ $(OBJECT_DIR)/threadDemo.o: ./src/threadDemo.cpp $(AMMONITE_HEADER_SOURCE)
@mkdir -p "$(OBJECT_DIR)"
$(CXX) ./src/threadDemo.cpp -c $(CXXFLAGS) -o "$@"

.PHONY: build demo threads debug library headers install uninstall clean cache icons $(AMMONITE_HEADER_INSTALL)
$(BUILD_DIR)/compile_flags.txt: Makefile
@mkdir -p "$(BUILD_DIR)"
@rm -fv "$(BUILD_DIR)/compile_flags.txt"
@for arg in $(CXXFLAGS); do \
echo $$arg >> "$(BUILD_DIR)/compile_flags.txt"; \
done
$(OBJECT_DIR)/%.linted: ./src/%.cpp $(BUILD_DIR)/compile_flags.txt .clang-tidy
clang-tidy -p "$(BUILD_DIR)" $(subst $(OBJECT_DIR),./src,$(subst .linted,.cpp,$(@)))
@mkdir -p "$$(dirname $@)"
@touch "$@"

.PHONY: build demo threads debug library headers install uninstall clean lint cache icons $(AMMONITE_HEADER_INSTALL)
build: demo threads
demo: $(BUILD_DIR)/demo
@if [[ "$(DEBUG)" != "true" ]]; then \
Expand Down Expand Up @@ -107,6 +124,7 @@ uninstall:
@if [[ -d "$(HEADER_DIR)/ammonite" ]]; then rm -rf "$(HEADER_DIR)/ammonite"; fi
clean: cache
@rm -rfv "$(BUILD_DIR)"
lint: $(LINT_OBJECTS)
cache:
@rm -rfv "$(CACHE_DIR)"
icons:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@
- Package names are correct for Debian, other distros may vary
- `make`
- `pkg-config`
- `coreutils`
- `g++` **OR** `clang`
- If using clang, use `CXX="clang++" make [TARGET]`
- When swapping between different compilers, run `make clean`
- ### Libraries:
- `libglm-dev libglfw3-dev libglew-dev libstb-dev libassimp-dev`
- `libdecor-0-0 libdecor-0-plugin-1-gtk` are required for Wayland window decorations
- ### Linting:
- `clang-tidy`
- ### Icons:
- `inkscape optipng`

Expand Down
3 changes: 2 additions & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

## Build system:
- ### Targets:
- `build`, `debug`, `library`, `demo` and `threads` support `-j[CORE COUNT]`
- `build`, `debug`, `library`, `demo`, `threads` and `lint` support `-j[CORE COUNT]`
- `make build` - Builds the demo and thread demo
- `make debug` - Cleans build directory, then runs `make build` in debug mode
- `make library` - Builds `build/libammonite.so`
Expand All @@ -25,6 +25,7 @@
- `make uninstall` - Removes installed library
- Custom install locations can be removed using the environment variable `INSTALL_DIR`
- `make icons` - Creates `assets/icons/icon-*.png` from `assets/icons/icon.svg`
- `make lint` - Lints the project using `clang-tidy`
- `make clean` - Cleans the build area (`build/`) and default runtime cache (`cache/`)
- `make cache` - Clears the default runtime binary cache, useful if running into issues with caching
- ### Flags:
Expand Down
6 changes: 2 additions & 4 deletions src/ammonite/graphics/renderHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ namespace ammonite {
static double maxError = (1.0 / 50000.0);
#define errorAdjustCoeff 1.01

//Figure out time budget remaining
double const targetFrameTime = 1.0 / *frameLimitPtr;
double spareTime = targetFrameTime - targetFrameTimer.getTime();

//Sleep for successively shorter intervals until the frametime budget is gone
double spareTime;
const double targetFrameTime = 1.0 / *frameLimitPtr;
while (targetFrameTime - targetFrameTimer.getTime() > maxError) {
spareTime = targetFrameTime - targetFrameTimer.getTime();
const auto sleepLength = std::chrono::nanoseconds(
Expand Down
6 changes: 4 additions & 2 deletions src/ammonite/models/modelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ namespace ammonite {
int index = 0;
for (unsigned int i = 0; i < meshPtr->mNumFaces; i++) {
aiFace face = meshPtr->mFaces[i];
std::memcpy(&newMesh->indices[index], &face.mIndices[0],
face.mNumIndices * sizeof(unsigned int));
if (&face.mIndices[0] != nullptr) {
std::memcpy(&newMesh->indices[index], &face.mIndices[0],
face.mNumIndices * sizeof(unsigned int));
}
index += face.mNumIndices;
}

Expand Down

0 comments on commit f4975f9

Please sign in to comment.