beta-0.89.2
[luatex.git] / source / texk / web2c / tangle-sh.in
blob6af8991f042867c2693d00f56e3cd79de08df078
1 #! @SHELL@
2 # tangle-sh: shell script to invoke tangle (or ctangle, etc).
4 #   Copyright (C) 2009, 2012 Peter Breitenlohner <tex-live@tug.org>
6 #   This file is free software; the copyright holder
7 #   gives unlimited permission to copy and/or distribute it,
8 #   with or without modifications, as long as this notice is preserved.
10 # Tangle may create several files, e.g., tex.p and tex.pool from tex.web
11 # and tex.ch.  The naive rule
12 #       tex.p tex.pool: tex.web tex.ch tangle$(EXEEXT)
13 #               $(tangle) tex tex
14 # could run 'tangle tex tex' twice in parallel and fail.  To avoid this
15 # and yet recover from removal of tex.p and/or tex.pool, we use an
16 # auxiliary stamp file tex-tangle and the two rules
17 #       tex.p tex.pool: tex-tangle
18 #               WEBINPUTS=.:$(srcdir) $(SHELL) ./tangle-sh $@ $(TANGLE) tex tex
19 #       tex-tangle: tex.web tex.ch tangle$(EXEEXT) tangle-sh
20 #               WEBINPUTS=.:$(srcdir) $(SHELL) ./tangle-sh $@ $(TANGLE) tex tex
21 # Compare Automake manual (info Automake) 27.9: Multiple Outputs
23 TEXMFCNF=@srcdir@/../kpathsea; export TEXMFCNF
25 env="TEXMFCNF=$TEXMFCNF"
26 test "x$WEBINPUTS" = x || env="WEBINPUTS=$WEBINPUTS $env"
27 test "x$CWEBINPUTS" = x || env="CWEBINPUTS=$CWEBINPUTS $env"
29 target=$1; shift
30 tangle=$1; shift
31 base=$1
33 do_tangle () {
34   echo timestamp >$stamp.tmp
35   if $AM_V_P; then
36     echo "$env $tangle $@"
37     $tangle "$@" || exit 1
38   else
39     case $base in
40       aleph | xetex) echo "  OTANGLE " $base;;
41       *tex | mf*) echo "  TANGLE  " $base;;
42       *) echo "  CTANGLE " $base;;
43     esac
44     $tangle "$@" >$base.out 2>&1; rc=$?
45     test $rc -eq 0 || { cat $base.out; exit $rc; }
46     rm -f $base.out
47   fi
48   mv -f $stamp.tmp $stamp
51 stamp=$base-tangle
52 case $target in
53   $stamp)
54     # Normal build.
55     rm -f $stamp.tmp
56     do_tangle "$@"
57   ;;
58   *)
59     # Recover from removal of $target
60     test -f $target && exit 0
61     trap "rm -rf $stamp $stamp.lock" 1 2 13 15
62     if mkdir $stamp.lock 2>/dev/null; then
63       # Code executed by the first process.
64       rm -f $stamp $stamp.tmp
65       do_tangle "$@"
66       rmdir $stamp.lock
67     else
68       # Code executed by the follower processes.
69       # Wait until the first process is done.
70       while test -d $stamp.lock; do sleep 1; done
71       # Succeed if and only if the first process succeeded.
72       test -f $stamp; exit $?
73     fi
74   ;;
75 esac
77 exit 0