am-ft: make the environment available earlier
[platform/upstream/automake.git] / t / objc-megademo.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 # Stress test on Objective C/C++.
18
19 required=libtoolize
20 am_create_testdir=empty
21 . test-init.sh
22
23 ## Autotools Input Files.
24
25 cat > configure.ac << 'END'
26 AC_INIT([play], [1.3], [bug-automake@gnu.org])
27
28 AC_CONFIG_SRCDIR([play.c])
29 AC_CONFIG_AUX_DIR([build-aux])
30 AC_CONFIG_MACRO_DIR([m4])
31
32 AM_INIT_AUTOMAKE
33
34 AM_PROG_AR
35 LT_INIT
36
37 AC_PROG_CC
38 AC_PROG_CXX
39 AC_PROG_OBJC
40 AC_PROG_OBJCXX
41
42 AC_LANG_PUSH([Objective C])
43 AC_CACHE_CHECK(
44   [whether the Objective C compiler really works],
45   [my_cv_objc_works],
46   [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#import <stdio.h>]],
47                                    [[printf ("foo\n");]])],
48                   [my_cv_objc_works=yes],
49                   [my_cv_objc_works=no])])
50 AC_LANG_POP([Objective C])
51
52 AC_LANG_PUSH([Objective C++])
53 AC_CACHE_CHECK(
54   [whether the Objective C++ compiler really works],
55   [my_cv_objcxx_works],
56   [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#import <iostream>]],
57                                    [[std::cout << "foo" << "\n";]])],
58                   [my_cv_objcxx_works=yes],
59                   [my_cv_objcxx_works=no])])
60 AC_LANG_POP([Objective C++])
61
62 if test $my_cv_objc_works != yes; then
63   AC_MSG_ERROR([couldn't find a working Objective C compiler], [77])
64 fi
65
66 if test $my_cv_objcxx_works != yes; then
67   AC_MSG_ERROR([couldn't find a working Objective C++ compiler], [77])
68 fi
69
70 AC_CONFIG_HEADERS([config.h])
71 AC_CONFIG_FILES([Makefile])
72
73 AC_OUTPUT
74 END
75
76 cat > Makefile.am << 'END'
77 bin_PROGRAMS = play
78 play_SOURCES = play.h play.c playxx.cxx playo.m playoxx.mm
79 play_LDADD = libfoo.la
80 play_LDFLAGS = -lobjc
81 lib_LTLIBRARIES = libfoo.la
82 libfoo_la_SOURCES = foo.h foo.c fooxx.cxx fooo.m foooxx.mm
83 END
84
85 ## Run Autotools.
86
87 libtoolize
88 $ACLOCAL
89 $AUTOHEADER
90 $AUTOCONF
91 $AUTOMAKE --add-missing
92
93 ## Program Sources.
94
95 cat > play.h << 'END'
96 #ifndef PLAY_H
97 #define PLAY_H
98
99 #include "foo.h"
100
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104
105 void hello_cxx (void);
106 void hello_objc (void);
107 void hello_objcxx (void);
108
109 #ifdef __OBJC__
110 @interface Hello_ObjC
111 { }
112 + (void)display;
113 @end
114 #endif /* __OBJC__ */
115
116 #ifdef __cplusplus
117 }
118
119 class Hello_CXX
120 {
121   public:
122     Hello_CXX() { }
123     virtual ~Hello_CXX () { }
124     void hello_cxx ();
125 };
126
127 #ifdef __OBJC__
128 @interface Hello_ObjCXX
129 { }
130 + (void)display;
131 @end
132
133 class Hello_OBJCXX
134 {
135   public:
136     Hello_OBJCXX () { }
137     virtual ~Hello_OBJCXX () { }
138     void hello_objcxx();
139 };
140 #endif /* __OBJC__ */
141
142 #endif /* __cplusplus */
143
144 #endif /* PLAY_H */
145 END
146
147 cat > play.c << 'END'
148 #include "play.h"
149 int main (void)
150 {
151   printf ("[Hello C,");
152   world_c ();
153   hello_cxx ();
154   hello_objc ();
155   hello_objcxx ();
156   return 0;
157 }
158 END
159
160 cat > playxx.cxx << 'END'
161 #include "play.h"
162
163 void hello_cxx(void)
164 {
165   Hello_CXX *hello = new Hello_CXX;
166   hello->hello_cxx();
167 }
168
169 void Hello_CXX::hello_cxx()
170 {
171   std::cout << "[Hello C++,";
172   World_CXX *world = new World_CXX;
173   world->world_cxx();
174 }
175 END
176
177 cat > playo.m << 'END'
178 #import "play.h"
179
180 void hello_objc (void)
181 {
182   [Hello_ObjC display];
183 }
184
185 @implementation Hello_ObjC
186 + (void)display
187 {
188   printf ("[Hello ObjC,");
189   [World_ObjC display];
190 }
191 @end
192 END
193
194 cat > playoxx.mm << 'END'
195 #import "play.h"
196
197 // Calling: C -> C++ -> ObjC
198
199 void hello_objcxx (void)
200 {
201   Hello_OBJCXX *hello = new Hello_OBJCXX;
202   hello->hello_objcxx ();
203 }
204
205 void Hello_OBJCXX::hello_objcxx ()
206 {
207   [Hello_ObjCXX display];
208 }
209
210 @implementation Hello_ObjCXX
211 + (void)display
212 {
213   std::cout << "[Hello ObjC++,";
214   [World_ObjCXX display];
215 }
216 @end
217 END
218
219 ## Library Sources.
220
221 cat > foo.h << 'END'
222 #ifndef FOO_H
223 #define FOO_H
224
225 #ifdef __cplusplus
226 #include <iostream>
227 extern "C" {
228 #else
229 #include <stdio.h>
230 #endif
231
232 void world_c (void);
233
234 #ifdef __OBJC__
235 @interface World_ObjC
236 { }
237 + (void)display;
238 @end
239 #endif /* __OBJC__ */
240
241 #ifdef __cplusplus
242 }
243
244 class World_CXX
245 {
246   public:
247     World_CXX() { }
248     virtual ~World_CXX () { }
249     void world_cxx ();
250 };
251
252 #ifdef __OBJC__
253 class World_OBJCXX
254 {
255   public:
256     World_OBJCXX () { }
257     virtual ~World_OBJCXX () { }
258     void world_objcxx ();
259 };
260
261 @interface World_ObjCXX
262 { }
263 + (void)display;
264 @end
265 #endif /* __OBJC__ */
266
267 #endif /* __cplusplus */
268
269 #endif /* FOO_H */
270 END
271
272 cat > foo.c << 'END'
273 #include "foo.h"
274
275 void world_c (void)
276 {
277   printf (" world C]\n");
278 }
279 END
280
281 cat > fooxx.cxx << 'END'
282 #include "foo.h"
283
284 void World_CXX::world_cxx ()
285 {
286   std::cout << " world C++]" << "\n";
287 }
288 END
289
290 cat > fooo.m << 'END'
291 #import "foo.h"
292
293 @implementation World_ObjC
294 + (void)display
295 {
296   printf (" world ObjC]\n");
297 }
298 @end
299 END
300
301 cat > foooxx.mm << 'END'
302 #import "foo.h"
303
304 // Calling: ObjC -> C++
305
306 @implementation World_ObjCXX
307 + (void)display
308 {
309   World_OBJCXX *world = new World_OBJCXX;
310   world->world_objcxx ();
311 }
312 @end
313
314 void World_OBJCXX::world_objcxx ()
315 {
316   std::cout << " world ObjC++]" << "\n";
317 }
318 END
319
320 ## Configure and build.
321
322 ./configure
323 $MAKE
324
325 if ! cross_compiling; then
326   unindent > exp << 'END'
327     [Hello C, world C]
328     [Hello C++, world C++]
329     [Hello ObjC, world ObjC]
330     [Hello ObjC++, world ObjC++]
331 END
332   ./play > got || { cat got; exit 1; }
333   cat exp
334   cat got
335   diff exp got
336 fi
337
338 $MAKE distcheck
339
340 :