summaryrefslogtreecommitdiff
path: root/scripts/sstate-sysroot-cruft.sh
blob: b7ed8ea8463b4a00c5135d7e04c5787b8c3392e4 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/sh

# Used to find files installed in sysroot which are not tracked by sstate manifest

# Global vars
tmpdir=

usage () {
  cat << EOF
Welcome to sysroot cruft finding utility.
$0 <OPTION>

Options:
  -h, --help
        Display this help and exit.

  --tmpdir=<tmpdir>
        Specify tmpdir, will use the environment variable TMPDIR if it is not specified.
	Something like /OE/oe-core/tmp-eglibc (no / at the end).

  --whitelist=<whitelist-file>
        Text file, each line is regular expression for paths we want to ignore in resulting diff.
        You can use diff file from the script output, if it contains only expected exceptions.
        '#' is used as regexp delimiter, so you don't need to prefix forward slashes in paths.
        ^ and $ is automatically added, so provide only the middle part.
        Lines starting with '#' are ignored as comments.
        All paths are relative to "sysroots" directory.
        Directories don't end with forward slash.
EOF
}

# Print error information and exit.
echo_error () {
  echo "ERROR: $1" >&2
  exit 1
}

while [ -n "$1" ]; do
  case $1 in
    --tmpdir=*)
      tmpdir=`echo $1 | sed -e 's#^--tmpdir=##' | xargs readlink -e`
      [ -d "$tmpdir" ] || echo_error "Invalid argument to --tmpdir"
      shift
        ;;
    --whitelist=*)
      fwhitelist=`echo $1 | sed -e 's#^--whitelist=##' | xargs readlink -e`
      [ -f "$fwhitelist" ] || echo_error "Invalid argument to --whitelist"
      shift
        ;;
    --help|-h)
      usage
      exit 0
        ;;
    *)
      echo "Invalid arguments $*"
      echo_error "Try '$0 -h' for more information."
        ;;
  esac
done

# sstate cache directory, use environment variable TMPDIR
# if it was not specified, otherwise, error.
[ -n "$tmpdir" ] || tmpdir=$TMPDIR
[ -n "$tmpdir" ] || echo_error "No tmpdir found!"
[ -d "$tmpdir" ] || echo_error "Invalid tmpdir \"$tmpdir\""

OUTPUT=${tmpdir}/sysroot.cruft.`date "+%s"`

# top level directories
WHITELIST="[^/]*"

# generated by base-passwd recipe
WHITELIST="${WHITELIST} \
  .*/etc/group-\? \
  .*/etc/passwd-\? \
"
# generated by pseudo-native
WHITELIST="${WHITELIST} \
  .*/var/pseudo \
  .*/var/pseudo/[^/]* \
"

# generated by package.bbclass:SHLIBSDIRS = "${PKGDATA_DIR}/${MLPREFIX}shlibs"
WHITELIST="${WHITELIST} \
  .*/shlibs \
  .*/pkgdata \
"

# generated by python
WHITELIST="${WHITELIST} \
  .*\.pyc \
  .*\.pyo \
  .*/__pycache__ \
"

# generated by lua
WHITELIST="${WHITELIST} \
  .*\.luac \
"

# generated by sgml-common-native
WHITELIST="${WHITELIST} \
  .*/etc/sgml/sgml-docbook.bak \
"

# generated by php
WHITELIST="${WHITELIST} \
  .*/usr/lib/php5/php/.channels/.* \
  .*/usr/lib/php5/php/.registry/.* \
  .*/usr/lib/php5/php/.depdb \
  .*/usr/lib/php5/php/.depdblock \
  .*/usr/lib/php5/php/.filemap \
  .*/usr/lib/php5/php/.lock \
"

# generated by toolchain
WHITELIST="${WHITELIST} \
  [^/]*-tcbootstrap/lib \
"

# generated by useradd.bbclass
WHITELIST="${WHITELIST} \
  [^/]*/home \
  [^/]*/home/xuser \
  [^/]*/home/xuser/.bashrc \
  [^/]*/home/xuser/.profile \
  [^/]*/home/builder \
  [^/]*/home/builder/.bashrc \
  [^/]*/home/builder/.profile \
"

# generated by image.py for WIC
# introduced in oe-core commit 861ce6c5d4836df1a783be3b01d2de56117c9863
WHITELIST="${WHITELIST} \
  [^/]*/imgdata \
  [^/]*/imgdata/[^/]*\.env \
"

# generated by fontcache.bbclass
WHITELIST="${WHITELIST} \
  .*/var/cache/fontconfig/ \
"

SYSROOTS="`readlink -f ${tmpdir}`/sysroots/"

mkdir ${OUTPUT}
find ${tmpdir}/sstate-control -name \*.populate-sysroot\* -o -name \*.populate_sysroot\* -o -name \*.package\* | xargs cat | grep sysroots | \
  sed 's#/$##g; s#///*#/#g' | \
  # work around for paths ending with / for directories and multiplied // (e.g. paths to native sysroot)
  sort | sed "s#^${SYSROOTS}##g" > ${OUTPUT}/master.list.all.txt
sort -u ${OUTPUT}/master.list.all.txt > ${OUTPUT}/master.list.txt # -u because some directories are listed for more recipes
find ${tmpdir}/sysroots/ | \
  sort | sed "s#^${SYSROOTS}##g" > ${OUTPUT}/sysroot.list.txt

diff ${OUTPUT}/master.list.all.txt ${OUTPUT}/master.list.txt > ${OUTPUT}/duplicates.txt
diff ${OUTPUT}/master.list.txt ${OUTPUT}/sysroot.list.txt > ${OUTPUT}/diff.all.txt

grep "^> ." ${OUTPUT}/diff.all.txt | sed 's/^> //g' > ${OUTPUT}/diff.txt
for item in ${WHITELIST}; do
  sed -i "\\#^${item}\$#d" ${OUTPUT}/diff.txt;
  echo "${item}" >> ${OUTPUT}/used.whitelist.txt
done

if [ -s "$fwhitelist" ] ; then
  cat $fwhitelist >> ${OUTPUT}/used.whitelist.txt
  cat $fwhitelist | grep -v '^#' | while read item; do
    sed -i "\\#^${item}\$#d" ${OUTPUT}/diff.txt;
  done
fi
# too many false positives for directories
# echo "Following files are installed in sysroot at least twice"
# cat ${OUTPUT}/duplicates

RESULT=`cat ${OUTPUT}/diff.txt | wc -l`

if [ "${RESULT}" != "0" ] ; then
  echo "ERROR: ${RESULT} issues were found."
  echo "ERROR: Following files are installed in sysroot, but not tracked by sstate:"
  cat ${OUTPUT}/diff.txt
else
  echo "INFO: All files are tracked by sstate or were explicitly ignored by this script"
fi

echo "INFO: Output written in: ${OUTPUT}"
exit ${RESULT}