summaryrefslogtreecommitdiff
path: root/make-permanently.in
blob: 659517a8af5ac971faf727ac0dee7c52ae2593ff (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
#!/bin/sh

# part of even-more-utils, version #VERSION#

# run make as soon as needed

if [ "x$1" = 'x-r' ]; then
  recursive=true
  shift
else
  recursive=false
fi

make "$@"

while true; do
  if ${recursive}; then
    inotifywait -e DELETE_SELF,CLOSE_WRITE -r * || break
  else
    make_content=$(
      sed '
        :a
          \/\\$/ {
            N
            s/\\\n//
            ta
          }
        /^\(\s\|$\)/d
      ' 'Makefile' \
      | while read -r line; do
        if [ -z "${line##\$(foreach *,*,*)*}" ]; then
          line="${line#\$(foreach }"
          var="${line%%,*}"
          line="${line#${var}*,}"
          runs="${line%,*}"
          line="${line#${runs},}"
          body="${line%%)*}"
          line="${line#${body})}"
          while [ $(printf '%s\n' "${body}" | tr -d '\n' | tr ')' '\n' | wc -l) -lt $(printf '%s\n' "${body}" | tr -d '\n' | tr '(' '\n' | wc -l) ] \
          && [ -z "${line##*)*}" ]; do
            new_body="${line%%)*}"
            body="${body})${new_body}"
            line="${line#${new_body})}"
          done
          for s in ${runs}; do
            printf '%s' "${body}" \
            | sed '
              s#\$('"${var}"')#'"${s}"'#g
            '
          done \
          | sed '
            s/^\s\+//
            s/\s\+$//
          '
        fi
        printf '%s\n' "${line}"
      done
    )
    substs=$(
      printf '%s\n' "${make_content}" \
      | sed '
        s/^\([^[:space:]=]\+\)=\(.*\)$/s@\1@ \2 @g;\n/
        t
        d
      '
    )
    watch_files=$(
      printf '%s\n' "${make_content}" \
      | sed "${substs}" \
      | sed -n '
        s/^\S.*: //
        T
        y/ /\n/
        p
      ' \
      | sed '
        /^\s*$/d
        y/%/*/
      ' \
      | xargs -r readlink -f \
      | sort -u
    )
    inotifywait -e DELETE_SELF,CLOSE_WRITE $(find ${watch_files} -type f) || break
  fi
  make "$@"
done