blob: cc6736553958f7d0c86df2539a341d3f77c4a94c [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 Yamadafd6e1242014-06-09 11:16:34 +090028SRCTREE=$(git rev-parse --show-toplevel 2>/dev/null)
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
45dorecord() {
46 [ $# -eq 0 ] && usage
47
48 FILES="$*"
49
50 CMT="`git rev-parse --short HEAD`"
51
52 OBJDUMP="${CROSS_COMPILE}objdump"
53 OBJDIFFD="$TMPD/$CMT"
54
55 [ ! -d "$OBJDIFFD" ] && mkdir -p "$OBJDIFFD"
56
57 for f in $FILES; do
58 dn="${f%/*}"
59 bn="${f##*/}"
60
61 [ ! -d "$OBJDIFFD/$dn" ] && mkdir -p "$OBJDIFFD/$dn"
62
63 # remove addresses for a more clear diff
64 # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
65 $OBJDUMP -D "$f" | sed "s/^[[:space:]]\+[0-9a-f]\+//" \
66 >"$OBJDIFFD/$dn/$bn"
67 done
68}
69
70dodiff() {
71 [ $# -ne 2 ] && [ $# -ne 0 ] && usage
72
73 if [ $# -eq 0 ]; then
74 SRC="`git rev-parse --short HEAD^`"
75 DST="`git rev-parse --short HEAD`"
76 else
77 SRC="`git rev-parse --short $1`"
78 DST="`git rev-parse --short $2`"
79 fi
80
81 DIFF="`which colordiff`"
82
83 if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
84 DIFF="`which diff`"
85 fi
86
87 SRCD="$TMPD/$SRC"
88 DSTD="$TMPD/$DST"
89
90 if [ ! -d "$SRCD" ]; then
Masahiro Yamada5ab370e2014-06-09 11:16:35 +090091 echo >&2 "ERROR: $SRCD doesn't exist"
Jason Cooper79192ca2014-04-07 18:30:29 +000092 exit 1
93 fi
94
95 if [ ! -d "$DSTD" ]; then
Masahiro Yamada5ab370e2014-06-09 11:16:35 +090096 echo >&2 "ERROR: $DSTD doesn't exist"
Jason Cooper79192ca2014-04-07 18:30:29 +000097 exit 1
98 fi
99
100 $DIFF -Nurd $SRCD $DSTD
101}
102
103doclean() {
104 [ $# -eq 0 ] && usage
105 [ $# -gt 1 ] && usage
106
107 if [ "x$1" = "xall" ]; then
108 rm -rf $TMPD/*
109 else
110 CMT="`git rev-parse --short $1`"
111
112 if [ -d "$TMPD/$CMT" ]; then
113 rm -rf $TMPD/$CMT
114 else
Masahiro Yamada5ab370e2014-06-09 11:16:35 +0900115 echo >&2 "$CMT not found"
Jason Cooper79192ca2014-04-07 18:30:29 +0000116 fi
117 fi
118}
119
120[ $# -eq 0 ] && usage
121
122case "$1" in
123 record)
124 shift
125 dorecord $*
126 ;;
127 diff)
128 shift
129 dodiff $*
130 ;;
131 clean)
132 shift
133 doclean $*
134 ;;
135 *)
Masahiro Yamada5ab370e2014-06-09 11:16:35 +0900136 echo >&2 "Unrecognized command '$1'"
Jason Cooper79192ca2014-04-07 18:30:29 +0000137 exit 1
138 ;;
139esac