yacc-mix-c-cxx.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #! /bin/sh
  2. # Copyright (C) 2011-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. # Check that many different Yacc parsers (both C and C++) can co-exists
  17. # in the same directory.
  18. required='cc c++ yacc'
  19. . test-init.sh
  20. cat >> configure.ac << 'END'
  21. AC_PROG_CC
  22. AC_PROG_CXX
  23. AC_PROG_YACC
  24. AC_OUTPUT
  25. END
  26. cat > Makefile.am << 'END'
  27. bin_PROGRAMS = c1 c2 cxx1 cxx2 cxx3
  28. AM_YFLAGS = -d
  29. c1_SOURCES = p.y p.h 1.c
  30. c2_SOURCES = p.y 2.c
  31. c2_YFLAGS =
  32. cxx1_SOURCES = parse.yy main1.cc parse.hh
  33. cxx2_SOURCES = parse2.y++ main2.c++
  34. cxx2_YFLAGS =
  35. cxx3_SOURCES = parse3.yxx main3.cxx
  36. BUILT_SOURCES = p.h parse.hh parse3.hxx
  37. END
  38. # The content of all the .c and .y files created below is valid C but
  39. # deliberately invalid C++.
  40. # Vice versa, the content of all the .c++, .cxx, .cc, .y++, .yxx and
  41. # .yy files created below is valid C++ but deliberately invalid C.
  42. cat > p.y <<'END'
  43. %{
  44. int yylex (void) { int new = 0; return new; }
  45. void yyerror (char *s) { return; }
  46. %}
  47. %token ZARDOZ
  48. %%
  49. x : 'x' {};
  50. %%
  51. END
  52. cat > 1.c <<'END'
  53. #include "p.h"
  54. int main ()
  55. {
  56. int new = ZARDOZ;
  57. return yyparse () + new;
  58. }
  59. END
  60. cat > 2.c <<'END'
  61. int main ()
  62. {
  63. int yyparse ();
  64. int new = 0;
  65. return yyparse () + new;
  66. }
  67. END
  68. cat > parse.yy <<'END'
  69. %{
  70. #include <cstdlib>
  71. #include "parse.hh"
  72. int yylex (void) { return 0; }
  73. void yyerror (const char *s) { return; }
  74. %}
  75. %token FOOBAR
  76. %%
  77. x : 'x' {};
  78. %%
  79. END
  80. cat > parse2.y++ <<'END'
  81. %{
  82. #include <cstdlib>
  83. int yylex (void) { return 0; }
  84. void yyerror (const char *s) { return; }
  85. %}
  86. %%
  87. x : 'x' {};
  88. %%
  89. END
  90. cat > main1.cc <<'END'
  91. using namespace std;
  92. #include "parse.hh"
  93. int main (int argc, char **argv)
  94. {
  95. int yyparse (void);
  96. return yyparse () + FOOBAR;
  97. }
  98. END
  99. cat > main2.c++ <<'END'
  100. using namespace std;
  101. int main (int argc, char **argv)
  102. {
  103. int yyparse (void);
  104. return yyparse ();
  105. }
  106. END
  107. edit () { sed -e 's/FOOBAR/BAZQUUX/' -e 's/"parse\.hh"/"parse3.hxx"/'; }
  108. edit <parse.yy >parse3.yxx
  109. edit <main1.cc >main3.cxx
  110. $ACLOCAL
  111. $AUTOCONF
  112. $AUTOMAKE -a
  113. # Try a VPATH and by default serial build first, and then an in-tree
  114. # and by default parallel build.
  115. for try in 0 1; do
  116. if test $try -eq 0; then
  117. # VPATH serial build.
  118. mkdir build
  119. cd build
  120. srcdir=..
  121. debug_info="ls -l . $srcdir"
  122. run_make=$MAKE
  123. elif test $try -eq 1; then
  124. # In-tree parallel build.
  125. srcdir=.
  126. debug_info="ls -l"
  127. case $MAKE in
  128. *\ -j*)
  129. # Degree of parallelism already specified by the user: do
  130. # not override it.
  131. run_make=$MAKE;;
  132. *)
  133. # Some make implementations (e.g., HP-UX) don't grok '-j',
  134. # some require no space between '-j' and the number of jobs
  135. # (e.g., older GNU make versions), and some *do* require a
  136. # space between '-j' and the number of jobs (e.g., Solaris
  137. # dmake). We need a runtime test to see what works.
  138. echo 'all:' > Makefile
  139. for run_make in "$MAKE -j3" "$MAKE -j 3" "$MAKE"; do
  140. $run_make && break
  141. done
  142. rm -f Makefile
  143. esac
  144. else
  145. echo "$me: invalid value of \$try '$try'" >&2
  146. exit 99
  147. fi
  148. $srcdir/configure
  149. $run_make
  150. $debug_info
  151. test -f p.c
  152. test -f p.h
  153. test -f c2-p.c
  154. test ! -e c2-p.h
  155. test -f parse.cc
  156. test -f parse.hh
  157. test -f parse3.cxx
  158. test -f parse3.hxx
  159. test -f cxx2-parse2.c++
  160. test ! -e parse2.h++
  161. test ! -e cxx2-parse2.h++
  162. # Minimal checks about recovering from header removal.
  163. rm -f p.h parse.hh parse3.hxx
  164. $run_make p.h parse.hh
  165. $debug_info
  166. test -f p.h
  167. test -f parse.hh
  168. test ! -e parse3.hxx
  169. $run_make
  170. $debug_info
  171. test -f parse3.hxx
  172. cd $srcdir
  173. done
  174. :