From 93bfe4c885451ad2d9617d84132b5cd340e11130 Mon Sep 17 00:00:00 2001 From: muxator Date: Fri, 29 Sep 2023 11:57:11 +0200 Subject: [PATCH] build: introduce clang-format in the code base, enforce via CI Individual developers should execute "make reformat-code" or equivalent at each commit; they can use a pre-commit hook if they want. The commit also adds a CI workflow that verifies that the tip of a PR (or of main) respects the clang-format rules. --- .clang-format | 11 +++++ .github/workflows/check-code-formatting.yml | 27 +++++++++++ Makefile | 50 +++++++++++++++++++++ README.md | 4 ++ 4 files changed, 92 insertions(+) create mode 100644 .clang-format create mode 100644 .github/workflows/check-code-formatting.yml create mode 100644 Makefile diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..c525129 --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +# Copyright (c) 2023 Bank of Italy +# Distributed under the GNU AGPLv3 software license, see the accompanying COPYING file. + +--- +Language: Cpp +BasedOnStyle: LLVM +ColumnLimit: 110 +# LineEnding: LF # requires clang-format >= 16, and ubuntu 22.04 has clang-format 15 at best +# InsertNewlineAtEOF: true # requires clang-format >= 16, and ubuntu 22.04 has clang-format 15 at best +PointerAlignment: Left +... diff --git a/.github/workflows/check-code-formatting.yml b/.github/workflows/check-code-formatting.yml new file mode 100644 index 0000000..1944f21 --- /dev/null +++ b/.github/workflows/check-code-formatting.yml @@ -0,0 +1,27 @@ +name: Check code formatting + +on: + push: + branches: + - main + pull_request: + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + check_format: + runs-on: ubuntu-22.04 + steps: + - name: Setup clang-format 15 (we'd like 16, but as of 2023-09-29 it's not there) + run: | + # TODO: once clang-16 becomes available, please enable the rules that + # are currently commented in /.clang_format + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-15 10 + sudo update-alternatives --set clang-format /usr/bin/clang-format-15 + - name: Checkout itcoin-fbft + uses: actions/checkout@v3 + with: + fetch-depth: 1 + - name: check code formatting + run: | + make check-code-formatting diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c6919e8 --- /dev/null +++ b/Makefile @@ -0,0 +1,50 @@ +# Copyright (c) 2023 Bank of Italy +# Distributed under the GNU AGPLv3 software license, see the accompanying COPYING file. + +.DEFAULT_GOAL := help + +# source: https://stackoverflow.com/questions/18136918/how-to-get-current-relative-directory-of-your-makefile#73509979 +MAKEFILE_ABS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([0-9a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef +export PRINT_HELP_PYSCRIPT + +# source: https://stackoverflow.com/questions/5618615/check-if-a-program-exists-from-a-makefile#25668869 +# +# please note that there should not be any tabs before the "ifeq/endif" +# statement +.PHONY: verify-prerequisites +verify-prerequisites: +ifeq (, $(shell command -v clang-format 2> /dev/null)) + $(error ERROR: please install clang-format) +endif + +.PHONY: check-code-formatting +# - globstar: enable recursive globbing via "**" in bash >= 4 (equivalent to +# shopt -s globstar) +# - nullglob: when a glob does not match anything, return "" instead of the +# literal glob text (equivalent to shopt -s globstar) +SHELL = /usr/bin/env bash -O globstar -O nullglob +check-code-formatting: verify-prerequisites ## Check if the code base is correctly formatted. Do not touch the files + clang-format --Werror --style=file:.clang-format --ferror-limit=20 --dry-run "${MAKEFILE_ABS_DIR}"/src/*/*.{h,hpp,c,cpp} + +.PHONY: reformat-code +# - globstar: enable recursive globbing via "**" in bash >= 4 (equivalent to +# shopt -s globstar) +# - nullglob: when a glob does not match anything, return "" instead of the +# literal glob text (equivalent to shopt -s globstar) +SHELL = /usr/bin/env bash -O globstar -O nullglob +reformat-code: verify-prerequisites ## Reformat the code base in the src directory. Can be used as pre-commit hook + clang-format --Werror --style=file:.clang-format -i --verbose "${MAKEFILE_ABS_DIR}"/src/**/*.{h,hpp,c,cpp} + +.PHONY: help +help: + @python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) diff --git a/README.md b/README.md index a44344c..b854205 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Frosted Byzantine Fault Tolerance (FBFT) [![tests](https://github.com/bancaditalia/itcoin-fbft/actions/workflows/test-itcoin-fbft.yml/badge.svg?branch=main&event=push)](https://github.com/bancaditalia/itcoin-fbft/actions/workflows/test-itcoin-fbft.yml) +[![clang-format](https://github.com/bancaditalia/itcoin-fbft/actions/workflows/check-code-formatting.yml/badge.svg?branch=main&event=push)](https://github.com/bancaditalia/itcoin-fbft/actions/workflows/check-code-formatting.yml) This repository contains the implementation of an itcoin "consensus node", which implements the Frosted Byzantine Fault Tolerance (FBFT) Proof-of-Authority consensus algorithm for the **itcoin** blockchain. @@ -14,12 +15,15 @@ Please note that this software is solely intended for testing and experimentatio 1. Install build and dev dependencies with apt (system-wide): +TODO: add documentation about clang-format in this file + ``` sudo apt install --no-install-recommends -y \ autoconf \ automake \ bsdextrautils \ ca-certificates \ + clang-format-15 \ cmake \ g++ \ gcc \