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