summaryrefslogtreecommitdiff
blob: 9d0d4799ecab3537044283c65617d768ed7dc51b (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
#!/bin/bash
# spdx-license-identifier: GPL-2.0-or-later
# Script to iterate over `ebuild foo-1.2.3.ebuild clean merge` and automatically add values to keeplibs.
# Usage: ./iterate-over-ebuild.sh foo-1.2.3.ebuild
# This script will run until the ebuild is merged, or until you interrupt it with Ctrl+C.
# It will add the libraries to keeplibs in the ebuild as it goes.

package="${1%.ebuild}"
tmpfile=$(mktemp)
iter=0
added=()

# Trap for Ctrl+C
trap 'cleanup' INT

cleanup() {
  echo "[$(date)]: Script interrupted."
  echo "$tmpfile" for this iteration\'s logs.
  exit 1
}

while true; do
  libs=()
  echo "[$(date)]: Processing $package; iteration $((++iter))"
  echo "So far, we've added:"
  if [ ${#added[@]} -eq 0 ]; then
    echo "  Nothing"
  fi
  for i in "${added[@]}"; do
    echo "  $i"
  done
  ebuild "${1}" clean merge 2>&1 | tee "$tmpfile"

  # Should only ever be one but whatever
  mapfile -t libs < <(grep 'ninja: error:' "$tmpfile" | awk '{print $3}' | cut -c 8- | awk -F/ '{OFS="/"; NF--; print}')

  if [ ${#libs[@]} -eq 0 ]; then
    echo "[$(date)]: No new libraries to whitelist."
  else
    for lib in "${libs[@]}"; do
      echo "[$(date)]: Whitelisting $lib"
      if grep -q "$lib$" "${1}"; then
        # Something went wrong if we're here but whatever.
        echo "[$(date)]: $lib already exists in keeplibs"
      else
        echo "[$(date)]: Adding $lib to keeplibs"
        sed -i "/^\s*local keeplibs=/a $lib" "${1}"
        added+=("$lib")
      fi
    done
  fi

  if grep -q "www-client/$package merged" "$tmpfile"; then
    rm "$tmpfile"
    break
  fi
  # Start with a clean slate for the next iteration
  rm "$tmpfile"
done