make check: avoid an error when no .git dir present.
[gnulib.git] / top / maint.mk
index b71304d..33803f3 100644 (file)
@@ -129,8 +129,23 @@ sc_m_rules_ = $(patsubst %, %.m, $(syntax-check-rules))
 .PHONY: $(sc_m_rules_)
 $(sc_m_rules_):
        @echo $(patsubst sc_%.m, %, $@)
-
-local-check := $(filter-out $(local-checks-to-skip), $(local-checks-available))
+       @date +%s.%N > .sc-start-$(basename $@)
+
+# Compute and print the elapsed time for each syntax-check rule.
+sc_z_rules_ = $(patsubst %, %.z, $(syntax-check-rules))
+.PHONY: $(sc_z_rules_)
+$(sc_z_rules_): %.z: %
+       @end=$$(date +%s.%N);                                           \
+       start=$$(cat .sc-start-$*);                                     \
+       rm -f .sc-start-$*;                                             \
+       awk -v s=$$start -v e=$$end                                     \
+         'END {printf "%.2f $(patsubst sc_%,%,$*)\n", e - s}' < /dev/null
+
+# The patsubst here is to replace each sc_% rule with its sc_%.z wrapper
+# that computes and prints elapsed time.
+local-check :=                                                         \
+  $(patsubst sc_%, sc_%.z,                                             \
+    $(filter-out $(local-checks-to-skip), $(local-checks-available)))
 
 syntax-check: $(local-check)
 #      @grep -nE '#  *include <(limits|std(def|arg|bool))\.h>'         \
@@ -630,6 +645,12 @@ sc_useless_cpp_parens:
        halt='found useless parentheses in cpp directive'               \
          $(_sc_search_regexp)
 
+# Prohibit checked in backup files.
+sc_prohibit_backup_files:
+       @$(VC_LIST) | grep '~$$' &&                             \
+         { echo '$(ME): found version controlled backup file' 1>&2;    \
+           exit 1; } || :
+
 # Require the latest GPL.
 sc_GPL_version:
        @prohibit='either ''version [^3]'                               \
@@ -645,9 +666,10 @@ sc_GFDL_version:
          $(_sc_search_regexp)
 
 # Don't use Texinfo @acronym{} as it is not a good idea.
+texinfo_suffix_re_ ?= \.(txi|texi(nfo)?)$$
 sc_texinfo_acronym:
-       @prohibit='@acronym{'                                           \
-       in_vc_files='\.texi$$'                                          \
+       @prohibit='@acronym\{'                                          \
+       in_vc_files='$(texinfo_suffix_re_)'                             \
        halt='found use of Texinfo @acronym{}'                          \
          $(_sc_search_regexp)
 
@@ -659,27 +681,40 @@ sc_prohibit_cvs_keyword:
        halt='do not use CVS keyword expansion'                         \
          $(_sc_search_regexp)
 
-# The following tail+perl pipeline would be more concise, and would
-# produce slightly better output (including counts) if written as
+# This Perl code is slightly obfuscated.  Not only is each "$" doubled
+# because it's in a Makefile, but the $$c's are comments;  we cannot
+# use "#" due to the way the script ends up concatenated onto one line.
+# It would be much more concise, and would produce better output (including
+# counts) if written as:
 #   perl -ln -0777 -e '/\n(\n+)$/ and print "$ARGV: ".length $1' ...
 # but that would be far less efficient, reading the entire contents
-# of each file, rather than just the last few bytes of each.
+# of each file, rather than just the last two bytes of each.
 #
-# This is a perl script that operates on the output of
-# tail -n1 TWO_OR_MORE_FILES
+# This is a perl script that is expected to be the single-quoted argument
+# to a command-line "-le".  The remaining arguments are file names.
 # Print the name of each file that ends in two or more newline bytes.
 # Exit nonzero if at least one such file is found, otherwise, exit 0.
+# Warn about, but otherwise ignore open failure.  Ignore seek/read failure.
 #
 # Use this if you want to remove trailing empty lines from selected files:
 #   perl -pi -0777 -e 's/\n\n+$/\n/' files...
 #
 detect_empty_lines_at_EOF_ =                                           \
-  /^==> ([^\n]+) <==\n\n\n/m and (print "$$1\n"), $$fail = 1;          \
+  foreach my $$f (@ARGV)                                               \
+    {                                                                  \
+      open F, "<", $$f or (warn "failed to open $$f: $$!\n"), next;    \
+      my $$p = sysseek (F, -2, 2);                                     \
+      my $$c = "seek failure probably means file has < 2 bytes; ignore"; \
+      my $$last_two_bytes;                                             \
+      defined $$p and $$p = sysread F, $$last_two_bytes, 2;            \
+      close F;                                                         \
+      $$c = "ignore read failure";                                     \
+      $$p && $$last_two_bytes eq "\n\n" and (print $$f), $$fail=1;     \
+    }                                                                  \
   END { exit defined $$fail }
 sc_prohibit_empty_lines_at_EOF:
-       @tail -n1 $$($(VC_LIST_EXCEPT)) /dev/null                       \
-           | perl -00 -ne '$(detect_empty_lines_at_EOF_)'              \
-         || { echo '$(ME): the above files end with empty line(s)'     \
+       @perl -le '$(detect_empty_lines_at_EOF_)' $$($(VC_LIST_EXCEPT)) \
+          || { echo '$(ME): the above files end with empty line(s)'     \
                1>&2; exit 1; } || :;                                   \
 
 # Make sure we don't use st_blocks.  Use ST_NBLOCKS instead.