From 28243dbc94c34e6f29ea94ebb2ef739dae0a36ca Mon Sep 17 00:00:00 2001 From: Colin Edwards Date: Mon, 8 Jul 2019 20:50:59 -0500 Subject: [PATCH] CI: Run clang format on linux and osx CI and fail if changes are made --- CI/before-script-linux.sh | 9 +++++++-- CI/before-script-osx.sh | 5 +++++ CI/check-format.sh | 11 +++++++++++ CI/install-dependencies-linux.sh | 13 ++++++++++++- CI/install-dependencies-osx.sh | 2 +- formatcode.sh | 31 +++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100755 CI/check-format.sh create mode 100755 formatcode.sh diff --git a/CI/before-script-linux.sh b/CI/before-script-linux.sh index 88ba09fda..e89ddb573 100755 --- a/CI/before-script-linux.sh +++ b/CI/before-script-linux.sh @@ -1,6 +1,11 @@ -#!/bin/sh -set -ex +#!/bin/bash +./formatcode.sh +if ! ./CI/check-format.sh; then + exit 1 +fi + +set -ex ccache -s || echo "CCache is not available." mkdir build && cd build cmake .. diff --git a/CI/before-script-osx.sh b/CI/before-script-osx.sh index 50a9a1916..331259ae2 100755 --- a/CI/before-script-osx.sh +++ b/CI/before-script-osx.sh @@ -3,6 +3,11 @@ export PATH=/usr/local/opt/ccache/libexec:$PATH git fetch --tags +./formatcode.sh +if ! ./CI/check-format.sh; then + exit 1 +fi + mkdir build cd build cmake -DENABLE_SPARKLE_UPDATER=ON \ diff --git a/CI/check-format.sh b/CI/check-format.sh new file mode 100755 index 000000000..7642c190a --- /dev/null +++ b/CI/check-format.sh @@ -0,0 +1,11 @@ +#!/bin/bash +dirty=$(git ls-files --modified) + +set +x +if [[ $dirty ]]; then + echo "=================================" + echo "Files were not formatted properly" + echo "$dirty" + echo "=================================" + exit 1 +fi \ No newline at end of file diff --git a/CI/install-dependencies-linux.sh b/CI/install-dependencies-linux.sh index 4ab021959..391c1818c 100755 --- a/CI/install-dependencies-linux.sh +++ b/CI/install-dependencies-linux.sh @@ -3,6 +3,16 @@ set -ex sudo add-apt-repository ppa:jonathonf/ffmpeg-3 -y curl -L https://packagecloud.io/github/git-lfs/gpgkey | sudo apt-key add - + +# gets us newer clang +sudo bash -c "cat >> /etc/apt/sources.list" << LLVMAPT +# 3.8 +deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-8 main +deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-8 main +LLVMAPT + +wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add - + sudo apt-get -qq update sudo apt-get install -y \ build-essential \ @@ -42,4 +52,5 @@ sudo apt-get install -y \ python3-dev \ qtbase5-dev \ libqt5svg5-dev \ - swig + swig \ + clang-format-8 diff --git a/CI/install-dependencies-osx.sh b/CI/install-dependencies-osx.sh index 24b801858..e8dcde0aa 100755 --- a/CI/install-dependencies-osx.sh +++ b/CI/install-dependencies-osx.sh @@ -26,7 +26,7 @@ sudo installer -pkg ./Packages.pkg -target / brew update #Base OBS Deps and ccache -brew install jack speexdsp ccache mbedtls +brew install jack speexdsp ccache mbedtls clang-format brew install https://gist.githubusercontent.com/DDRBoxman/b3956fab6073335a4bf151db0dcbd4ad/raw/ed1342a8a86793ea8c10d8b4d712a654da121ace/qt.rb brew install https://gist.githubusercontent.com/DDRBoxman/4cada55c51803a2f963fa40ce55c9d3e/raw/572c67e908bfbc1bcb8c476ea77ea3935133f5b5/swig.rb diff --git a/formatcode.sh b/formatcode.sh new file mode 100755 index 000000000..09f07eeff --- /dev/null +++ b/formatcode.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Original source https://github.com/Project-OSRM/osrm-backend/blob/master/scripts/format.sh + +set +x +set -o errexit +set -o pipefail +set -o nounset + +# Runs the Clang Formatter in parallel on the code base. +# Return codes: +# - 1 there are files to be formatted +# - 0 everything looks fine + +# Get CPU count +OS=$(uname) +NPROC=1 +if [[ $OS = "Linux" ]] ; then + NPROC=$(nproc) +elif [[ ${OS} = "Darwin" ]] ; then + NPROC=$(sysctl -n hw.physicalcpu) +fi + +# Discover clang-format +if type clang-format-8 2> /dev/null ; then + CLANG_FORMAT=clang-format-8 +else + CLANG_FORMAT=clang-format +fi + +find . -type d \( -path ./deps -o -path ./cmake -o -path ./plugins/decklink/win -o -path ./plugins/decklink/mac -o -path ./plugins/decklink/linux \) -prune -type f -o -name '*.h' -or -name '*.hpp' -or -name '*.m' -or -name '*.mm' -or -name '*.c' -or -name '*.cpp' \ +| xargs -I{} -P ${NPROC} ${CLANG_FORMAT} -i -style=file -fallback-style=none {} \ No newline at end of file