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