preproc-demo.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #! /bin/sh
  2. # Copyright (C) 2013-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 of a package using pre-processing substitutions '%reldir%' and
  17. # '%canon_reldir%', and their respective shorthands '%D%' and '%C%'.
  18. am_create_testdir=empty
  19. required=cc
  20. . test-init.sh
  21. if cross_compiling; then
  22. WE_ARE_CROSS_COMPILING=yes
  23. else
  24. WE_ARE_CROSS_COMPILING=no
  25. fi
  26. export WE_ARE_CROSS_COMPILING
  27. SAFE_PRINT_FAIL=; unset SAFE_PRINT_FAIL
  28. cat > configure.ac << 'END'
  29. AC_INIT([GNU Demo], [0.7], [bug-automake@gnu.org])
  30. AC_CONFIG_AUX_DIR([build-aux])
  31. AM_INIT_AUTOMAKE([1.12.6 foreign subdir-objects -Wall])
  32. AM_CONDITIONAL([NATIVE_BUILD], [test $WE_ARE_CROSS_COMPILING != yes])
  33. AC_CONFIG_FILES([Makefile])
  34. AC_PROG_CC
  35. AM_PROG_CC_C_O
  36. AM_PROG_AR
  37. AC_PROG_RANLIB
  38. AC_OUTPUT
  39. END
  40. mkdir build-aux lib lib/tests src tests
  41. ## Top level.
  42. cat > Makefile.am << 'END'
  43. bin_PROGRAMS =
  44. check_PROGRAMS =
  45. noinst_LIBRARIES =
  46. AM_CPPFLAGS =
  47. AM_TESTS_ENVIRONMENT =
  48. CLEANFILES =
  49. EXTRA_DIST =
  50. LDADD =
  51. TESTS =
  52. include $(srcdir)/src/progs.am
  53. include $(srcdir)/lib/gnulib.am
  54. include $(srcdir)/tests/check.am
  55. END
  56. ## Src subdir.
  57. cat > src/progs.am <<'END'
  58. bin_PROGRAMS += %reldir%/hello
  59. bin_PROGRAMS += %D%/goodbye
  60. %canon_reldir%_goodbye_SOURCES = %D%/hello.c
  61. %C%_goodbye_CPPFLAGS = $(AM_CPPFLAGS) -DGREETINGS='"Goodbye"'
  62. # The testsuite should have access to our built programs.
  63. AM_TESTS_ENVIRONMENT += \
  64. PROGDIR='$(top_builddir)/%reldir%'; \
  65. export PROGDIR; \
  66. PATH='$(abs_builddir)/%reldir%'$(PATH_SEPARATOR)$$PATH; \
  67. export PATH;
  68. END
  69. cat > src/hello.c <<'END'
  70. #include "safe-print.h"
  71. #include <stdlib.h>
  72. #include <stdio.h>
  73. #ifndef GREETINGS
  74. # define GREETINGS "Hello"
  75. #endif
  76. int
  77. main (void)
  78. {
  79. safe_print (stdout, GREETINGS ", World!\n");
  80. exit (EXIT_SUCCESS);
  81. }
  82. END
  83. ## Lib subdir.
  84. cat > lib/gnulib.am << 'END'
  85. noinst_LIBRARIES += %D%/libgnu.a
  86. AM_CPPFLAGS += -I%D% -I$(top_srcdir)/%D%
  87. LDADD += $(noinst_LIBRARIES)
  88. %C%_libgnu_a_SOURCES = \
  89. %D%/safe-print.c \
  90. %D%/safe-print.h
  91. if NATIVE_BUILD
  92. include %D%/tests/gnulib-check.am
  93. endif
  94. END
  95. cat > lib/safe-print.c <<'END'
  96. #include "safe-print.h"
  97. #include <string.h>
  98. #include <stdlib.h>
  99. void
  100. safe_print (FILE *fp, const char * str)
  101. {
  102. if (fprintf (fp, "%s", str) != strlen (str)
  103. || fflush (fp) != 0 || ferror (fp))
  104. {
  105. fprintf (stderr, "I/O error\n");
  106. exit (EXIT_FAILURE);
  107. }
  108. }
  109. END
  110. cat > lib/safe-print.h <<'END'
  111. #include <stdio.h>
  112. void safe_print (FILE *, const char *);
  113. END
  114. ## Lib/Tests (sub)subdir.
  115. cat > lib/tests/gnulib-check.am <<'END'
  116. check_PROGRAMS += %D%/safe-print-test
  117. TESTS += $(check_PROGRAMS)
  118. END
  119. cat > lib/tests/safe-print-test.c <<'END'
  120. #include "safe-print.h"
  121. int
  122. main (void)
  123. {
  124. safe_print (stdout, "dummy\n");
  125. return 0;
  126. }
  127. END
  128. ## Tests subdir.
  129. cat > tests/check.am <<'END'
  130. TEST_EXTENSIONS = .sh
  131. SH_LOG_COMPILER = $(SHELL)
  132. AM_TESTS_ENVIRONMENT += EXEEXT='$(EXEEXT)'; export EXEEXT;
  133. handwritten_TESTS = \
  134. %D%/hello.sh \
  135. %D%/built.sh
  136. TESTS += $(handwritten_TESTS)
  137. EXTRA_DIST += $(handwritten_TESTS)
  138. TESTS += %D%/goodbye.sh
  139. CLEANFILES += %D%/goodbye.sh
  140. %D%/goodbye.sh: %D%/hello.sh
  141. $(MKDIR_P) %D%
  142. rm -f $@ $@-t
  143. sed -e 's/hello/goodbye/' \
  144. -e 's/Hello/Goodbye/' \
  145. < $(srcdir)/%D%/hello.sh >$@-t
  146. chmod a-w,a+x $@-t && mv -f $@-t $@
  147. END
  148. cat > tests/hello.sh <<'END'
  149. #!/bin/sh
  150. set -x -e
  151. if test "$WE_ARE_CROSS_COMPILING" = yes; then
  152. echo Skipping: cannot run in cross-compilation mode
  153. exit 77
  154. else
  155. hello || exit 1
  156. test "`hello`" = 'Hello, World!' || exit 1
  157. fi
  158. END
  159. cat > tests/built.sh <<'END'
  160. #!/bin/sh
  161. set -x
  162. test -n "$PROGDIR" || exit 99
  163. test -f "$PROGDIR/hello$EXEEXT" || exit 1
  164. test -x "$PROGDIR/hello$EXEEXT" || exit 1
  165. test -f "$PROGDIR/goodbye$EXEEXT" || exit 1
  166. test -x "$PROGDIR/goodbye$EXEEXT" || exit 1
  167. END
  168. ## Go.
  169. $ACLOCAL
  170. $AUTOCONF
  171. $AUTOMAKE --add-missing --copy
  172. test ! -e compile
  173. test -f build-aux/compile
  174. ./configure
  175. $MAKE
  176. run_make -O check VERBOSE=x
  177. cat tests/built.log
  178. cat tests/hello.log
  179. cat tests/goodbye.log
  180. if cross_compiling; then
  181. test ! -e lib/tests/safe-print-test.log
  182. count_test_results total=3 pass=1 fail=0 xpass=0 xfail=0 skip=2 error=0
  183. else
  184. count_test_results total=4 pass=4 fail=0 xpass=0 xfail=0 skip=0 error=0
  185. fi
  186. $MAKE distcheck
  187. :