Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 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 | |
Masahiro Yamada | 18165ef | 2014-06-09 11:16:37 +0900 | [diff] [blame^] | 28 | SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd) |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 29 | |
Masahiro Yamada | fd6e124 | 2014-06-09 11:16:34 +0900 | [diff] [blame] | 30 | if [ -z "$SRCTREE" ]; then |
Masahiro Yamada | 5ab370e | 2014-06-09 11:16:35 +0900 | [diff] [blame] | 31 | echo >&2 "ERROR: Not a git repository." |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 32 | exit 1 |
| 33 | fi |
| 34 | |
Masahiro Yamada | fd6e124 | 2014-06-09 11:16:34 +0900 | [diff] [blame] | 35 | TMPD=$SRCTREE/.tmp_objdiff |
| 36 | |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 37 | usage() { |
Masahiro Yamada | 5ab370e | 2014-06-09 11:16:35 +0900 | [diff] [blame] | 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>" |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 42 | exit 1 |
| 43 | } |
| 44 | |
Masahiro Yamada | 18165ef | 2014-06-09 11:16:37 +0900 | [diff] [blame^] | 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 | |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 57 | dorecord() { |
| 58 | [ $# -eq 0 ] && usage |
| 59 | |
| 60 | FILES="$*" |
| 61 | |
| 62 | CMT="`git rev-parse --short HEAD`" |
| 63 | |
| 64 | OBJDUMP="${CROSS_COMPILE}objdump" |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 65 | |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 66 | for f in $FILES; do |
Masahiro Yamada | 18165ef | 2014-06-09 11:16:37 +0900 | [diff] [blame^] | 67 | dir=$(get_output_dir $f) |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 68 | bn="${f##*/}" |
| 69 | |
Masahiro Yamada | 18165ef | 2014-06-09 11:16:37 +0900 | [diff] [blame^] | 70 | [ ! -d "$dir" ] && mkdir -p $dir |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 71 | |
| 72 | # remove addresses for a more clear diff |
| 73 | # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and |
Masahiro Yamada | 18165ef | 2014-06-09 11:16:37 +0900 | [diff] [blame^] | 74 | $OBJDUMP -D $f | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dir/$bn |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 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 |
Masahiro Yamada | 5ab370e | 2014-06-09 11:16:35 +0900 | [diff] [blame] | 99 | echo >&2 "ERROR: $SRCD doesn't exist" |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | if [ ! -d "$DSTD" ]; then |
Masahiro Yamada | 5ab370e | 2014-06-09 11:16:35 +0900 | [diff] [blame] | 104 | echo >&2 "ERROR: $DSTD doesn't exist" |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 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 |
Masahiro Yamada | 5ab370e | 2014-06-09 11:16:35 +0900 | [diff] [blame] | 123 | echo >&2 "$CMT not found" |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 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 | *) |
Masahiro Yamada | 5ab370e | 2014-06-09 11:16:35 +0900 | [diff] [blame] | 144 | echo >&2 "Unrecognized command '$1'" |
Jason Cooper | 79192ca | 2014-04-07 18:30:29 +0000 | [diff] [blame] | 145 | exit 1 |
| 146 | ;; |
| 147 | esac |