Skip to content

Instantly share code, notes, and snippets.

@tristan0x
Last active August 6, 2018 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristan0x/b629bdf79ec500d8b83bad31fda86e80 to your computer and use it in GitHub Desktop.
Save tristan0x/b629bdf79ec500d8b83bad31fda86e80 to your computer and use it in GitHub Desktop.
Enable https://github.com/tristan0x/git-cmake-format in a CMake existing project
---
Language: Cpp
BasedOnStyle: LLVM
AlwaysBreakTemplateDeclarations: true
BraceWrapping:
FixNamespaceComments: true
IndentWidth: 4
PointerAlignment: Left
#!/bin/sh
set -e
# This script is meant for quick & install install via:
# $ curl -fsSL <gist> -o git-cmake-format-install.sh
# $ sh git-cmake-format-install.sh
CLANG_FORMAT_STYLE='https://gist.githubusercontent.com/tristan0x/b629bdf79ec500d8b83bad31fda86e80/raw/bfbb5118086a2862c82f486aa8305d2778eca405/.clang-format'
SUBMODULE_PATH=${1:-deps/git-cmake-format}
bold=$(tput bold) normal=$(tput sgr0)
if ! [ -d .git ] ; then
echo "Cannot find .git directory. Abort" >&2
exit 1
fi
if ! git diff-index --quiet HEAD -- ;then
echo "Error: working copy has uncommitted changes. Abort" >&2
exit 1
fi
if ! [ -f CMakeLists.txt ] ; then
echo "Cannot find CMakeLists.txt. Abort" >&2
fi
if ! [ -f .gitmodules ] || ! grep -q "${SUBMODULE_PATH}" .gitmodules ; then
echo "${bold}Adding '$SUBMODULE_PATH' git submodule${normal}"
git submodule add -b bbp \
https://github.com/BlueBrain/git-cmake-format.git \
"$SUBMODULE_PATH"
fi
if ! grep -q "${SUBMODULE_PATH}" CMakeLists.txt ; then
echo "${bold}Patching CMakefile to include '$SUBMODULE_PATH'${normal}"
sed -i "s@^\(project(.*\)\$@\1\nadd_subdirectory(${SUBMODULE_PATH})@" CMakeLists.txt
git add CMakeLists.txt
fi
if ! [ -f .clang-format ] ; then
echo "${bold}Adding HPC team .clang-format${normal}"
curl -fsSL "$CLANG_FORMAT_STYLE" -o .clang-format
git add .clang-format
fi
if ! [ -d build ] ; then
echo "${bold}Building CMake project in new 'build' directory${normal}"
mkdir build
(cd build ; cmake .. || true)
else
echo "${bold}Building CMake project in 'build' directory${normal}"
(cd build ; cmake . || true)
fi
if [ -f build/CMakeCache.txt ] ; then
echo "${bold}Formatting C/C++ source files:${normal}"
clang_format=`grep ^CLANG_FORMAT_EXECUTABLE: build/CMakeCache.txt | cut -d= -f2`
if [ "x$clang_format" = x ] ;then
clang_format=clang-format
fi
IFS=$"\n"
git ls-files | while IFS= read -r file
do
case "$file" in
*.h|*.cpp|*.hpp|*.c|*.cc|*.hh|*.cxx|*.hxx)
echo "-- $file"
"$clang_format" -style=file -i "$file"
git add "$file"
;;
esac
done
IFS="$OLD_IFS"
fi
echo "${bold}Listing changes with command: git status${normal}"
git status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment