summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/tangle-sh.in
blob: 3221dc7616074392e9c2f7392b1dc9cb36e57c32 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! @SHELL@
# $Id$
# tangle-sh: shell script to invoke tangle, ctangle, otangle.
#
#   Copyright 2015-2022 Karl Berry <tex-live@tug.org>
#   Copyright 2009-2015 Peter Breitenlohner <tex-live@tug.org>
#
#   This file is free software; the copyright holder
#   gives unlimited permission to copy and/or distribute it,
#   with or without modifications, as long as this notice is preserved.
#
# Tangle may create several files, e.g., tex.p and tex.pool from tex.web
# and tex.ch.  The simple but naive rule
#	tex.p tex.pool: tex.web tex.ch tangle$(EXEEXT)
#		$(tangle) tex tex
# could run 'tangle tex tex' twice in parallel and thus fail.
# 
# To avoid this and yet recover from removal of tex.p and/or tex.pool,
# we use an auxiliary stamp file tex-tangle and the two identical rules:
#	tex.p tex.pool: tex-tangle
#		# recover from deletion of target files:
#		WEBINPUTS=.:$(srcdir) $(SHELL) ./tangle-sh $@ $(TANGLE) tex tex
#	tex-tangle: tex.web tex.ch tangle$(EXEEXT) tangle-sh
#		# normal build:
#		WEBINPUTS=.:$(srcdir) $(SHELL) ./tangle-sh $@ $(TANGLE) tex tex
#
# Confusingly, both rules invoke this script (tangle-sh[.in]), but have
# quite different purposes, as indicated above. As a result, make will
# always run tangle-sh twice when a target needs to be rebuilt.
# 
# The actual running of tangle happens in the second rule, when we are
# making the stamp file. The first rule is to recover from deletion of
# the derived files.
# 
# This approach is in the Automake manual, Multiple Outputs node:
# https://gnu.org/s/automake/manual/html_node/Multiple-Outputs.html
#   
# By the way, the name of the stamp file (foo-tangle) is embedded both
# in this script and in the various *.am files, so it can't be changed lightly.

TEXMFCNF=@srcdir@/../kpathsea; export TEXMFCNF

env="TEXMFCNF=$TEXMFCNF"
test "x$WEBINPUTS" = x || env="WEBINPUTS=$WEBINPUTS $env"
test "x$CWEBINPUTS" = x || env="CWEBINPUTS=$CWEBINPUTS $env"

prg=`basename $0`
target=$1; shift
tangle=$1; shift
base=$1

do_tangle () {
  echo timestamp >$stamp.tmp
  if $AM_V_P; then
    echo "$prg: $env $tangle $@"
    $tangle "$@" || exit 1
  else
    case $base in
      aleph | xetex) echo "  OTANGLE " $base;;
         *tex | mf*) echo "  TANGLE  " $base;;
                  *) echo "  CTANGLE " $base;;
    esac
    $tangle "$@" >$base.out 2>&1; rc=$?
    test $rc -eq 0 || { cat $base.out; exit $rc; }
    rm -f $base.out
  fi
  mv -f $stamp.tmp $stamp
}

stamp=$base-tangle
case $target in
  $stamp)
    # Normal build.
    rm -f $stamp.tmp
    do_tangle "$@"
  ;;
  *)
    # Recover from removal of $target if necessary.
    if test -f $target; then
      $AM_V_P && echo "$prg: checking for $target ... ok"
      exit 0
    else
      $AM_V_P && echo "$prg: recovering $target"
    fi
    #
    trap "rm -rf $stamp $stamp.lock" 1 2 13 15
    if mkdir $stamp.lock 2>/dev/null; then
      # Code executed by the first process.
      rm -f $stamp $stamp.tmp
      do_tangle "$@"
      rmdir $stamp.lock
    else
      # Code executed by the follower processes.
      # Wait until the first process is done.
      while test -d $stamp.lock; do sleep 1; done
      # Succeed if and only if the first process succeeded.
      test -f $stamp; exit $?
    fi
  ;;
esac

exit 0