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