From e5b4a231fc92557b0f1cc4917c69966171754bda Mon Sep 17 00:00:00 2001 From: Daniil Zhukov Date: Sun, 7 Jul 2024 00:55:25 +0200 Subject: [PATCH] ci: tidy and format checks --- .github/actions/setup-compilers/action.yml | 21 +++++++++++++ .github/workflows/cmake-multi-platform.yml | 33 ++++++++++++-------- Makefile | 35 ++++++++++++++++------ 3 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 .github/actions/setup-compilers/action.yml diff --git a/.github/actions/setup-compilers/action.yml b/.github/actions/setup-compilers/action.yml new file mode 100644 index 0000000..9eb0b57 --- /dev/null +++ b/.github/actions/setup-compilers/action.yml @@ -0,0 +1,21 @@ +name: "Setup compilers" +description: "Setup compilers and build tools" +inputs: + clang: + required: false + default: "18" + gcc: + required: false + default: "14" +runs: + using: "composite" + steps: + - shell: bash + run: | + sudo apt update + sudo add-apt-repository universe multiverse + sudo apt update + sudo apt install gcc-${{ inputs.gcc }} g++-${{ inputs.gcc }} + wget https://apt.llvm.org/llvm.sh + chmod u+x llvm.sh + sudo ./llvm.sh ${{ inputs.clang }} \ No newline at end of file diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 9cf28be..d1aadb2 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -7,11 +7,29 @@ on: branches: [ "main" ] jobs: + tidy: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - name: Setup compilers + uses: ./.github/actions/setup-compilers + - name: Run clang tidy + run: make tidy CI=1 + + format: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - name: Setup compilers + uses: ./.github/actions/setup-compilers + - name: Run clang format + run: make format CI=1 + build: runs-on: ${{ matrix.os }} strategy: - + fail-fast: false matrix: os: [ubuntu-24.04] @@ -35,16 +53,7 @@ jobs: echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" - name: Setup compilers id: compilers - shell: bash - run: | - sudo apt update - sudo add-apt-repository universe multiverse - sudo apt update - sudo apt install gcc-14 g++-14 - wget https://apt.llvm.org/llvm.sh - chmod u+x llvm.sh - sudo ./llvm.sh 18 - + uses: ./.github/actions/setup-compilers - name: Configure CMake # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type @@ -66,4 +75,4 @@ jobs: working-directory: ${{ steps.strings.outputs.build-output-dir }} # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest --build-config ${{ matrix.build_type }} + run: ctest --build-config ${{ matrix.build_type }} \ No newline at end of file diff --git a/Makefile b/Makefile index bb83cd5..a01bfe6 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,24 @@ MAKEFILE_DIR = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) TESTS_BIN = $(MAKEFILE_DIR)/build/cserver_tests +CC = clang CXX = clang++ CMAKE_CMD ?= cmake CMAKE_BUILD_TYPE ?= Debug -CMAKE_GENERATOR ?= Ninja + +ifdef CI + CMAKE_GENERATOR ?= "Unix Makefiles" +else + CMAKE_GENERATOR ?= Ninja +endif CMAKE_FLAGS += -DCMAKE_CXX_COMPILER=$(CXX) \ - -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) \ - -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ - -DENABLE_TESTS=1 \ - -DENABLE_EXAMPLES=1 + -DCMAKE_C_COMPILER=$(CC) \ + -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ + -DENABLE_TESTS=1 \ + -DENABLE_EXAMPLES=1 # Only need to handle Ninja here. # Make will inherit the VERBOSE variable, and the -j, -l, and -n flags. @@ -28,6 +35,12 @@ else BUILD_TOOL = $(MAKE) endif +ifdef CI + FORMAT_FLAGS = -n --Werror +else + FORMAT_FLAGS = -i +endif + all: $(TESTS_BIN) build/.ran-cmake: @@ -42,8 +55,8 @@ cmake: $(MAKE) build/.ran-cmake format: - clang-format -Werror -i \ - $(shell find include -name *.hpp) \ + clang-format $(FORMAT_FLAGS) \ + $(shell find include -name *.hpp) \ $(shell find examples -name *.cpp) \ $(shell find tests -name *.cpp) @@ -53,11 +66,15 @@ clean: distclean: rm -rf build -# Test - test: $(TESTS_BIN) $(TESTS_BIN) +tidy: build/.ran-cmake + clang-tidy -p build \ + $(shell find include -name *.hpp) \ + $(shell find examples -name *.cpp) \ + $(shell find tests -name *.cpp) + FORCE: ; .PHONY: all cmake test format clean distclean \ No newline at end of file