Skip to content

Commit

Permalink
[INIT] from aduulm_sandbox @ 17cd99c2
Browse files Browse the repository at this point in the history
  • Loading branch information
janstrohbeck committed Feb 18, 2019
0 parents commit 5213aa3
Show file tree
Hide file tree
Showing 9 changed files with 789 additions and 0 deletions.
107 changes: 107 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
project(git-clang-format-cmake)
cmake_minimum_required(VERSION 3.3)

find_package(Git REQUIRED)
find_package(PythonInterp REQUIRED)

set(GCF_DIR ${CMAKE_CURRENT_LIST_DIR} PARENT_SCOPE)

macro(GCF_CHECK_OPTION_OR_DEFAULT ARG_NAME DEFAULT)
if(NOT DEFINED GCF_${ARG_NAME})
if(NOT DEFINED GCF_GLOBAL_${ARG_NAME})
set(GCF_${ARG_NAME} ${DEFAULT})
else()
set(GCF_${ARG_NAME} ${GCF_GLOBAL_${ARG_NAME}})
endif()
endif()
endmacro()

function(create_git_hook)
set(options ABORT_COMMIT)
set(oneValueArgs FORMAT_STYLE MODES IGNORE_DIRS TIDY_CHECKS cpp_FORMATTERS cpp_LINTERS cmake_FORMATTERS cmake_LINTERS py_FORMATTERS py_LINTERS)
set(multiValueArgs "")
cmake_parse_arguments(GCF "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN} )

set(GCF_SCRIPT ${GCF_DIR}/git-cmake-format.py)
set(GCF_BUILD_DIR "${CMAKE_BINARY_DIR}")
set(GCF_SOURCE_DIR "${CMAKE_SOURCE_DIR}")

if(NOT DEFINED GCF_IGNORE_DIRS)
set(GCF_IGNORE_DIRS "")
endif()

# clang-format style
GCF_CHECK_OPTION_OR_DEFAULT("CLANG_FORMAT_STYLE" "file")
# Checks executed by clang-tidy
GCF_CHECK_OPTION_OR_DEFAULT("CLANG_TIDY_CHECKS"
"readability-*,bugprone-*,modernize-*,google-*")
# Comma-separated list of tools to run
GCF_CHECK_OPTION_OR_DEFAULT("MODES" "format,lint")
# Abort commit if files would be formatted
GCF_CHECK_OPTION_OR_DEFAULT("ABORT_COMMIT" 0)

# Comma-separated lists of formatters/linters for specific file types
GCF_CHECK_OPTION_OR_DEFAULT("cpp_FORMATTERS" "clang-format")
GCF_CHECK_OPTION_OR_DEFAULT("cpp_LINTERS" "clang-tidy")
GCF_CHECK_OPTION_OR_DEFAULT("cmake_FORMATTERS" "") # cmake-format
GCF_CHECK_OPTION_OR_DEFAULT("cmake_LINTERS" "")
GCF_CHECK_OPTION_OR_DEFAULT("py_FORMATTERS" "") # autopep8
GCF_CHECK_OPTION_OR_DEFAULT("py_LINTERS" "") # pylint

string(REPLACE "," ";" GCF_cpp_FORMATTERS_LIST "${GCF_cpp_FORMATTERS}")
string(REPLACE "," ";" GCF_cpp_LINTERS_LIST "${GCF_cpp_LINTERS}")
string(REPLACE "," ";" GCF_cmake_FORMATTERS_LIST "${GCF_cmake_FORMATTERS}")
string(REPLACE "," ";" GCF_cmake_LINTERS_LIST "${GCF_cmake_LINTERS}")
string(REPLACE "," ";" GCF_py_FORMATTERS_LIST "${GCF_py_FORMATTERS}")
string(REPLACE "," ";" GCF_py_LINTERS_LIST "${GCF_py_LINTERS}")

list(APPEND CMAKE_MODULE_PATH ${GCF_DIR})
if("clang-format" IN_LIST GCF_cpp_FORMATTERS_LIST)
find_package(ClangFormat REQUIRED)
endif()
if("cmake-format" IN_LIST GCF_cmake_FORMATTERS_LIST)
find_program(GCF_CMAKE_FORMAT_PATH cmake-format REQUIRED)
endif()
if("autopep8" IN_LIST GCF_py_FORMATTERS_LIST)
find_program(GCF_AUTOPEP_PATH autopep8 REQUIRED)
endif()

if("clang-tidy" IN_LIST GCF_cpp_LINTERS_LIST)
find_package(ClangTidy REQUIRED)
endif()
if("pylint" IN_LIST GCF_py_LINTERS_LIST)
find_program(GCF_PYLINT_PATH pylint REQUIRED)
endif()

execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --show-toplevel
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GCF_GIT_ROOT
OUTPUT_STRIP_TRAILING_WHITESPACE)

# --absolute-git-dir is not supported on git=2.7.3 which is installed on Ubuntu 16.04
execute_process(COMMAND sh -c "readlink -f $(${GIT_EXECUTABLE} rev-parse --git-dir)"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GCF_GIT_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)

if(NOT GCF_GIT_ROOT)
message(WARNING "Not in a git repository")
else()
configure_file(
"${GCF_DIR}/pre-commit.template.sh"
"${GCF_GIT_DIR}/hooks/pre-commit"
@ONLY)

get_filename_component(GCF_PROJECT_NAME "${CMAKE_BINARY_DIR}" NAME)
configure_file(
"${GCF_DIR}/project.template.yaml"
"${GCF_GIT_DIR}/hooks/.${GCF_PROJECT_NAME}.config.yaml"
@ONLY)

configure_file(
"${GCF_DIR}/run_hooks.template.sh"
"${GCF_GIT_ROOT}/run_hooks"
@ONLY)
endif()
endfunction()
90 changes: 90 additions & 0 deletions FindClangFormat.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#
#.rst:
# FindClangFormat
# ---------------
#
# The module defines the following variables
#
# ``CLANG_FORMAT_EXECUTABLE``
# Path to clang-format executable
# ``CLANG_FORMAT_FOUND``
# True if the clang-format executable was found.
# ``CLANG_FORMAT_VERSION``
# The version of clang-format found
# ``CLANG_FORMAT_VERSION_MAJOR``
# The clang-format major version if specified, 0 otherwise
# ``CLANG_FORMAT_VERSION_MINOR``
# The clang-format minor version if specified, 0 otherwise
# ``CLANG_FORMAT_VERSION_PATCH``
# The clang-format patch version if specified, 0 otherwise
# ``CLANG_FORMAT_VERSION_COUNT``
# Number of version components reported by clang-format
#
# Example usage:
#
# .. code-block:: cmake
#
# find_package(ClangFormat)
# if(CLANG_FORMAT_FOUND)
# message("clang-format executable found: ${CLANG_FORMAT_EXECUTABLE}\n"
# "version: ${CLANG_FORMAT_VERSION}")
# endif()

find_program(CLANG_FORMAT_EXECUTABLE
NAMES clang-format-6.0 clang-format clang-format-5.0
clang-format-4.0 clang-format-3.9
clang-format-3.8 clang-format-3.7
clang-format-3.6 clang-format-3.5
clang-format-3.4 clang-format-3.3
DOC "clang-format executable"
)
mark_as_advanced(CLANG_FORMAT_EXECUTABLE)

# Extract version from command "clang-format -version"
if(CLANG_FORMAT_EXECUTABLE)
execute_process(COMMAND sh -c "${CLANG_FORMAT_EXECUTABLE} -version | grep version"
OUTPUT_VARIABLE clang_format_version
# ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)

if (clang_format_version MATCHES "version .*")
# clang_format_version sample: "clang-format version 3.9.1-4ubuntu3~16.04.1 (tags/RELEASE_391/rc2)"
string(REGEX REPLACE
"clang-format version ([.0-9]+).*" "\\1"
CLANG_FORMAT_VERSION "${clang_format_version}")
# CLANG_FORMAT_VERSION sample: "3.9.1"

# Extract version components
string(REPLACE "." ";" clang_format_version "${CLANG_FORMAT_VERSION}")
list(LENGTH clang_format_version CLANG_FORMAT_VERSION_COUNT)
if(CLANG_FORMAT_VERSION_COUNT GREATER 0)
list(GET clang_format_version 0 CLANG_FORMAT_VERSION_MAJOR)
else()
set(CLANG_FORMAT_VERSION_MAJOR 0)
endif()
if(CLANG_FORMAT_VERSION_COUNT GREATER 1)
list(GET clang_format_version 1 CLANG_FORMAT_VERSION_MINOR)
else()
set(CLANG_FORMAT_VERSION_MINOR 0)
endif()
if(CLANG_FORMAT_VERSION_COUNT GREATER 2)
list(GET clang_format_version 2 CLANG_FORMAT_VERSION_PATCH)
else()
set(CLANG_FORMAT_VERSION_PATCH 0)
endif()
if(CLANG_FORMAT_VERSION_MAJOR LESS 6)
message(FATAL_ERROR "Your installed clang-format version is too old! clang-format v6.0.0 or later is required!")
endif()
else()
message(FATAL_ERROR "Could not detect version of installed clang-format!")
endif()
unset(clang_format_version)
else()
message(FATAL_ERROR "Could not find clang-format! You need to install it first!")
endif()

if(CLANG_FORMAT_EXECUTABLE)
set(CLANG_FORMAT_FOUND TRUE)
else()
set(CLANG_FORMAT_FOUND FALSE)
endif()
90 changes: 90 additions & 0 deletions FindClangTidy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#
#.rst:
# FindClangTidy
# ---------------
#
# The module defines the following variables
#
# ``CLANG_TIDY_EXECUTABLE``
# Path to clang-tidy executable
# ``CLANG_TIDY_FOUND``
# True if the clang-tidy executable was found.
# ``CLANG_TIDY_VERSION``
# The version of clang-tidy found
# ``CLANG_TIDY_VERSION_MAJOR``
# The clang-tidy major version if specified, 0 otherwise
# ``CLANG_TIDY_VERSION_MINOR``
# The clang-tidy minor version if specified, 0 otherwise
# ``CLANG_TIDY_VERSION_PATCH``
# The clang-tidy patch version if specified, 0 otherwise
# ``CLANG_TIDY_VERSION_COUNT``
# Number of version components reported by clang-tidy
#
# Example usage:
#
# .. code-block:: cmake
#
# find_package(ClangTidy)
# if(CLANG_TIDY_FOUND)
# message("clang-tidy executable found: ${CLANG_TIDY_EXECUTABLE}\n"
# "version: ${CLANG_TIDY_VERSION}")
# endif()

find_program(CLANG_TIDY_EXECUTABLE
NAMES clang-tidy-6.0 clang-tidy clang-tidy-5.0
clang-tidy-4.0 clang-tidy-3.9
clang-tidy-3.8 clang-tidy-3.7
clang-tidy-3.6 clang-tidy-3.5
clang-tidy-3.4 clang-tidy-3.3
DOC "clang-tidy executable"
)
mark_as_advanced(CLANG_TIDY_EXECUTABLE)

# Extract version from command "clang-tidy -version"
if(CLANG_TIDY_EXECUTABLE)
execute_process(COMMAND sh -c "${CLANG_TIDY_EXECUTABLE} -version | grep version"
OUTPUT_VARIABLE clang_tidy_version
# ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)

if (clang_tidy_version MATCHES "version .*")
# clang_tidy_version sample: " LLVM version 3.9.1"
string(REGEX REPLACE
" *LLVM version ([.0-9]+).*" "\\1"
CLANG_TIDY_VERSION "${clang_tidy_version}")
# CLANG_TIDY_VERSION sample: "3.9.1"

# Extract version components
string(REPLACE "." ";" clang_tidy_version "${CLANG_TIDY_VERSION}")
list(LENGTH clang_tidy_version CLANG_TIDY_VERSION_COUNT)
if(CLANG_TIDY_VERSION_COUNT GREATER 0)
list(GET clang_tidy_version 0 CLANG_TIDY_VERSION_MAJOR)
else()
set(CLANG_TIDY_VERSION_MAJOR 0)
endif()
if(CLANG_TIDY_VERSION_COUNT GREATER 1)
list(GET clang_tidy_version 1 CLANG_TIDY_VERSION_MINOR)
else()
set(CLANG_TIDY_VERSION_MINOR 0)
endif()
if(CLANG_TIDY_VERSION_COUNT GREATER 2)
list(GET clang_tidy_version 2 CLANG_TIDY_VERSION_PATCH)
else()
set(CLANG_TIDY_VERSION_PATCH 0)
endif()
if(CLANG_TIDY_VERSION_MAJOR LESS 6)
message(FATAL_ERROR "Your installed clang-tidy version is too old! clang-tidy v6.0.0 or later is required!")
endif()
else()
message(FATAL_ERROR "Could not detect version of installed clang-tidy!")
endif()
unset(clang_tidy_version)
else()
message(FATAL_ERROR "Could not find clang-tidy! You need to install it first!")
endif()

if(CLANG_TIDY_EXECUTABLE)
set(CLANG_TIDY_FOUND TRUE)
else()
set(CLANG_TIDY_FOUND FALSE)
endif()
Loading

0 comments on commit 5213aa3

Please sign in to comment.