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