Skip to content
Snippets Groups Projects
Commit da656a16 authored by Alexis Girault's avatar Alexis Girault
Browse files

WIP: Add Uncrustify for code style

- Still need to edit the configuration file
- Removed example target for kwstyle
parent 828da97d
No related branches found
No related tags found
No related merge requests found
#-----------------------------------------------------------------------------
# Define and configure configuration files
# Define configuration files
#-----------------------------------------------------------------------------
set(kwstyle_configuration_file "Utilities/KWStyle/${PROJECT_NAME}.kws.xml")
set(kwstyle_overwrite_file "Utilities/KWStyle/KWStyleOverwrite.txt")
set(kwstyle_examples_files_list_file "Utilities/KWStyle/${PROJECT_NAME}ExamplesFiles.txt")
configure_file(
${CMAKE_SOURCE_DIR}/${kwstyle_examples_files_list_file}.in
${CMAKE_BINARY_DIR}/${kwstyle_examples_files_list_file}
)
#-----------------------------------------------------------------------------
# Setup git hook for KWStyle executable path
......@@ -43,61 +38,4 @@ if(GIT_FOUND)
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif()
endif()
#-----------------------------------------------------------------------------
# Define formatting options for error messages of build target output
#-----------------------------------------------------------------------------
option(KWSTYLE_USE_VIM_FORMAT
"Set KWStyle to generate errors with a VIM-compatible format."
OFF
)
option(KWSTYLE_USE_GCC_FORMAT
"Set KWStyle to generate errors with a GCC-compatible format."
OFF
)
option(KWSTYLE_USE_MSVC_FORMAT
"Set KWStyle to generate errors with a VisualStudio-compatible format."
${MSVC_IDE} # default to TRUE only with a Visual Studio IDE
)
mark_as_advanced(KWSTYLE_USE_VIM_FORMAT)
mark_as_advanced(KWSTYLE_USE_GCC_FORMAT)
mark_as_advanced(KWSTYLE_USE_MSVC_FORMAT)
#-----------------------------------------------------------------------------
# Define Editor format
#-----------------------------------------------------------------------------
set(kwstyle_editor_format "")
if(KWSTYLE_USE_VIM_FORMAT)
list(APPEND kwstyle_editor_format -vim)
endif()
if(KWSTYLE_USE_GCC_FORMAT)
list(APPEND kwstyle_editor_format -gcc)
endif()
if(KWSTYLE_USE_MSVC_FORMAT)
list(APPEND kwstyle_editor_format -msvc)
endif()
#-----------------------------------------------------------------------------
# Check only one format is set to TRUE
#-----------------------------------------------------------------------------
list(LENGTH kwstyle_editor_format kwstyle_editor_format_length)
if(kwstyle_editor_format_length GREATER 1)
message(FATAL_ERROR "At most, only one of KWSTYLE_USE_*_FORMAT can be set to TRUE.")
endif()
#-----------------------------------------------------------------------------
# Add build target for examples
#-----------------------------------------------------------------------------
add_custom_target(KWStyleCheckExamples
COMMAND ${KWSTYLE_EXECUTABLE}
-xml ${CMAKE_SOURCE_DIR}/${kwstyle_configuration_file}
-o ${CMAKE_SOURCE_DIR}/${kwstyle_overwrite_file}
-D ${CMAKE_BINARY_DIR}/${kwstyle_examples_files_list_file}
-v
${kwstyle_editor_format}
COMMENT
"Examples Style Checker"
WORKING_DIRECTORY
${CMAKE_SOURCE_DIR} # the paths in kwstyle_configuration_file are relative
)
endif()
\ No newline at end of file
#-----------------------------------------------------------------------------
# Define configuration files
#-----------------------------------------------------------------------------
set(UNCRUSTIFY_CONFIG "${CMAKE_SOURCE_DIR}/Utilities/Uncrustify/${PROJECT_NAME}Uncrustify.cfg")
#-----------------------------------------------------------------------------
# Setup git hook for KWStyle executable path
#-----------------------------------------------------------------------------
find_package(Git)
if(GIT_FOUND)
if(EXISTS "${UNCRUSTIFY_EXECUTABLE}" AND EXISTS "${UNCRUSTIFY_CONFIG}")
message(STATUS "Uncrustify found: enabling in git hook")
execute_process(COMMAND ${GIT_EXECUTABLE} config uncrustify.path "${UNCRUSTIFY_EXECUTABLE}"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
execute_process(COMMAND ${GIT_EXECUTABLE} config uncrustify.conf "${UNCRUSTIFY_CONFIG}"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
execute_process( COMMAND ${GIT_EXECUTABLE} config uncrustify.enabled true
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
else()
if(NOT EXISTS "${UNCRUSTIFY_EXECUTABLE}")
message(STATUS "Uncrustify executable not found: disabling in git hook")
execute_process(COMMAND ${GIT_EXECUTABLE} config --unset uncrustify.path
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif()
if(NOT EXISTS "${UNCRUSTIFY_CONFIG}")
message(STATUS "Uncrustify config file not found: disabling in git hook")
execute_process(COMMAND ${GIT_EXECUTABLE} config --unset uncrustify.conf
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif()
execute_process(COMMAND ${GIT_EXECUTABLE} config uncrustify.enabled false
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif()
endif()
set(proj Uncrustify)
string(TOUPPER "${proj}" PROJ)
#-----------------------------------------------------------------------------
# Make sure this file is included only once
#-----------------------------------------------------------------------------
get_filename_component(CMAKE_CURRENT_LIST_FILENAME ${CMAKE_CURRENT_LIST_FILE} NAME_WE)
if(${CMAKE_CURRENT_LIST_FILENAME}_FILE_INCLUDED)
return()
endif()
set(${CMAKE_CURRENT_LIST_FILENAME}_FILE_INCLUDED 1)
#-----------------------------------------------------------------------------
# Sanity checks
#-----------------------------------------------------------------------------
if(DEFINED ${PROJ}_EXECUTABLE AND NOT EXISTS ${${PROJ}_EXECUTABLE})
message(FATAL_ERROR "${PROJ}_EXECUTABLE variable is defined but corresponds to non-existing file")
endif()
#-----------------------------------------------------------------------------
# Set dependency list
#-----------------------------------------------------------------------------
set(${proj}_DEPENDENCIES "")
#-----------------------------------------------------------------------------
# If needs to download and build
#-----------------------------------------------------------------------------
if(NOT DEFINED ${PROJ}_EXECUTABLE AND NOT ${USE_SYSTEM_${PROJ}})
#-----------------------------------------------------------------------------
# Set project Git info
#-----------------------------------------------------------------------------
set(${proj}_REPOSITORY ${git_protocol}://github.com/uncrustify/uncrustify.git)
set(${proj}_GIT_TAG cf7bbac22c65df32dea3f0abf949cba80fc66d00)
#-----------------------------------------------------------------------------
# Set project directory
#-----------------------------------------------------------------------------
set(${proj}_SOURCE_DIR ${CMAKE_BINARY_DIR}/Superbuild/${proj})
set(${proj}_DIR ${CMAKE_BINARY_DIR}/Superbuild/${proj}-build)
#-----------------------------------------------------------------------------
# Add project
#-----------------------------------------------------------------------------
include(ExternalProject)
ExternalProject_add(${proj}
GIT_REPOSITORY ${${proj}_REPOSITORY}
GIT_TAG ${${proj}_GIT_TAG}
UPDATE_COMMAND ""
SOURCE_DIR ${${proj}_SOURCE_DIR}
BINARY_DIR ${${proj}_DIR}
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=${${proj}_DIR}
DEPENDS ${${proj}_DEPENDENCIES}
)
set(${PROJ}_EXECUTABLE ${${proj}_DIR}/bin/uncrustify)
#-----------------------------------------------------------------------------
# If using system
#-----------------------------------------------------------------------------
else()
if(${USE_SYSTEM_${PROJ}})
find_program(${PROJ}_EXECUTABLE ${proj} DOC "Path of ${proj} program")
if(NOT ${PROJ}_EXECUTABLE)
message(FATAL_ERROR "To use the system ${proj}, set ${PROJ}_EXECUTABLE")
endif()
endif()
endif()
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.2)
project(iMSTK VERSION 0.0.1 LANGUAGES CXX)
......@@ -57,18 +57,10 @@ else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
#-----------------------------------------------------------------------------
# Define ThirdParty dependencies
#-----------------------------------------------------------------------------
# KWStyle
option(${PROJECT_NAME}_USE_KWSTYLE "Use KWStyle to check for coding standards violations." ON)
option(USE_SYSTEM_KWSTYLE "Exclude KWStyle from superbuild and use an existing build." OFF)
mark_as_advanced(USE_SYSTEM_KWSTYLE)
#-----------------------------------------------------------------------------
# Git protocol option
#-----------------------------------------------------------------------------
if(NOT GIT_EXECUTABL )
if(NOT GIT_EXECUTABLE)
find_package(Git REQUIRED)
endif()
......@@ -78,6 +70,19 @@ if(NOT ${PROJECT_NAME}_USE_GIT_PROTOCOL)
set(git_protocol "http")
endif()
#-----------------------------------------------------------------------------
# Define ThirdParty dependencies
#-----------------------------------------------------------------------------
# KWStyle
option(${PROJECT_NAME}_USE_KWSTYLE "Use KWStyle to check for coding standards violations." OFF)
option(USE_SYSTEM_KWSTYLE "Exclude KWStyle from superbuild and use an existing build." OFF)
mark_as_advanced(USE_SYSTEM_KWSTYLE)
# Uncrustify
option(${PROJECT_NAME}_USE_UNCRUSTIFY "Use Uncrustify to check for coding standards violations." ON)
option(USE_SYSTEM_UNCRUSTIFY "Exclude Uncrustify from superbuild and use an existing build." OFF)
mark_as_advanced(USE_SYSTEM_UNCRUSTIFY)
#-----------------------------------------------------------------------------
# Superbuild
#-----------------------------------------------------------------------------
......@@ -86,7 +91,15 @@ mark_as_advanced(${PROJECT_NAME}_SUPERBUILD)
set(${PROJECT_NAME}_BINARY_INNER_SUBDIR ${PROJECT_NAME}-build)
if(${PROJECT_NAME}_SUPERBUILD)
include(External_KWStyle)
if(${PROJECT_NAME}_USE_KWSTYLE)
include(External_KWStyle)
endif()
if(${PROJECT_NAME}_USE_UNCRUSTIFY)
include(External_Uncrustify)
endif()
endif()
#-----------------------------------------------------------------------------
......@@ -94,7 +107,12 @@ endif()
#-----------------------------------------------------------------------------
# KWStyle
if(${PROJECT_NAME}_USE_KWSTYLE)
include(ConfigKWStyle)
include(SetupKWStyleHook)
endif()
# Uncrustify
if(${PROJECT_NAME}_USE_UNCRUSTIFY)
include(SetupUncrustifyHook)
endif()
#--------------------------------------------------------------------------
......
......@@ -29,3 +29,167 @@ if test $lastSetupForDevelopment -lt $SetupForDevelopment_VERSION; then
./Utilities/SetupForDevelopment.sh
'
fi
##################################################################
# git pre-commit hook that runs an Uncrustify stylecheck.
# Features:
# - abort commit when commit does not comply with the style guidelines
# - create a patch of the proposed style changes
#
# More info on Uncrustify: http://uncrustify.sourceforge.net/
# This file is part of a set of unofficial pre-commit hooks available
# at github.
# Link: https://github.com/githubbrowser/Pre-commit-hooks
# Contact: David Martin, david.martin.mailbox@googlemail.com
##################################################################
# CONFIGURATION
# set uncrustify path or executable
# UNCRUSTIFY="/usr/bin/uncrustify"
UNCRUSTIFY=$(git config --get uncrustify.path)
# set uncrustify config location
# CONFIG="/home/user/.config/uncrustify.cfg"
CONFIG=$(git config --get uncrustify.conf)
# the source language: C, CPP, D, CS, JAVA, PAWN, VALA, OC, OC+
# use AUTO to let Uncrustify decide which language a given file uses.
# the detected language is printed to the console when Uncrustify is called.
# override if the automatic detection seems off.
# SOURCE_LANGUAGE="AUTO"
SOURCE_LANGUAGE="AUTO"
# remove any older patches from previous commits. Set to true or false.
# DELETE_OLD_PATCHES=false
DELETE_OLD_PATCHES=false
# only parse files with the extensions in FILE_EXTS. Set to true or false.
# if false every changed file in the commit will be parsed with Uncrustify.
# if true only files matching one of the extensions are parsed with Uncrustify.
# PARSE_EXTS=true
PARSE_EXTS=true
# file types to parse. Only effective when PARSE_EXTS is true.
# FILE_EXTS=".c .h .cpp .hpp"
FILE_EXTS=".c .h .cpp .hpp"
##################################################################
# There should be no need to change anything below this line.
# exit on error
set -e
# check whether the given file matches any of the set extensions
matches_extension() {
local filename="$(basename -- "$1")"
local extension=".${filename##*.}"
local ext
for ext in $FILE_EXTS; do [ "$ext" = "$extension" ] && return 0; done
return 1
}
# necessary check for initial commit
if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# make sure the config file and executable are correctly set
if [ ! -f "$CONFIG" ] ; then
printf "Error: uncrustify config file not found.\n"
printf "Set the correct path in ${BASH_SOURCE%/*}.\n"
exit 1
fi
if ! command -v "$UNCRUSTIFY" > /dev/null ; then
printf "Error: uncrustify executable not found.\n"
printf "Set the correct path in ${BASH_SOURCE%/*}.\n"
exit 1
fi
# create a filename to store our generated patch
prefix="pre-commit-uncrustify"
suffix="$(date +%C%y-%m-%d_%Hh%Mm%Ss)"
patch="/tmp/$prefix-$suffix.patch"
# clean up any older uncrustify patches
$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
# create one patch containing all changes to the files
# sed to remove quotes around the filename, if inserted by the system
# (done sometimes, if the filename contains special characters, like the quote itself)
git diff-index --cached --diff-filter=ACMR --name-only $against -- | \
sed -e 's/^"\(.*\)"$/\1/' | \
while read file
do
# ignore file if we do check for file extensions and the file
# does not match any of the extensions specified in $FILE_EXTS
if $PARSE_EXTS && ! matches_extension "$file"; then
continue;
fi
# escape special characters in the source filename:
# - '\': backslash needs to be escaped
# - '*': used as matching string => '*' would mean expansion
# (curiously, '?' must not be escaped)
# - '[': used as matching string => '[' would mean start of set
# - '|': used as sed split char instead of '/', so it needs to be escaped
# in the filename
# printf %s particularly important if the filename contains the % character
file_escaped_source=$(printf "%s" "$file" | sed -e 's/[\*[|]/\\&/g')
# escape special characters in the target filename:
# phase 1 (characters escaped in the output diff):
# - '\': backslash needs to be escaped in the output diff
# - '"': quote needs to be escaped in the output diff if present inside
# of the filename, as it used to bracket the entire filename part
# phase 2 (characters escaped in the match replacement):
# - '\': backslash needs to be escaped again for sed itself
# (i.e. double escaping after phase 1)
# - '&': would expand to matched string
# - '|': used as sed split char instead of '/'
# printf %s particularly important if the filename contains the % character
file_escaped_target=$(printf "%s" "$file" | sed -e 's/[\"]/\\&/g' -e 's/[\&|]/\\&/g')
# Uncrustify detects the language automatically if it is not specified
language_option=""
if [ "$SOURCE_LANGUAGE" != "AUTO" ] ; then
language_option="-l $SOURCE_LANGUAGE"
fi
# uncrustify our sourcefile, create a patch with diff and append it to our $patch
# The sed call is necessary to transform the patch from
# --- $file timestamp
# +++ - timestamp
# to both lines working on the same file and having a a/ and b/ prefix.
# Else it can not be applied with 'git apply'.
"$UNCRUSTIFY" -c "$CONFIG" -f "$file" $language_option | \
diff -u -- "$file" - | \
sed -e "1s|--- $file_escaped_source|--- \"a/$file_escaped_target\"|" -e "2s|+++ -|+++ \"b/$file_escaped_target\"|" >> "$patch"
done
# if no patch has been generated all is ok, clean up the file stub and exit
if [ ! -s "$patch" ] ; then
printf "Files in this commit comply with the uncrustify rules.\n"
rm -f "$patch"
exit 0
fi
# a patch has been created, notify the user and exit
printf "\nThe following differences were found between the code to commit "
printf "and the uncrustify rules:\n\n"
cat "$patch"
printf "\nYou can apply these changes with:\n git apply $patch\n"
printf "(may need to be called from the root directory of your repository)\n"
printf "Aborting commit. Apply changes and commit again or skip checking with"
printf " --no-verify (not recommended).\n"
exit 1
\ No newline at end of file
/home/virtualfls/Projects/IMSTK/src_refactoring/Examples/*/*.h
/home/virtualfls/Projects/IMSTK/src_refactoring/Examples/*/*.cxx
@CMAKE_SOURCE_DIR@/Examples/*/*.h
@CMAKE_SOURCE_DIR@/Examples/*/*.cxx
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment