Merge branch '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_diff_automake_in_automake \
43 sc_diff_aclocal_in_automake \
44 sc_perl_syntax \
45 sc_no_brace_variable_expansions \
46 sc_rm_minus_f \
47 sc_no_for_variable_in_macro \
48 sc_mkinstalldirs \
49 sc_pre_normal_post_install_uninstall \
50 sc_perl_no_undef \
51 sc_perl_no_split_regex_space \
52 sc_cd_in_backquotes \
53 sc_cd_relative_dir \
54 sc_perl_at_uscore_in_scalar_context \
55 sc_perl_local \
56 sc_AMDEP_TRUE_in_automake_in \
57 sc_tests_make_without_am_makeflags \
58 $(sc_obsolete_requirements_rules) \
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 ## These check avoids accidental configure substitutions in the source.
77 ## There are exactly 9 lines that should be modified from automake.in to
78 ## automake, and 10 lines that should be modified from aclocal.in to
79 ## aclocal; these wors out to 32 and 34 lines of diffs, respectively.
80 sc_diff_automake_in_automake:
81         @if test `diff $(srcdir)/automake.in automake | wc -l` -ne 32; then \
82           echo "found too many diffs between automake.in and automake" 1>&2; \
83           diff -c $(srcdir)/automake.in automake; \
84           exit 1; \
85         fi
86 sc_diff_aclocal_in_aclocal:
87         @if test `diff $(srcdir)/aclocal.in aclocal | wc -l` -ne 34; then \
88           echo "found too many diffs between aclocal.in and aclocal" 1>&2; \
89           diff -c $(srcdir)/aclocal.in aclocal; \
90           exit 1; \
91         fi
92
93 ## Syntax check with default Perl (on my machine, Perl 5).
94 sc_perl_syntax:
95         @perllibdir="./lib$(PATH_SEPARATOR)$(srcdir)/lib" $(PERL) -c -w automake
96         @perllibdir="./lib$(PATH_SEPARATOR)$(srcdir)/lib" $(PERL) -c -w aclocal
97
98 ## Expect no instances of '${...}'.  However, $${...} is ok, since that
99 ## is a shell construct, not a Makefile construct.
100 sc_no_brace_variable_expansions:
101         @if grep -v '^ *#' $(ams) | grep -F '$${' | grep -F -v '$$$$'; then \
102           echo "Found too many uses of '\$${' in the lines above." 1>&2; \
103           exit 1;                               \
104         else :; fi
105
106 ## Make sure 'rm' is called with '-f'.
107 sc_rm_minus_f:
108         @if grep -v '^#' $(ams) $(xtests) \
109            | grep -vE '/(spy-rm\.tap|subobj-clean.*-pr10697\.sh):' \
110            | grep -E '\<rm ([^-]|\-[^f ]*\>)'; \
111         then \
112           echo "Suspicious 'rm' invocation." 1>&2; \
113           exit 1;                               \
114         else :; fi
115
116 ## Never use something like "for file in $(FILES)", this doesn't work
117 ## if FILES is empty or if it contains shell meta characters (e.g. $ is
118 ## commonly used in Java filenames).
119 sc_no_for_variable_in_macro:
120         @if grep 'for .* in \$$(' $(ams) | grep -v '/Makefile\.am:'; then \
121           echo 'Use "list=$$(mumble); for var in $$$$list".' 1>&2 ; \
122           exit 1; \
123         else :; fi
124
125 ## Make sure all invocations of mkinstalldirs are correct.
126 sc_mkinstalldirs:
127         @if grep -n 'mkinstalldirs' $(ams) \
128               | grep -F -v '$$(mkinstalldirs)' \
129               | grep -v '^\./Makefile.am:[0-9][0-9]*:  *lib/mkinstalldirs \\$$'; \
130         then \
131           echo "Found incorrect use of mkinstalldirs in the lines above" 1>&2; \
132           exit 1; \
133         else :; fi
134
135 ## Make sure all calls to PRE/NORMAL/POST_INSTALL/UNINSTALL
136 sc_pre_normal_post_install_uninstall:
137         @if grep -E -n '\((PRE|NORMAL|POST)_(|UN)INSTALL\)' $(ams) | \
138               grep -v ':##' | grep -v ':        @\$$('; then \
139           echo "Found incorrect use of PRE/NORMAL/POST_INSTALL/UNINSTALL in the lines above" 1>&2; \
140           exit 1; \
141         else :; fi
142
143 ## We never want to use "undef", only "delete", but for $/.
144 sc_perl_no_undef:
145         @if grep -n -w 'undef ' $(srcdir)/automake.in | \
146               grep -F -v 'undef $$/'; then \
147           echo "Found undef in automake.in; use delete instead" 1>&2; \
148           exit 1; \
149         fi
150
151 ## We never want split (/ /,...), only split (' ', ...).
152 sc_perl_no_split_regex_space:
153         @if grep -n 'split (/ /' $(srcdir)/automake.in; then \
154           echo "Found bad split in the lines above." 1>&2; \
155           exit 1; \
156         fi
157
158 ## Look for cd within backquotes
159 sc_cd_in_backquotes:
160         @if grep -n '^[^#]*` *cd ' $(srcdir)/automake.in $(ams); then \
161           echo "Consider using \$$(am__cd) in the lines above." 1>&2; \
162           exit 1; \
163         fi
164
165 ## Look for cd to a relative directory (may be influenced by CDPATH).
166 ## Skip some known directories that are OK.
167 sc_cd_relative_dir:
168         @if grep -n '^[^#]*cd ' $(srcdir)/automake.in $(ams) | \
169               grep -v 'echo.*cd ' | \
170               grep -v 'am__cd =' | \
171               grep -v '^[^#]*cd [./]' | \
172               grep -v '^[^#]*cd \$$(top_builddir)' | \
173               grep -v '^[^#]*cd "\$$\$$am__cwd' | \
174               grep -v '^[^#]*cd \$$(abs' | \
175               grep -v '^[^#]*cd "\$$(DESTDIR)'; then \
176           echo "Consider using \$$(am__cd) in the lines above." 1>&2; \
177           exit 1; \
178         fi
179
180 ## Using @_ in a scalar context is most probably a programming error.
181 sc_perl_at_uscore_in_scalar_context:
182         @if grep -Hn '[^@_A-Za-z0-9][_A-Za-z0-9]*[^) ] *= *@_' $(srcdir)/automake.in; then \
183           echo "Using @_ in a scalar context in the lines above." 1>&2; \
184           exit 1; \
185         fi
186
187 ## Allow only few variables to be localized in Automake.
188 sc_perl_local:
189         @if egrep -v '^[ \t]*local \$$[_~]( *=|;)' $(srcdir)/automake.in | \
190                 grep '^[ \t]*local [^*]'; then \
191           echo "Please avoid 'local'." 1>&2; \
192           exit 1; \
193         fi
194
195 ## Don't let AMDEP_TRUE substitution appear in automake.in.
196 sc_AMDEP_TRUE_in_automake_in:
197         @if grep '@AMDEP''_TRUE@' $(srcdir)/automake.in; then \
198           echo "Don't put AMDEP_TRUE substitution in automake.in" 1>&2; \
199           exit 1; \
200         fi
201
202 ## Recursive make invocations should always pass $(AM_MAKEFLAGS)
203 ## to $(MAKE), for portability to non-GNU make.
204 sc_tests_make_without_am_makeflags:
205         @if grep '^[^#].*(MAKE) ' $(ams) $(srcdir)/automake.in \
206             | grep -v 'AM_MAKEFLAGS' \
207             | grep -v '/am/header-vars\.am:.*am--echo.*| $$(MAKE) -f *-'; \
208         then \
209           echo 'Use $$(MAKE) $$(AM_MAKEFLAGS).' 1>&2; \
210           exit 1; \
211         fi
212
213 ## Look out for some obsolete variables.
214 sc_tests_obsolete_variables:
215         @vars=" \
216           using_tap \
217           test_prefer_config_shell \
218           original_AUTOMAKE \
219           original_ACLOCAL \
220           parallel_tests \
221           am_parallel_tests \
222         "; \
223         seen=""; \
224         for v in $$vars; do \
225           if grep -E "\b$$v\b" $(xtests) $(xdefs); then \
226             seen="$$seen $$v"; \
227           fi; \
228         done; \
229         if test -n "$$seen"; then \
230           for v in $$seen; do \
231             case $$v in \
232               parallel_tests|am_parallel_tests) v2=am_serial_tests;; \
233               *) v2=am_$$v;; \
234             esac; \
235             echo "Variable '$$v' is obsolete, use '$$v2' instead." 1>&2; \
236           done; \
237           exit 1; \
238         else :; fi
239
240 ## Look out for obsolete requirements specified in the test cases.
241 sc_obsolete_requirements_rules = sc_no_texi2dvi-o sc_no_makeinfo-html
242 modern-requirement.texi2dvi-o = texi2dvi
243 modern-requirement.makeinfo-html = makeinfo
244
245 $(sc_obsolete_requirements_rules): sc_no_% :
246         @if grep -E 'required=.*\b$*\b' $(xtests); then \
247           echo "Requirement '$*' is obsolete and shouldn't" \
248                "be used anymore." >&2; \
249           echo "You should use '$(modern-requirement.$*)' instead." >&2; \
250           exit 1; \
251         fi
252
253 ## Tests should never call some programs directly, but only through the
254 ## corresponding variable (e.g., '$MAKE', not 'make').  This will allow
255 ## the programs to be overridden at configure time (for less brittleness)
256 ## or by the user at make time (to allow better testsuite coverage).
257 sc_tests_plain_check_rules = \
258   sc_tests_plain_egrep \
259   sc_tests_plain_fgrep \
260   sc_tests_plain_make \
261   sc_tests_plain_perl \
262   sc_tests_plain_automake \
263   sc_tests_plain_aclocal \
264   sc_tests_plain_autoconf \
265   sc_tests_plain_autoupdate \
266   sc_tests_plain_autom4te \
267   sc_tests_plain_autoheader \
268   sc_tests_plain_autoreconf
269
270 toupper = $(shell echo $(1) | LC_ALL=C tr '[a-z]' '[A-Z]')
271
272 $(sc_tests_plain_check_rules): sc_tests_plain_% :
273         @# The leading ':' in the grep below is what is printed by the
274         @# preceding 'grep -v' after the file name.
275         @# It works here as a poor man's substitute for beginning-of-line
276         @# marker.
277         @if grep -v '^[         ]*#' $(xtests) \
278            | $(EGREP) '(:|\bif|\bnot|[;!{\|\(]|&&|\|\|)[        ]*?$*\b'; \
279          then \
280            echo 'Do not run "$*" in the above tests.' \
281                 'Use "$$$(call toupper,$*)" instead.' 1>&2; \
282            exit 1; \
283         fi
284
285 ## Tests should only use END and EOF for here documents
286 ## (so that the next test is effective).
287 sc_tests_here_document_format:
288         @if grep '<<' $(xtests) | grep -Ev '\b(END|EOF)\b|\bcout <<'; then \
289           echo 'Use here documents with "END" and "EOF" only, for greppability.' 1>&2; \
290           exit 1; \
291         fi
292
293 ## Tests should never call exit directly, but use Exit.
294 ## This is so that the exit status is transported correctly across the 0 trap.
295 ## Ignore comments and our testsuite's own self tests.
296 sc_tests_Exit_not_exit:
297         @found=false; for file in $(xtests); do \
298           case $$file in */self-check-*) continue;; esac; \
299           res=`sed -n -e '/^#/d' -e '/<<.*END/,/^END/b' -e '/<<.*EOF/,/^EOF/b' \
300                       -e '/exit [$$0-9]/p' $$file`; \
301           if test -n "$$res"; then \
302             echo "$$file:$$res"; \
303             found=true; \
304           fi; \
305         done; \
306         if $$found; then \
307           echo 'Do not call plain "exit", use "Exit" instead, in above tests.' 1>&2; \
308           exit 1; \
309         fi
310
311 ## Use AUTOMAKE_fails when appropriate
312 sc_tests_automake_fails:
313         @if grep -v '^#' $(xtests) | grep '\$$AUTOMAKE.*&&.*[eE]xit'; then \
314           echo 'Use AUTOMAKE_fails + grep to catch automake failures in the above tests.' 1>&2;  \
315           exit 1; \
316         fi
317
318 ## Setting 'required' after sourcing './defs' is a bug.
319 sc_tests_required_after_defs:
320         @for file in $(xtests); do \
321           if out=`sed -n '/defs/,$${/required=/p;}' $$file`; test -n "$$out"; then \
322             echo 'Do not set "required" after sourcing "defs" in '"$$file: $$out" 1>&2; \
323             exit 1; \
324           fi; \
325         done
326
327 ## Overriding a Makefile macro on the command line is not portable when
328 ## recursive targets are used.  Better use an envvar.  SHELL is an
329 ## exception, POSIX says it can't come from the environment.  V, DESTDIR,
330 ## DISTCHECK_CONFIGURE_FLAGS and DISABLE_HARD_ERRORS are exceptions, too,
331 ## as package authors are urged not to initialize them anywhere.
332 ## Finally, 'exp' is used by some ad-hoc checks, where we ensure it's
333 ## ok to override it from the command line.
334 sc_tests_overriding_macros_on_cmdline:
335         @if grep -E '\$$MAKE .*(SHELL=.*=|=.*SHELL=)' $(xtests); then \
336           echo 'Rewrite "$$MAKE foo=bar SHELL=$$SHELL" as "foo=bar $$MAKE -e SHELL=$$SHELL"' 1>&2; \
337           echo ' in the above lines, it is more portable.' 1>&2; \
338           exit 1; \
339         fi
340 # The first s/// tries to account for usages like "$MAKE || st=$?".
341 # 'DISTCHECK_CONFIGURE_FLAGS' and 'exp' are allowed to contain whitespace in
342 # their definitions, hence the more complex last three substitutions below.
343 # Also, the 'make-dryrun.sh' is whitelisted, since there we need to
344 # override variables from the command line in order to cover the expected
345 # code paths.
346         @tests=`for t in $(xtests); do \
347                   case $$t in */make-dryrun.sh);; *) echo $$t;; esac; \
348                 done`; \
349         if sed -e 's/ || .*//' -e 's/ && .*//' \
350                 -e 's/ DESTDIR=[^ ]*/ /' -e 's/ SHELL=[^ ]*/ /' \
351                 -e 's/ V=[^ ]*/ /' -e 's/ DISABLE_HARD_ERRORS=[^ ]*/ /' \
352                 -e "s/ DISTCHECK_CONFIGURE_FLAGS='[^']*'/ /" \
353                 -e 's/ DISTCHECK_CONFIGURE_FLAGS="[^"]*"/ /' \
354                 -e 's/ DISTCHECK_CONFIGURE_FLAGS=[^ ]/ /' \
355                 -e "s/ exp='[^']*'/ /" \
356                 -e 's/ exp="[^"]*"/ /' \
357                 -e 's/ exp=[^ ]/ /' \
358               $$tests | grep '\$$MAKE .*='; then \
359           echo 'Rewrite "$$MAKE foo=bar" as "foo=bar $$MAKE -e" in the above lines,' 1>&2; \
360           echo 'it is more portable.' 1>&2; \
361           exit 1; \
362         fi
363         @if grep 'SHELL=.*\$$MAKE' $(xtests); then \
364           echo '$$MAKE ignores the SHELL envvar, use "$$MAKE SHELL=$$SHELL" in' 1>&2; \
365           echo 'the above lines.' 1>&2; \
366           exit 1; \
367         fi
368
369 ## Never use 'sleep 1' to create files with different timestamps.
370 ## Use '$sleep' instead.  Some filesystems (e.g., Windows) have only
371 ## a 2sec resolution.
372 sc_tests_plain_sleep:
373         @if grep -E '\bsleep +[12345]\b' $(xtests); then \
374           echo 'Do not use "sleep x" in the above tests.  Use "$$sleep" instead.' 1>&2; \
375           exit 1; \
376         fi
377
378 ## fgrep and egrep are not required by POSIX.
379 sc_m4_am_plain_egrep_fgrep:
380         @if grep -E '\b[ef]grep\b' $(ams) $(srcdir)/m4/*.m4; then \
381           echo 'Do not use egrep or fgrep in the above files,' \
382                'they are not portable.' 1>&2; \
383           exit 1; \
384         fi
385
386 ## Prefer 'configure.ac' over the obsolescent 'configure.in' as the name
387 ## for configure input files in our testsuite.  The latter  has been
388 ## deprecated for several years (at least since autoconf 2.50).
389 sc_tests_no_configure_in:
390         @if grep -E '\bconfigure\\*\.in\b' $(xtests) $(xdefs) \
391               | grep -Ev '/backcompat.*\.(sh|tap):' \
392               | grep -Ev '/autodist-configure-no-subdir\.sh:' \
393               | grep -Ev '/(configure|help)\.sh:' \
394               | grep .; \
395         then \
396           echo "Use 'configure.ac', not 'configure.in', as the name" >&2; \
397           echo "for configure input files in the test cases above." >&2; \
398           exit 1; \
399         fi
400
401 ## Rule to ensure that the testsuite has been run before.  We don't depend
402 ## on 'check' here, because that would be very wasteful in the common case.
403 ## We could run "make check RECHECK_LOGS=" and avoid toplevel races with
404 ## AM_RECURSIVE_TARGETS.  Suggest keeping test directories around for
405 ## greppability of the Makefile.in files.
406 sc_ensure_testsuite_has_run:
407         @if test ! -f '$(TEST_SUITE_LOG)'; then \
408           echo 'Run "env keep_testdirs=yes make check" before' \
409                'running "make maintainer-check"' >&2; \
410           exit 1; \
411         fi
412 .PHONY: sc_ensure_testsuite_has_run
413
414 ## Ensure our warning and error messages do not contain duplicate 'warning:' prefixes.
415 ## This test actually depends on the testsuite having been run before.
416 sc_tests_logs_duplicate_prefixes: sc_ensure_testsuite_has_run
417         @if grep -E '(warning|error):.*(warning|error):' t/*.log; then \
418           echo 'Duplicate warning/error message prefixes seen in above tests.' >&2; \
419           exit 1; \
420         fi
421
422 ## Ensure variables are listed before rules in Makefile.in files we generate.
423 sc_tests_makefile_variable_order: sc_ensure_testsuite_has_run
424         @st=0; \
425         for file in `find t -name Makefile.in -print`; do \
426           latevars=`sed -n \
427             -e :x -e 's/#.*//' \
428             -e '/\\\\$$/{' -e N -e 'b x' -e '}' \
429             -e '# Literal TAB.' \
430             -e '1,/^    /d' \
431             -e '# Allow @ so we match conditionals.' \
432             -e '/^ *[a-zA-Z_@]\{1,\} *=/p' $$file`; \
433           if test -n "$$latevars"; then \
434             echo "Variables are expanded too late in $$file:" >&2; \
435             echo "$$latevars" | sed 's/^/  /' >&2; \
436             st=1; \
437           fi; \
438         done; \
439         test $$st -eq 0 || { \
440           echo 'Ensure variables are expanded before rules' >&2; \
441           exit 1; \
442         }
443
444 ## Using ':' as a PATH separator is not portable.
445 sc_tests_PATH_SEPARATOR:
446         @if grep -E '\bPATH=.*:.*' $(xtests) ; then \
447           echo "Use '\$$PATH_SEPARATOR', not ':', in PATH definitions" \
448                "above." 1>&2; \
449           exit 1; \
450         fi
451
452 ## Try to make sure all @...@ substitutions are covered by our
453 ## substitution rule.
454 sc_perl_at_substs:
455         @if test `grep -E '^[^#]*@[A-Za-z_0-9]+@' aclocal | wc -l` -ne 0; then \
456           echo "Unresolved @...@ substitution in aclocal" 1>&2; \
457           exit 1; \
458         fi
459         @if test `grep -E '^[^#]*@[A-Za-z_0-9]+@' automake | wc -l` -ne 0; then \
460           echo "Unresolved @...@ substitution in automake" 1>&2; \
461           exit 1; \
462         fi
463
464 sc_unquoted_DESTDIR:
465         @if grep -E "[^\'\"]\\\$$\(DESTDIR" $(ams); then \
466           echo 'Suspicious unquoted DESTDIR uses.' 1>&2 ; \
467           exit 1; \
468         fi
469
470 sc_tabs_in_texi:
471         @if grep '      ' $(srcdir)/doc/automake.texi; then \
472           echo 'Do not use tabs in the manual.' 1>&2; \
473           exit 1; \
474         fi
475
476 sc_at_in_texi:
477         @if grep -E '([^@]|^)@([         ][^@]|$$)' $(srcdir)/doc/automake.texi; \
478         then \
479           echo 'Unescaped @.' 1>&2; \
480           exit 1; \
481         fi
482
483 $(syntax_check_rules): automake aclocal
484 maintainer-check: $(syntax_check_rules)
485 .PHONY: maintainer-check $(syntax_check_rules)
486
487 ## Check that the list of tests given in the Makefile is equal to the
488 ## list of all test scripts in the Automake testsuite.
489 maintainer-check: maintainer-check-list-of-tests