depcomp.sh 12 KB

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