gdb/
[binutils-gdb.git] / gdb / contrib / cc-with-tweaks.sh
1 #! /bin/sh
2 # Wrapper around gcc to tweak the output in various ways when running
3 # the testsuite.
4
5 # Copyright (C) 2010-2012 Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 # This program requires gdb and objcopy in addition to gcc.
20 # The default values are gdb from the build tree and objcopy from $PATH.
21 # They may be overridden by setting environment variables GDB and OBJCOPY
22 # respectively.
23 # We assume the current directory is either $obj/gdb or $obj/gdb/testsuite.
24 #
25 # Example usage:
26 #
27 # bash$ cd $objdir/gdb/testsuite
28 # bash$ runtest \
29 # CC_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
30 # CXX_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
31 #
32 # For documentation on index files: info -f gdb.info -n "Index Files"
33 # For information about 'dwz', see the announcement:
34 # http://gcc.gnu.org/ml/gcc/2012-04/msg00686.html
35 # (More documentation is to come.)
36
37 # ARGS determine what is done. They can be:
38 # -z compress using dwz
39 # -m compress using dwz -m
40 # -i make an index
41 # If nothing is given, no changes are made
42
43 myname=cc-with-tweaks.sh
44
45 if [ -z "$GDB" ]
46 then
47 if [ -f ./gdb ]
48 then
49 GDB="./gdb"
50 elif [ -f ../gdb ]
51 then
52 GDB="../gdb"
53 elif [ -f ../../gdb ]
54 then
55 GDB="../../gdb"
56 else
57 echo "$myname: unable to find usable gdb" >&2
58 exit 1
59 fi
60 fi
61
62 OBJCOPY=${OBJCOPY:-objcopy}
63
64 DWZ=${DWZ:-dwz}
65
66 have_link=unknown
67 next_is_output_file=no
68 output_file=a.out
69
70 want_index=false
71 want_dwz=false
72 want_multi=false
73
74 while [ $# -gt 0 ]; do
75 case "$1" in
76 -z) want_dwz=true ;;
77 -i) want_index=true ;;
78 -m) want_multi=true ;;
79 *) break ;;
80 esac
81 shift
82 done
83
84 for arg in "$@"
85 do
86 if [ "$next_is_output_file" = "yes" ]
87 then
88 output_file="$arg"
89 next_is_output_file=no
90 continue
91 fi
92
93 # Poor man's gcc argument parser.
94 # We don't need to handle all arguments, we just need to know if we're
95 # doing a link and what the output file is.
96 # It's not perfect, but it seems to work well enough for the task at hand.
97 case "$arg" in
98 "-c") have_link=no ;;
99 "-E") have_link=no ;;
100 "-S") have_link=no ;;
101 "-o") next_is_output_file=yes ;;
102 esac
103 done
104
105 if [ "$next_is_output_file" = "yes" ]
106 then
107 echo "$myname: Unable to find output file" >&2
108 exit 1
109 fi
110
111 if [ "$have_link" = "no" ]
112 then
113 "$@"
114 exit $?
115 fi
116
117 index_file="${output_file}.gdb-index"
118 if [ "$want_index" = true ] && [ -f "$index_file" ]
119 then
120 echo "$myname: Index file $index_file exists, won't clobber." >&2
121 exit 1
122 fi
123
124 output_dir="${output_file%/*}"
125 [ "$output_dir" = "$output_file" ] && output_dir="."
126
127 "$@"
128 rc=$?
129 [ $rc != 0 ] && exit $rc
130 if [ ! -f "$output_file" ]
131 then
132 echo "$myname: Internal error: $output_file missing." >&2
133 exit 1
134 fi
135
136 if [ "$want_index" = true ]; then
137 $GDB --batch-silent -nx -ex "set auto-load no" -ex "file $output_file" -ex "save gdb-index $output_dir"
138 rc=$?
139 [ $rc != 0 ] && exit $rc
140
141 # GDB might not always create an index. Cope.
142 if [ -f "$index_file" ]
143 then
144 $OBJCOPY --add-section .gdb_index="$index_file" \
145 --set-section-flags .gdb_index=readonly \
146 "$output_file" "$output_file"
147 rc=$?
148 else
149 rc=0
150 fi
151 [ $rc != 0 ] && exit $rc
152 fi
153
154 if [ "$want_dwz" = true ]; then
155 $DWZ "$output_file" > /dev/null 2>&1
156 elif [ "$want_multi" = true ]; then
157 cp $output_file ${output_file}.alt
158 $DWZ -m ${output_file}.dwz "$output_file" ${output_file}.alt > /dev/null 2>&1
159 fi
160
161 rm -f "$index_file"
162 exit $rc