Blame view

scripts/objdiff 2.69 KB
79192ca8e   Jason Cooper   scripts: objdiff:...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #!/bin/bash
  
  # objdiff - a small script for validating that a commit or series of commits
  # didn't change object code.
  #
  # Copyright 2014, Jason Cooper <jason@lakedaemon.net>
  #
  # Licensed under the terms of the GNU GPL version 2
  
  # usage example:
  #
  # $ git checkout COMMIT_A
  # $ <your fancy build command here>
  # $ ./scripts/objdiff record path/to/*.o
  #
  # $ git checkout COMMIT_B
  # $ <your fancy build command here>
  # $ ./scripts/objdiff record path/to/*.o
  #
  # $ ./scripts/objdiff diff COMMIT_A COMMIT_B
  # $
  
  # And to clean up (everything is in .tmp_objdiff/*)
  # $ ./scripts/objdiff clean all
  #
  # Note: 'make mrproper' will also remove .tmp_objdiff
18165efa8   Masahiro Yamada   scripts: objdiff:...
27
  SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
79192ca8e   Jason Cooper   scripts: objdiff:...
28

fd6e12423   Masahiro Yamada   scripts: objdiff:...
29
  if [ -z "$SRCTREE" ]; then
5ab370e91   Masahiro Yamada   scripts: objdiff:...
30
  	echo >&2 "ERROR: Not a git repository."
79192ca8e   Jason Cooper   scripts: objdiff:...
31
32
  	exit 1
  fi
fd6e12423   Masahiro Yamada   scripts: objdiff:...
33
  TMPD=$SRCTREE/.tmp_objdiff
79192ca8e   Jason Cooper   scripts: objdiff:...
34
  usage() {
5ab370e91   Masahiro Yamada   scripts: objdiff:...
35
  	echo >&2 "Usage: $0 <command> <args>"
7fa0e6db3   Masahiro Yamada   scripts: objdiff:...
36
  	echo >&2 "  record    <list of object files or directories>"
5ab370e91   Masahiro Yamada   scripts: objdiff:...
37
38
  	echo >&2 "  diff      <commitA> <commitB>"
  	echo >&2 "  clean     all | <commit>"
79192ca8e   Jason Cooper   scripts: objdiff:...
39
40
  	exit 1
  }
18165efa8   Masahiro Yamada   scripts: objdiff:...
41
42
43
44
45
46
47
48
49
50
51
  get_output_dir() {
  	dir=${1%/*}
  
  	if [ "$dir" = "$1" ]; then
  		dir=.
  	fi
  
  	dir=$(cd $dir; pwd)
  
  	echo $TMPD/$CMT${dir#$SRCTREE}
  }
7fa0e6db3   Masahiro Yamada   scripts: objdiff:...
52
53
54
55
56
57
58
59
60
61
62
  do_objdump() {
  	dir=$(get_output_dir $1)
  	base=${1##*/}
  	dis=$dir/${base%.o}.dis
  
  	[ ! -d "$dir" ] && mkdir -p $dir
  
  	# remove addresses for a cleaner diff
  	# http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
  	$OBJDUMP -D $1 | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dis
  }
79192ca8e   Jason Cooper   scripts: objdiff:...
63
64
65
66
67
68
69
70
  dorecord() {
  	[ $# -eq 0 ] && usage
  
  	FILES="$*"
  
  	CMT="`git rev-parse --short HEAD`"
  
  	OBJDUMP="${CROSS_COMPILE}objdump"
79192ca8e   Jason Cooper   scripts: objdiff:...
71

7fa0e6db3   Masahiro Yamada   scripts: objdiff:...
72
73
74
75
76
77
78
79
80
  	for d in $FILES; do
  		if [ -d "$d" ]; then
  			for f in $(find $d -name '*.o')
  			do
  				do_objdump $f
  			done
  		else
  			do_objdump $d
  		fi
79192ca8e   Jason Cooper   scripts: objdiff:...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  	done
  }
  
  dodiff() {
  	[ $# -ne 2 ] && [ $# -ne 0 ] && usage
  
  	if [ $# -eq 0 ]; then
  		SRC="`git rev-parse --short HEAD^`"
  		DST="`git rev-parse --short HEAD`"
  	else
  		SRC="`git rev-parse --short $1`"
  		DST="`git rev-parse --short $2`"
  	fi
  
  	DIFF="`which colordiff`"
  
  	if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
  		DIFF="`which diff`"
  	fi
  
  	SRCD="$TMPD/$SRC"
  	DSTD="$TMPD/$DST"
  
  	if [ ! -d "$SRCD" ]; then
5ab370e91   Masahiro Yamada   scripts: objdiff:...
105
  		echo >&2 "ERROR: $SRCD doesn't exist"
79192ca8e   Jason Cooper   scripts: objdiff:...
106
107
108
109
  		exit 1
  	fi
  
  	if [ ! -d "$DSTD" ]; then
5ab370e91   Masahiro Yamada   scripts: objdiff:...
110
  		echo >&2 "ERROR: $DSTD doesn't exist"
79192ca8e   Jason Cooper   scripts: objdiff:...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  		exit 1
  	fi
  
  	$DIFF -Nurd $SRCD $DSTD
  }
  
  doclean() {
  	[ $# -eq 0 ] && usage
  	[ $# -gt 1 ] && usage
  
  	if [ "x$1" = "xall" ]; then
  		rm -rf $TMPD/*
  	else
  		CMT="`git rev-parse --short $1`"
  
  		if [ -d "$TMPD/$CMT" ]; then
  			rm -rf $TMPD/$CMT
  		else
5ab370e91   Masahiro Yamada   scripts: objdiff:...
129
  			echo >&2 "$CMT not found"
79192ca8e   Jason Cooper   scripts: objdiff:...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  		fi
  	fi
  }
  
  [ $# -eq 0 ] &&	usage
  
  case "$1" in
  	record)
  		shift
  		dorecord $*
  		;;
  	diff)
  		shift
  		dodiff $*
  		;;
  	clean)
  		shift
  		doclean $*
  		;;
  	*)
5ab370e91   Masahiro Yamada   scripts: objdiff:...
150
  		echo >&2 "Unrecognized command '$1'"
79192ca8e   Jason Cooper   scripts: objdiff:...
151
152
153
  		exit 1
  		;;
  esac