common/bin/difflib.sh

Tue, 03 Jul 2012 16:11:12 -0700

author
erikj
date
Tue, 03 Jul 2012 16:11:12 -0700
changeset 458
c8d320b48626
parent 445
efd26e051e50
permissions
-rw-r--r--

7181504: Update of latest build-infra Makefiles
Reviewed-by: ohair

ohair@425 1 #!/bin/bash
ohair@425 2 #
ohair@425 3 # Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
ohair@425 4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@425 5 #
ohair@425 6 # This code is free software; you can redistribute it and/or modify it
ohair@425 7 # under the terms of the GNU General Public License version 2 only, as
ohair@425 8 # published by the Free Software Foundation.
ohair@425 9 #
ohair@425 10 # This code is distributed in the hope that it will be useful, but WITHOUT
ohair@425 11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@425 12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@425 13 # version 2 for more details (a copy is included in the LICENSE file that
ohair@425 14 # accompanied this code).
ohair@425 15 #
ohair@425 16 # You should have received a copy of the GNU General Public License version
ohair@425 17 # 2 along with this work; if not, write to the Free Software Foundation,
ohair@425 18 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@425 19 #
ohair@425 20 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@425 21 # or visit www.oracle.com if you need additional information or have any
ohair@425 22 # questions.
ohair@425 23 #
ohair@425 24
ohair@425 25 # Simple tool to diff two shared libraries.
ohair@425 26 # Criterias: two shared libraries are considered equal if:
ohair@425 27 # the file sizes are the same AND the symbols outputs from the nm command are equal
ohair@425 28
ohair@425 29 if [ $# -lt 2 ]
ohair@425 30 then
ohair@425 31 echo "Diff two shared libs. Return codes:"
ohair@425 32 echo "0 - no diff"
ohair@425 33 echo "1 - Identical symbols AND size, BUT not bytewise identical"
ohair@425 34 echo "2 - Identical symbols BUT NEW size"
ohair@425 35 echo "3 - Differences, content BUT SAME size"
ohair@425 36 echo "4 - Differences, content AND size"
ohair@425 37 echo "10 - Could not perform diff"
ohair@425 38 echo "Use 'quiet' to disable any output."
ohair@425 39 echo "Syntax: $0 file1 file2 [quiet]"
ohair@425 40 exit 10
ohair@425 41 fi
ohair@425 42
ohair@425 43 if [ ! -f $1 ]
ohair@425 44 then
ohair@425 45 echo $1 does not exist
ohair@425 46 exit 10
ohair@425 47 fi
ohair@425 48
ohair@425 49 if [ ! -f $2 ]
ohair@425 50 then
ohair@425 51 echo $2 does not exist
ohair@425 52 exit 10
ohair@425 53 fi
ohair@425 54
ohair@425 55 if [ "`uname`" == "SunOS" ]; then
erikj@458 56 if [ -f "`which gnm`" ]; then
erikj@458 57 NM=gnm
erikj@458 58 # Jonas 2012-05-29: solaris native nm produces radically different output than gnm
erikj@458 59 # so if using that...we need different filter than "cut -f 2-"
erikj@458 60 #
erikj@458 61 elif [ -f "`which nm`" ]; then
erikj@458 62 NM=nm
erikj@458 63 else
erikj@458 64 echo "No nm command found"
erikj@458 65 exit 10
erikj@458 66 fi
erikj@445 67 LDD=ldd
ohair@425 68 elif [ $OSTYPE == "cygwin" ]; then
ohair@425 69 NM="$VS100COMNTOOLS/../../VC/bin/amd64/dumpbin.exe"
ohair@425 70 NM_ARGS=/exports
erikj@445 71 LDD=
erikj@445 72 elif [ "`uname`" == "Darwin" ]; then
erikj@445 73 NM=nm
erikj@445 74 LDD="otool -L"
ohair@425 75 else
ohair@425 76 NM=nm
erikj@445 77 LDD=ldd
ohair@425 78 fi
ohair@425 79
ohair@425 80 # Should the differences be viewed?
ohair@425 81 VIEW=
ohair@425 82 # You can do export DIFF=meld to view
ohair@425 83 # any differences using meld instead.
ohair@425 84 if [ -n "$DIFF" ]; then
ohair@425 85 DIFF="$DIFF"
ohair@425 86 else
ohair@425 87 DIFF=diff
ohair@425 88 fi
ohair@425 89 OLD=$(cd $(dirname $1) && pwd)/$(basename $1)
ohair@425 90 NEW=$(cd $(dirname $2) && pwd)/$(basename $2)
ohair@425 91
erikj@458 92 OLD_SIZE=$(ls -l "$OLD" | awk '{ print $5 }')
erikj@458 93 NEW_SIZE=$(ls -l "$NEW" | awk '{ print $5 }')
ohair@425 94
ohair@425 95 if [ $# -gt 3 ]
ohair@425 96 then
ohair@425 97 ROOT1=$(cd $3 && pwd)
ohair@425 98 ROOT2=$(cd $4 && pwd)
ohair@425 99 OLD_NAME=$(echo $OLD | sed "s|$ROOT1/||")
ohair@425 100 NEW_NAME=$(echo $NEW | sed "s|$ROOT2/||")
ohair@425 101 if [ "x$5" == "xview" ]; then VIEW=view; fi
ohair@425 102 else
ohair@425 103 ROOT1=$(dirname $OLD)/
ohair@425 104 ROOT2=$(dirname $NEW)/
ohair@425 105 OLD_NAME=$OLD
ohair@425 106 NEW_NAME=$NEW
ohair@425 107 if [ "x$3" == "xview" ]; then VIEW=view; fi
ohair@425 108 fi
ohair@425 109
ohair@425 110 OLD_SUFFIX="${OLD##*.}"
ohair@425 111 NEW_SUFFIX="${NEW##*.}"
ohair@425 112 if [ "$OLD_SUFFIX" != "$NEW_SUFFIX" ]; then
ohair@425 113 echo The files do not have the same suffix type!
ohair@425 114 exit 10
ohair@425 115 fi
ohair@425 116
ohair@425 117 if [ "$OLD_SUFFIX" != "so" ] && [ "$OLD_SUFFIX" != "dylib" ] && [ "$OLD_SUFFIX" != "dll" ]; then
ohair@425 118 echo The files have to be .so, .dylib or .dll! They are $OLD_SUFFIX
ohair@425 119 exit 10
ohair@425 120 fi
ohair@425 121
ohair@425 122 TYPE="$OLD_SUFFIX"
ohair@425 123
ohair@425 124 if cmp $OLD $NEW > /dev/null
ohair@425 125 then
ohair@425 126 # The files were bytewise identical.
ohair@425 127 echo Identical: $OLD_NAME
ohair@425 128 exit 0
ohair@425 129 fi
ohair@425 130
erikj@458 131 OLD_SYMBOLS=$COMPARE_ROOT/nm.$OLD_NAME.old
erikj@458 132 NEW_SYMBOLS=$COMPARE_ROOT/nm.$NEW_NAME.new
ohair@425 133
ohair@425 134 mkdir -p $(dirname $OLD_SYMBOLS)
ohair@425 135 mkdir -p $(dirname $NEW_SYMBOLS)
ohair@425 136
ohair@425 137 if [ $OSTYPE == "cygwin" ]; then
ohair@425 138 "$NM" $NM_ARGS $OLD | grep " = " > $OLD_SYMBOLS
ohair@425 139 "$NM" $NM_ARGS $NEW | grep " = " > $NEW_SYMBOLS
ohair@425 140 "$NM" $NM_ARGS $OLD > $OLD_SYMBOLS.full
ohair@425 141 "$NM" $NM_ARGS $NEW > $NEW_SYMBOLS.full
ohair@425 142 else
ohair@425 143 # Strip the addresses, just compare the ordering of the symbols.
ohair@425 144 $NM $OLD | cut -f 2- -d ' ' > $OLD_SYMBOLS
ohair@425 145 $NM $NEW | cut -f 2- -d ' ' > $NEW_SYMBOLS
ohair@425 146 # But store the full information for easy diff access.
ohair@425 147 $NM $OLD > $OLD_SYMBOLS.full
ohair@425 148 $NM $NEW > $NEW_SYMBOLS.full
ohair@425 149 fi
ohair@425 150
ohair@425 151 DIFFS=$(LANG=C diff $OLD_SYMBOLS $NEW_SYMBOLS)
ohair@425 152
ohair@425 153 RESULT=0
ohair@425 154
erikj@445 155 if [ "${LDD}" ]
erikj@445 156 then
erikj@445 157 NAME=`basename $OLD`
erikj@445 158 TMP=$COMPARE_ROOT/ldd/ldd.${NAME}
erikj@445 159 rm -rf "${TMP}"
erikj@445 160 mkdir -p "${TMP}"
erikj@445 161
erikj@445 162 (cd "${TMP}" && cp $OLD . && ${LDD} ${NAME} | awk '{ print $1;}' | sort | tee dep.old | uniq > dep.uniq.old)
erikj@445 163 (cd "${TMP}" && cp $NEW . && ${LDD} ${NAME} | awk '{ print $1;}' | sort | tee dep.new | uniq > dep.uniq.new)
erikj@445 164 (cd "${TMP}" && rm -f ${NAME})
erikj@445 165
erikj@445 166 DIFFS_DEP=$(LANG=C diff "${TMP}/dep.old" "${TMP}/dep.new")
erikj@445 167 DIFFS_UNIQ_DEP=$(LANG=C diff "${TMP}/dep.uniq.old" "${TMP}/dep.uniq.new")
erikj@445 168
erikj@445 169 DEP_MSG=
erikj@445 170 if [ -z "${DIFFS_UNIQ_DEP}" -a -z "${DIFFS_DEP}" ]; then
erikj@445 171 DEP_MSG="Identical dependencies"
erikj@445 172 elif [ -z "${DIFFS_UNIQ_DEP}" ]; then
erikj@445 173 DEP_MSG="Redundant duplicate dependencies added"
erikj@445 174 RES=1
erikj@445 175 else
erikj@445 176 DEP_MSG="DIFFERENT dependencies"
erikj@445 177 RES=1
erikj@445 178 fi
erikj@445 179 fi
erikj@445 180
ohair@425 181 if [ -n "$DIFFS" ]; then
ohair@425 182 if [ $OLD_SIZE -ne $NEW_SIZE ]
ohair@425 183 then
erikj@445 184 echo Differences, content AND size : $DEP_MSG : $OLD_NAME
ohair@425 185 RESULT=4
ohair@425 186 else
erikj@445 187 echo Differences, content BUT SAME size: $DEP_MSG : $OLD_NAME
ohair@425 188 RESULT=3
ohair@425 189 fi
ohair@425 190 if [ "x$VIEW" == "xview" ]; then
ohair@425 191 LANG=C $DIFF $OLD_SYMBOLS $NEW_SYMBOLS
ohair@425 192 fi
ohair@425 193 else
ohair@425 194 if [ $OLD_SIZE -ne $NEW_SIZE ]
ohair@425 195 then
erikj@445 196 echo Identical symbols BUT NEW size : $DEP_MSG : $OLD_NAME
ohair@425 197 RESULT=2
ohair@425 198 else
erikj@445 199 echo Identical symbols AND size, BUT not bytewise identical: $DEP_MSG : $OLD_NAME
ohair@425 200 RESULT=1
ohair@425 201 fi
ohair@425 202 fi
ohair@425 203
ohair@425 204 exit $RESULT
ohair@425 205
ohair@425 206
ohair@425 207

mercurial