Merge branch 'silent-custom' into maint
[platform/upstream/automake.git] / syntax-checks.mk
1 # Maintainer checks for Automake.  Requires GNU make.
2
3 # Copyright (C) 2012 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # We also have to take into account VPATH builds (where some generated
19 # tests might be in '$(builddir)' rather than in '$(srcdir)'), TAP-based
20 # tests script (which have a '.tap' extension) and helper scripts used
21 # by other test cases (which have a '.sh' extension).
22 xtests := $(shell \
23   if test $(srcdir) = .; then \
24      dirs=.; \
25    else \
26      dirs='$(srcdir) .'; \
27    fi; \
28    for d in $$dirs; do \
29      for s in tap sh; do \
30        ls $$d/t/*.$$s $$d/t/ax/*.$$s 2>/dev/null; \
31      done; \
32    done | sort)
33
34 xdefs = $(srcdir)/defs $(srcdir)/defs-static.in
35
36 ams := $(shell find $(srcdir) -name '*.dir' -prune -o -name '*.am' -print)
37
38 # Some simple checks, and then ordinary check.  These are only really
39 # guaranteed to work on my machine.
40 syntax_check_rules = \
41 $(sc_tests_plain_check_rules) \
42 sc_test_names \
43 sc_diff_automake_in_automake \
44 sc_diff_aclocal_in_automake \
45 sc_perl_syntax \
46 sc_no_brace_variable_expansions \
47 sc_rm_minus_f \
48 sc_no_for_variable_in_macro \
49 sc_mkinstalldirs \
50 sc_pre_normal_post_install_uninstall \
51 sc_perl_no_undef \
52 sc_perl_no_split_regex_space \
53 sc_cd_in_backquotes \
54 sc_cd_relative_dir \
55 sc_perl_at_uscore_in_scalar_context \
56 sc_perl_local \
57 sc_AMDEP_TRUE_in_automake_in \
58 sc_tests_make_without_am_makeflags \
59 sc_tests_obsolete_variables \
60 sc_tests_here_document_format \
61 sc_tests_Exit_not_exit \
62 sc_tests_automake_fails \
63 sc_tests_required_after_defs \
64 sc_tests_overriding_macros_on_cmdline \
65 sc_tests_plain_sleep \
66 sc_m4_am_plain_egrep_fgrep \
67 sc_tests_no_configure_in \
68 sc_tests_PATH_SEPARATOR \
69 sc_tests_logs_duplicate_prefixes \
70 sc_tests_makefile_variable_order \
71 sc_perl_at_substs \
72 sc_unquoted_DESTDIR \
73 sc_tabs_in_texi \
74 sc_at_in_texi
75
76 ## Look for test whose names can cause spurious failures when used as
77 ## first argument to AC_INIT (chiefly because they might contain an
78 ## m4/m4sugar builtin or macro name).
79 m4_builtins = \
80   __gnu__ \
81   __unix__ \
82   bpatsubst \
83   bregexp \
84   builtin \
85   changecom \
86   changequote \
87   changeword \
88   debugfile \
89   debugmode \
90   decr \
91   define \
92   defn \
93   divert \
94   divnum \
95   dnl \
96   dumpdef \
97   errprint \
98   esyscmd \
99   eval \
100   format \
101   ifdef \
102   ifelse \
103   include \
104   incr \
105   index \
106   indir \
107   len \
108   m4exit \
109   m4wrap \
110   maketemp \
111   mkstemp \
112   patsubst \
113   popdef \
114   pushdef \
115   regexp \
116   shift \
117   sinclude \
118   substr \
119   symbols \
120   syscmd \
121   sysval \
122   traceoff \
123   traceon \
124   translit \
125   undefine \
126   undivert
127 sc_test_names:
128         @m4_builtin_rx=`echo $(m4_builtins) | sed 's/ /|/g'`; \
129          m4_macro_rx="\\<($$m4_builtin_rx)\\>|\\<_?(A[CUMHS]|m4)_"; \
130          if { \
131            for t in $(xtests); do echo $$t; done \
132              | LC_ALL=C grep -E "$$m4_macro_rx"; \
133          }; then \
134            echo "the names of the tests above can be problematic" 1>&2; \
135            echo "Avoid test names that contain names of m4 macros" 1>&2; \
136            exit 1; \
137          fi
138
139 ## These check avoids accidental configure substitutions in the source.
140 ## There are exactly 9 lines that should be modified from automake.in to
141 ## automake, and 10 lines that should be modified from aclocal.in to
142 ## aclocal; these wors out to 32 and 34 lines of diffs, respectively.
143 sc_diff_automake_in_automake:
144         @if test `diff $(srcdir)/automake.in automake | wc -l` -ne 32; then \
145           echo "found too many diffs between automake.in and automake" 1>&2; \
146           diff -c $(srcdir)/automake.in automake; \
147           exit 1; \
148         fi
149 sc_diff_aclocal_in_aclocal:
150         @if test `diff $(srcdir)/aclocal.in aclocal | wc -l` -ne 34; then \
151           echo "found too many diffs between aclocal.in and aclocal" 1>&2; \
152           diff -c $(srcdir)/aclocal.in aclocal; \
153           exit 1; \
154         fi
155
156 ## Syntax check with default Perl (on my machine, Perl 5).
157 sc_perl_syntax:
158         @perllibdir="./lib$(PATH_SEPARATOR)$(srcdir)/lib" $(PERL) -c -w automake
159         @perllibdir="./lib$(PATH_SEPARATOR)$(srcdir)/lib" $(PERL) -c -w aclocal
160
161 ## Expect no instances of '${...}'.  However, $${...} is ok, since that
162 ## is a shell construct, not a Makefile construct.
163 sc_no_brace_variable_expansions:
164         @if grep -v '^ *#' $(ams) | grep -F '$${' | grep -F -v '$$$$'; then \
165           echo "Found too many uses of '\$${' in the lines above." 1>&2; \
166           exit 1;                               \
167         else :; fi
168
169 ## Make sure 'rm' is called with '-f'.
170 sc_rm_minus_f:
171         @if grep -v '^#' $(ams) $(xtests) \
172            | grep -vE '/(spy-rm\.tap|subobj-clean.*-pr10697\.sh):' \
173            | grep -E '\<rm ([^-]|\-[^f ]*\>)'; \
174         then \
175           echo "Suspicious 'rm' invocation." 1>&2; \
176           exit 1;                               \
177         else :; fi
178
179 ## Never use something like "for file in $(FILES)", this doesn't work
180 ## if FILES is empty or if it contains shell meta characters (e.g. $ is
181 ## commonly used in Java filenames).
182 sc_no_for_variable_in_macro:
183         @if grep 'for .* in \$$(' $(ams) | grep -v '/Makefile\.am:'; then \
184           echo 'Use "list=$$(mumble); for var in $$$$list".' 1>&2 ; \
185           exit 1; \
186         else :; fi
187
188 ## Make sure all invocations of mkinstalldirs are correct.
189 sc_mkinstalldirs:
190         @if grep -n 'mkinstalldirs' $(ams) \
191               | grep -F -v '$$(mkinstalldirs)' \
192               | grep -v '^\./Makefile.am:[0-9][0-9]*:  *lib/mkinstalldirs \\$$'; \
193         then \
194           echo "Found incorrect use of mkinstalldirs in the lines above" 1>&2; \
195           exit 1; \
196         else :; fi
197
198 ## Make sure all calls to PRE/NORMAL/POST_INSTALL/UNINSTALL
199 sc_pre_normal_post_install_uninstall:
200         @if grep -E -n '\((PRE|NORMAL|POST)_(|UN)INSTALL\)' $(ams) | \
201               grep -v ':##' | grep -v ':        @\$$('; then \
202           echo "Found incorrect use of PRE/NORMAL/POST_INSTALL/UNINSTALL in the lines above" 1>&2; \
203           exit 1; \
204         else :; fi
205
206 ## We never want to use "undef", only "delete", but for $/.
207 sc_perl_no_undef:
208         @if grep -n -w 'undef ' $(srcdir)/automake.in | \
209               grep -F -v 'undef $$/'; then \
210           echo "Found undef in automake.in; use delete instead" 1>&2; \
211           exit 1; \
212         fi
213
214 ## We never want split (/ /,...), only split (' ', ...).
215 sc_perl_no_split_regex_space:
216         @if grep -n 'split (/ /' $(srcdir)/automake.in; then \
217           echo "Found bad split in the lines above." 1>&2; \
218           exit 1; \
219         fi
220
221 ## Look for cd within backquotes
222 sc_cd_in_backquotes:
223         @if grep -n '^[^#]*` *cd ' $(srcdir)/automake.in $(ams); then \
224           echo "Consider using \$$(am__cd) in the lines above." 1>&2; \
225           exit 1; \
226         fi
227
228 ## Look for cd to a relative directory (may be influenced by CDPATH).
229 ## Skip some known directories that are OK.
230 sc_cd_relative_dir:
231         @if grep -n '^[^#]*cd ' $(srcdir)/automake.in $(ams) | \
232               grep -v 'echo.*cd ' | \
233               grep -v 'am__cd =' | \
234               grep -v '^[^#]*cd [./]' | \
235               grep -v '^[^#]*cd \$$(top_builddir)' | \
236               grep -v '^[^#]*cd "\$$\$$am__cwd' | \
237               grep -v '^[^#]*cd \$$(abs' | \
238               grep -v '^[^#]*cd "\$$(DESTDIR)'; then \
239           echo "Consider using \$$(am__cd) in the lines above." 1>&2; \
240           exit 1; \
241         fi
242
243 ## Using @_ in a scalar context is most probably a programming error.
244 sc_perl_at_uscore_in_scalar_context:
245         @if grep -Hn '[^@_A-Za-z0-9][_A-Za-z0-9]*[^) ] *= *@_' $(srcdir)/automake.in; then \
246           echo "Using @_ in a scalar context in the lines above." 1>&2; \
247           exit 1; \
248         fi
249
250 ## Allow only few variables to be localized in Automake.
251 sc_perl_local:
252         @if egrep -v '^[ \t]*local \$$[_~]( *=|;)' $(srcdir)/automake.in | \
253                 grep '^[ \t]*local [^*]'; then \
254           echo "Please avoid 'local'." 1>&2; \
255           exit 1; \
256         fi
257
258 ## Don't let AMDEP_TRUE substitution appear in automake.in.
259 sc_AMDEP_TRUE_in_automake_in:
260         @if grep '@AMDEP''_TRUE@' $(srcdir)/automake.in; then \
261           echo "Don't put AMDEP_TRUE substitution in automake.in" 1>&2; \
262           exit 1; \
263         fi
264
265 ## Recursive make invocations should always pass $(AM_MAKEFLAGS)
266 ## to $(MAKE), for portability to non-GNU make.
267 sc_tests_make_without_am_makeflags:
268         @if grep '^[^#].*(MAKE) ' $(ams) $(srcdir)/automake.in \
269             | grep -v 'AM_MAKEFLAGS' \
270             | grep -v '/am/header-vars\.am:.*am--echo.*| $$(MAKE) -f *-'; \
271         then \
272           echo 'Use $$(MAKE) $$(AM_MAKEFLAGS).' 1>&2; \
273           exit 1; \
274         fi
275
276 ## Look out for some obsolete variables.
277 sc_tests_obsolete_variables:
278         @vars=" \
279           using_tap \
280           test_prefer_config_shell \
281           original_AUTOMAKE \
282           original_ACLOCAL \
283           parallel_tests \
284           am_parallel_tests \
285         "; \
286         seen=""; \
287         for v in $$vars; do \
288           if grep -E "\b$$v\b" $(xtests) $(xdefs); then \
289             seen="$$seen $$v"; \
290           fi; \
291         done; \
292         if test -n "$$seen"; then \
293           for v in $$seen; do \
294             case $$v in \
295               parallel_tests|am_parallel_tests) v2=am_serial_tests;; \
296               *) v2=am_$$v;; \
297             esac; \
298             echo "Variable '$$v' is obsolete, use '$$v2' instead." 1>&2; \
299           done; \
300           exit 1; \
301         else :; fi
302
303 ## Tests should never call some programs directly, but only through the
304 ## corresponding variable (e.g., '$MAKE', not 'make').  This will allow
305 ## the programs to be overridden at configure time (for less brittleness)
306 ## or by the user at make time (to allow better testsuite coverage).
307 sc_tests_plain_check_rules = \
308   sc_tests_plain_egrep \
309   sc_tests_plain_fgrep \
310   sc_tests_plain_make \
311   sc_tests_plain_perl \
312   sc_tests_plain_automake \
313   sc_tests_plain_aclocal \
314   sc_tests_plain_autoconf \
315   sc_tests_plain_autoupdate \
316   sc_tests_plain_autom4te \
317   sc_tests_plain_autoheader \
318   sc_tests_plain_autoreconf
319
320 toupper = $(shell echo $(1) | LC_ALL=C tr '[a-z]' '[A-Z]')
321
322 $(sc_tests_plain_check_rules): sc_tests_plain_% :
323         @# The leading ':' in the grep below is what is printed by the
324         @# preceding 'grep -v' after the file name.
325         @# It works here as a poor man's substitute for beginning-of-line
326         @# marker.
327         @if grep -v '^[         ]*#' $(xtests) \
328            | $(EGREP) '(:|\bif|\bnot|[;!{\|\(]|&&|\|\|)[        ]*?$*\b'; \
329          then \
330            echo 'Do not run "$*" in the above tests.' \
331                 'Use "$$$(call toupper,$*)" instead.' 1>&2; \
332            exit 1; \
333         fi
334
335 ## Tests should only use END and EOF for here documents
336 ## (so that the next test is effective).
337 sc_tests_here_document_format:
338         @if grep '<<' $(xtests) | grep -Ev '\b(END|EOF)\b|\bcout <<'; then \
339           echo 'Use here documents with "END" and "EOF" only, for greppability.' 1>&2; \
340           exit 1; \
341         fi
342
343 ## Tests should never call exit directly, but use Exit.
344 ## This is so that the exit status is transported correctly across the 0 trap.
345 ## Ignore comments and our testsuite's own self tests.
346 sc_tests_Exit_not_exit:
347         @found=false; for file in $(xtests); do \
348           case $$file in */self-check-*) continue;; esac; \
349           res=`sed -n -e '/^#/d' -e '/<<.*END/,/^END/b' -e '/<<.*EOF/,/^EOF/b' \
350                       -e '/exit [$$0-9]/p' $$file`; \
351           if test -n "$$res"; then \
352             echo "$$file:$$res"; \
353             found=true; \
354           fi; \
355         done; \
356         if $$found; then \
357           echo 'Do not call plain "exit", use "Exit" instead, in above tests.' 1>&2; \
358           exit 1; \
359         fi
360
361 ## Use AUTOMAKE_fails when appropriate
362 sc_tests_automake_fails:
363         @if grep -v '^#' $(xtests) | grep '\$$AUTOMAKE.*&&.*[eE]xit'; then \
364           echo 'Use AUTOMAKE_fails + grep to catch automake failures in the above tests.' 1>&2;  \
365           exit 1; \
366         fi
367
368 ## Setting 'required' after sourcing './defs' is a bug.
369 sc_tests_required_after_defs:
370         @for file in $(xtests); do \
371           if out=`sed -n '/defs/,$${/required=/p;}' $$file`; test -n "$$out"; then \
372             echo 'Do not set "required" after sourcing "defs" in '"$$file: $$out" 1>&2; \
373             exit 1; \
374           fi; \
375         done
376
377 ## Overriding a Makefile macro on the command line is not portable when
378 ## recursive targets are used.  Better use an envvar.  SHELL is an
379 ## exception, POSIX says it can't come from the environment.  V, DESTDIR,
380 ## DISTCHECK_CONFIGURE_FLAGS and DISABLE_HARD_ERRORS are exceptions, too,
381 ## as package authors are urged not to initialize them anywhere.
382 ## Finally, 'exp' is used by some ad-hoc checks, where we ensure it's
383 ## ok to override it from the command line.
384 sc_tests_overriding_macros_on_cmdline:
385         @if grep -E '\$$MAKE .*(SHELL=.*=|=.*SHELL=)' $(xtests); then \
386           echo 'Rewrite "$$MAKE foo=bar SHELL=$$SHELL" as "foo=bar $$MAKE -e SHELL=$$SHELL"' 1>&2; \
387           echo ' in the above lines, it is more portable.' 1>&2; \
388           exit 1; \
389         fi
390 # The first s/// tries to account for usages like "$MAKE || st=$?".
391 # 'DISTCHECK_CONFIGURE_FLAGS' and 'exp' are allowed to contain whitespace in
392 # their definitions, hence the more complex last three substitutions below.
393 # Also, the 'make-dryrun.sh' is whitelisted, since there we need to
394 # override variables from the command line in order to cover the expected
395 # code paths.
396         @tests=`for t in $(xtests); do \
397                   case $$t in */make-dryrun.sh);; *) echo $$t;; esac; \
398                 done`; \
399         if sed -e 's/ || .*//' -e 's/ && .*//' \
400                 -e 's/ DESTDIR=[^ ]*/ /' -e 's/ SHELL=[^ ]*/ /' \
401                 -e 's/ V=[^ ]*/ /' -e 's/ DISABLE_HARD_ERRORS=[^ ]*/ /' \
402                 -e "s/ DISTCHECK_CONFIGURE_FLAGS='[^']*'/ /" \
403                 -e 's/ DISTCHECK_CONFIGURE_FLAGS="[^"]*"/ /' \
404                 -e 's/ DISTCHECK_CONFIGURE_FLAGS=[^ ]/ /' \
405                 -e "s/ exp='[^']*'/ /" \
406                 -e 's/ exp="[^"]*"/ /' \
407                 -e 's/ exp=[^ ]/ /' \
408               $$tests | grep '\$$MAKE .*='; then \
409           echo 'Rewrite "$$MAKE foo=bar" as "foo=bar $$MAKE -e" in the above lines,' 1>&2; \
410           echo 'it is more portable.' 1>&2; \
411           exit 1; \
412         fi
413         @if grep 'SHELL=.*\$$MAKE' $(xtests); then \
414           echo '$$MAKE ignores the SHELL envvar, use "$$MAKE SHELL=$$SHELL" in' 1>&2; \
415           echo 'the above lines.' 1>&2; \
416           exit 1; \
417         fi
418
419 ## Never use 'sleep 1' to create files with different timestamps.
420 ## Use '$sleep' instead.  Some filesystems (e.g., Windows) have only
421 ## a 2sec resolution.
422 sc_tests_plain_sleep:
423         @if grep -E '\bsleep +[12345]\b' $(xtests); then \
424           echo 'Do not use "sleep x" in the above tests.  Use "$$sleep" instead.' 1>&2; \
425           exit 1; \
426         fi
427
428 ## fgrep and egrep are not required by POSIX.
429 sc_m4_am_plain_egrep_fgrep:
430         @if grep -E '\b[ef]grep\b' $(ams) $(srcdir)/m4/*.m4; then \
431           echo 'Do not use egrep or fgrep in the above files,' \
432                'they are not portable.' 1>&2; \
433           exit 1; \
434         fi
435
436 ## Prefer 'configure.ac' over the obsolescent 'configure.in' as the name
437 ## for configure input files in our testsuite.  The latter  has been
438 ## deprecated for several years (at least since autoconf 2.50).
439 sc_tests_no_configure_in:
440         @if grep -E '\bconfigure\\*\.in\b' $(xtests) $(xdefs) \
441               | grep -Ev '/backcompat.*\.(sh|tap):' \
442               | grep -Ev '/autodist-configure-no-subdir\.sh:' \
443               | grep -Ev '/(configure|help)\.sh:' \
444               | grep .; \
445         then \
446           echo "Use 'configure.ac', not 'configure.in', as the name" >&2; \
447           echo "for configure input files in the test cases above." >&2; \
448           exit 1; \
449         fi
450
451 ## Rule to ensure that the testsuite has been run before.  We don't depend
452 ## on 'check' here, because that would be very wasteful in the common case.
453 ## We could run "make check RECHECK_LOGS=" and avoid toplevel races with
454 ## AM_RECURSIVE_TARGETS.  Suggest keeping test directories around for
455 ## greppability of the Makefile.in files.
456 sc_ensure_testsuite_has_run:
457         @if test ! -f '$(TEST_SUITE_LOG)'; then \
458           echo 'Run "env keep_testdirs=yes make check" before' \
459                'running "make maintainer-check"' >&2; \
460           exit 1; \
461         fi
462 .PHONY: sc_ensure_testsuite_has_run
463
464 ## Ensure our warning and error messages do not contain duplicate 'warning:' prefixes.
465 ## This test actually depends on the testsuite having been run before.
466 sc_tests_logs_duplicate_prefixes: sc_ensure_testsuite_has_run
467         @if grep -E '(warning|error):.*(warning|error):' t/*.log; then \
468           echo 'Duplicate warning/error message prefixes seen in above tests.' >&2; \
469           exit 1; \
470         fi
471
472 ## Ensure variables are listed before rules in Makefile.in files we generate.
473 sc_tests_makefile_variable_order: sc_ensure_testsuite_has_run
474         @st=0; \
475         for file in `find t -name Makefile.in -print`; do \
476           latevars=`sed -n \
477             -e :x -e 's/#.*//' \
478             -e '/\\\\$$/{' -e N -e 'b x' -e '}' \
479             -e '# Literal TAB.' \
480             -e '1,/^    /d' \
481             -e '# Allow @ so we match conditionals.' \
482             -e '/^ *[a-zA-Z_@]\{1,\} *=/p' $$file`; \
483           if test -n "$$latevars"; then \
484             echo "Variables are expanded too late in $$file:" >&2; \
485             echo "$$latevars" | sed 's/^/  /' >&2; \
486             st=1; \
487           fi; \
488         done; \
489         test $$st -eq 0 || { \
490           echo 'Ensure variables are expanded before rules' >&2; \
491           exit 1; \
492         }
493
494 ## Using ':' as a PATH separator is not portable.
495 sc_tests_PATH_SEPARATOR:
496         @if grep -E '\bPATH=.*:.*' $(xtests) ; then \
497           echo "Use '\$$PATH_SEPARATOR', not ':', in PATH definitions" \
498                "above." 1>&2; \
499           exit 1; \
500         fi
501
502 ## Try to make sure all @...@ substitutions are covered by our
503 ## substitution rule.
504 sc_perl_at_substs:
505         @if test `grep -E '^[^#]*@[A-Za-z_0-9]+@' aclocal | wc -l` -ne 0; then \
506           echo "Unresolved @...@ substitution in aclocal" 1>&2; \
507           exit 1; \
508         fi
509         @if test `grep -E '^[^#]*@[A-Za-z_0-9]+@' automake | wc -l` -ne 0; then \
510           echo "Unresolved @...@ substitution in automake" 1>&2; \
511           exit 1; \
512         fi
513
514 sc_unquoted_DESTDIR:
515         @if grep -E "[^\'\"]\\\$$\(DESTDIR" $(ams); then \
516           echo 'Suspicious unquoted DESTDIR uses.' 1>&2 ; \
517           exit 1; \
518         fi
519
520 sc_tabs_in_texi:
521         @if grep '      ' $(srcdir)/doc/automake.texi; then \
522           echo 'Do not use tabs in the manual.' 1>&2; \
523           exit 1; \
524         fi
525
526 sc_at_in_texi:
527         @if grep -E '([^@]|^)@([         ][^@]|$$)' $(srcdir)/doc/automake.texi; \
528         then \
529           echo 'Unescaped @.' 1>&2; \
530           exit 1; \
531         fi
532
533 $(syntax_check_rules): automake aclocal
534 maintainer-check: $(syntax_check_rules)
535 .PHONY: maintainer-check $(syntax_check_rules)
536
537 ## Check that the list of tests given in the Makefile is equal to the
538 ## list of all test scripts in the Automake testsuite.
539 maintainer-check: maintainer-check-list-of-tests