bison++.in 6.13 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
#! /bin/sh
2

Benoit Perrot's avatar
Benoit Perrot committed
3
4
5
# Any tool failure is a failure of the script.
set -e

Akim Demaille's avatar
Akim Demaille committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
stderr ()
{
  local i
  for i
  do
    echo "$i"
  done | sed >&2 -e "s/^/$me: /"
}

verbose ()
{
  if "$verbose"; then
    stderr "$@"
  fi
}

fatal ()
{
  stderr "$@"
  exit 1
}

usage ()
{
  cat <<EOF
usage: bison++ OPTIONS... -- INPUT OUTPUT BISON-OPTIONS
32

Akim Demaille's avatar
Akim Demaille committed
33
34
35
36
37
38
39
40
41
42
Options:
  -h, --help             display this message and exit successfully
  --location-dir=DIR     where to install position.hh and location.hh.
                         E.g., "\$(top_srcdir)/include/foo"
  --location-prefix=DIR  where #include must look for these headers.
                         E.g., "foo"
  --verbose              display diffs
EOF
  exit 0
}
Benoit Perrot's avatar
Benoit Perrot committed
43

Akim Demaille's avatar
Akim Demaille committed
44
45
46
47
48
49
50
51
52
53
54
## ---------------------- ##
## Command line parsing.  ##
## ---------------------- ##

get_options ()
{
  while test $# != 0
  do
    # Handle --option=value by splitting apart and putting back on argv.
    case $1 in
      (--*=*)
55
56
57
58
59
        opt=$(echo "$1" | sed -e 's/=.*//')
        val=$(echo "$1" | sed -e 's/[^=]*=//')
        shift
        set dummy "$opt" "$val" ${1+"$@"}; shift
        ;;
Akim Demaille's avatar
Akim Demaille committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    esac

    case $1 in
      (-h | --help   )    usage;;
      (--location-dir)    shift; location_dir=$1;;
      (--location-prefix) shift; location_prefix=$1/;;
      (--verbose)         verbose=:
                          move_if_change="$move_if_change --verbose";;
      (--) shift; break;;
      (*) fatal "invalid argument: $1";;
     esac
     shift
  done

  input=$1
  shift
  output=$1
  shift
  bisonflags="$@"
}

Akim Demaille's avatar
Akim Demaille committed
81
82
83
84
noext ()
{
  echo "$1" | sed -e 's/\.[^.]*$//'
}
Roland Levillain's avatar
Roland Levillain committed
85
86
87
88
89
90

# fix_bison_output FILE
# ---------------------
# Fixes in place.
fix_bison_output ()
{
Akim Demaille's avatar
Akim Demaille committed
91
  local file="$1"
Akim Demaille's avatar
Akim Demaille committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  perl -pi \
    -e "
BEGIN
{
  \$location_prefix = '$location_prefix';
  \$namespace = '$namespace';
  \$output_dir = '$output_dir';
  \$tmp = '$tmp';
}" \
 -e '
BEGIN
{
  ($TMP = uc $tmp) =~ s/[^A-Z0-9]+/_/g;
  ($OUTPUT_DIR = uc $output_dir) =~ s/[^A-Z0-9]+/_/g;
}

# We compiled in a tmp directory, remove references to it.
s{$tmp/}{$output_dir/}g;
s{$TMP}{$OUTPUT_DIR}g;
Akim Demaille's avatar
Akim Demaille committed
111
112
113
114

# In case position.hh and location.hh were moved.
s{include \"(position.hh|location.hh)\"}
 {include <$location_prefix\$1>}g
Akim Demaille's avatar
Akim Demaille committed
115
  if $location_prefix ne "";
Akim Demaille's avatar
Akim Demaille committed
116
117

# Kill trailing blanks.
Akim Demaille's avatar
Akim Demaille committed
118
119
s/[ \t]+$//;
'  "$file"
Akim Demaille's avatar
Akim Demaille committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

  # Fuse the switches of the parser.
  case $file in
    (*.cc) $fuse_switch $file
          # diff -uw $file.bak $file || true
          ;;
  esac
}

# compile_xml_file XML XSL OUT-EXT
# --------------------------------
# By transforming XML = FILE.xml with XSL.xsl, create FILE.OUT-EXT if needed.
# "Needed" means FILE.OUT-EXT does not exist, or XML was updated.
compile_xml_file ()
{
  local xml="$1"
  local xsl="$2.xsl"
  local out
  out=$(basename "$xml" ".xml").$3

  # "cmp NON-EXISTENT FILE" does what we want: fail.
  if ! cmp -s "$xml" "$(out "$xml")" \
      || test ! -f "$(out "$out")"; then
    local compile="xsltproc $($BISON --print-datadir)/xslt/$xsl"
    if ! $compile "$xml" >"$(tmp "$out")"; then
      stderr "cannot convert $xml report to $out"
      # Produce a stub for the HTML for sake of the Makefile.
      cat >"$(tmp "$out")" <<EOF
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
  <head>
    <title>Cannot run xsltproc</title>
  </head>
  <body>
    <p>Cannot run xsltproc</p>
  </body>
</html>
EOF
    fi
    install "$out"
  fi
}

# out FILE
# --------
# Return path name of FILE once installed in its destination directory.
out ()
{
  local f
  f=$(basename "$1")
  case $f in
    (position.hh|location.hh) echo "$location_dir/$f";;
    (*)                       echo "$output_dir/$f";;
  esac
}

Akim Demaille's avatar
Akim Demaille committed
179
# tmp FILE
Akim Demaille's avatar
Akim Demaille committed
180
181
182
183
184
185
# --------
# Return path name of FILE in the tmp directory.
tmp ()
{
  echo "$tmp/$(basename "$1")"
}
Roland Levillain's avatar
Roland Levillain committed
186

Akim Demaille's avatar
Akim Demaille committed
187
188
189
190
191
192
193
194
195
196
197
198
# install FILE
# ------------
# Install FILE in its destination directory.
install ()
{
  local dest
  dest=$(out "$1")
  local dir
  dir=$(dirname "$dest")
  mkdir -p "$dir" ||
    fatal "cannot create $dir"
  $move_if_change "$(tmp "$1")" "$dest"
Roland Levillain's avatar
Roland Levillain committed
199
200
}

Akim Demaille's avatar
Akim Demaille committed
201
202
203
204
205
206
207
## ------ ##
## Main.  ##
## ------ ##

# Exit status.
status=0

208
: ${BISON='@top_srcdir@/@BISON@'}
Akim Demaille's avatar
Akim Demaille committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

me=$(basename $0)
fuse_switch='@abs_srcdir@/fuse-switch'
move_if_change='@abs_srcdir@/move-if-change --color'
verbose=false

get_options "$@"

input_base=$(basename "$input")
input_dir=$(dirname "$input")

output_noext=$(noext "$output")
output_base=$(basename "$output")
output_base_noext=$(noext "$output_base")
output_dir=$(dirname "$output")

: ${location_dir=$output_dir}

# The namespace we are in.
# FIXME: We need $srcdir to be able to compute it.
namespace=parse

if $BISON --xml --version >/dev/null 2>&1; then
  bisonflags="$bisonflags --xml"
fi

# A tmp dir.
tmp=$output_noext.dir
rm -rf $tmp
mkdir -p $tmp

# Compile in this dir.
# Don't use `ln -s' as its semantics of paths sucks.
set +e
$BISON $bisonflags $input -o $tmp/$output_base
status=$?
set -e

Akim Demaille's avatar
Akim Demaille committed
247
248
for file in $tmp/*
do
Akim Demaille's avatar
Akim Demaille committed
249
250
  test $file != "$tmp/*" ||
    fatal "did not find any file in $tmp"
Akim Demaille's avatar
Akim Demaille committed
251
252
253
  base=$(basename $file)
  case $status:$base in
    (*:$input_base)
254
255
        # Leave it here.
        ;;
Akim Demaille's avatar
Akim Demaille committed
256
257
258

    # Success or not, install it.
    (*:*.xml)
259
260
261
262
263
264
265
266
        # Computing the HTML is slow.  Do it when the XML changed.
        fix_bison_output "$file"
        compile_xml_file "$file" "xml2xhtml" "html"
        # Compiling the dot output is quite long, it would be better
        # to do that from the Makefile, so that -j applies.
        # compile_xml_file "$file" "xml2dot" "dot"
        install "$file"
        ;;
Akim Demaille's avatar
Akim Demaille committed
267
268

    (0:*.hh)
269
270
271
272
273
        fix_bison_output "$file"
        # To save cycles, if the file differs only on sync lines,
        # update it (to be right), but keep the original timestamps.
        if test -r "$(out "$file")" &&
           diff -I '^#line' -I '/\* Line .* of .*  \*/' -q \
Akim Demaille's avatar
Akim Demaille committed
274
275
                "$file" "$(out "$file")"
        then
276
277
278
279
280
281
            touch -r "$(out "$file")" "$file"
            cp "$(out "$file")" "$(out "$file").bak"
            verbose "kept the stamps of $file"
        fi
        install "$file"
        ;;
Akim Demaille's avatar
Akim Demaille committed
282
283

    (*:*.output)
284
285
    install "$file"
    ;;
Akim Demaille's avatar
Akim Demaille committed
286
287

    (0:*)
288
289
290
    fix_bison_output "$file"
    install "$file"
    ;;
Akim Demaille's avatar
Akim Demaille committed
291
292
  esac
done
Benoit Perrot's avatar
Benoit Perrot committed
293
294

# Get rid of the tmp dir.
Akim Demaille's avatar
Akim Demaille committed
295
# rm -rf $tmp
296
exit $status