home · contact · privacy
6358bbb7e2bca749b09eb50bae884da6eb7218d8
[plomrogue] / build / redo_scripts / redo-ifchange
1 #!/bin/sh
2 # redo-ifchange – bourne shell implementation of DJB redo
3 # Copyright © 2014  Nils Dagsson Moskopp (erlehmann)
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9
10 # Dieses Programm hat das Ziel, die Medienkompetenz der Leser zu
11 # steigern. Gelegentlich packe ich sogar einen handfesten Buffer
12 # Overflow oder eine Format String Vulnerability zwischen die anderen
13 # Codezeilen und schreibe das auch nicht dran.
14
15 _add_dependency() {
16   parent="$1"
17   dependency="$2"
18   # Do not record circular dependencies.
19   [ "$parent" = "$dependency" ] && exit 1
20   local base; _dirsplit "$parent"
21   [ -d "$REDO_DIR/$dir" ] || mkdir -p "$REDO_DIR/$dir"
22   # Naive implementation: Append dependency, then remove double dependencies.
23   dependencies=$( (echo "$dependency"; cat "$REDO_DIR/$parent".dependencies 2>/dev/null ) | sort -u)
24   echo "$dependencies" > "$REDO_DIR/$parent".dependencies
25   # FIXME: Remove added dependency from non-existence dependencies.
26 }
27
28 _add_dependency_ne() {
29   parent="$1"
30   dependency_ne="$2"
31   # Do not record circular dependencies.
32   [ "$parent" = "$dependency_ne" ] && exit 1
33   # Do not record existing files as non-existence dependencies.
34   [ -e "$dependency_ne" ] && return
35   local base; _dirsplit "$parent"
36   [ -d "$REDO_DIR/$dir" ] || mkdir -p "$REDO_DIR/$dir"
37   # Naive implementation: Append dependency, then remove double dependencies.
38   dependencies_ne=$( (echo "$dependency_ne"; cat "$REDO_DIR/$parent".dependencies_ne 2>/dev/null ) | sort -u)
39   echo "$dependencies_ne" > "$REDO_DIR/$parent".dependencies_ne
40   # FIXME: Remove added non-existence dependency from dependencies.
41 }
42
43 _add_target() {
44   local target="$1"
45   # Exit if called without a target.
46   [ -z "$target" ] && exit 1
47   local base; _dirsplit "$target"
48   [ -d "$REDO_DIR/$dir" ] || mkdir -p "$REDO_DIR/$dir"
49   # If target file exists, record ctime and md5sum.
50   if [ -e "$target" ]; then
51     stat -c%Y "$target" > "$REDO_DIR/$target".ctime
52     md5sum < "$target" > "$REDO_DIR/$target".md5sum
53   fi
54 }
55
56 _dependencies_uptodate() {
57   target="$1"
58   target_relpath=${target##$REDO_BASE/}
59   # If no dependencies exist, they are by definition up to date.
60   if [ ! -e "$REDO_DIR/$target".dependencies ]; then
61     _echo_debug_message "$REDO_DEPTH$dir$target_relpath has no dependencies."
62     return 0
63   fi
64   _echo_debug_message "$REDO_DEPTH$dir$target_relpath non-existant dependency check:"
65   dependencies_ne=$(cat "$REDO_DIR/$target".dependencies_ne 2>/dev/null || :)
66   for dependency_ne in $dependencies_ne; do
67     dependency_ne_relpath=${dependency_ne##$REDO_BASE/}
68     _echo_debug_message "$REDO_DEPTH$dir$target_relpath depends on non-existence of $dependency_ne_relpath"
69     # If a non-existence dependency exists, it is out of date.
70     # Dependencies, e.g. on default.do files may also be out of date.
71     # Naive implementation: Delete dependency information and rebuild.
72     if [ -e "$dependency_ne" ]; then
73       rm "$REDO_DIR/$target".dependencies
74       rm "$REDO_DIR/$target".dependencies_ne
75       return 1
76     # If a non-existence dependency does not exist, but is also a
77     # dependency, it existed in the past. Naive implementation: Delete
78     # dependency information and pretend the (actually up-to-date)
79     # dependency is out of date to generate dependency information.
80     # TODO: Only remove non-existence dependency from dependencies,
81     # and do no return at this point; maybe escape filename properly.
82     elif (grep -q '^'"$dependency_ne"'$' "$REDO_DIR/$target".dependencies \
83       2>/dev/null); then
84       rm "$REDO_DIR/$target".dependencies
85       return 1
86     fi
87   done
88   _echo_debug_message "$REDO_DEPTH$dir$target_relpath non-existence dependencies uptodate."
89   _echo_debug_message "$REDO_DEPTH$dir$target_relpath dependency check:"
90   if [ "$REDO_SHUFFLE" = "1" ]; then
91     dependencies=$(shuf "$REDO_DIR/$target".dependencies 2>/dev/null || :)
92   else
93     dependencies=$(cat "$REDO_DIR/$target".dependencies 2>/dev/null || :)
94   fi
95   for dependency in $dependencies; do
96     dependency_relpath=${dependency##$REDO_BASE/}
97     dir=''
98     _echo_debug_message "$REDO_DEPTH$dir$target_relpath depends on $dependency_relpath"
99     if ( ! _is_uptodate "$dependency" ); then
100       _echo_debug_message \
101         "$REDO_DEPTH$dir$target_relpath dependency $dependency_relpath not uptodate."
102       return 1
103     fi
104     if ( ! _dependencies_uptodate "$dependency"); then
105       _echo_debug_message \
106         "$REDO_DEPTH$dir$target_relpath dependency $dependency_relpath dependencies not uptodate."
107       return 1
108     fi
109     # In the case that two targets depend on the same target and are
110     # built after another, when the second target is built, redo finds
111     # its dependencies to be up-to-date. This implies that the second
112     # target would not be built. If a dependency build timestamp is
113     # greater than or equal to a targets build timestamp, assume the
114     # dependency has been rebuilt and the target should be rebuilt.
115     if [ -e "$REDO_DIR/$target_abspath.buildtime" ]; then \
116       read target_ts < "$REDO_DIR/$target_abspath.buildtime";
117     else
118       target_ts=0
119     fi
120     if [ -e "$REDO_DIR/$dependency.buildtime" ]; then
121       read dependency_ts < "$REDO_DIR/$dependency.buildtime"
122     else
123        dependency_ts=0
124     fi
125     if [ "$dependency_ts" -ge "$target_ts" ]; then
126       _echo_debug_message \
127         "$REDO_DEPTH$dir$target_relpath dependency $dependency_relpath uptodate, but built later than $target_relpath."
128       return 1
129     fi
130   done
131   _echo_debug_message "$REDO_DEPTH$dir$target_relpath dependencies uptodate."
132   return 0
133 }
134
135 _dirsplit() {
136   base=${1##*/}
137   dir=${1%"$base"}
138 }
139
140 _dir_shovel() {
141   local dir base
142   xdir="$1" xbase="$2" xbasetmp="$2"
143   while [ ! -d "$xdir" -a -n "$xdir" ]; do
144     _dirsplit "${xdir%/}"
145     xbasetmp=${base}__"$xbase"
146     xdir="$dir" xbase="$base"/"$xbase"
147     echo "xbasetmp='$xbasetmp'" >&2
148   done
149 }
150
151 _do() {
152   local dir="$1" target="$2" tmp="$3"
153   # Add target to parent targets dependencies.
154   target_abspath="$PWD/$target"
155   if [ -n "$REDO_TARGET" ]; then
156     _add_dependency "$REDO_TARGET" "$target_abspath"
157   fi
158   target_relpath="${target_abspath##$REDO_BASE/}"
159   if ( ! _is_uptodate "$target_abspath" || ! _dependencies_uptodate "$target_abspath" ); then
160     dofile="$target".do
161     base="$target"
162     ext=
163     [ -e "$target.do" ] || _find_dofile "$target"
164     # Add non existing .do file to non-existence dependencies so target is built when
165     # .do file in question is created.
166     _add_dependency_ne "$(readlink -f "$target")" "$PWD/$target.do"
167     if [ ! -e "$dofile" ]; then
168       # If .do file does not exist and target exists, it is a source file.
169       if [ -e "$target" ]; then
170         _add_target "$(readlink -f $target)"
171         # Portable timestamps for systems supporting GNU date format '%N'.
172         date +%s%N | tr -d '%N' > "$REDO_DIR/$target_abspath".buildtime
173         return 0
174       # If .do file does not exist and target does not exist, stop.
175       else
176         echo "redo: $target: no .do file" >&2
177         exit 1
178       fi
179     # Add .do file to dependencies so target is built when .do file changes.
180     else
181       _add_dependency "$(readlink -f "$target")" "$PWD/$dofile"
182       _add_target "$PWD/$dofile"
183     fi
184     printf '%sredo %s%s%s%s%s\n' \
185       "$green" "$REDO_DEPTH" "$bold" "$target_relpath" "$plain" >&2
186     ( _run_dofile "$target" "$base" "$tmp.tmp" )
187     rv="$?"
188     if [ $rv != 0 ]; then
189       rm -f "$tmp.tmp" "$tmp.tmp2"
190       # Exit code 123 conveys that target was considered uptodate at runtime.
191       if [ $rv != 123 ]; then
192         printf "%sredo: %s%s: got exit code %s.%s\n" \
193           "$red" "$REDO_DEPTH" "$target" "$rv" "$plain" >&2
194         exit 1
195       fi
196     fi
197     mv "$tmp.tmp" "$target" 2>/dev/null ||
198     ! test -s "$tmp.tmp2" ||
199     mv "$tmp.tmp2" "$target" 2>/dev/null
200     rm -f "$tmp.tmp2"
201     # Portable timestamps for systems supporting GNU date format '%N'.
202     date +%s%N | tr -d '%N' > "$REDO_DIR/$target_abspath".buildtime
203   else
204     _echo_debug_message "$REDO_DEPTH$dir$target is up to date."
205   fi
206   # Some do files (like all.do) do not usually generate output.
207   if [ -e "$target" ]; then
208     _add_target "$(readlink -f $target)"
209   fi
210 }
211
212 _echo_debug_message() {
213   [ "$REDO_DEBUG" = "1" ] && echo "$@" >&2
214 }
215
216 _find_dofile() {
217   local prefix=
218   while :; do
219     _find_dofile_pwd "$1"
220     [ -e "$dofile" ] && break
221     [ "$PWD" = "/" ] && break
222     target=${PWD##*/}/"$target"
223     tmp=${PWD##*/}/"$tmp"
224     prefix=${PWD##*/}/"$prefix"
225     cd ..
226   done
227   base="$prefix""$base"
228 }
229
230 _find_dofile_pwd() {
231   dofile=default."$1".do
232   while :; do
233     dofile=default.${dofile#default.*.}
234     [ -e "$dofile" -o "$dofile" = default.do ] && break
235   done
236   ext=${dofile#default}
237   ext=${ext%.do}
238   base=${1%$ext}
239 }
240
241 _is_uptodate() {
242   target="$1"
243   target_relpath=${target##$REDO_BASE/}
244   # If a target does not exist, it is by definition out of date.
245   if [ ! -e "$target" ]; then
246     _echo_debug_message "$REDO_DEPTH$dir$target_relpath does not exist."
247     return 1
248   else
249     # If a file exists, but has no build date, it is by a previously unseen
250     # source file. A previously unseen source file is  by definition up to date.
251     if [ ! -e "$REDO_DIR/$target".ctime ]; then
252       _echo_debug_message "$REDO_DEPTH$dir$target_relpath.ctime does not exist."
253       # Add source file to dependencies so target is built when source file changes.
254       _add_target "$(readlink -f $target)"
255       return 0
256     else
257       _echo_debug_message "$REDO_DEPTH$dir$target_relpath.ctime exists."
258       # If a file exists and has an entry in the always database, it is never
259       # up to date.
260       if [ -e "$REDO_DIR/$target".always ]; then
261         _echo_debug_message "$REDO_DEPTH$dir$target_relpath.always exists."
262         return 1
263       fi
264       # If a file exists and has an entry in the stamp database, it might not
265       # be up to date. redo-stamp decides if the file is up to date at runtime.
266       if [ -e "$REDO_DIR/$target".stamp ]; then
267         _echo_debug_message "$REDO_DEPTH$dir$target_relpath.stamp exists."
268         return 1
269       fi
270       # If a file exists and has an entry in the dependency database and ctime
271       # is the same as in the entry in the ctime database, it is up to date.
272       if (export LC_ALL=C; stat -c%Y "$target" | grep -Fqx -f "$REDO_DIR/$target".ctime); then
273         _echo_debug_message "$REDO_DEPTH$dir$target_relpath.ctime match."
274         return 0
275       else
276         _echo_debug_message "$REDO_DEPTH$dir$target_relpath.ctime mismatch."
277         # If a file exists and has an entry in the dependency database and
278         # ctime is different from the entry in the ctime database, but md5sum
279         # is the same as md5sum in the md5sum database, it is up to date.
280         if (md5sum < "$target" | grep -Fqx -f "$REDO_DIR/$target".md5sum); then
281           _echo_debug_message "$REDO_DEPTH$dir$target_relpath.md5sum match."
282           return 0
283         else
284           _echo_debug_message "$REDO_DEPTH$dir$target_relpath.md5sum mismatch."
285           return 1
286         fi
287       fi
288     fi
289   fi
290   return 1
291 }
292
293 _run_dofile() {
294   export REDO_DEPTH="$REDO_DEPTH  "
295   export REDO_TARGET="$PWD"/"$target"
296   local line1
297   set -e
298   read line1 <"$PWD/$dofile" || true
299   cmd=${line1#"#!"}
300   # If the first line of a do file does not have a hashbang (#!), use /bin/sh.
301   if [ "$cmd" = "$line1" ] || [ "$cmd" = "/bin/sh" ]; then
302     if [ "$REDO_XTRACE" = "1" ]; then
303       cmd="/bin/sh -ex"
304     else
305       cmd="/bin/sh -e"
306     fi
307   fi
308   $cmd "$PWD/$dofile" "$@" >"$tmp.tmp2"
309 }
310
311 set +e
312 if [ -n "$1" ]; then
313   for target; do
314     _dirsplit "$target"
315     _dir_shovel "$dir" "$base"
316     dir="$xdir" base="$xbase" basetmp="$xbasetmp"
317     ( cd "$dir" && _do "$dir" "$base" "$basetmp" )
318     [ "$?" = 0 ] || exit 1
319   done
320 fi