1#!/bin/bash
2#
3# Copyright OpenEmbedded Contributors
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
7# Used to compare sstate checksums between MACHINES.
8# Execute script and compare generated list.M files.
9# Using bash to have PIPESTATUS variable.
10
11# It's also usefull to keep older sstate checksums
12# to be able to find out why something is rebuilding
13# after updating metadata
14
15# $ diff \
16#     sstate-diff/1349348392/fake-cortexa8/list.M \
17#     sstate-diff/1349348392/fake-cortexa9/list.M \
18#     | wc -l
19# 538
20
21# Then to compare sigdata use something like:
22# $ ls sstate-diff/1349348392/*/armv7a-vfp-neon*/linux-libc-headers/*do_configure*sigdata*
23#   sstate-diff/1349348392/fake-cortexa8/armv7a-vfp-neon-oe-linux-gnueabi/linux-libc-headers/3.4.3-r0.do_configure.sigdata.cb73b3630a7b8191e72fc469c5137025
24#   sstate-diff/1349348392/fake-cortexa9/armv7a-vfp-neon-oe-linux-gnueabi/linux-libc-headers/3.4.3-r0.do_configure.sigdata.f37ada177bf99ce8af85914df22b5a0b
25# $ bitbake-diffsigs stamps.1349348392/*/armv7a-vfp-neon*/linux-libc-headers/*do_configure*sigdata*
26#   basehash changed from 8d0bd67bb1da6f68717760fc3ef43171 to e869fa61426e88e9c30726ba88a1216a
27#   Variable TUNE_CCARGS value changed from  -march=armv7-a     -mthumb-interwork -mfloat-abi=softfp -mfpu=neon -mtune=cortex-a8 to  -march=armv7-a     -mthumb-interwork -mfloat-abi=softfp -mfpu=neon -mtune=cortex-a9
28
29# Global vars
30tmpdir=
31machines=
32targets=
33default_machines="qemuarm qemux86 qemux86-64"
34default_targets="core-image-base"
35analyze="N"
36
37usage () {
38  cat << EOF
39Welcome to utility to compare sstate checksums between different MACHINEs.
40$0 <OPTION>
41
42Options:
43  -h, --help
44        Display this help and exit.
45
46  --tmpdir=<tmpdir>
47        Specify tmpdir, will use the environment variable TMPDIR if it is not specified.
48        Something like /OE/oe-core/tmp-eglibc (no / at the end).
49
50  --machines=<machines>
51        List of MACHINEs separated by space, will use the environment variable MACHINES if it is not specified.
52        Default value is "qemuarm qemux86 qemux86-64".
53
54  --targets=<targets>
55        List of targets separated by space, will use the environment variable TARGETS if it is not specified.
56        Default value is "core-image-base".
57
58  --analyze
59        Show the differences between MACHINEs. It assumes:
60        * First 2 MACHINEs in --machines parameter have the same TUNE_PKGARCH
61        * Third optional MACHINE has different TUNE_PKGARCH - only native and allarch recipes are compared).
62        * Next MACHINEs are ignored
63EOF
64}
65
66# Print error information and exit.
67echo_error () {
68  echo "ERROR: $1" >&2
69  exit 1
70}
71
72while [ -n "$1" ]; do
73  case $1 in
74    --tmpdir=*)
75      tmpdir=`echo $1 | sed -e 's#^--tmpdir=##' | xargs readlink -e`
76      [ -d "$tmpdir" ] || echo_error "Invalid argument to --tmpdir"
77      shift
78        ;;
79    --machines=*)
80      machines=`echo $1 | sed -e 's#^--machines="*\([^"]*\)"*#\1#'`
81      shift
82        ;;
83    --targets=*)
84      targets=`echo $1 | sed -e 's#^--targets="*\([^"]*\)"*#\1#'`
85      shift
86        ;;
87    --analyze)
88      analyze="Y"
89      shift
90        ;;
91    --help|-h)
92      usage
93      exit 0
94        ;;
95    *)
96      echo "Invalid arguments $*"
97      echo_error "Try '$0 -h' for more information."
98        ;;
99  esac
100done
101
102# tmpdir directory, use environment variable TMPDIR
103# if it was not specified, otherwise, error.
104[ -n "$tmpdir" ] || tmpdir=$TMPDIR
105[ -n "$tmpdir" ] || echo_error "No tmpdir found!"
106[ -d "$tmpdir" ] || echo_error "Invalid tmpdir \"$tmpdir\""
107[ -n "$machines" ] || machines=$MACHINES
108[ -n "$machines" ] || machines=$default_machines
109[ -n "$targets" ] || targets=$TARGETS
110[ -n "$targets" ] || targets=$default_targets
111
112OUTPUT=${tmpdir}/sstate-diff/`date "+%s"`
113declare -i RESULT=0
114
115for M in ${machines}; do
116  [ -d ${tmpdir}/stamps/ ] && find ${tmpdir}/stamps/ -name \*sigdata\* | xargs rm -f
117  mkdir -p ${OUTPUT}/${M}
118  export MACHINE=${M}
119  bitbake -S none ${targets} 2>&1 | tee -a ${OUTPUT}/${M}/log;
120  RESULT+=${PIPESTATUS[0]}
121  if ls ${tmpdir}/stamps/* >/dev/null 2>/dev/null ; then
122    cp -ra ${tmpdir}/stamps/* ${OUTPUT}/${M}
123    find ${OUTPUT}/${M} -name \*sigdata\* | sed "s#${OUTPUT}/${M}/##g" | sort > ${OUTPUT}/${M}/list
124    M_UNDERSCORE=`echo ${M} | sed 's/-/_/g'`
125    sed "s/^${M_UNDERSCORE}-/MACHINE/g" ${OUTPUT}/${M}/list | sort > ${OUTPUT}/${M}/list.M
126    find ${tmpdir}/stamps/ -name \*sigdata\* | xargs rm -f
127  else
128    printf "ERROR: no sigdata files were generated for MACHINE $M in ${tmpdir}/stamps\n";
129  fi
130done
131
132COMPARE_TASKS="do_configure.sigdata do_populate_sysroot.sigdata do_package_write_ipk.sigdata do_package_write_rpm.sigdata do_package_write_deb.sigdata do_package_write_tar.sigdata"
133
134function compareSignatures() {
135  MACHINE1=$1
136  MACHINE2=$2
137  PATTERN="$3"
138  PRE_PATTERN=""
139  [ -n "${PATTERN}" ] || PRE_PATTERN="-v"
140  [ -n "${PATTERN}" ] || PATTERN="MACHINE"
141  for TASK in $COMPARE_TASKS; do
142    printf "\n\n === Comparing signatures for task ${TASK} between ${MACHINE1} and ${MACHINE2} ===\n" | tee -a ${OUTPUT}/signatures.${MACHINE2}.${TASK}.log
143    diff ${OUTPUT}/${MACHINE1}/list.M ${OUTPUT}/${MACHINE2}/list.M | grep ${PRE_PATTERN} "${PATTERN}" | grep ${TASK} > ${OUTPUT}/signatures.${MACHINE2}.${TASK}
144    for i in `cat ${OUTPUT}/signatures.${MACHINE2}.${TASK} | sed 's#[^/]*/\([^/]*\)/.*#\1#g' | sort -u | xargs`; do
145      [ -e ${OUTPUT}/${MACHINE1}/*/$i/*${TASK}* ] || echo "INFO: ${i} task ${TASK} doesn't exist in ${MACHINE1}" >&2
146      [ -e ${OUTPUT}/${MACHINE1}/*/$i/*${TASK}* ] || continue
147      [ -e ${OUTPUT}/${MACHINE2}/*/$i/*${TASK}* ] || echo "INFO: ${i} task ${TASK} doesn't exist in ${MACHINE2}" >&2
148      [ -e ${OUTPUT}/${MACHINE2}/*/$i/*${TASK}* ] || continue
149      printf "ERROR: $i different signature for task ${TASK} between ${MACHINE1} and ${MACHINE2}\n";
150      bitbake-diffsigs ${OUTPUT}/${MACHINE1}/*/$i/*${TASK}* ${OUTPUT}/${MACHINE2}/*/$i/*${TASK}*;
151      echo "$i" >> ${OUTPUT}/failed-recipes.log
152      echo
153    done | tee -a ${OUTPUT}/signatures.${MACHINE2}.${TASK}.log
154    # don't create empty files
155    ERRORS=`grep "^ERROR.*" ${OUTPUT}/signatures.${MACHINE2}.${TASK}.log | wc -l`
156    if [ "${ERRORS}" != "0" ] ; then
157      echo "ERROR: ${ERRORS} errors found in ${OUTPUT}/signatures.${MACHINE2}.${TASK}.log"
158      RESULT+=${ERRORS}
159    fi
160  done
161}
162
163function compareMachines() {
164  [ "$#" -ge 2 ] && compareSignatures $1 $2
165  [ "$#" -ge 3 ] && compareSignatures $1 $3 "\(^< all\)\|\(^< x86_64-linux\)\|\(^< i586-linux\)"
166}
167
168if [ "${analyze}" = "Y" ] ; then
169  compareMachines ${machines}
170fi
171
172if [ "${RESULT}" != "0" -a -f ${OUTPUT}/failed-recipes.log ] ; then
173  cat ${OUTPUT}/failed-recipes.log | sort -u >${OUTPUT}/failed-recipes.log.u && mv ${OUTPUT}/failed-recipes.log.u ${OUTPUT}/failed-recipes.log
174  echo "ERROR: ${RESULT} issues were found in these recipes: `cat ${OUTPUT}/failed-recipes.log | xargs`"
175fi
176
177echo "INFO: Output written in: ${OUTPUT}"
178exit ${RESULT}
179