common/bin/difflib.sh

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

mercurial