summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/ctangleboot-sh.in
blob: 516ad0339a886924a4b8ec2860c8948de2cc4060 (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
#! @SHELL@
# $Id$
# ctangleboot-sh: shell script to invoke ctangleboot and, if required,
#   update c{tangle,web}boot.[ch]in from c{tangle,web}.[ch].
# For details see tangle-sh[.in].
#
#   Copyright 2015-2022 Karl Berry <tex-live@tug.org>
#   Copyright 2009-2012 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.
#
prg=`basename $0`
target=$1; shift
base=$1
case $base in
  common) file=cweb; exts=c;;
  ctangle) file=ctangle; exts=c;;
  *) echo "Usage: $0 TARGET common|ctangle [CHANGEFILE] [OUTFILE]" >&2
     exit 1;;
esac

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

do_ctangleboot () {
  echo timestamp >$stamp.tmp
  if $AM_V_P; then
    echo "$prg: @CTANGLEBOOT@ $@"
    @CTANGLEBOOT@ "$@" || exit 1
    test -s $file.$exts \
    || { echo "$prg: $file.$exts empty." >&2; rm -f $file.$exts; exit 1; }
  else
    echo "  CTANGLE " $base
    @CTANGLEBOOT@ "$@" >$base.out 2>&1; rc=$?
    # succeed and generate nonempty file
    (test $rc -eq 0 && test -s $file.$exts) \
    || { cat $base.out; rm -f $file.$exts exit $rc; }
    rm -f $base.out
  fi
  mv -f $stamp.tmp $stamp
}

stamp=$base-ctangle
case $target in
  $stamp)
    # Normal build.
    rm -f $stamp.tmp
    do_ctangleboot "$@"
  ;;
  *)
    # 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_ctangleboot "$@"
      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

for ext in $exts; do
  src=$file.$ext
  dst=@srcdir@/${file}boot.${ext}in
  sed -e 's,@srcdir@/cwebdir,cwebdir,' \
      $src >$stamp.tmp
  if cmp -s $stamp.tmp $dst; then
    rm -f $stamp.tmp
  else
    if $AM_V_P; then
      echo "Updating $src => $dst"
    else
      echo "  UPD     " ${file}boot.${ext}in
    fi
    mv -f $stamp.tmp $dst || exit 1
  fi
done

exit 0