1#!/bin/sh
2#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5# Used to find files installed in sysroot which are not tracked by sstate manifest
6
7# Global vars
8tmpdir=
9
10usage () {
11  cat << EOF
12Welcome to sysroot cruft finding utility.
13$0 <OPTION>
14
15Options:
16  -h, --help
17        Display this help and exit.
18
19  --tmpdir=<tmpdir>
20        Specify tmpdir, will use the environment variable TMPDIR if it is not specified.
21	Something like /OE/oe-core/tmp-eglibc (no / at the end).
22
23  --whitelist=<whitelist-file>
24        Text file, each line is regular expression for paths we want to ignore in resulting diff.
25        You can use diff file from the script output, if it contains only expected exceptions.
26        '#' is used as regexp delimiter, so you don't need to prefix forward slashes in paths.
27        ^ and $ is automatically added, so provide only the middle part.
28        Lines starting with '#' are ignored as comments.
29        All paths are relative to "sysroots" directory.
30        Directories don't end with forward slash.
31EOF
32}
33
34# Print error information and exit.
35echo_error () {
36  echo "ERROR: $1" >&2
37  exit 1
38}
39
40while [ -n "$1" ]; do
41  case $1 in
42    --tmpdir=*)
43      tmpdir=`echo $1 | sed -e 's#^--tmpdir=##' | xargs readlink -e`
44      [ -d "$tmpdir" ] || echo_error "Invalid argument to --tmpdir"
45      shift
46        ;;
47    --whitelist=*)
48      fwhitelist=`echo $1 | sed -e 's#^--whitelist=##' | xargs readlink -e`
49      [ -f "$fwhitelist" ] || echo_error "Invalid argument to --whitelist"
50      shift
51        ;;
52    --help|-h)
53      usage
54      exit 0
55        ;;
56    *)
57      echo "Invalid arguments $*"
58      echo_error "Try '$0 -h' for more information."
59        ;;
60  esac
61done
62
63# sstate cache directory, use environment variable TMPDIR
64# if it was not specified, otherwise, error.
65[ -n "$tmpdir" ] || tmpdir=$TMPDIR
66[ -n "$tmpdir" ] || echo_error "No tmpdir found!"
67[ -d "$tmpdir" ] || echo_error "Invalid tmpdir \"$tmpdir\""
68
69OUTPUT=${tmpdir}/sysroot.cruft.`date "+%s"`
70
71# top level directories
72WHITELIST="[^/]*"
73
74# generated by base-passwd recipe
75WHITELIST="${WHITELIST} \
76  .*/etc/group-\? \
77  .*/etc/passwd-\? \
78"
79# generated by pseudo-native
80WHITELIST="${WHITELIST} \
81  .*/var/pseudo \
82  .*/var/pseudo/[^/]* \
83"
84
85# generated by package.bbclass:SHLIBSDIRS = "${PKGDATA_DIR}/${MLPREFIX}shlibs"
86WHITELIST="${WHITELIST} \
87  .*/shlibs \
88  .*/pkgdata \
89"
90
91# generated by python
92WHITELIST="${WHITELIST} \
93  .*\.pyc \
94  .*\.pyo \
95  .*/__pycache__ \
96"
97
98# generated by lua
99WHITELIST="${WHITELIST} \
100  .*\.luac \
101"
102
103# generated by sgml-common-native
104WHITELIST="${WHITELIST} \
105  .*/etc/sgml/sgml-docbook.bak \
106"
107
108# generated by php
109WHITELIST="${WHITELIST} \
110  .*/usr/lib/php5/php/.channels \
111  .*/usr/lib/php5/php/.channels/.* \
112  .*/usr/lib/php5/php/.registry \
113  .*/usr/lib/php5/php/.registry/.* \
114  .*/usr/lib/php5/php/.depdb \
115  .*/usr/lib/php5/php/.depdblock \
116  .*/usr/lib/php5/php/.filemap \
117  .*/usr/lib/php5/php/.lock \
118"
119
120# generated by toolchain
121WHITELIST="${WHITELIST} \
122  [^/]*-tcbootstrap/lib \
123"
124
125# generated by useradd.bbclass
126WHITELIST="${WHITELIST} \
127  [^/]*/home \
128  [^/]*/home/xuser \
129  [^/]*/home/xuser/.bashrc \
130  [^/]*/home/xuser/.profile \
131  [^/]*/home/builder \
132  [^/]*/home/builder/.bashrc \
133  [^/]*/home/builder/.profile \
134"
135
136# generated by image.py for WIC
137# introduced in oe-core commit 861ce6c5d4836df1a783be3b01d2de56117c9863
138WHITELIST="${WHITELIST} \
139  [^/]*/imgdata \
140  [^/]*/imgdata/[^/]*\.env \
141"
142
143# generated by fontcache.bbclass
144WHITELIST="${WHITELIST} \
145  .*/var/cache/fontconfig/ \
146"
147
148# created by oe.utils.write_ld_so_conf which is used from few bbclasses and recipes:
149# meta/classes/image-prelink.bbclass:    oe.utils.write_ld_so_conf(d)
150# meta/classes/insane.bbclass:                oe.utils.write_ld_so_conf(d)
151# meta/classes/insane.bbclass:                oe.utils.write_ld_so_conf(d)
152# meta/recipes-gnome/gobject-introspection/gobject-introspection_1.48.0.bb:    oe.utils.write_ld_so_conf(d)
153# meta/recipes-gnome/gobject-introspection/gobject-introspection_1.48.0.bb:        oe.utils.write_ld_so_conf(d)
154# introduced in oe-core commit 7fd1d7e639c2ed7e0699937a5cb245c187b7c811
155# and more visible since added to gobject-introspection in 10e0c1a3a452baa05d160a92a54b2e33cf0fd061
156WHITELIST="${WHITELIST} \
157  [^/]*/etc/ld.so.conf \
158"
159
160SYSROOTS="`readlink -f ${tmpdir}`/sysroots/"
161
162mkdir ${OUTPUT}
163find ${tmpdir}/sstate-control -name \*.populate-sysroot\* -o -name \*.populate_sysroot\* -o -name \*.package\* | xargs cat | grep sysroots | \
164  sed 's#/$##g; s#///*#/#g' | \
165  # work around for paths ending with / for directories and multiplied // (e.g. paths to native sysroot)
166  sort | sed "s#^${SYSROOTS}##g" > ${OUTPUT}/master.list.all.txt
167sort -u ${OUTPUT}/master.list.all.txt > ${OUTPUT}/master.list.txt # -u because some directories are listed for more recipes
168find ${tmpdir}/sysroots/ | \
169  sort | sed "s#^${SYSROOTS}##g" > ${OUTPUT}/sysroot.list.txt
170
171diff ${OUTPUT}/master.list.all.txt ${OUTPUT}/master.list.txt > ${OUTPUT}/duplicates.txt
172diff ${OUTPUT}/master.list.txt ${OUTPUT}/sysroot.list.txt > ${OUTPUT}/diff.all.txt
173
174grep "^> ." ${OUTPUT}/diff.all.txt | sed 's/^> //g' > ${OUTPUT}/diff.txt
175for item in ${WHITELIST}; do
176  sed -i "\\#^${item}\$#d" ${OUTPUT}/diff.txt;
177  echo "${item}" >> ${OUTPUT}/used.whitelist.txt
178done
179
180if [ -s "$fwhitelist" ] ; then
181  cat $fwhitelist >> ${OUTPUT}/used.whitelist.txt
182  cat $fwhitelist | grep -v '^#' | while read item; do
183    sed -i "\\#^${item}\$#d" ${OUTPUT}/diff.txt;
184  done
185fi
186# too many false positives for directories
187# echo "Following files are installed in sysroot at least twice"
188# cat ${OUTPUT}/duplicates
189
190RESULT=`cat ${OUTPUT}/diff.txt | wc -l`
191
192if [ "${RESULT}" != "0" ] ; then
193  echo "ERROR: ${RESULT} issues were found."
194  echo "ERROR: Following files are installed in sysroot, but not tracked by sstate:"
195  cat ${OUTPUT}/diff.txt
196else
197  echo "INFO: All files are tracked by sstate or were explicitly ignored by this script"
198fi
199
200echo "INFO: Output written in: ${OUTPUT}"
201exit ${RESULT}
202