diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2021-04-05 13:19:19 +0200 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2021-04-05 13:19:19 +0200 |
commit | 661b635de6d65c27b57be22ecc0e3feacc45401e (patch) | |
tree | a57616bf23c8a2d5da89667d000c85818e2cec31 | |
parent | 0f1d99f75f7247d214b53173c66c9b8d24fcae74 (diff) | |
download | ccache-661b635de6d65c27b57be22ecc0e3feacc45401e.tar.gz |
Use Clang-Format executable from muttleyxd/clang-tools-static-binaries
misc/format-files now executes misc/clang-format, which works like this:
1. If environment variable CLANG_FORMAT is set, execute $CLANG_FORMAT.
2. Otherwise, if <ccache-top-dir>/misc/.clang-format-exe exists, execute
that program.
3. Otherwise, download a statically linked clang-format executable,
verify its integrity, place it in
<ccache-top-dir>/misc/.clang-format-exe and execute it.
This makes it possible for us to lock Clang-Format to version 10 and
remove most “// clang-format off/on” comments.
-rw-r--r-- | .clang-format | 2 | ||||
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | CMakeLists.txt | 36 | ||||
-rw-r--r-- | CONTRIBUTING.md | 12 | ||||
-rwxr-xr-x | misc/clang-format | 65 | ||||
-rwxr-xr-x | misc/format-files | 2 | ||||
-rw-r--r-- | src/Args.hpp | 6 | ||||
-rw-r--r-- | src/CacheEntryWriter.cpp | 5 | ||||
-rw-r--r-- | src/Fd.hpp | 4 | ||||
-rw-r--r-- | src/File.hpp | 4 |
10 files changed, 93 insertions, 46 deletions
diff --git a/.clang-format b/.clang-format index f40eea44..cf1d44ad 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,4 @@ -# This configuration should work with Clang-Format 6.0 and higher. +# This configuration should work with Clang-Format 10 and higher. --- Language: Cpp BasedOnStyle: LLVM @@ -1,6 +1,9 @@ # Typical build directories /build*/ +# Downloaded tools +misc/.clang-format-exe + # Emacs save files *~ diff --git a/CMakeLists.txt b/CMakeLists.txt index fc2ff665..0825f2af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -176,26 +176,16 @@ endif() # # Special formatting targets # -find_program( - CLANG_FORMAT_EXE - NAMES "clang-format" - DOC "Path to clang-format executable.") -mark_as_advanced(CLANG_FORMAT_EXE) # Don't show in CMake UIs - -if(NOT CLANG_FORMAT_EXE) - message(STATUS "clang-format not found") -else() - add_custom_target( - format - COMMAND misc/format-files --all - COMMENT "Formatting code" - USES_TERMINAL - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - - add_custom_target( - check_format - COMMAND misc/format-files --all --check - COMMENT "Checking code formatting" - USES_TERMINAL - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) -endif() +add_custom_target( + format + COMMAND misc/format-files --all + COMMENT "Formatting code" + USES_TERMINAL + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + +add_custom_target( + check_format + COMMAND misc/format-files --all --check + COMMENT "Checking code formatting" + USES_TERMINAL + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cb35bc0b..ad4b8aa8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -56,12 +56,12 @@ little. Source code formatting is defined by `.clang-format` in the root directory. The format is loosely based on [LLVM's code formatting -style](https://llvm.org/docs/CodingStandards.html) with some exceptions. It's -highly recommended to install -[Clang-Format](https://clang.llvm.org/docs/ClangFormat.html) 6.0 or newer and -run `make format` to format changes according to ccache's code style. Or even -better: set up your editor to run Clang-Format automatically when saving. If you -don't run Clang-Format then the ccache authors have to do it for you. +style](https://llvm.org/docs/CodingStandards.html) with some exceptions. Run +`make format` (or `ninja format` if you use Ninja) to format changes according +to ccache's code style. Or even better: set up your editor to run +`<ccache-top-dir>/misc/clang-format` (or any other Clang-Format version 10 +binary) automatically when saving. Newer Clang-Format versions likely also work +fine. Please follow these conventions: diff --git a/misc/clang-format b/misc/clang-format new file mode 100755 index 00000000..7b66672f --- /dev/null +++ b/misc/clang-format @@ -0,0 +1,65 @@ +#!/bin/sh +# +# This script executes clang-format in the following order: +# +# 1. If environment variable CLANG_FORMAT is set, execute $CLANG_FORMAT. +# 2. Otherwise, if <ccache-top-dir>/misc/.clang-format-exe exists, execute that +# program. +# 3. Otherwise, download a statically linked clang-format executable, verify its +# integrity, place it in <ccache-top-dir>/misc/.clang-format-exe and execute +# it. + +set -eu + +if [ -n "${CLANG_FORMAT:-}" ]; then + exec "$CLANG_FORMAT" "$@" +fi + +top_dir="$(dirname "$0")" +clang_format_exe="$top_dir/.clang-format-exe" +clang_format_version=10 +url_prefix="https://github.com/muttleyxd/clang-tools-static-binaries/releases/download/master-22538c65/clang-format-${clang_format_version}_" + +if [ ! -x "$clang_format_exe" ]; then + case "$(uname -s | tr '[:upper:]' '[:lower:]')" in + *mingw*|*cygwin*|*msys*) + url_suffix=windows-amd64.exe + checksum=0b21dfb9041437eebcc44032096e4dfba9ee7b668ee6867ada71ea3541f7b09657ea592a5b1cf659bc9f8072bdc477dfc9da07b3edacb7834b70e68d7a3fc887 + ;; + *darwin*) + url_suffix=macosx-amd64 + checksum=8458753e13d3cbb7949a302beab812bed6d9dd9001c6e9618e5ba2e438233633ae04704a4e150aa2abfbaf103f1df4bc4a77b306012f44b37e543964bd527951 + ;; + *linux*) + url_suffix=linux-amd64 + checksum=3c4aaa90ad83313a1d7b350b30f9ad62578be73241f00f7d6e92838289e0b734fab80dee9dfcbd1546135bdb4e3601cfb2cf6b47360fba0bfc961e5a7cab2015 + ;; + *) + echo "Error: Please set CLANG_FORMAT to clang-format version $clang_format_version" >&2 + exit 1 + ;; + esac + + url="$url_prefix$url_suffix" + + if command -v wget >/dev/null; then + wget -qO "$clang_format_exe.tmp" "$url" + elif command -v curl >/dev/null; then + curl -so "$clang_format_exe.tmp" -L --retry 20 "$url" + else + echo "Error: Neither wget nor curl found" >&2 + exit 1 + fi + + if ! command -v sha512sum >/dev/null; then + echo "Warning: sha512sum not found, not verifying clang-format integrity" >&2 + elif [ "$(sha512sum "$clang_format_exe.tmp" | awk '{print $1}')" != "$checksum" ]; then + echo "Error: Bad checksum of downloaded clang-format" >&2 + exit 1 + fi + + chmod +x "$clang_format_exe.tmp" + mv "$clang_format_exe.tmp" "$clang_format_exe" +fi + +exec "$clang_format_exe" "$@" diff --git a/misc/format-files b/misc/format-files index 111705ad..44340b9f 100755 --- a/misc/format-files +++ b/misc/format-files @@ -27,7 +27,7 @@ if [ -n "$all" ]; then exec sh "$0" $check $(git ls-files '*.[ch]' '*.[ch]pp' ':!:src/third_party') fi -clang_format=${CLANG_FORMAT:-clang-format} +clang_format="$(dirname "$0")/clang-format" [ -t 1 ] && cf_color="--color=always" || cf_color="" if [ -n "${VERBOSE:-}" ]; then diff --git a/src/Args.hpp b/src/Args.hpp index ae428096..48fb77df 100644 --- a/src/Args.hpp +++ b/src/Args.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Joel Rosdahl and other contributors +// Copyright (C) 2020-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -115,18 +115,14 @@ Args::size() const return m_args.size(); } -// clang-format off inline const std::string& Args::operator[](size_t i) const -// clang-format on { return m_args[i]; } -// clang-format off inline std::string& Args::operator[](size_t i) -// clang-format on { return m_args[i]; } diff --git a/src/CacheEntryWriter.cpp b/src/CacheEntryWriter.cpp index 8f45adfa..6c128e54 100644 --- a/src/CacheEntryWriter.cpp +++ b/src/CacheEntryWriter.cpp @@ -24,12 +24,9 @@ CacheEntryWriter::CacheEntryWriter(FILE* stream, Compression::Type compression_type, int8_t compression_level, uint64_t payload_size) - // clang-format off : m_compressor( - Compressor::create_from_type(compression_type, stream, compression_level) - ) + Compressor::create_from_type(compression_type, stream, compression_level)) { - // clang-format on uint8_t header_bytes[15]; memcpy(header_bytes, magic, 4); header_bytes[4] = version; @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Joel Rosdahl and other contributors +// Copyright (C) 2020-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -72,10 +72,8 @@ Fd::get() const return m_fd; } -// clang-format off inline int Fd::operator*() const -// clang-format on { ASSERT(m_fd != -1); return m_fd; diff --git a/src/File.hpp b/src/File.hpp index 6f0dd21c..f8f6c0be 100644 --- a/src/File.hpp +++ b/src/File.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2020 Joel Rosdahl and other contributors +// Copyright (C) 2019-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -89,10 +89,8 @@ inline File::operator bool() const return m_file; } -// clang-format off inline FILE* File::operator*() const -// clang-format on { return m_file; } |