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