ylwrap: preserve subdirectories in "#line" munging
[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 -F '$${' $(ams) | 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 -v '/spy-rm\.tap:' \
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           parallel_tests \
281           test_prefer_config_shell \
282           original_AUTOMAKE \
283           original_ACLOCAL \
284         "; \
285         seen=""; \
286         for v in $$vars; do \
287           if grep -E "\b$$v\b" $(xtests) $(xdefs); then \
288             seen="$$seen $$v"; \
289           fi; \
290         done; \
291         if test -n "$$seen"; then \
292           for v in $$seen; do \
293             echo "Variable '$$v' is obsolete, use 'am_$$v' instead." 1>&2; \
294           done; \
295           exit 1; \
296         else :; fi
297
298 ## Tests should never call some programs directly, but only through the
299 ## corresponding variable (e.g., '$MAKE', not 'make').  This will allow
300 ## the programs to be overridden at configure time (for less brittleness)
301 ## or by the user at make time (to allow better testsuite coverage).
302 sc_tests_plain_check_rules = \
303   sc_tests_plain_egrep \
304   sc_tests_plain_fgrep \
305   sc_tests_plain_make \
306   sc_tests_plain_perl \
307   sc_tests_plain_automake \
308   sc_tests_plain_aclocal \
309   sc_tests_plain_autoconf \
310   sc_tests_plain_autoupdate \
311   sc_tests_plain_autom4te \
312   sc_tests_plain_autoheader \
313   sc_tests_plain_autoreconf
314
315 toupper = $(shell echo $(1) | LC_ALL=C tr '[a-z]' '[A-Z]')
316
317 $(sc_tests_plain_check_rules): sc_tests_plain_% :
318         @# The leading ':' in the grep below is what is printed by the
319         @# preceding 'grep -v' after the file name.
320         @# It works here as a poor man's substitute for beginning-of-line
321         @# marker.
322         @if grep -v '^[         ]*#' $(xtests) \
323            | $(EGREP) '(:|\bif|\bnot|[;!{\|\(]|&&|\|\|)[        ]*?$*\b'; \
324          then \
325            echo 'Do not run "$*" in the above tests.' \
326                 'Use "$$$(call toupper,$*)" instead.' 1>&2; \
327            exit 1; \
328         fi
329
330 ## Tests should only use END and EOF for here documents
331 ## (so that the next test is effective).
332 sc_tests_here_document_format:
333         @if grep '<<' $(xtests) | grep -Ev '\b(END|EOF)\b|\bstd::cout <<'; then \
334           echo 'Use here documents with "END" and "EOF" only, for greppability.' 1>&2; \
335           exit 1; \
336         fi
337
338 ## Tests should never call exit directly, but use Exit.
339 ## This is so that the exit status is transported correctly across the 0 trap.
340 ## Ignore comments, testsuite self tests, and one perl line in ext2.sh.
341 sc_tests_Exit_not_exit:
342         @found=false; for file in $(xtests); do \
343           case $$file in */self-check-*) continue;; esac; \
344           res=`sed -n -e '/^#/d; /^\$$PERL/d' -e '/<<.*END/,/^END/b' \
345                       -e '/<<.*EOF/,/^EOF/b' -e '/exit [$$0-9]/p' $$file`; \
346           if test -n "$$res"; then \
347             echo "$$file:$$res"; \
348             found=true; \
349           fi; \
350         done; \
351         if $$found; then \
352           echo 'Do not call plain "exit", use "Exit" instead, in above tests.' 1>&2; \
353           exit 1; \
354         fi
355
356 ## Use AUTOMAKE_fails when appropriate
357 sc_tests_automake_fails:
358         @if grep -v '^#' $(xtests) | grep '\$$AUTOMAKE.*&&.*[eE]xit'; then \
359           echo 'Use AUTOMAKE_fails + grep to catch automake failures in the above tests.' 1>&2;  \
360           exit 1; \
361         fi
362
363 ## Setting 'required' after sourcing './defs' is a bug.
364 sc_tests_required_after_defs:
365         @for file in $(xtests); do \
366           if out=`sed -n '/defs/,$${/required=/p;}' $$file`; test -n "$$out"; then \
367             echo 'Do not set "required" after sourcing "defs" in '"$$file: $$out" 1>&2; \
368             exit 1; \
369           fi; \
370         done
371
372 ## Overriding a Makefile macro on the command line is not portable when
373 ## recursive targets are used.  Better use an envvar.  SHELL is an
374 ## exception, POSIX says it can't come from the environment.  V, DESTDIR,
375 ## DISTCHECK_CONFIGURE_FLAGS and DISABLE_HARD_ERRORS are exceptions, too,
376 ## as package authors are urged not to initialize them anywhere.
377 ## Finally, 'exp' is used by some ad-hoc checks, where we ensure it's
378 ## ok to override it from the command line.
379 sc_tests_overriding_macros_on_cmdline:
380         @if grep -E '\$$MAKE .*(SHELL=.*=|=.*SHELL=)' $(xtests); then \
381           echo 'Rewrite "$$MAKE foo=bar SHELL=$$SHELL" as "foo=bar $$MAKE -e SHELL=$$SHELL"' 1>&2; \
382           echo ' in the above lines, it is more portable.' 1>&2; \
383           exit 1; \
384         fi
385 # The first s/// tries to account for usages like "$MAKE || st=$?".
386 # 'DISTCHECK_CONFIGURE_FLAGS' and 'exp' are allowed to contain whitespace in
387 # their definitions, hence the more complex last three substitutions below.
388 # Also, the 'make-dryrun.sh' is whitelisted, since there we need to
389 # override variables from the command line in order to cover the expected
390 # code paths.
391         @tests=`for t in $(xtests); do \
392                   case $$t in */make-dryrun.sh);; *) echo $$t;; esac; \
393                 done`; \
394         if sed -e 's/ || .*//' -e 's/ && .*//' \
395                 -e 's/ DESTDIR=[^ ]*/ /' -e 's/ SHELL=[^ ]*/ /' \
396                 -e 's/ V=[^ ]*/ /' -e 's/ DISABLE_HARD_ERRORS=[^ ]*/ /' \
397                 -e "s/ DISTCHECK_CONFIGURE_FLAGS='[^']*'/ /" \
398                 -e 's/ DISTCHECK_CONFIGURE_FLAGS="[^"]*"/ /' \
399                 -e 's/ DISTCHECK_CONFIGURE_FLAGS=[^ ]/ /' \
400                 -e "s/ exp='[^']*'/ /" \
401                 -e 's/ exp="[^"]*"/ /' \
402                 -e 's/ exp=[^ ]/ /' \
403               $$tests | grep '\$$MAKE .*='; then \
404           echo 'Rewrite "$$MAKE foo=bar" as "foo=bar $$MAKE -e" in the above lines,' 1>&2; \
405           echo 'it is more portable.' 1>&2; \
406           exit 1; \
407         fi
408         @if grep 'SHELL=.*\$$MAKE' $(xtests); then \
409           echo '$$MAKE ignores the SHELL envvar, use "$$MAKE SHELL=$$SHELL" in' 1>&2; \
410           echo 'the above lines.' 1>&2; \
411           exit 1; \
412         fi
413
414 ## Never use 'sleep 1' to create files with different timestamps.
415 ## Use '$sleep' instead.  Some filesystems (e.g., Windows) have only
416 ## a 2sec resolution.
417 sc_tests_plain_sleep:
418         @if grep -E '\bsleep +[12345]\b' $(xtests); then \
419           echo 'Do not use "sleep x" in the above tests.  Use "$$sleep" instead.' 1>&2; \
420           exit 1; \
421         fi
422
423 ## fgrep and egrep are not required by POSIX.
424 sc_m4_am_plain_egrep_fgrep:
425         @if grep -E '\b[ef]grep\b' $(ams) $(srcdir)/m4/*.m4; then \
426           echo 'Do not use egrep or fgrep in the above files,' \
427                'they are not portable.' 1>&2; \
428           exit 1; \
429         fi
430
431 ## Prefer 'configure.ac' over the obsolescent 'configure.in' as the name
432 ## for configure input files in our testsuite.  The latter  has been
433 ## deprecated for several years (at least since autoconf 2.50).
434 sc_tests_no_configure_in:
435         @if grep -E '\bconfigure\\*\.in\b' $(xtests) $(xdefs) \
436               | grep -Ev '/backcompat.*\.(sh|tap):' \
437               | grep -Ev '/autodist-configure-no-subdir\.sh:' \
438               | grep -Ev '/(configure|help)\.sh:' \
439               | grep .; \
440         then \
441           echo "Use 'configure.ac', not 'configure.in', as the name" >&2; \
442           echo "for configure input files in the test cases above." >&2; \
443           exit 1; \
444         fi
445
446 ## Rule to ensure that the testsuite has been run before.  We don't depend
447 ## on 'check' here, because that would be very wasteful in the common case.
448 ## We could run "make check RECHECK_LOGS=" and avoid toplevel races with
449 ## AM_RECURSIVE_TARGETS.  Suggest keeping test directories around for
450 ## greppability of the Makefile.in files.
451 sc_ensure_testsuite_has_run:
452         @if test ! -f '$(TEST_SUITE_LOG)'; then \
453           echo 'Run "env keep_testdirs=yes make check" before' \
454                'running "make maintainer-check"' >&2; \
455           exit 1; \
456         fi
457 .PHONY: sc_ensure_testsuite_has_run
458
459 ## Ensure our warning and error messages do not contain duplicate 'warning:' prefixes.
460 ## This test actually depends on the testsuite having been run before.
461 sc_tests_logs_duplicate_prefixes: sc_ensure_testsuite_has_run
462         @if grep -E '(warning|error):.*(warning|error):' t/*.log; then \
463           echo 'Duplicate warning/error message prefixes seen in above tests.' >&2; \
464           exit 1; \
465         fi
466
467 ## Ensure variables are listed before rules in Makefile.in files we generate.
468 sc_tests_makefile_variable_order: sc_ensure_testsuite_has_run
469         @st=0; \
470         for file in `find t -name Makefile.in -print`; do \
471           latevars=`sed -n \
472             -e :x -e 's/#.*//' \
473             -e '/\\\\$$/{' -e N -e 'b x' -e '}' \
474             -e '# Literal TAB.' \
475             -e '1,/^    /d' \
476             -e '# Allow @ so we match conditionals.' \
477             -e '/^ *[a-zA-Z_@]\{1,\} *=/p' $$file`; \
478           if test -n "$$latevars"; then \
479             echo "Variables are expanded too late in $$file:" >&2; \
480             echo "$$latevars" | sed 's/^/  /' >&2; \
481             st=1; \
482           fi; \
483         done; \
484         test $$st -eq 0 || { \
485           echo 'Ensure variables are expanded before rules' >&2; \
486           exit 1; \
487         }
488
489 ## Using ':' as a PATH separator is not portable.
490 sc_tests_PATH_SEPARATOR:
491         @if grep -E '\bPATH=.*:.*' $(xtests) ; then \
492           echo "Use '\$$PATH_SEPARATOR', not ':', in PATH definitions" \
493                "above." 1>&2; \
494           exit 1; \
495         fi
496
497 ## Try to make sure all @...@ substitutions are covered by our
498 ## substitution rule.
499 sc_perl_at_substs:
500         @if test `grep -E '^[^#]*@[A-Za-z_0-9]+@' aclocal | wc -l` -ne 0; then \
501           echo "Unresolved @...@ substitution in aclocal" 1>&2; \
502           exit 1; \
503         fi
504         @if test `grep -E '^[^#]*@[A-Za-z_0-9]+@' automake | wc -l` -ne 0; then \
505           echo "Unresolved @...@ substitution in automake" 1>&2; \
506           exit 1; \
507         fi
508
509 sc_unquoted_DESTDIR:
510         @if grep -E "[^\'\"]\\\$$\(DESTDIR" $(ams); then \
511           echo 'Suspicious unquoted DESTDIR uses.' 1>&2 ; \
512           exit 1; \
513         fi
514
515 sc_tabs_in_texi:
516         @if grep '      ' $(srcdir)/doc/automake.texi; then \
517           echo 'Do not use tabs in the manual.' 1>&2; \
518           exit 1; \
519         fi
520
521 sc_at_in_texi:
522         @if grep -E '([^@]|^)@([         ][^@]|$$)' $(srcdir)/doc/automake.texi; \
523         then \
524           echo 'Unescaped @.' 1>&2; \
525           exit 1; \
526         fi
527
528 $(syntax_check_rules): automake aclocal
529 maintainer-check: $(syntax_check_rules)
530 .PHONY: maintainer-check $(syntax_check_rules)
531
532 ## Check that the list of tests given in the Makefile is equal to the
533 ## list of all test scripts in the Automake testsuite.
534 maintainer-check: maintainer-check-list-of-tests