]> git.karo-electronics.de Git - karo-tx-linux.git/blob - scripts/objdiff
scripts: objdiff: improve path flexibility for record command
[karo-tx-linux.git] / scripts / objdiff
1 #!/bin/bash
2
3 # objdiff - a small script for validating that a commit or series of commits
4 # didn't change object code.
5 #
6 # Copyright 2014, Jason Cooper <jason@lakedaemon.net>
7 #
8 # Licensed under the terms of the GNU GPL version 2
9
10 # usage example:
11 #
12 # $ git checkout COMMIT_A
13 # $ <your fancy build command here>
14 # $ ./scripts/objdiff record path/to/*.o
15 #
16 # $ git checkout COMMIT_B
17 # $ <your fancy build command here>
18 # $ ./scripts/objdiff record path/to/*.o
19 #
20 # $ ./scripts/objdiff diff COMMIT_A COMMIT_B
21 # $
22
23 # And to clean up (everything is in .tmp_objdiff/*)
24 # $ ./scripts/objdiff clean all
25 #
26 # Note: 'make mrproper' will also remove .tmp_objdiff
27
28 SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
29
30 if [ -z "$SRCTREE" ]; then
31         echo >&2 "ERROR: Not a git repository."
32         exit 1
33 fi
34
35 TMPD=$SRCTREE/.tmp_objdiff
36
37 usage() {
38         echo >&2 "Usage: $0 <command> <args>"
39         echo >&2 "  record    <list of object files>"
40         echo >&2 "  diff      <commitA> <commitB>"
41         echo >&2 "  clean     all | <commit>"
42         exit 1
43 }
44
45 get_output_dir() {
46         dir=${1%/*}
47
48         if [ "$dir" = "$1" ]; then
49                 dir=.
50         fi
51
52         dir=$(cd $dir; pwd)
53
54         echo $TMPD/$CMT${dir#$SRCTREE}
55 }
56
57 dorecord() {
58         [ $# -eq 0 ] && usage
59
60         FILES="$*"
61
62         CMT="`git rev-parse --short HEAD`"
63
64         OBJDUMP="${CROSS_COMPILE}objdump"
65
66         for f in $FILES; do
67                 dir=$(get_output_dir $f)
68                 bn="${f##*/}"
69
70                 [ ! -d "$dir" ] && mkdir -p $dir
71
72                 # remove addresses for a more clear diff
73                 # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
74                 $OBJDUMP -D $f | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dir/$bn
75         done
76 }
77
78 dodiff() {
79         [ $# -ne 2 ] && [ $# -ne 0 ] && usage
80
81         if [ $# -eq 0 ]; then
82                 SRC="`git rev-parse --short HEAD^`"
83                 DST="`git rev-parse --short HEAD`"
84         else
85                 SRC="`git rev-parse --short $1`"
86                 DST="`git rev-parse --short $2`"
87         fi
88
89         DIFF="`which colordiff`"
90
91         if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
92                 DIFF="`which diff`"
93         fi
94
95         SRCD="$TMPD/$SRC"
96         DSTD="$TMPD/$DST"
97
98         if [ ! -d "$SRCD" ]; then
99                 echo >&2 "ERROR: $SRCD doesn't exist"
100                 exit 1
101         fi
102
103         if [ ! -d "$DSTD" ]; then
104                 echo >&2 "ERROR: $DSTD doesn't exist"
105                 exit 1
106         fi
107
108         $DIFF -Nurd $SRCD $DSTD
109 }
110
111 doclean() {
112         [ $# -eq 0 ] && usage
113         [ $# -gt 1 ] && usage
114
115         if [ "x$1" = "xall" ]; then
116                 rm -rf $TMPD/*
117         else
118                 CMT="`git rev-parse --short $1`"
119
120                 if [ -d "$TMPD/$CMT" ]; then
121                         rm -rf $TMPD/$CMT
122                 else
123                         echo >&2 "$CMT not found"
124                 fi
125         fi
126 }
127
128 [ $# -eq 0 ] && usage
129
130 case "$1" in
131         record)
132                 shift
133                 dorecord $*
134                 ;;
135         diff)
136                 shift
137                 dodiff $*
138                 ;;
139         clean)
140                 shift
141                 doclean $*
142                 ;;
143         *)
144                 echo >&2 "Unrecognized command '$1'"
145                 exit 1
146                 ;;
147 esac