objc-megademo.sh 5.5 KB

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