blob: 62e51dae2138db450555f3d15dbd14cc53c53652 [file] [log] [blame]
Masahiro Yamada9cf2a452014-04-15 13:29:00 +09001#!/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
Masahiro Yamada537cda02014-08-05 15:56:45 +090028SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090029
Masahiro Yamada537cda02014-08-05 15:56:45 +090030if [ -z "$SRCTREE" ]; then
31 echo >&2 "ERROR: Not a git repository."
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090032 exit 1
33fi
34
Masahiro Yamada537cda02014-08-05 15:56:45 +090035TMPD=$SRCTREE/.tmp_objdiff
36
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090037usage() {
Masahiro Yamada537cda02014-08-05 15:56:45 +090038 echo >&2 "Usage: $0 <command> <args>"
39 echo >&2 " record <list of object files or directories>"
40 echo >&2 " diff <commitA> <commitB>"
41 echo >&2 " clean all | <commit>"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090042 exit 1
43}
44
Masahiro Yamada537cda02014-08-05 15:56:45 +090045get_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
57do_objdump() {
58 dir=$(get_output_dir $1)
59 base=${1##*/}
60 dis=$dir/${base%.o}.dis
61
62 [ ! -d "$dir" ] && mkdir -p $dir
63
64 # remove addresses for a cleaner diff
65 # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
66 $OBJDUMP -D $1 | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dis
67}
68
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090069dorecord() {
70 [ $# -eq 0 ] && usage
71
72 FILES="$*"
73
74 CMT="`git rev-parse --short HEAD`"
75
76 OBJDUMP="${CROSS_COMPILE}objdump"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090077
Masahiro Yamada537cda02014-08-05 15:56:45 +090078 for d in $FILES; do
79 if [ -d "$d" ]; then
80 for f in $(find $d -name '*.o')
81 do
82 do_objdump $f
83 done
84 else
85 do_objdump $d
86 fi
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090087 done
88}
89
90dodiff() {
91 [ $# -ne 2 ] && [ $# -ne 0 ] && usage
92
93 if [ $# -eq 0 ]; then
94 SRC="`git rev-parse --short HEAD^`"
95 DST="`git rev-parse --short HEAD`"
96 else
97 SRC="`git rev-parse --short $1`"
98 DST="`git rev-parse --short $2`"
99 fi
100
101 DIFF="`which colordiff`"
102
103 if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
104 DIFF="`which diff`"
105 fi
106
107 SRCD="$TMPD/$SRC"
108 DSTD="$TMPD/$DST"
109
110 if [ ! -d "$SRCD" ]; then
Masahiro Yamada537cda02014-08-05 15:56:45 +0900111 echo >&2 "ERROR: $SRCD doesn't exist"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900112 exit 1
113 fi
114
115 if [ ! -d "$DSTD" ]; then
Masahiro Yamada537cda02014-08-05 15:56:45 +0900116 echo >&2 "ERROR: $DSTD doesn't exist"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900117 exit 1
118 fi
119
120 $DIFF -Nurd $SRCD $DSTD
121}
122
123doclean() {
124 [ $# -eq 0 ] && usage
125 [ $# -gt 1 ] && usage
126
127 if [ "x$1" = "xall" ]; then
128 rm -rf $TMPD/*
129 else
130 CMT="`git rev-parse --short $1`"
131
132 if [ -d "$TMPD/$CMT" ]; then
133 rm -rf $TMPD/$CMT
134 else
Masahiro Yamada537cda02014-08-05 15:56:45 +0900135 echo >&2 "$CMT not found"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900136 fi
137 fi
138}
139
140[ $# -eq 0 ] && usage
141
142case "$1" in
143 record)
144 shift
145 dorecord $*
146 ;;
147 diff)
148 shift
149 dodiff $*
150 ;;
151 clean)
152 shift
153 doclean $*
154 ;;
155 *)
Masahiro Yamada537cda02014-08-05 15:56:45 +0900156 echo >&2 "Unrecognized command '$1'"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900157 exit 1
158 ;;
159esac