cc-with-tweaks.sh revision 1.8
1#!/usr/bin/env bash
2# Wrapper around gcc to tweak the output in various ways when running
3# the testsuite.
4
5# Copyright (C) 2010-2019 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.  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".
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 \
30#   CC_FOR_TARGET="/bin/bash $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
31#   CXX_FOR_TARGET="/bin/bash $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
32#
33# For documentation on Fission and dwp files:
34#     http://gcc.gnu.org/wiki/DebugFission
35#     http://gcc.gnu.org/wiki/DebugFissionDWP
36# For documentation on index files: info -f gdb.info -n "Index Files"
37# For information about 'dwz', see the announcement:
38#     http://gcc.gnu.org/ml/gcc/2012-04/msg00686.html
39# (More documentation is to come.)
40
41# ARGS determine what is done.  They can be:
42# -Z invoke objcopy --compress-debug-sections
43# -z compress using dwz
44# -m compress using dwz -m
45# -i make an index
46# -p create .dwp files (Fission), you need to also use gcc option -gsplit-dwarf
47# If nothing is given, no changes are made
48
49myname=cc-with-tweaks.sh
50mydir=`dirname "$0"`
51
52if [ -z "$GDB" ]
53then
54    if [ -f ./gdb ]
55    then
56	GDB="./gdb -data-directory data-directory"
57    elif [ -f ../gdb ]
58    then
59	GDB="../gdb -data-directory ../data-directory"
60    elif [ -f ../../gdb ]
61    then
62	GDB="../../gdb -data-directory ../../data-directory"
63    else
64	echo "$myname: unable to find usable gdb" >&2
65	exit 1
66    fi
67fi
68
69OBJCOPY=${OBJCOPY:-objcopy}
70READELF=${READELF:-readelf}
71
72DWZ=${DWZ:-dwz}
73DWP=${DWP:-dwp}
74
75have_link=unknown
76next_is_output_file=no
77output_file=a.out
78
79want_index=false
80want_dwz=false
81want_multi=false
82want_dwp=false
83want_objcopy_compress=false
84
85while [ $# -gt 0 ]; do
86    case "$1" in
87	-Z) want_objcopy_compress=true ;;
88	-z) want_dwz=true ;;
89	-i) want_index=true ;;
90	-m) want_multi=true ;;
91	-p) want_dwp=true ;;
92	*) break ;;
93    esac
94    shift
95done
96
97if [ "$want_index" = true ]
98then
99    if [ -z "$GDB_ADD_INDEX" ]
100    then
101	if [ -f $mydir/gdb-add-index.sh ]
102	then
103	    GDB_ADD_INDEX="$mydir/gdb-add-index.sh"
104	else
105	    echo "$myname: unable to find usable contrib/gdb-add-index.sh" >&2
106	    exit 1
107	fi
108    fi
109fi
110
111for arg in "$@"
112do
113    if [ "$next_is_output_file" = "yes" ]
114    then
115	output_file="$arg"
116	next_is_output_file=no
117	continue
118    fi
119
120    # Poor man's gcc argument parser.
121    # We don't need to handle all arguments, we just need to know if we're
122    # doing a link and what the output file is.
123    # It's not perfect, but it seems to work well enough for the task at hand.
124    case "$arg" in
125    "-c") have_link=no ;;
126    "-E") have_link=no ;;
127    "-S") have_link=no ;;
128    "-o") next_is_output_file=yes ;;
129    esac
130done
131
132if [ "$next_is_output_file" = "yes" ]
133then
134    echo "$myname: Unable to find output file" >&2
135    exit 1
136fi
137
138if [ "$have_link" = "no" ]
139then
140    "$@"
141    exit $?
142fi
143
144index_file="${output_file}.gdb-index"
145if [ "$want_index" = true ] && [ -f "$index_file" ]
146then
147    echo "$myname: Index file $index_file exists, won't clobber." >&2
148    exit 1
149fi
150
151output_dir="${output_file%/*}"
152[ "$output_dir" = "$output_file" ] && output_dir="."
153
154"$@"
155rc=$?
156[ $rc != 0 ] && exit $rc
157if [ ! -f "$output_file" ]
158then
159    echo "$myname: Internal error: $output_file missing." >&2
160    exit 1
161fi
162
163if [ "$want_objcopy_compress" = true ]; then
164    $OBJCOPY --compress-debug-sections "$output_file"
165    rc=$?
166    [ $rc != 0 ] && exit $rc
167fi
168
169if [ "$want_index" = true ]; then
170    # Filter out these messages which would stop dejagnu testcase run:
171    # echo "$myname: No index was created for $file" 1>&2
172    # echo "$myname: [Was there no debuginfo? Was there already an index?]" 1>&2
173    GDB=$GDB $GDB_ADD_INDEX "$output_file" 2>&1|grep -v "^${GDB_ADD_INDEX##*/}: " >&2
174    rc=${PIPESTATUS[0]}
175    [ $rc != 0 ] && exit $rc
176fi
177
178if [ "$want_dwz" = true ]; then
179    $DWZ "$output_file" > /dev/null 2>&1
180elif [ "$want_multi" = true ]; then
181    cp $output_file ${output_file}.alt
182    $DWZ -m ${output_file}.dwz "$output_file" ${output_file}.alt > /dev/null 2>&1
183fi
184
185if [ "$want_dwp" = true ]; then
186    dwo_files=$($READELF -wi "${output_file}" | grep _dwo_name | \
187	sed -e 's/^.*: //' | sort | uniq)
188    rc=0
189    if [ -n "$dwo_files" ]; then
190	$DWP -o "${output_file}.dwp" ${dwo_files} > /dev/null
191	rc=$?
192	[ $rc != 0 ] && exit $rc
193	rm -f ${dwo_files}
194    fi
195fi
196
197rm -f "$index_file"
198exit $rc
199