am-ft: make the environment available earlier
[platform/upstream/automake.git] / t / cxx-demo.sh
1 #! /bin/sh
2 # Copyright (C) 2012-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 on C++ support.
18
19 required=c++
20 am_create_testdir=empty
21 . test-init.sh
22
23 cat > configure.ac << 'END'
24 AC_INIT([GNU C++ Demo], [1.3], [bug-automake@gnu.org])
25 AC_CONFIG_SRCDIR([play.c++])
26 AC_CONFIG_AUX_DIR([build-aux])
27 AM_INIT_AUTOMAKE
28 # The C compiler shouldn't be required in any way.
29 CC=false; AC_SUBST([CC])
30 AC_PROG_CXX
31 AH_BOTTOM([
32 #ifndef GREETINGS
33 #  define GREETINGS "Howdy"
34 #endif])
35 AC_DEFINE([OK_AC], [1],
36           [Give "good to go" declaration from configure.ac])
37 AC_CONFIG_HEADERS([config.hxx])
38 AC_CONFIG_FILES([Makefile])
39 AC_OUTPUT
40 END
41
42 cat > Makefile.am << 'END'
43 AUTOMAKE_OPTIONS = subdir-objects
44
45 bin_PROGRAMS = work play
46 common_sources = common.hpp foo.cpp sub/bar.cc
47 AM_CPPFLAGS = -DOK_AM=1
48 play_SOURCES = play.c++ play.hh $(common_sources)
49 work_SOURCES = work.cxx work.h++ $(common_sources)
50 work_CXXFLAGS = -D'GREETINGS="Good morning"'
51
52 .PHONY: test-objs
53 check-local: test-objs
54 test-objs:
55         test -f play.$(OBJEXT)
56         test -f foo.$(OBJEXT)
57         test -f sub/bar.$(OBJEXT)
58         test -f work-foo.$(OBJEXT)
59         test -f sub/work-bar.$(OBJEXT)
60         test -f work-work.$(OBJEXT)
61 END
62
63 mkdir sub build-aux
64
65 $ACLOCAL
66 $AUTOHEADER
67 test -f config.hxx.in
68 $AUTOCONF
69 $AUTOMAKE --add-missing
70 test -f build-aux/depcomp
71 # Not needed by C++ compilers.
72 test ! -e build-aux/compile
73
74 cat > work.h++ << 'END'
75 #define ACTION "work"
76 class Hello_CXX
77 {
78   public:
79     Hello_CXX() { }
80     virtual ~Hello_CXX () { }
81     void hello_cxx_class ();
82 };
83 END
84
85 cat > play.hh << 'END'
86 #define ACTION "play"
87 void hello_cxx_function (void);
88 END
89
90 cat > common.hpp << 'END'
91 /* Common header. */
92
93 #include <config.hxx>
94
95 #if !OK_AM
96 #error "missing OK from Makefile.am"
97 choke me
98 #endif
99
100 #if !OK_AC
101 #error "missing OK from configure.ac"
102 choke me
103 #endif
104
105 #include <iostream>
106 END
107
108 cat > work.cxx << 'END'
109 #include "common.hpp"
110 #include "work.h++"
111 #include <cstdlib>
112 using namespace std;
113 int main (void)
114 {
115   cout << "We are working :-(" << endl;
116   Hello_CXX *hello = new Hello_CXX;
117   hello->hello_cxx_class ();
118   return EXIT_SUCCESS;
119 }
120 END
121
122 cat > play.c++ << 'END'
123 #include "common.hpp"
124 #include "play.hh"
125 int main (void)
126 {
127   std::cout << "We are playing :-)" << std::endl;
128   hello_cxx_function ();
129   return 0;
130 }
131 END
132
133 cat > foo.cpp <<'END'
134 #include <config.hxx>
135 #include "work.h++"
136 #include <iostream>
137 using namespace std;
138 void Hello_CXX::hello_cxx_class (void)
139 {
140   cout << GREETINGS << ", " << ACTION << "." << endl;
141 }
142 END
143
144 cat > sub/bar.cc << 'END'
145 #include <config.hxx>
146 #include "play.hh"
147 #include <stdio.h>
148 void hello_cxx_function (void)
149 {
150   printf ("%s, %s!\n", GREETINGS, ACTION);
151 }
152 END
153
154 ./configure
155 $MAKE
156 $MAKE test-objs
157
158 if ! cross_compiling; then
159   unindent > exp.play << 'END'
160     We are playing :-)
161     Howdy, play!
162 END
163   unindent > exp.work << 'END'
164     We are working :-(
165     Good morning, work.
166 END
167   for p in play work; do
168     # The program must run correctly (exit status = 0).
169     ./$p
170     # And it must have the expected output.  Note that we strip extra
171     # CR characters (if any), to cater to MinGW programs on MSYS.
172     # See automake bug#14493.
173     ./$p | tr -d '\015' > got.$p || { cat got.$p; exit 1; }
174     cat exp.$p
175     cat got.$p
176     diff exp.$p got.$p
177   done
178 fi
179
180 $MAKE distcheck
181
182 :