From d832267e2367e6377a08501c6a590a63460f6e1d Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Sat, 10 Feb 2018 17:34:01 +0000 Subject: [PATCH] Problem: make format-check not implemented with autotools Solution: port implementation from zproject --- Makefile.am | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 13 +++++++++++++ 2 files changed, 64 insertions(+) diff --git a/Makefile.am b/Makefile.am index 5b3d9496..3c1ce183 100644 --- a/Makefile.am +++ b/Makefile.am @@ -882,6 +882,57 @@ MAINTAINERCLEANFILES = \ $(srcdir)/configure \ `find "$(srcdir)" -type f -name Makefile.in -print` +if WITH_CLANG_FORMAT +ALL_SOURCE_FILES = $(wildcard \ + $(top_srcdir)/src/*.c \ + $(top_srcdir)/src/*.cc \ + $(top_srcdir)/src/*.cpp \ + $(top_srcdir)/src/*.h \ + $(top_srcdir)/src/*.hpp \ + $(top_srcdir)/tests/*.c \ + $(top_srcdir)/tests/*.cc \ + $(top_srcdir)/tests/*.cpp \ + $(top_srcdir)/tests/*.h \ + $(top_srcdir)/tests/*.hpp \ + $(top_srcdir)/perf/*.c \ + $(top_srcdir)/perf/*.cc \ + $(top_srcdir)/perf/*.cpp \ + $(top_srcdir)/perf/*.h \ + $(top_srcdir)/perf/*.hpp \ + $(top_srcdir)/tools/*.c \ + $(top_srcdir)/tools/*.cc \ + $(top_srcdir)/tools/*.cpp \ + $(top_srcdir)/tools/*.h \ + $(top_srcdir)/tools/*.hpp \ + $(top_srcdir)/include/*.h \ + ) + +# Check if any sources need to be fixed, report the filenames and an error code +clang-format-check: $(ALL_SOURCE_FILES) + @FAILED=0 ; IFS=";" ; IDS="`printf '\n\b'`" ; export IFS IDS; \ + for FILE in $(ALL_SOURCE_FILES) ; do \ + test -s $$FILE || continue ; \ + $(CLANG_FORMAT) -style=file -output-replacements-xml "$$FILE" | grep "/dev/null && \ + { echo "$$FILE is not correctly formatted" >&2 ; FAILED=1; } ; \ + done; \ + if test "$$FAILED" != 0 ; then \ + exit 1 ; \ + fi + +# Change source formatting +clang-format: $(ALL_SOURCE_FILES) + $(CLANG_FORMAT) -style=file -i $(ALL_SOURCE_FILES) + +# Change source formatting AND report the diff +clang-format-diff: clang-format + git diff $(ALL_SOURCE_FILES) + +else +clang-format clang-format-check clang-format-diff: + @echo "Install the clang-format program, reconfigure and re-run this request" + @exit 1 +endif + @CODE_COVERAGE_RULES@ dist-hook: diff --git a/configure.ac b/configure.ac index 12093b91..dd614b5e 100644 --- a/configure.ac +++ b/configure.ac @@ -800,6 +800,19 @@ if test "x$enable_libunwind" != "xno"; then ]) fi +# See if clang-format is in PATH; the result unblocks the relevant recipes +WITH_CLANG_FORMAT="" +AS_IF([test x"$CLANG_FORMAT" = x], + [AC_PATH_PROG([CLANG_FORMAT], [clang-format], [])], + [AC_CHECK_PROG([WITH_CLANG_FORMAT], [$CLANG_FORMAT], [true], [fail])]) +AS_IF([test x"$CLANG_FORMAT" != x && test x"$WITH_CLANG_FORMAT" = x], + [AS_IF([test -x "$CLANG_FORMAT"], + [WITH_CLANG_FORMAT=true], + [AC_CHECK_PROG([WITH_CLANG_FORMAT], [$CLANG_FORMAT], [true], [false])])]) +AS_IF([test "$WITH_CLANG_FORMAT" = fail], + [AC_MSG_ERROR([Caller explicitly referenced CLANG_FORMAT=$CLANG_FORMAT which was not found])]) +AM_CONDITIONAL([WITH_CLANG_FORMAT], [$WITH_CLANG_FORMAT]) + # Subst LIBZMQ_EXTRA_CFLAGS & CXXFLAGS & LDFLAGS AC_SUBST(LIBZMQ_EXTRA_CFLAGS) AC_SUBST(LIBZMQ_EXTRA_CXXFLAGS)