common/bin/difflib.sh

Tue, 18 Sep 2012 11:29:16 -0700

author
ohair
date
Tue, 18 Sep 2012 11:29:16 -0700
changeset 478
2ba6f4da4bf3
parent 458
c8d320b48626
permissions
-rw-r--r--

7197849: Update new build-infra makefiles
Reviewed-by: ihse, erikj, ohrstrom, tbell

     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 #
    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
    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
    43 if [ ! -f $1 ]
    44 then
    45   echo $1 does not exist
    46   exit 10
    47 fi
    49 if [ ! -f $2 ]
    50 then
    51   echo $2 does not exist
    52   exit 10
    53 fi
    55 if [ "`uname`" == "SunOS" ]; then
    56     if [ -f "`which gnm`" ]; then
    57         NM=gnm
    58 # Jonas 2012-05-29: solaris native nm produces radically different output than gnm
    59 #                   so if using that...we need different filter than "cut -f 2-"
    60 #
    61     elif [ -f "`which nm`" ]; then
    62         NM=nm
    63     else
    64         echo "No nm command found"
    65         exit 10
    66     fi
    67     LDD=ldd
    68 elif [ $OSTYPE == "cygwin" ]; then
    69     NM="$VS100COMNTOOLS/../../VC/bin/amd64/dumpbin.exe"
    70     NM_ARGS=/exports
    71     LDD=
    72 elif [ "`uname`" == "Darwin" ]; then
    73     NM=nm
    74     LDD="otool -L"
    75 else
    76     NM=nm
    77     LDD=ldd
    78 fi
    80 # Should the differences be viewed?
    81 VIEW=
    82 # You can do export DIFF=meld to view
    83 # any differences using meld instead.
    84 if [ -n "$DIFF" ]; then
    85     DIFF="$DIFF"
    86 else
    87     DIFF=diff
    88 fi
    89 OLD=$(cd $(dirname $1) && pwd)/$(basename $1)
    90 NEW=$(cd $(dirname $2) && pwd)/$(basename $2)
    92 OLD_SIZE=$(ls -l "$OLD" | awk '{ print $5 }')
    93 NEW_SIZE=$(ls -l "$NEW" | awk '{ print $5 }')
    95 if [ $# -gt 3 ]
    96 then
    97     ROOT1=$(cd $3 && pwd)
    98     ROOT2=$(cd $4 && pwd)
    99     OLD_NAME=$(echo $OLD | sed "s|$ROOT1/||")
   100     NEW_NAME=$(echo $NEW | sed "s|$ROOT2/||")
   101     if [ "x$5" == "xview" ]; then VIEW=view; fi
   102 else
   103     ROOT1=$(dirname $OLD)/
   104     ROOT2=$(dirname $NEW)/
   105     OLD_NAME=$OLD
   106     NEW_NAME=$NEW
   107     if [ "x$3" == "xview" ]; then VIEW=view; fi
   108 fi
   110 OLD_SUFFIX="${OLD##*.}"
   111 NEW_SUFFIX="${NEW##*.}"
   112 if [ "$OLD_SUFFIX" != "$NEW_SUFFIX" ]; then
   113     echo The files do not have the same suffix type!
   114     exit 10
   115 fi
   117 if [ "$OLD_SUFFIX" != "so" ] && [ "$OLD_SUFFIX" != "dylib" ] && [ "$OLD_SUFFIX" != "dll" ]; then
   118     echo The files have to be .so, .dylib or .dll! They are $OLD_SUFFIX
   119     exit 10
   120 fi
   122 TYPE="$OLD_SUFFIX"
   124 if cmp $OLD $NEW > /dev/null
   125 then
   126     # The files were bytewise identical.
   127     echo Identical: $OLD_NAME
   128     exit 0
   129 fi
   131 OLD_SYMBOLS=$COMPARE_ROOT/nm.$OLD_NAME.old
   132 NEW_SYMBOLS=$COMPARE_ROOT/nm.$NEW_NAME.new
   134 mkdir -p $(dirname $OLD_SYMBOLS)
   135 mkdir -p $(dirname $NEW_SYMBOLS)
   137 if [ $OSTYPE == "cygwin" ]; then
   138     "$NM" $NM_ARGS $OLD | grep " = " > $OLD_SYMBOLS
   139     "$NM" $NM_ARGS $NEW | grep " = " > $NEW_SYMBOLS
   140     "$NM" $NM_ARGS $OLD > $OLD_SYMBOLS.full
   141     "$NM" $NM_ARGS $NEW > $NEW_SYMBOLS.full
   142 else
   143     # Strip the addresses, just compare the ordering of the symbols.
   144     $NM $OLD | cut -f 2- -d ' ' > $OLD_SYMBOLS
   145     $NM $NEW | cut -f 2- -d ' ' > $NEW_SYMBOLS
   146     # But store the full information for easy diff access.
   147     $NM $OLD  > $OLD_SYMBOLS.full
   148     $NM $NEW  > $NEW_SYMBOLS.full
   149 fi
   151 DIFFS=$(LANG=C diff $OLD_SYMBOLS $NEW_SYMBOLS)
   153 RESULT=0
   155 if [ "${LDD}" ]
   156 then
   157     NAME=`basename $OLD`
   158     TMP=$COMPARE_ROOT/ldd/ldd.${NAME}
   159     rm -rf "${TMP}"
   160     mkdir -p "${TMP}"
   162     (cd "${TMP}" && cp $OLD . && ${LDD} ${NAME} | awk '{ print $1;}' | sort | tee dep.old | uniq > dep.uniq.old)
   163     (cd "${TMP}" && cp $NEW . && ${LDD} ${NAME} | awk '{ print $1;}' | sort | tee dep.new | uniq > dep.uniq.new)
   164     (cd "${TMP}" && rm -f ${NAME})
   166     DIFFS_DEP=$(LANG=C diff "${TMP}/dep.old" "${TMP}/dep.new")
   167     DIFFS_UNIQ_DEP=$(LANG=C diff "${TMP}/dep.uniq.old" "${TMP}/dep.uniq.new")
   169     DEP_MSG=
   170     if [ -z "${DIFFS_UNIQ_DEP}" -a -z "${DIFFS_DEP}" ]; then
   171        DEP_MSG="Identical dependencies"
   172     elif [ -z "${DIFFS_UNIQ_DEP}" ]; then
   173        DEP_MSG="Redundant duplicate dependencies added"
   174        RES=1
   175     else
   176        DEP_MSG="DIFFERENT dependencies"
   177        RES=1
   178     fi
   179 fi
   181 if [ -n "$DIFFS" ]; then
   182    if [ $OLD_SIZE -ne $NEW_SIZE ]
   183    then
   184        echo Differences, content AND size     : $DEP_MSG : $OLD_NAME 
   185        RESULT=4
   186    else
   187        echo Differences, content BUT SAME size: $DEP_MSG : $OLD_NAME 
   188        RESULT=3
   189    fi
   190    if [ "x$VIEW" == "xview" ]; then
   191        LANG=C $DIFF $OLD_SYMBOLS $NEW_SYMBOLS
   192    fi
   193 else
   194    if [ $OLD_SIZE -ne $NEW_SIZE ]
   195    then
   196        echo Identical symbols BUT NEW size    : $DEP_MSG : $OLD_NAME 
   197        RESULT=2
   198    else
   199        echo Identical symbols AND size, BUT not bytewise identical: $DEP_MSG : $OLD_NAME 
   200        RESULT=1
   201    fi
   202 fi
   204 exit $RESULT

mercurial