cxx-demo.sh 3.8 KB

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