Merge branch 'branch-1.13.2' into maint
[platform/upstream/automake.git] / t / ax / depcomp.sh
1 #! /bin/sh
2 # Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # Check dependency tracking in various flavours.
18 # Contains both libtool and non-libtool case.
19 # Sourced by the various (autogenerated) 'depcomp*.tap' tests.
20 # Examples of reported failures that motivated those test are
21 # listed below.
22
23 # -------------------------------------------------------------------------
24
25 # <http://lists.gnu.org/archive/html/automake-patches/2011-04/msg00028.html>
26 #
27 # Here's the bug: makedepend will prefix VPATH to the object file name,
28 # thus the second make will invoke depcomp with object='../../src/foo.o',
29 # causing errors such as:
30 #
31 #  touch: cannot touch '../../src/.deps/foo.TPo': No such file or directory
32 #  makedepend: error:  cannot open "../../src/.deps/foo.TPo"
33 #  ../../depcomp: line 560: ../../src/.deps/foo.TPo: No such file or directory
34
35 # -------------------------------------------------------------------------
36
37 # <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8473>
38 # <http://lists.gnu.org/archive/html/automake-patches/2011-04/msg00079.html>
39 #
40 # Here's the bug: hp depmode will prefix VPATH to the object file name,
41 # thus the second gmake will invoke depcomp with object='../../src/foo.o',
42 # causing errors such as (broken on multiple lines for clarity):
43 #
44 #   cpp: "", line 0: error 4066: Cannot create
45 #        "../../gllib/.deps/nonblocking.TPo" file for
46 #        "-M../../gllib/.deps/nonblocking.TPo" option.
47 #        (No such file or directory[errno=2])
48 #
49
50 # -------------------------------------------------------------------------
51
52 # <http://lists.gnu.org/archive/html/automake-patches/2011-04/msg00140.html>
53 # <http://lists.gnu.org/archive/html/automake-patches/2011-05/msg00019.html>
54 #
55 # A partial failure of an earlier version of this test; some bad
56 # post-processing of the '*.Po' files led to the following broken
57 # contents of 'src/sub/.deps/subfoo.Po':
58 #
59 #  > sub/subfoo.o: ../../depmod-data.dir/src/sub/subfoo.c \
60 #  >   ../../depmod-data.dir/src/foo.h
61 #  > ../../depmod-data.dir/src/sub/subfoo.c \:
62 #  >   ../../depmod-data.dir/src/foo.h:
63 #
64 # which caused make to die with an error like:
65 #
66 #  "sub/.deps/subfoo.Po:3: *** missing separator.  Stop."
67
68 # -------------------------------------------------------------------------
69
70 # This code expects test-init.sh has already been included in advance.
71
72 ocwd=$(pwd) || fatal_ "getting current working directory"
73 longpath=this-is/a-path/which-has/quite-a/definitely/truly/long_long_name
74 cachevar=am_cv_CC_dependencies_compiler_type
75
76 srctree=depmod-1.0
77 mkdir $srctree
78 cd $srctree
79
80 cd_top ()
81 {
82   cd "$ocwd" || fatal_ "cannot chdir back to top directory"
83 }
84
85 delete ()
86 {
87   test -f "$1" || fatal_ "$1: file does not exist"
88   rm -f "$1" || fatal_ "$1: couldn't remove"
89 }
90
91 edit ()
92 {
93   file=$1; shift
94   sed "$@" <"$file" > t && mv -f t "$file" \
95     || fatal_ "$file: editing of file failed"
96 }
97
98 rewrite ()
99 {
100   file=$1; shift
101   "$@" > "$file" || fatal_ "$file: couldn't rewrite"
102 }
103
104 setup_srcdir ()
105 {
106   srcdir=$1 # This is intended to be global.
107   mkdir -p "$srcdir" \
108     || fatal_ "couldn't create source directory '$srcdir'"
109   cp -pR "$ocwd/$srctree"/* "$srcdir"/ \
110     || fatal_ "couldn't populate source directory '$srcdir'"
111 }
112
113
114 check_no_depfiles ()
115 {
116   find . -name '*.Plo' -o -name '*.Po' | grep . && return 1
117   return 0
118 }
119
120 check_distclean ()
121 {
122   # "make distcleancheck" can only run from a VPATH build.
123   if test $vpath = no; then
124     make_ok distclean && check_no_depfiles
125   else
126     $MAKE distcleancheck
127   fi
128 }
129
130 cat > configure.ac <<END
131 AC_INIT([$me], [1.0])
132 AC_CONFIG_AUX_DIR([build-aux])
133 AM_INIT_AUTOMAKE([subdir-objects])
134 AC_PROG_CC
135 AM_PROG_AR
136 $(if test $depcomp_with_libtool = yes; then
137     echo AC_PROG_LIBTOOL
138   else
139     echo AC_PROG_RANLIB
140   fi)
141 AC_CONFIG_FILES([Makefile src/Makefile])
142 AC_OUTPUT
143 END
144
145 mkdir build-aux sub src src/sub2
146
147 case $depcomp_with_libtool in
148   yes)
149     po=Plo objext=lo a=la
150     normalized_target=libfoo_la
151     # On platforms requiring that no undefined symbols exist in order
152     # to build shared libraries (e.g. Windows DLLs), you have to
153     # explicitly declare that the libtool library you are building
154     # does not actually have any undefined symbols, for libtool to
155     # even try to build it as a shared library.  Without that
156     # explicit declaration, libtool falls back to a static library
157     # only, regardless of any --enable-shared flags etc.
158     LIBPRIMARY=LTLIBRARIES LINKADD=LIBADD NOUNDEF=-no-undefined
159     libbaz_ldflags="libbaz_${a}_LDFLAGS = $NOUNDEF"
160     echo lib_LTLIBRARIES = libfoo.la >> Makefile.am
161     make_ok ()
162     {
163       $MAKE ${1+"$@"} >output 2>&1 || { cat output; return 1; }
164       cat output
165       $FGREP 'unknown directive' output && return 1
166       rm -f output
167       # Checks for stray files possibly left around by less common
168       # depmodes.
169       find . -name '*.[ud]' | grep . && return 1
170       return 0
171     }
172     ;;
173   no)
174     po=Po objext='$(OBJEXT)' a=a
175     normalized_target=foo
176     LIBPRIMARY=LIBRARIES LINKADD=LDADD NOUNDEF=
177     libbaz_ldflags=
178     echo bin_PROGRAMS = foo >> Makefile.am
179     make_ok ()
180     {
181       $MAKE ${1+"$@"}
182     }
183     ;;
184   *)
185     fatal_ "invalid value '$depcomp_with_libtool' for variable" \
186            "\$depcomp_with_libtool"
187     ;;
188 esac
189
190 cat >> Makefile.am <<END
191 SUBDIRS = src
192 # We include subfoo only to be sure that the munging in depcomp
193 # doesn't remove too much from the object file name.
194 ${normalized_target}_SOURCES = foo.c sub/subfoo.c foo.h sub/subfoo.h
195 ${normalized_target}_LDFLAGS = $NOUNDEF
196 ${normalized_target}_${LINKADD} = src/libbaz.$a
197
198 .PHONY: grep-test
199 grep-test:
200 ## For debugging.
201         cat \$(DEPDIR)/foo.$po || :
202         cat sub/\$(DEPDIR)/subfoo.$po || :
203         cat src/\$(DEPDIR)/baz.$po || :
204         cat src/sub2/\$(DEPDIR)/sub2foo.$po || :
205 ## Checks are done here.
206         grep '^foo.$objext.*:' \$(DEPDIR)/foo.$po
207         grep '^sub/subfoo\.$objext.*:' sub/\$(DEPDIR)/subfoo.$po
208         grep '^baz\.$objext.*:' src/\$(DEPDIR)/baz.$po
209         grep '^sub2/sub2foo\.$objext.*:' src/sub2/\$(DEPDIR)/sub2foo.$po
210 END
211
212 cat > src/Makefile.am <<END
213 noinst_${LIBPRIMARY} = libbaz.$a
214 # We include sub2foo only to be sure that the munging in depcomp
215 # doesn't remove too much from the object file name.
216 libbaz_${a}_SOURCES = baz.c sub2/sub2foo.c baz.h sub2/sub2foo.h
217 $libbaz_ldflags
218 END
219
220 cat > foo.c <<'END'
221 #include "foo.h"
222 #include "src/baz.h"
223 #include <stdlib.h>
224 int main (void)
225 {
226   printf ("foo bar\n");
227   exit (EXIT_SUCCESS + subfoo () + baz ());
228 }
229 END
230
231 cat > foo.h <<'END'
232 #include <stdio.h>
233 #include "sub/subfoo.h"
234 END
235
236 cat > sub/subfoo.c <<'END'
237 #include "sub/subfoo.h"
238 int subfoo (void) { return 0; }
239 END
240
241 echo '/* empty */' > src/sub2/sub2foo.h
242
243 cat > sub/subfoo.h <<'END'
244 #include <stdio.h>
245 extern int subfoo (void);
246 END
247
248 cat > src/baz.c <<'END'
249 #include "baz.h"
250 int baz (void) { return 0; }
251 END
252
253 cat > src/baz.h <<'END'
254 extern int baz (void);
255 END
256
257 cat > src/sub2/sub2foo.c <<'END'
258 #include "sub2foo.h"
259 int sub2foo (void) { return 0; }
260 END
261
262 test $depcomp_with_libtool = no || libtoolize \
263   || fatal_ "libtoolize failed"
264 $ACLOCAL && $AUTOCONF && $AUTOMAKE -a \
265   || fatal_ "autotools failed"
266 test -f build-aux/depcomp \
267   || fatal_ "depcomp script not installed"
268
269 # To offer extra coverage for the depmodes (like "aix" of "hp2") where the
270 # name of the compiler-generated depfiles can depend on whether libtool is
271 # in use *and* on which kind of libraries libtool is building (static,
272 # shared, or both), we would like to run the libtool-oriented tests thrice:
273 # once after having run configure with the '--disable-shared' option, once
274 # after having run it with the '--enable-shared' options, and once by
275 # leaving it to configure to automatically select which kind of library (or
276 # libraries) to build.
277 #
278 # But doing such three-fold checks unconditionally for all the depmodes
279 # would slow down the already too slow libtool tests unacceptably (up to a
280 # 150-200% factor), with no real gain in coverage for most of the depmodes.
281 # So, since the depmodes that would benefit from the extra tests are never
282 # forced to configure in out tests below, but can only be automatically
283 # selected by '--enable-dependency-tracking', we make this threefold check
284 # only in this later case.
285
286 if test $depmode,$depcomp_with_libtool = auto,yes; then
287   do_all_tests ()
288   {
289     do_test default
290     do_test noshared --disable-shared
291     do_test nostatic --disable-static
292   }
293 else
294   do_all_tests () { do_test; }
295 fi
296
297 case $depmode in
298   auto)
299     displayed_depmode='..*' # At least one character long.
300     cfg_deptrack=--enable-dependency-tracking ;;
301   disabled)
302     displayed_depmode=none
303     cfg_deptrack=--disable-dependency-tracking ;;
304   *)
305     displayed_depmode="(cached) $depmode"
306     cfg_deptrack="$cachevar=$depmode"
307     # Sanity check: ensure the cache variable we force is truly
308     # used by configure.
309     $FGREP $cachevar configure \
310       || fatal_ "configure lacks required cache variable '$cachevar'";;
311 esac
312
313 cd_top
314
315 do_test ()
316 {
317   cd_top
318   if test $vpath = no; then
319     pfx="in-tree build"
320   else
321     pfx="$vpath VPATH"
322   fi
323   if test $# -gt 0; then
324     subdir=$1; shift
325     pfx="$pfx, $subdir"
326     test -d $subdir || mkdir $subdir || fatal_ "creating directory $subdir"
327     cd $subdir
328   fi
329   pfx="[$pfx]"
330   case $vpath in
331     simple)
332       mkdir -p vpath-simple/build
333       cd vpath-simple/build
334       setup_srcdir ..
335       ;;
336     long)
337       mkdir -p vpath-long/src vpath-long/wrk
338       cd vpath-long/wrk
339       setup_srcdir ../src/$longpath
340       ;;
341     absolute)
342       mkdir -p vpath-abs/build
343       cd vpath-abs/build
344       absdir=$(cd .. && pwd) || fatal_ "getting absolute directory"
345       setup_srcdir "$absdir/vpath-abs"
346       unset absdir
347       ;;
348     no)
349       mkdir intree
350       cd intree
351       setup_srcdir .
352       ;;
353     *)
354       fatal_ "invalid value '$vpath' for variable \$vpath"
355       ;;
356   esac
357
358   command_ok_ \
359     "$pfx configure" \
360     "$srcdir/configure" $cfg_deptrack ${1+"$@"} >stdout
361   cat stdout
362
363   command_ok_ \
364     "$pfx right depmode selected" \
365     grep "^checking dependency style .*\.\.\. $displayed_depmode$" stdout
366   rm -f stdout
367
368   command_ok_ "$pfx simple make" make_ok
369   # Some bugs in VPATH builds only kick in during a rebuild.
370   command_ok_ "$pfx clean & rebuild" eval '$MAKE clean && make_ok'
371
372   if test $depmode = disabled; then
373       command_ok_ "$pfx no dependency files generated" check_no_depfiles
374       r=ok \
375         && grep "[ $tab]depmode=none" Makefile \
376         && rewrite "$srcdir"/src/sub2/sub2foo.h echo 'choke me' \
377         && delete "$srcdir"/sub/subfoo.h \
378         && make_ok \
379         || r='not ok'
380       result_ "$r" "$pfx dependency tracking is truly disabled"
381   elif grep "[ $tab]depmode=none" Makefile; then
382     skip_row_ 2 -r "automatic dependency tracking couldn't be activated"
383   else
384     command_ok_ "$pfx generated $po files look correct" $MAKE grep-test
385     r=ok \
386       && : "Some checks in the subdir." \
387       && $sleep \
388       && : "Ensure rebuild rules really kick in." \
389       && rewrite "$srcdir"/src/sub2/sub2foo.h echo 'choke me' \
390       && cd src \
391       && not $MAKE  \
392       && cd .. \
393       && : "Ensure the deleted header bug is fixed." \
394       && delete "$srcdir"/src/sub2/sub2foo.h \
395       && edit "$srcdir"/src/sub2/sub2foo.c -e 1d \
396       && cd src \
397       && make_ok \
398       && : "Now do similar checks for the parent directory." \
399       && cd .. \
400       && rewrite "$srcdir"/sub/subfoo.h echo 'choke me' \
401       && not $MAKE \
402       && delete "$srcdir"/sub/subfoo.h \
403       && edit "$srcdir"/sub/subfoo.c -e 1d \
404       && edit "$srcdir"/foo.h -e 2d \
405       && make_ok \
406       || r='not ok'
407     result_ "$r" "$pfx dependency tracking works"
408   fi
409
410   command_ok_ "$pfx make distclean" check_distclean
411   cd_top
412 }
413
414 for vpath in no simple long absolute; do
415   do_all_tests
416 done
417
418 :