2012-12-14 Yufeng Zhang <yufeng.zhang@arm.com>
[deliverable/binutils-gdb.git] / gdb / contrib / cc-with-tweaks.sh
CommitLineData
ea65fe05 1#! /bin/sh
853254db
TT
2# Wrapper around gcc to tweak the output in various ways when running
3# the testsuite.
ea65fe05 4
0b302171 5# Copyright (C) 2010-2012 Free Software Foundation, Inc.
ea65fe05
DE
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.
0b5574da
DE
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
63b46266
YPK
22# respectively. Note that GDB should contain the gdb binary as well as the
23# -data-directory flag, e.g., "foo/gdb -data-directory foo/data-directory".
ea65fe05
DE
24# We assume the current directory is either $obj/gdb or $obj/gdb/testsuite.
25#
26# Example usage:
27#
28# bash$ cd $objdir/gdb/testsuite
29# bash$ runtest \
aec8845c
JK
30# CC_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
31# CXX_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
ea65fe05
DE
32#
33# For documentation on index files: info -f gdb.info -n "Index Files"
853254db
TT
34# For information about 'dwz', see the announcement:
35# http://gcc.gnu.org/ml/gcc/2012-04/msg00686.html
36# (More documentation is to come.)
ea65fe05 37
853254db 38# ARGS determine what is done. They can be:
ea9f10bb 39# -Z invoke objcopy --compress-debug-sections
853254db
TT
40# -z compress using dwz
41# -m compress using dwz -m
42# -i make an index
43# If nothing is given, no changes are made
44
45myname=cc-with-tweaks.sh
ea65fe05
DE
46
47if [ -z "$GDB" ]
48then
49 if [ -f ./gdb ]
50 then
63b46266 51 GDB="./gdb -data-directory data-directory"
ea65fe05
DE
52 elif [ -f ../gdb ]
53 then
63b46266 54 GDB="../gdb -data-directory ../data-directory"
b8e9bd6c
DE
55 elif [ -f ../../gdb ]
56 then
63b46266 57 GDB="../../gdb -data-directory ../../data-directory"
ea65fe05
DE
58 else
59 echo "$myname: unable to find usable gdb" >&2
60 exit 1
61 fi
62fi
63
64OBJCOPY=${OBJCOPY:-objcopy}
80626a55 65READELF=${READELF:-readelf}
ea65fe05 66
853254db 67DWZ=${DWZ:-dwz}
80626a55 68DWP=${DWP:-dwp}
853254db 69
ea65fe05
DE
70have_link=unknown
71next_is_output_file=no
00e14314 72output_file=a.out
ea65fe05 73
853254db
TT
74want_index=false
75want_dwz=false
76want_multi=false
80626a55 77want_dwp=false
ea9f10bb 78want_objcopy_compress=false
853254db
TT
79
80while [ $# -gt 0 ]; do
81 case "$1" in
ea9f10bb 82 -Z) want_objcopy_compress=true ;;
853254db
TT
83 -z) want_dwz=true ;;
84 -i) want_index=true ;;
85 -m) want_multi=true ;;
80626a55 86 -p) want_dwp=true ;;
853254db
TT
87 *) break ;;
88 esac
89 shift
90done
91
ea65fe05
DE
92for arg in "$@"
93do
94 if [ "$next_is_output_file" = "yes" ]
95 then
96 output_file="$arg"
97 next_is_output_file=no
98 continue
99 fi
100
101 # Poor man's gcc argument parser.
102 # We don't need to handle all arguments, we just need to know if we're
103 # doing a link and what the output file is.
104 # It's not perfect, but it seems to work well enough for the task at hand.
105 case "$arg" in
106 "-c") have_link=no ;;
107 "-E") have_link=no ;;
108 "-S") have_link=no ;;
109 "-o") next_is_output_file=yes ;;
110 esac
111done
112
113if [ "$next_is_output_file" = "yes" ]
114then
115 echo "$myname: Unable to find output file" >&2
116 exit 1
117fi
118
119if [ "$have_link" = "no" ]
120then
121 "$@"
122 exit $?
123fi
124
125index_file="${output_file}.gdb-index"
853254db 126if [ "$want_index" = true ] && [ -f "$index_file" ]
ea65fe05
DE
127then
128 echo "$myname: Index file $index_file exists, won't clobber." >&2
129 exit 1
130fi
131
132output_dir="${output_file%/*}"
133[ "$output_dir" = "$output_file" ] && output_dir="."
134
135"$@"
136rc=$?
137[ $rc != 0 ] && exit $rc
138if [ ! -f "$output_file" ]
139then
140 echo "$myname: Internal error: $output_file missing." >&2
141 exit 1
142fi
143
ea9f10bb
TT
144if [ "$want_objcopy_compress" = true ]; then
145 $OBJCOPY --compress-debug-sections "$output_file"
146 rc=$?
147 [ $rc != 0 ] && exit $rc
148fi
149
853254db
TT
150if [ "$want_index" = true ]; then
151 $GDB --batch-silent -nx -ex "set auto-load no" -ex "file $output_file" -ex "save gdb-index $output_dir"
ea65fe05 152 rc=$?
853254db
TT
153 [ $rc != 0 ] && exit $rc
154
155 # GDB might not always create an index. Cope.
156 if [ -f "$index_file" ]
157 then
158 $OBJCOPY --add-section .gdb_index="$index_file" \
159 --set-section-flags .gdb_index=readonly \
160 "$output_file" "$output_file"
161 rc=$?
162 else
163 rc=0
164 fi
165 [ $rc != 0 ] && exit $rc
166fi
167
168if [ "$want_dwz" = true ]; then
169 $DWZ "$output_file" > /dev/null 2>&1
170elif [ "$want_multi" = true ]; then
171 cp $output_file ${output_file}.alt
172 $DWZ -m ${output_file}.dwz "$output_file" ${output_file}.alt > /dev/null 2>&1
ea65fe05
DE
173fi
174
80626a55
DE
175if [ "$want_dwp" = true ]; then
176 dwo_files=$($READELF -wi "${output_file}" | grep _dwo_name | \
177 sed -e 's/^.*: //' | sort | uniq)
178 $DWP -o "${output_file}.dwp" ${dwo_files} > /dev/null
179 rm -f ${dwo_files}
180fi
181
ea65fe05
DE
182rm -f "$index_file"
183exit $rc
This page took 0.172133 seconds and 4 git commands to generate.