Merge branch 'maint'
[platform/upstream/automake.git] / t / lex-header.sh
1 #! /bin/sh
2 # Copyright (C) 2011-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 # Automake lex support can work with flex '--header-file' option (see
18 # bugs #8844 and #9933).
19
20 required='cc flex'
21 . test-init.sh
22
23 # Here, we need to use the use flex option '--header-file', but some
24 # older flex versions don't support is (see automake bug#11524 and
25 # bug#12836).  Skip this test if such an old flex version is detected.
26 $LEX --help | grep '.*--header-file' \
27   || skip_ "flex doesn't support the --header-file' option"
28
29 cat >> configure.ac << 'END'
30 AC_PROG_CC
31 AC_PROG_LEX
32 AC_OUTPUT
33 END
34
35 cat > Makefile.am << 'END'
36 bin_PROGRAMS = foo
37 foo_SOURCES = lexer.l main.c mylex.h
38 foo_LFLAGS = --header-file=mylex.h
39 BUILT_SOURCES = mylex.h
40 # Recover from removal of header.
41 mylex.h: foo-lexer.c
42         test -f $@ || rm -f foo-lexer.c
43         test -f $@ || $(MAKE) $(AM_MAKEFLAGS) foo-lexer.c
44 END
45
46 cat > lexer.l << 'END'
47 %option noyywrap
48 %{
49 #define YY_NO_UNISTD_H 1
50 %}
51 %%
52 "GOOD"   return EOF;
53 .
54 %%
55 END
56
57 cat > main.c <<'END'
58 #include "mylex.h"
59 int main (void)
60 {
61   /* We don't use a 'while' loop here (like a real lexer would do)
62      to avoid possible hangs. */
63   if (yylex () == EOF)
64     return 0;
65   else
66     return 1;
67 }
68 END
69
70 $ACLOCAL
71 $AUTOCONF
72 $AUTOMAKE -a
73
74 ./configure
75
76 # Program should build and run.
77 $MAKE
78 if ! cross_compiling; then
79   echo GOOD | ./foo
80   echo BAD | ./foo && exit 1
81   : For shells with busted 'set -e'.
82 fi
83
84 # Recovering from header removal.
85 rm -f mylex.h
86 $MAKE
87 test -f mylex.h
88
89 # Sanity check on distribution.
90 yl_distcheck
91
92 :