test: protect more yacc declarations against C vs. C++ linkage.
[automake.git] / t / objc-megademo.sh
blob3a7c12835b980365bfa4beca75ee5569c3d74608
1 #! /bin/sh
2 # Copyright (C) 2012-2024 Free Software Foundation, Inc.
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.
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.
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/>.
17 # Stress test on Objective C/C++.
19 required=libtoolize
20 am_create_testdir=empty
21 . test-init.sh
23 ## Autotools Input Files.
25 cat > configure.ac << 'END'
26 AC_INIT([play], [1.3], [bug-automake@gnu.org])
28 AC_CONFIG_SRCDIR([play.c])
29 AC_CONFIG_AUX_DIR([build-aux])
30 AC_CONFIG_MACRO_DIR([m4])
32 AM_INIT_AUTOMAKE
34 AM_PROG_AR
35 LT_INIT
37 AC_PROG_CC
38 AC_PROG_CXX
39 AC_PROG_OBJC
40 AC_PROG_OBJCXX
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])
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++])
62 if test $my_cv_objc_works != yes; then
63 AC_MSG_ERROR([couldn't find a working Objective C compiler], [77])
66 if test $my_cv_objcxx_works != yes; then
67 AC_MSG_ERROR([couldn't find a working Objective C++ compiler], [77])
70 AC_CONFIG_HEADERS([config.h])
71 AC_CONFIG_FILES([Makefile])
73 AC_OUTPUT
74 END
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
85 ## Run Autotools.
87 libtoolize
88 $ACLOCAL
89 $AUTOHEADER
90 $AUTOCONF
91 $AUTOMAKE --add-missing
93 ## Program Sources.
95 cat > play.h << 'END'
96 #ifndef PLAY_H
97 #define PLAY_H
99 #include "foo.h"
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
105 void hello_cxx (void);
106 void hello_objc (void);
107 void hello_objcxx (void);
109 #ifdef __OBJC__
110 @interface Hello_ObjC
112 + (void)display;
113 @end
114 #endif /* __OBJC__ */
116 #ifdef __cplusplus
119 class Hello_CXX
121 public:
122 Hello_CXX() { }
123 virtual ~Hello_CXX () { }
124 void hello_cxx ();
127 #ifdef __OBJC__
128 @interface Hello_ObjCXX
130 + (void)display;
131 @end
133 class Hello_OBJCXX
135 public:
136 Hello_OBJCXX () { }
137 virtual ~Hello_OBJCXX () { }
138 void hello_objcxx();
140 #endif /* __OBJC__ */
142 #endif /* __cplusplus */
144 #endif /* PLAY_H */
147 cat > play.c << 'END'
148 #include "play.h"
149 int main (void)
151 printf ("[Hello C,");
152 world_c ();
153 hello_cxx ();
154 hello_objc ();
155 hello_objcxx ();
156 return 0;
160 cat > playxx.cxx << 'END'
161 #include "play.h"
163 void hello_cxx(void)
165 Hello_CXX *hello = new Hello_CXX;
166 hello->hello_cxx();
169 void Hello_CXX::hello_cxx()
171 std::cout << "[Hello C++,";
172 World_CXX *world = new World_CXX;
173 world->world_cxx();
177 cat > playo.m << 'END'
178 #import "play.h"
180 void hello_objc (void)
182 [Hello_ObjC display];
185 @implementation Hello_ObjC
186 + (void)display
188 printf ("[Hello ObjC,");
189 [World_ObjC display];
191 @end
194 cat > playoxx.mm << 'END'
195 #import "play.h"
197 // Calling: C -> C++ -> ObjC
199 void hello_objcxx (void)
201 Hello_OBJCXX *hello = new Hello_OBJCXX;
202 hello->hello_objcxx ();
205 void Hello_OBJCXX::hello_objcxx ()
207 [Hello_ObjCXX display];
210 @implementation Hello_ObjCXX
211 + (void)display
213 std::cout << "[Hello ObjC++,";
214 [World_ObjCXX display];
216 @end
219 ## Library Sources.
221 cat > foo.h << 'END'
222 #ifndef FOO_H
223 #define FOO_H
225 #ifdef __cplusplus
226 #include <iostream>
227 extern "C" {
228 #else
229 #include <stdio.h>
230 #endif
232 void world_c (void);
234 #ifdef __OBJC__
235 @interface World_ObjC
237 + (void)display;
238 @end
239 #endif /* __OBJC__ */
241 #ifdef __cplusplus
244 class World_CXX
246 public:
247 World_CXX() { }
248 virtual ~World_CXX () { }
249 void world_cxx ();
252 #ifdef __OBJC__
253 class World_OBJCXX
255 public:
256 World_OBJCXX () { }
257 virtual ~World_OBJCXX () { }
258 void world_objcxx ();
261 @interface World_ObjCXX
263 + (void)display;
264 @end
265 #endif /* __OBJC__ */
267 #endif /* __cplusplus */
269 #endif /* FOO_H */
272 cat > foo.c << 'END'
273 #include "foo.h"
275 void world_c (void)
277 printf (" world C]\n");
281 cat > fooxx.cxx << 'END'
282 #include "foo.h"
284 void World_CXX::world_cxx ()
286 std::cout << " world C++]" << "\n";
290 cat > fooo.m << 'END'
291 #import "foo.h"
293 @implementation World_ObjC
294 + (void)display
296 printf (" world ObjC]\n");
298 @end
301 cat > foooxx.mm << 'END'
302 #import "foo.h"
304 // Calling: ObjC -> C++
306 @implementation World_ObjCXX
307 + (void)display
309 World_OBJCXX *world = new World_OBJCXX;
310 world->world_objcxx ();
312 @end
314 void World_OBJCXX::world_objcxx ()
316 std::cout << " world ObjC++]" << "\n";
320 ## Configure and build.
322 ./configure
323 $MAKE
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++]
332 ./play > got || { cat got; exit 1; }
333 cat exp
334 cat got
335 diff exp got
338 $MAKE distcheck