gen-figures-mk 4.13 KB
Newer Older
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
#! /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/>.

me=`basename $0`

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

# Get this list as argument?
inputs()
{
  # Remove the .cc extension before sorting file names.
  find examples -name \*.cc -a \! -path examples/trash/\*	\
    | sed 's/.cc$//'						\
    | sort							\
    | sed 's/$/.cc/'
}

# get_outputs FILE
# ----------------
get_outputs()
{
  local outputs=
  local i=1
  # Use Perl instead of sed for portability reasons.
  for output in `perl -ne						    \
  "print if s|^\\s*doc::(p.m)save\\s*\\([^;]+,\\s*\"([^\"]+)\"\\);\$|\\2.\\1|" \
  "$1"`
  do
    # Add a number to the file name.
    # FIXME: Maybe we should let the Perl script above do this...
    num_output=`echo $output | sed "s,\\(.p.m\\),-$i\\1,"`
    if test -z "$outputs"; then
      outputs="$num_output"
    else
      outputs="$outputs $num_output"
    fi
    i=`expr $i + 1`
  done
  echo "$outputs"
}

# upcase STRING
# -------------
upcase()
{
  # Use enumerated sets to be portable.
  echo "$1" | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
}

# canonicalize STRING
# -------------------
canonicalize()
{
  echo "$1" | tr .- _
}

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

88
# List of generated figures.
89
90
fig_vars=

91
92
93
94
95
96
97
98
99
100
101
102
103
104
# gen_var VARIABLE [ITEMS...]
# ---------------------------
# Generate a Make variable named VARIABLE containing ITEMS.
gen_var()
{
  local v
  {
    echo "$1 ="
    shift
    for v; do
      echo "  $v"
    done
  } | backslashify
}
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
cat<<EOF
## Generated by $me.  Do not edit by hand.

## Figures depend on the timestamp associated with their generator.
## See also examples-outputs.mk.

EOF

for file in `inputs`; do
  # FIXME: Ugly hack to handle the case of examples/ima-save.cc, since
  # this program does not use a `doc::p?msave' routine...
  if test "X$file" = "Xexamples/ima-save.cc"; then
    # Hard-coded value.
    figures="ima_save.pbm"
  else
    figures=`get_outputs "$file"`
  fi

  if test -n "$figures"; then
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
    # 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$,,'`
    canonical_var=`canonicalize "$canonical_name"`
    fig_var="`upcase "$canonical_var"`_FIGURES"

    # Populate the list of variables of figures.
    fig_vars="$fig_vars \$($fig_var)"

    # Generate a Make variable for the figures produced by FILE.
    {
      echo "$fig_var ="
      for figure in $figures; do
        fig_path="  \$(srcdir)/figures/$figure"
        echo "$fig_path"
      done
    } | backslashify

    # Generate a Make rule to regen the figures produced by FILE.
    base=`basename "$file" .cc`
    dir=`dirname "$file"`
    timestamp="\$(srcdir)/$dir/$base.stamp"
    cat <<EOF
151
152
153
154
155
156
157
158
159
160
161
162
163
164
\$($fig_var): $timestamp
## Recover from the removal of \$@
	@if test -f \$@; then :; else \\
	  rm -f \$<; \\
	  \$(MAKE) \$(AM_MAKEFLAGS) \$<; \\
	fi

EOF
  fi
done


# Output the list of figures.
echo "# The list of all generated figures."
165
gen_var FIGURES $fig_vars