blob: 499eb4bc1e8daed28474c4fbe68a66aea6a645d7 [file] [log] [blame]
Jason Cooper79192ca2014-04-07 18:30:29 +00001#!/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 Yamada18165ef2014-06-09 11:16:37 +090028SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
Jason Cooper79192ca2014-04-07 18:30:29 +000029
Masahiro Yamadafd6e1242014-06-09 11:16:34 +090030if [ -z "$SRCTREE" ]; then
Masahiro Yamada5ab370e2014-06-09 11:16:35 +090031 echo >&2 "ERROR: Not a git repository."
Jason Cooper79192ca2014-04-07 18:30:29 +000032 exit 1
33fi
34
Masahiro Yamadafd6e1242014-06-09 11:16:34 +090035TMPD=$SRCTREE/.tmp_objdiff
36
Jason Cooper79192ca2014-04-07 18:30:29 +000037usage() {
Masahiro Yamada5ab370e2014-06-09 11:16:35 +090038 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>"
Jason Cooper79192ca2014-04-07 18:30:29 +000042 exit 1
43}
44
Masahiro Yamada18165ef2014-06-09 11:16:37 +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
Jason Cooper79192ca2014-04-07 18:30:29 +000057dorecord() {
58 [ $# -eq 0 ] && usage
59
60 FILES="$*"
61
62 CMT="`git rev-parse --short HEAD`"
63
64 OBJDUMP="${CROSS_COMPILE}objdump"
Jason Cooper79192ca2014-04-07 18:30:29 +000065
Jason Cooper79192ca2014-04-07 18:30:29 +000066 for f in $FILES; do
Masahiro Yamada18165ef2014-06-09 11:16:37 +090067 dir=$(get_output_dir $f)
Jason Cooper79192ca2014-04-07 18:30:29 +000068 bn="${f##*/}"
69
Masahiro Yamada18165ef2014-06-09 11:16:37 +090070 [ ! -d "$dir" ] && mkdir -p $dir
Jason Cooper79192ca2014-04-07 18:30:29 +000071
72 # remove addresses for a more clear diff
73 # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
Masahiro Yamada18165ef2014-06-09 11:16:37 +090074 $OBJDUMP -D $f | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dir/$bn
Jason Cooper79192ca2014-04-07 18:30:29 +000075 done
76}
77
78dodiff() {
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
Masahiro Yamada5ab370e2014-06-09 11:16:35 +090099 echo >&2 "ERROR: $SRCD doesn't exist"
Jason Cooper79192ca2014-04-07 18:30:29 +0000100 exit 1
101 fi
102
103 if [ ! -d "$DSTD" ]; then
Masahiro Yamada5ab370e2014-06-09 11:16:35 +0900104 echo >&2 "ERROR: $DSTD doesn't exist"
Jason Cooper79192ca2014-04-07 18:30:29 +0000105 exit 1
106 fi
107
108 $DIFF -Nurd $SRCD $DSTD
109}
110
111doclean() {
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
Masahiro Yamada5ab370e2014-06-09 11:16:35 +0900123 echo >&2 "$CMT not found"
Jason Cooper79192ca2014-04-07 18:30:29 +0000124 fi
125 fi
126}
127
128[ $# -eq 0 ] && usage
129
130case "$1" in
131 record)
132 shift
133 dorecord $*
134 ;;
135 diff)
136 shift
137 dodiff $*
138 ;;
139 clean)
140 shift
141 doclean $*
142 ;;
143 *)
Masahiro Yamada5ab370e2014-06-09 11:16:35 +0900144 echo >&2 "Unrecognized command '$1'"
Jason Cooper79192ca2014-04-07 18:30:29 +0000145 exit 1
146 ;;
147esac