| 1 |
#! @SHELL@ |
| 2 |
# $Id: tangle-sh.in 61575 2022-01-11 22:47:10Z karl $ |
| 3 |
# tangle-sh: shell script to invoke tangle, ctangle, otangle. |
| 4 |
# |
| 5 |
# Copyright 2015-2022 Karl Berry <[email protected]> |
| 6 |
# Copyright 2009-2015 Peter Breitenlohner <[email protected]> |
| 7 |
# |
| 8 |
# This file is free software; the copyright holder |
| 9 |
# gives unlimited permission to copy and/or distribute it, |
| 10 |
# with or without modifications, as long as this notice is preserved. |
| 11 |
# |
| 12 |
# Tangle may create several files, e.g., tex.p and tex.pool from tex.web |
| 13 |
# and tex.ch. The simple but naive rule |
| 14 |
# tex.p tex.pool: tex.web tex.ch tangle$(EXEEXT) |
| 15 |
# $(tangle) tex tex |
| 16 |
# could run 'tangle tex tex' twice in parallel and thus fail. |
| 17 |
# |
| 18 |
# To avoid this and yet recover from removal of tex.p and/or tex.pool, |
| 19 |
# we use an auxiliary stamp file tex-tangle and the two identical rules: |
| 20 |
# tex.p tex.pool: tex-tangle |
| 21 |
# # recover from deletion of target files: |
| 22 |
# WEBINPUTS=.:$(srcdir) $(SHELL) ./tangle-sh $@ $(TANGLE) tex tex |
| 23 |
# tex-tangle: tex.web tex.ch tangle$(EXEEXT) tangle-sh |
| 24 |
# # normal build: |
| 25 |
# WEBINPUTS=.:$(srcdir) $(SHELL) ./tangle-sh $@ $(TANGLE) tex tex |
| 26 |
# |
| 27 |
# Confusingly, both rules invoke this script (tangle-sh[.in]), but have |
| 28 |
# quite different purposes, as indicated above. As a result, make will |
| 29 |
# always run tangle-sh twice when a target needs to be rebuilt. |
| 30 |
# |
| 31 |
# The actual running of tangle happens in the second rule, when we are |
| 32 |
# making the stamp file. The first rule is to recover from deletion of |
| 33 |
# the derived files. |
| 34 |
# |
| 35 |
# This approach is in the Automake manual, Multiple Outputs node: |
| 36 |
# https://gnu.org/s/automake/manual/html_node/Multiple-Outputs.html |
| 37 |
# |
| 38 |
# By the way, the name of the stamp file (foo-tangle) is embedded both |
| 39 |
# in this script and in the various *.am files, so it can't be changed lightly. |
| 40 |
|
| 41 |
TEXMFCNF=@srcdir@/../kpathsea; export TEXMFCNF |
| 42 |
|
| 43 |
env="TEXMFCNF=$TEXMFCNF" |
| 44 |
test "x$WEBINPUTS" = x || env="WEBINPUTS=$WEBINPUTS $env" |
| 45 |
test "x$CWEBINPUTS" = x || env="CWEBINPUTS=$CWEBINPUTS $env" |
| 46 |
|
| 47 |
prg=`basename $0` |
| 48 |
target=$1; shift |
| 49 |
tangle=$1; shift |
| 50 |
base=$1 |
| 51 |
|
| 52 |
do_tangle () { |
| 53 |
echo timestamp >$stamp.tmp |
| 54 |
if $AM_V_P; then |
| 55 |
echo "$prg: $env $tangle $@" |
| 56 |
$tangle "$@" || exit 1 |
| 57 |
else |
| 58 |
case $base in |
| 59 |
aleph | xetex) echo " OTANGLE " $base;; |
| 60 |
*tex | mf*) echo " TANGLE " $base;; |
| 61 |
*) echo " CTANGLE " $base;; |
| 62 |
esac |
| 63 |
$tangle "$@" >$base.out 2>&1; rc=$? |
| 64 |
test $rc -eq 0 || { cat $base.out; exit $rc; } |
| 65 |
rm -f $base.out |
| 66 |
fi |
| 67 |
mv -f $stamp.tmp $stamp |
| 68 |
} |
| 69 |
|
| 70 |
stamp=$base-tangle |
| 71 |
case $target in |
| 72 |
$stamp) |
| 73 |
# Normal build. |
| 74 |
rm -f $stamp.tmp |
| 75 |
do_tangle "$@" |
| 76 |
;; |
| 77 |
*) |
| 78 |
# Recover from removal of $target if necessary. |
| 79 |
if test -f $target; then |
| 80 |
$AM_V_P && echo "$prg: checking for $target ... ok" |
| 81 |
exit 0 |
| 82 |
else |
| 83 |
$AM_V_P && echo "$prg: recovering $target" |
| 84 |
fi |
| 85 |
# |
| 86 |
trap "rm -rf $stamp $stamp.lock" 1 2 13 15 |
| 87 |
if mkdir $stamp.lock 2>/dev/null; then |
| 88 |
# Code executed by the first process. |
| 89 |
rm -f $stamp $stamp.tmp |
| 90 |
do_tangle "$@" |
| 91 |
rmdir $stamp.lock |
| 92 |
else |
| 93 |
# Code executed by the follower processes. |
| 94 |
# Wait until the first process is done. |
| 95 |
while test -d $stamp.lock; do sleep 1; done |
| 96 |
# Succeed if and only if the first process succeeded. |
| 97 |
test -f $stamp; exit $? |
| 98 |
fi |
| 99 |
;; |
| 100 |
esac |
| 101 |
|
| 102 |
exit 0 |