gen-split-examples-mk 3.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#! /bin/sh

# Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE).
#
# This file is part of Olena.
#
# Olena is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, version 2 of the License.
#
# Olena is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Olena.  If not, see <http://www.gnu.org/licenses/>.

# To be used from milena/doc/'s source directory.

21
22
# FIXME: Factor gen-split-examples-mk and gen-split-outputs-mk.

23
24
me=`basename $0`

25
26
27
# Use the C locale to have a deterministic sort.
export LC_ALL=C

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
die ()
{
  echo >&2 "$me: $@"
  exit 1
}

# backslashify
# ------------
# Read lines from the standard input and write them on the standard
# output with an extra trailing backslash, except for the last line.
backslashify()
{
  # Set IFS to nothing to prevent `read' from trimming spaces or tabs.
  IFS= read last_line
  while IFS= read line; do
    echo "$last_line \\"
    last_line=$line
  done
  echo "$last_line"
}

# Get this list as argument?
inputs()
{
52
53
54
55
56
  # Remove the .cc extension before sorting file names.
  find examples -name \*.cc			\
    | sed 's/.cc$//'				\
    | sort					\
    | sed 's/$/.cc/'
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
}

output_dir=examples/split

# Use a temporary file to dump rules at the end of the output.
tempfile=`mktemp`
# Set up automatic cleanup.
cleanup() { rm -f "$tempfile"; exit; }
trap cleanup 0 1 2 3 6 15

# Header.
cat <<EOF
## Generated by $me.  Do not edit by hand.

EOF

# Output target names in a variable.
{
  echo "SPLIT_EXAMPLES ="
  inputs | while read file; do
    # Count the number of opening and closing marks.
    nopening=`grep -c '// *\\\{' "$file"`
    nclosing=`grep -c '// *\\\}' "$file"`
    test "$nopening" -eq "$nclosing" \
      || die "Unbalanced marks in $file ($nopening vs $nclosing)"

    # If there is no mark, skip this file.
    test "$nopening" -eq 0 && continue

    # FIXME: Programs of which sources are located in a subdirectory of
    # examples/ take the path as prefix of their name.  This is a pain.
    # It'd be much simpler to have all sources in the same directory.
    canonical_name=`echo "$file"		\
                      | sed -e 's,examples/,,'	\
                            -e 'y,/,_,'		\
                            -e 's,\.cc$,,'`
    # Timestamp split examples (outputs) depend on.
    timestamp="\$(srcdir)/$output_dir/$canonical_name.cc.stamp"
    # List of targets associated to FILE.
    targets=
    {
      i=1
      while test "$i" -le "$nopening"; do
        # i-th part of the split file.
        part="\$(srcdir)/$output_dir/$canonical_name-$i.cc.raw"

        if test -z "$targets"; then
          targets="$part"
        else
          targets="$targets $part"
        fi
        echo "  $part"
        i=`expr $i + 1`
      done

    # Collect rules in a temporary file...
    cat >>"$tempfile" <<EOF


$timestamp: \$(srcdir)/$file
	@rm -f \$@.tmp
	@touch \$@.tmp
	\$(srcdir)/tools/split_sample.sh \$< cc raw \$(srcdir)/$output_dir
	@mv -f \$@.tmp \$@
EXTRA_DIST += $timestamp
MAINTAINERCLEANFILES += $timestamp

$targets: $timestamp
## Recover from the removal of \$@
	@if test -f \$@; then :; else \\
	  rm -f \$<; \\
	  \$(MAKE) \$(AM_MAKEFLAGS) \$<; \\
	fi
EOF
    }
  done
} | backslashify

# ...and output them at the end.
cat "$tempfile"