Skip to content

Commit

Permalink
Refactor: format, include-guards, copyrights, git-hooks (#222)
Browse files Browse the repository at this point in the history
* refactor: change copyright comments
* refactor: replace include-guards by pragma-once
* update: clang-format config
* feature: git-hook for format
* fix: target `clang-format`
* refactor: reapply updated clang-format config
* update: bump version to 0.1.17
* fix: clang-format version selector

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon authored Oct 26, 2023
1 parent e1c2393 commit d117c3c
Show file tree
Hide file tree
Showing 718 changed files with 4,827 additions and 4,018 deletions.
23 changes: 18 additions & 5 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
#
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

BasedOnStyle: Google
NamespaceIndentation: All
BreakBeforeBinaryOperators: NonAssignment
AlignOperands: false
AlignOperands: AlignAfterOperator
DerivePointerAlignment: false
PointerAlignment: Right
BinPackArguments: true
BinPackParameters: true
BinPackArguments: false
BinPackParameters: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
IncludeBlocks: Preserve
#InsertBraces: true
InsertBraces: true
InsertTrailingCommas: Wrapped
SortIncludes: CaseSensitive
QualifierAlignment: Custom
QualifierOrder: ['inline', 'static', 'constexpr', 'const', 'volatile', 'type', 'restrict']
ReferenceAlignment: Right
# uncomment in clang-format-16
#RemoveSemicolon: true
#InsertNewlineAtEOF: true
27 changes: 27 additions & 0 deletions .githooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[//]: # (
Copyright Quadrivium LLC
All Rights Reserved
SPDX-License-Identifier: Apache-2.0
)

# Git Hooks

This folder _might_ contain some git-hooks to execute various checkup in Kagome.

To activate presented (_and all future_) hooks just execute **once** shell-script [activate_hooks.sh](./activate_hooks.sh) from Kagome project root path.

## pre-commit

This hook check existing `clang-format` and `cmake-format` and their versions.
If they have recommended version, specific checkup is enabled.

Each changed C++ file (`.hpp` and `.cpp` extensions) gets processed by `clang-format`.

Each changed CMake file (`CMakeLists.txt` and `.cmake` extension) gets processed by `cmake-format`

Commit will be blocked while there are any differences between original files and `clang-format/cmake-format` output files.

## etc.

_Other hooks might be provided by maintainers in the future._

22 changes: 22 additions & 0 deletions .githooks/activate_hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
#
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

GIT=$(which git)

if [ -z "${GIT}" ]; then
echo "Command ``git'' command not found"
exit 1
fi

cd "$(dirname "$0")/.." || (echo "Run script from file" | exit 1)

chmod +x .githooks/*

${GIT} config --local core.hooksPath .githooks || (echo "Hooks activation has failed" | exit 1)

echo "Hooks activated successfully"
exit 0
91 changes: 91 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/bin/sh
#
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

if git rev-parse --verify HEAD >/dev/null 2>&1; then
BASE=HEAD
else
# Initial commit: diff BASE an empty tree object
BASE=$(git hash-object -t tree /dev/null)
fi

# check clang-format binary
CLANG_FORMAT_ENABLED=1
CLANG_FORMAT=$(which clang-format-15)
if [ -z "${CLANG_FORMAT}" ]; then
CLANG_FORMAT=$(which clang-format)
if [ -z "${CLANG_FORMAT}" ]; then
echo "Command clang-format is not found" >&2
echo "Please, install clang-format version 15 to enable checkup C++-files formatting over git pre-commit hook" >&2
CLANG_FORMAT_ENABLED=0
fi
fi

# check clang-format version
if [ $CLANG_FORMAT_ENABLED ]; then
CLANG_FORMAT_VERSION=$($CLANG_FORMAT --version | sed -r "s/.*version ([[:digit:]]+).*/\1/")

if [ "$CLANG_FORMAT_VERSION" != "15" ]; then
echo "Please, install clang-format version 15 to enable checkup C++-files formatting over git pre-commit hook" >&2
CLANG_FORMAT_ENABLED=0
fi
fi

FILES=$(git diff --staged --diff-filter=ACMR --name-only)

# check c++ files' format with clang-format
CXX_RES=0
if [ $CLANG_FORMAT_ENABLED ]; then
# for FILE in $(git diff-index --name-only "${BASE}" --diff-filter=ACMR | grep -e "\\.[ch]pp$"); do
for FILE in $(echo "$FILES" | grep -e "\\.[ch]pp$"); do
O_HASH=$(shasum <"${FILE}")
F_HASH=$(${CLANG_FORMAT} --style=file "$FILE" | shasum)
if [ "${O_HASH}" != "${F_HASH}" ]; then
echo "File looks nonformatted: $FILE"
CXX_RES=1
fi
done

if [ $CXX_RES = 1 ]; then
CLANG_FORMAT_VERSION_FULL=$($CLANG_FORMAT --version | sed -r "s/.*version ([[:digit:]\.]+).*/\1/")
echo "Used clang-format version $CLANG_FORMAT_VERSION_FULL" >&2
fi
fi

## check cmake-format binary
#CMAKE_FORMAT_ENABLED=1
#CMAKE_FORMAT=$(which cmake-format)
#if [ -z "${CMAKE_FORMAT}" ]; then
# echo "Command cmake-format is not found" >&2
# echo "Please, install cmake-format version 15 to enable checkup cmake-files formatting over git pre-commit hook" >&2
# CMAKE_FORMAT_ENABLED=0
#fi
#
## check cmake-files' format with cmake-format
#CMAKE_RES=0
#if [ $CMAKE_FORMAT_ENABLED ]; then
# for FILE in $(echo "$FILES" | grep -e "\(\(CMakeLists\\.txt\)\|\(\\.cmake\)\)$"); do
# O_HASH=$(shasum <"${FILE}")
# F_HASH=$(${CMAKE_FORMAT} "$FILE" | shasum)
# if [ "${O_HASH}" != "${F_HASH}" ]; then
# echo "File looks nonformatted: $FILE"
# CMAKE_RES=1
# fi
# done
#
# if [ $CMAKE_RES = 1 ]; then
# CMAKE_FORMAT_VERSION_FULL=$($CMAKE_FORMAT --version)
# echo "Used cmake-format version $CMAKE_FORMAT_VERSION_FULL" >&2
# fi
#fi

# result of checks
if [ "$CXX_RES" = "1" ] || [ "$CMAKE_RES" = "1" ]; then
echo "Formatter required" >&2
exit 1
fi

exit 0
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cmake_policy(SET CMP0135 NEW)

include("cmake/Hunter/init.cmake")

project(libp2p VERSION 0.1.15 LANGUAGES C CXX)
project(libp2p VERSION 0.1.17 LANGUAGES C CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Expand Down Expand Up @@ -94,6 +94,7 @@ if (CLANG_TIDY)
include(cmake/clang-tidy.cmake)
endif ()
if (CLANG_FORMAT)
set(RECOMMENDED_CLANG_FORMAT_VERSION 15)
include(cmake/clang-format.cmake)
endif ()

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ Target C++ compilers are:

To clone repository execute
```
git clone https://github.com/soramitsu/libp2p.git
git clone https://github.com/libp2p/libp2p-cpp.git
```

### Build cpp-libp2p

First build will likely take long time. However, you can cache binaries to [hunter-binary-cache](https://github.com/soramitsu/hunter-binary-cache) or even download binaries from the cache in case someone has already compiled project with the same compiler. To do so you need to set up two environment variables:
First build will likely take long time. However, you can cache binaries to [hunter-binary-cache](https://github.com/qdrvm/hunter-binary-cache) or even download binaries from the cache in case someone has already compiled project with the same compiler. To do so you need to set up two environment variables:
```
GITHUB_HUNTER_USERNAME=<github account name>
GITHUB_HUNTER_TOKEN=<github token>
Expand Down Expand Up @@ -75,18 +75,18 @@ find_package(libp2p REQUIRED)
To set which version of cpp-libp2p to use it is required to add these lines to Hunter/config.cmake file:
```cmake
hunter_config(libp2p
URL https://github.com/soramitsu/libp2p/archive/dad84a03a9651c7c2bb8a8f17d0e5ea67bd10b4f.zip
URL https://github.com/libp2p/cpp-libp2p/archive/dad84a03a9651c7c2bb8a8f17d0e5ea67bd10b4f.zip
SHA1 860742c6e3e9736d68b392513d795e09572780aa
)
```
Where URL is the link to archive of certain commit in cpp-libp2p repo and SHA1 is the checksum of this archive.
By simply updating URL and SHA1 it is possible to change the version of cpp-libp2p in another project.

Example of adding cpp-libp2p to other project can be found [here](https://github.com/soramitsu/kagome/blob/3edda60f27d378a21fc57cd8bec7f0f519203318/cmake/dependencies.cmake#L59) and [here](https://github.com/soramitsu/kagome/blob/3edda60f27d378a21fc57cd8bec7f0f519203318/cmake/Hunter/config.cmake#L24)
Example of adding cpp-libp2p to other project can be found [here](https://github.com/qdrvm/kagome/blob/3edda60f27d378a21fc57cd8bec7f0f519203318/cmake/dependencies.cmake#L59) and [here](https://github.com/qdrvm/kagome/blob/3edda60f27d378a21fc57cd8bec7f0f519203318/cmake/Hunter/config.cmake#L24)

## Notable users

* https://github.com/soramitsu/kagome
* https://github.com/qdrvm/kagome
* https://github.com/filecoin-project/cpp-filecoin

## Maintenance
Expand Down
2 changes: 1 addition & 1 deletion cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Template for add custom hunter config
# Useful when there is a need for a non-default version or arguments of a dependency,
# or when a project not registered in soramistu-hunter should be added.
# or when a project not registered in Hunter should be added.
#
# hunter_config(
# package-name
Expand Down
4 changes: 2 additions & 2 deletions cmake/Hunter/init.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# specify GITHUB_HUNTER_TOKEN and GITHUB_HUNTER_USERNAME to automatically upload binary cache to github.com/soramitsu/hunter-binary-cache
# specify GITHUB_HUNTER_TOKEN and GITHUB_HUNTER_USERNAME to automatically upload binary cache to github.com/qdrvm/hunter-binary-cache
# https://docs.hunter.sh/en/latest/user-guides/hunter-user/github-cache-server.html
string(COMPARE EQUAL "$ENV{GITHUB_HUNTER_TOKEN}" "" password_is_empty)
string(COMPARE EQUAL "$ENV{GITHUB_HUNTER_USERNAME}" "" username_is_empty)

# binary cache can be uploaded to soramitsu/hunter-binary-cache so others will not build same dependencies twice
# binary cache can be uploaded to qdrvm/hunter-binary-cache so others will not build same dependencies twice
if (NOT password_is_empty AND NOT username_is_empty)
option(HUNTER_RUN_UPLOAD "Upload cache binaries" YES)
message("Binary cache uploading is ENABLED.")
Expand Down
56 changes: 49 additions & 7 deletions cmake/clang-format.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
file(GLOB_RECURSE
ALL_CXX_SOURCE_FILES
include/*.[ch]pp
include/*.[ch]
src/*.[ch]pp
src/*.[ch]
test/*.[ch]pp
Expand All @@ -8,16 +10,56 @@ file(GLOB_RECURSE

# Adding clang-format target if executable is found
if(NOT CLANG_FORMAT_BIN)
find_program(CLANG_FORMAT_BIN "clang-format")
# Find particular version
find_program(CLANG_FORMAT_BIN NAMES clang-format-${RECOMMENDED_CLANG_FORMAT_VERSION})
if(NOT CLANG_FORMAT_BIN)
# Find default version and check it
find_program(CLANG_FORMAT_BIN NAMES clang-format)
if(CLANG_FORMAT_BIN)
execute_process(
COMMAND ${CLANG_FORMAT_BIN} --version
COMMAND sed -r "s/.*version \([[:digit:]]+\).*/\\1/"
OUTPUT_VARIABLE DEFAULT_CLANG_FORMAT_VERSION
)
math(EXPR DEFAULT_CLANG_FORMAT_VERSION "0 + 0${DEFAULT_CLANG_FORMAT_VERSION}")

if (${RECOMMENDED_CLANG_FORMAT_VERSION} GREATER_EQUAL ${DEFAULT_CLANG_FORMAT_VERSION})
set(BEGIN ${RECOMMENDED_CLANG_FORMAT_VERSION})
set(END ${DEFAULT_CLANG_FORMAT_VERSION})
else()
set(BEGIN ${DEFAULT_CLANG_FORMAT_VERSION})
set(END ${RECOMMENDED_CLANG_FORMAT_VERSION})
endif ()
# Try to find newest version
foreach(CLANG_FORMAT_VERSION RANGE ${BEGIN} ${END} -1)
find_program(CLANG_FORMAT_BIN_ NAMES clang-format-${CLANG_FORMAT_VERSION})
if(CLANG_FORMAT_BIN_)
message(STATUS "Found clang-format version ${CLANG_FORMAT_VERSION}")
set(CLANG_FORMAT_BIN ${CLANG_FORMAT_BIN_})
break()
endif()
endforeach()
endif()
endif()
endif()

if(CLANG_FORMAT_BIN)
message(STATUS "Target clang-format enabled")
execute_process(
COMMAND ${CLANG_FORMAT_BIN} --version
COMMAND sed -r "s/.*version \([[:digit:]]+\).*/\\1/"
OUTPUT_VARIABLE CLANG_FORMAT_VERSION
)
math(EXPR CLANG_FORMAT_VERSION "0 + 0${CLANG_FORMAT_VERSION}")
if(NOT ${CLANG_FORMAT_VERSION} EQUAL ${RECOMMENDED_CLANG_FORMAT_VERSION})
message(WARNING "Found clang-format version ${CLANG_FORMAT_VERSION}, "
"but version ${RECOMMENDED_CLANG_FORMAT_VERSION} is recommended")
endif()

message(STATUS "Target clang-format is enabled")
add_custom_target(
clang-format
COMMAND "${CLANG_FORMAT_BIN}"
-style=file
-i
${ALL_CXX_SOURCE_FILES}
clang-format
COMMAND "${CLANG_FORMAT_BIN}"
-i
${ALL_CXX_SOURCE_FILES}
)
endif()
5 changes: 3 additions & 2 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

Expand Down Expand Up @@ -43,6 +44,6 @@ find_package(tsl_hat_trie CONFIG REQUIRED)
hunter_add_package(Boost.DI)
find_package(Boost.DI CONFIG REQUIRED)

# https://github.com/soramitsu/libp2p-sqlite-modern-cpp/tree/hunter
# https://github.com/qdrvm/libp2p-sqlite-modern-cpp/tree/hunter
hunter_add_package(SQLiteModernCpp)
find_package(SQLiteModernCpp CONFIG REQUIRED)
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version: '3'

services:
dev:
image: soramitsu/kagome-dev:8
image: qdrvm/kagome-dev:8
tty: true
stdin_open: true
volumes:
Expand Down
3 changes: 2 additions & 1 deletion example/01-echo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

Expand Down
3 changes: 2 additions & 1 deletion example/01-echo/libp2p_echo_client.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down
3 changes: 2 additions & 1 deletion example/01-echo/libp2p_echo_server.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down
3 changes: 2 additions & 1 deletion example/02-kademlia/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#

Expand Down
2 changes: 1 addition & 1 deletion example/02-kademlia/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ After connection was established you can start chatting by typing a message in t
```log
Hello! Where are you from?
12D3KooWN4U92XG49kSjJ6p5TUEWxj76V432HfbmQAXwryLApkue < Hello! Where are you from?
12D3KooWN4U92XG49kSjJ6p5TUEWxj76V432HfbmQAXwryLApkue > Hi! I'm from Soramitsu.
12D3KooWN4U92XG49kSjJ6p5TUEWxj76V432HfbmQAXwryLApkue > Hi! I'm from Mars.
```

### Note
Expand Down
3 changes: 2 additions & 1 deletion example/02-kademlia/rendezvous_chat.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down
Loading

0 comments on commit d117c3c

Please sign in to comment.