bison++.in 6.24 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
#! /bin/sh
Akim Demaille's avatar
Akim Demaille committed
2
set -x
3
4
5
# Exit status.
status=0

Benoit Perrot's avatar
Benoit Perrot committed
6
7
8
# Any tool failure is a failure of the script.
set -e

Akim Demaille's avatar
Akim Demaille committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
: ${BISON='@BISON@'}

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
37

Akim Demaille's avatar
Akim Demaille committed
38
39
40
41
42
43
44
45
46
47
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
48
49

me=$(basename $0)
Akim Demaille's avatar
Akim Demaille committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
fuse_switch='@abs_srcdir@/fuse-switch'
move_if_change='@abs_srcdir@/move-if-change --color'
verbose=false

## ---------------------- ##
## Command line parsing.  ##
## ---------------------- ##

get_options ()
{
  while test $# != 0
  do
    # Handle --option=value by splitting apart and putting back on argv.
    case $1 in
      (--*=*)
	opt=$(echo "$1" | sed -e 's/=.*//')
	val=$(echo "$1" | sed -e 's/[^=]*=//')
	shift
	set dummy "$opt" "$val" ${1+"$@"}; shift
	;;
    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="$@"
}


## ------ ##
## Main.  ##
## ------ ##

get_options "$@"
Benoit Perrot's avatar
Benoit Perrot committed
97

98
input_base=$(basename "$input")
Akim Demaille's avatar
Akim Demaille committed
99
100
input_dir=$(dirname "$input")

101
output_base=$(basename "$output")
Roland Levillain's avatar
Roland Levillain committed
102
output_base_noext=$(echo "$output_base" | sed -e 's/\.[^.]*//')
103
output_dir=$(dirname "$output")
Roland Levillain's avatar
Roland Levillain committed
104

Akim Demaille's avatar
Akim Demaille committed
105
106
107
108
109
110
: ${location_dir=$output_dir}

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

Roland Levillain's avatar
Roland Levillain committed
111
if $BISON --xml --version >/dev/null 2>&1; then
Akim Demaille's avatar
Akim Demaille committed
112
  bisonflags="$bisonflags --xml"
Roland Levillain's avatar
Roland Levillain committed
113
114
fi

Benoit Perrot's avatar
Benoit Perrot committed
115
116
117
118
119
120
121
122
123
124
# Alexandre Duret-Lutz also notes that in VPATH-builds $(srcdir) can
# be an absolute path depending on how ./configure is called ...
# In that case

#   bison $(srcdir)/parsetiger.yy [...]

# will hard code the path in the maintainer's tree.  Hence, do not use
# paths with Bison, chdir there.

# A tmp dir.
Roland Levillain's avatar
Roland Levillain committed
125
tmp=$output_base_noext.dir
Benoit Perrot's avatar
Benoit Perrot committed
126
127
128
129
130
rm -rf $tmp
mkdir $tmp

# Compile in this dir.
# Don't use `ln -s' as its semantics of paths sucks.
131
cp $input $tmp
Benoit Perrot's avatar
Benoit Perrot committed
132
cd $tmp
133
set +e
Akim Demaille's avatar
Akim Demaille committed
134
$BISON $bisonflags $input_base -o $output_base
135
136
137
status=$?
set -e

Roland Levillain's avatar
Roland Levillain committed
138
139
140
141
142
143

# fix_bison_output FILE
# ---------------------
# Fixes in place.
fix_bison_output ()
{
Akim Demaille's avatar
Akim Demaille committed
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  local file="$1"
  local base
  base=$(basename "$file")
  local base_noext
  base_noext=$(echo "$file" | sed -e 's/\..*//')
  perl -pi -e "
# Fix doxygen tags.
s|\Q\\file $base\E\b|\\\\file $namespace/$base|g;

# Fix sync lines for composite paths.
s|(^#line.*)$base_noext|\$1$input_dir/$base_noext|g
  if '$input_dir' ne '$output_dir';

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

# Kill trailing blanks.
s/[ \t]+\$//;
"  "$1"

  # 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
}

# out FILE
# --------
# Return path name of FILE in the tmp directory.
tmp ()
{
  echo "$tmp/$(basename "$1")"
}
Roland Levillain's avatar
Roland Levillain committed
231

Akim Demaille's avatar
Akim Demaille committed
232
233
234
235
236
237
238
239
240
241
242
243
# 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
244
245
246
247
248
}

# Go back to where we were and use relative paths instead of using
# absolute file names.  The messages look nicer.
cd ..
Akim Demaille's avatar
Akim Demaille committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
for file in $tmp/*
do
  base=$(basename $file)
  base_noext=$(echo "$base" | sed -e 's/\.[^.]*//')
  case $status:$base in
    (*:$input_base)
	# Leave it here.
	;;

    # Success or not, install it.
    (*:*.xml)
	# 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"
	;;

    (0:*.hh)
	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 \
                "$file" "$(out "$file")"
        then
	    touch -r "$(out "$file")" "$file"
	    cp "$(out "$file")" "$(out "$file").bak"
	    verbose "kept the stamps of $file"
	fi
	install "$file"
	;;

    (*:*.output)
	install "$file"
	;;

    (0:*)
	fix_bison_output "$file"
	install "$file"
	;;
  esac
done
Benoit Perrot's avatar
Benoit Perrot committed
294
295

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