c-demo.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. # Demo on C support, also testing automatic dependency tracking,
  17. # conditional SUBDIRS and convenience libraries.
  18. required=cc
  19. am_create_testdir=empty
  20. . test-init.sh
  21. cat > configure.ac << 'END'
  22. AC_INIT([GNU C Demo], [22.3.2], [bug-automake@gnu.org])
  23. AC_CONFIG_SRCDIR([tests/test.test])
  24. AC_CONFIG_AUX_DIR([build-aux])
  25. AM_INIT_AUTOMAKE
  26. AC_PROG_CC
  27. AM_PROG_AR
  28. AC_PROG_RANLIB
  29. AM_CONDITIONAL([RUN_TESTS], [test x"$run_tests" != x"no"])
  30. AC_CONFIG_FILES([Makefile src/Makefile lib/Makefile tests/Makefile])
  31. AC_OUTPUT
  32. END
  33. if cross_compiling; then
  34. run_tests=no
  35. else
  36. run_tests=yes
  37. fi
  38. export run_tests
  39. mkdir build-aux lib src tests
  40. cat > Makefile.am <<'END'
  41. SUBDIRS = lib src
  42. if RUN_TESTS
  43. SUBDIRS += tests
  44. endif
  45. .PHONY: test-objs
  46. check-local: test-objs
  47. test-objs:
  48. test -f src/zardoz-main.$(OBJEXT)
  49. test -f lib/foo.$(OBJEXT)
  50. test -f lib/bar.$(OBJEXT)
  51. END
  52. cat > src/Makefile.am << 'END'
  53. bin_PROGRAMS = zardoz
  54. zardoz_SOURCES = main.c
  55. zardoz_LDADD = $(top_builddir)/lib/lib-convenience.a
  56. zardoz_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
  57. END
  58. cat > lib/Makefile.am << 'END'
  59. noinst_LIBRARIES = lib-convenience.a
  60. lib_convenience_a_SOURCES = foo.c
  61. lib_convenience_a_SOURCES += bar.c
  62. dist_lib_convenience_a_SOURCES = bar.h
  63. nodist_lib_convenience_a_SOURCES = foo.h
  64. # We want this to be auto-generated an removed by "make clean", to
  65. # ensure that cleaning rules work correctly; an older implementation
  66. # of automatic dependency tracking support suffered of weaknesses in
  67. # this situation, see the "historical comments" reported in:
  68. # http://lists.gnu.org/archive/html/automake-patches/2012-06/msg00033.html
  69. foo.h: $(srcdir)/foo.c
  70. sed -n 's/.*foo *(.*/&;/p' "$(srcdir)/foo.c" >$@-t
  71. test 1 -eq `wc -l <$@-t`
  72. chmod a-w $@-t && mv -f $@-t $@
  73. BUILT_SOURCES = foo.h
  74. CLEANFILES = $(BUILT_SOURCES)
  75. check-local:
  76. test -f ${top_srcdir}/tests/test.test
  77. END
  78. cat > tests/Makefile.am << 'END'
  79. AUTOMAKE_OPTIONS = parallel-tests
  80. TEST_LOG_COMPILER = $(SHELL)
  81. TESTS = test.test
  82. EXTRA_DIST = $(TESTS)
  83. END
  84. cat > tests/test.test << 'END'
  85. #!/bin/sh
  86. set -x; set -e;
  87. ../src/zardoz
  88. test "`../src/zardoz`" = 'Foo, Bar!'
  89. END
  90. $ACLOCAL
  91. $AUTOCONF
  92. $AUTOMAKE --add-missing
  93. test -f build-aux/depcomp
  94. test -f build-aux/compile # We have per-target flags on C sources.
  95. # Don't reject slow dependency extractors.
  96. ./configure --enable-dependency-tracking
  97. cat > src/main.c << 'END'
  98. #include "foo.h"
  99. #include "bar.h"
  100. int main (void)
  101. {
  102. printf ("%s, %s!\n", foo (), bar ());
  103. return 0;
  104. }
  105. END
  106. cat > lib/foo.c << 'END'
  107. #include "foo.h"
  108. static char s[4];
  109. volatile char *foo (void)
  110. {
  111. s[0] = 'F';
  112. s[1] = s[2] = 'o';
  113. s[3] = '\0';
  114. return s;
  115. }
  116. END
  117. cat > lib/bar.c << 'END'
  118. #include "bar.h"
  119. const char *bar (void)
  120. {
  121. return BARBAR;
  122. }
  123. END
  124. cat > lib/bar.h << 'END'
  125. #define BARBAR "Bar"
  126. const char *bar (void);
  127. END
  128. $MAKE
  129. ls -l . src lib # For debugging.
  130. $MAKE test-objs
  131. VERBOSE=x $MAKE check
  132. if cross_compiling; then
  133. test ! -e tests/test-suite.log
  134. test ! -e tests/test.log
  135. else
  136. test -f tests/test-suite.log
  137. grep 'Foo, Bar!' tests/test.log
  138. fi
  139. $MAKE distcheck
  140. if ! cross_compiling && ! grep "[ $tab]depmode=none" Makefile; then
  141. # Let's check automatic dependency tracking.
  142. sed 's/^\(#define BARBAR \).*/\1 "Zap"/' lib/bar.h > t
  143. mv -f t lib/bar.h
  144. $MAKE
  145. ./src/zardoz
  146. test "$(./src/zardoz)" = 'Foo, Zap!'
  147. fi
  148. $MAKE clean
  149. test ! -e lib/foo.h
  150. test -f lib/bar.h
  151. :