Hi.  I realized the main reason I don't consistenly enforce code
indentation in the projects I work on is that it isn't tested before
releases (or via CICD builds).  The syntax-check framework to the
rescue!  Not everyone will like this, but as usual it is simple to
disable on a per-project basis, and I do think it is a generally good
rule to always have indented code in projects to avoid slow detoriation
of code style.  Thoughts?  I'm enabling this for a couple of projects
now, and it catched some newly added code with poor indentation.

Btw, I think one of the really big advantages with gnulib's
maintainer-makefile are the automatic syntax-checks -- but have we
documented or promoted this feature anywhere?

/Simon
From 8f043c699535cdb0ffa0dee8d624ca38aada2deb Mon Sep 17 00:00:00 2001
From: Simon Josefsson <si...@josefsson.org>
Date: Fri, 3 Sep 2021 21:59:43 +0200
Subject: [PATCH] maintainer-makefile: Add indentation syntax-check rule.

* top/maint.mk (indent): Use AM_V_GEN and indent_args.
(sc_indent): New rule.
(indent_args): New variable.
(C_SOURCES): New variable, initialized to reasonable C files.
(exclude_file_name_regexp--indent): New variable.
---
 ChangeLog    |  9 +++++++++
 top/maint.mk | 28 ++++++++++++++++++++++++----
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1b1372cb7..51d1f3d37 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-09-03  Simon Josefsson  <si...@josefsson.org>
+
+	maintainer-makefile: Add indentation syntax-check rule.
+	* top/maint.mk (indent): Use AM_V_GEN and indent_args.
+	(sc_indent): New rule.
+	(indent_args): New variable.
+	(C_SOURCES): New variable, initialized to reasonable C files.
+	(exclude_file_name_regexp--indent): New variable.
+
 2021-08-30  Paul Eggert  <egg...@cs.ucla.edu>
 
 	idx: add commentary
diff --git a/top/maint.mk b/top/maint.mk
index 6a3ea9606..72070a7b0 100644
--- a/top/maint.mk
+++ b/top/maint.mk
@@ -1631,12 +1631,32 @@ refresh-po:
 	ls $(PODIR)/*.po | $(SED) 's/\.po//;s,$(PODIR)/,,' | \
 	  sort >> $(PODIR)/LINGUAS
 
- # Running indent once is not idempotent, but running it twice is.
+# Indentation
+
+indent_args ?= -ppi 1
+C_SOURCES ?= $$($(VC_LIST_EXCEPT) | grep '\.[ch]\(.in\)\?$$')
 INDENT_SOURCES ?= $(C_SOURCES)
+exclude_file_name_regexp--indent ?= $(exclude_file_name_regexp--sc_indent)
+
 .PHONY: indent
-indent:
-	indent $(INDENT_SOURCES)
-	indent $(INDENT_SOURCES)
+indent: # Running indent once is not idempotent, but running it twice is.
+	$(AM_V_GEN)indent $(indent_args) $(INDENT_SOURCES) && \
+	indent $(indent_args) $(INDENT_SOURCES)
+
+sc_indent:
+	@if ! command -v indent > /dev/null; then			\
+	    echo 1>&2 '$(ME): sc_indent: indent is missing';		\
+	else								\
+	  fail=0; files="$(INDENT_SOURCES)";				\
+	  for f in $$files; do						\
+	    indent $(indent_args) -st $$f				\
+		| indent $(indent_args) -st -				\
+		| diff -u $$f - || fail=1;				\
+	  done;								\
+	  test $$fail = 1 &&						\
+	    { echo 1>&2 '$(ME): code format error, try "make indent"';	\
+	      exit 1; } || :;						\
+	fi
 
 # If you want to set UPDATE_COPYRIGHT_* environment variables,
 # put the assignments in this variable.
-- 
2.30.2

Attachment: signature.asc
Description: PGP signature

Reply via email to