1#!/bin/sh
2
3# Build script to build GDB with all targets enabled.
4
5# Copyright (C) 2008-2024 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# Make certain that the script is not running in an internationalized
20# environment. The script is grepping for GDB's output.
21
22# Contributed by Markus Deuling <deuling@de.ibm.com>.
23# Based on gdb_mbuild.sh from Richard Earnshaw.
24
25
26LANG=c ; export LANG
27LC_ALL=c ; export LC_ALL
28
29# Prints a usage message.
30usage()
31{
32  cat <<EOF
33Usage: gdb_buildall.sh [ <options> ... ] <srcdir> <builddir>
34
35Options:
36
37  --bfd64        Enable 64-bit BFD.
38  --clean        Delete build directory after check.
39  -e <regexp>    Regular expression for selecting the targets to build.
40  --force        Force rebuild.
41  -j <makejobs>  Run <makejobs> in parallel.  Passed to make.
42		 On a single cpu machine, 2 is recommended.
43 Arguments:
44   <srcdir>       Source code directory.
45   <builddir>     Build directory.
46
47 Environment variables examined (with default if not defined):
48   MAKE (make)"
49EOF
50  exit 1
51}
52
53### Command line options.
54makejobs=
55force=false
56targexp=""
57bfd_flag=""
58clean=false
59while test $# -gt 0
60do
61  case "$1" in
62  -j )
63      # Number of parallel make jobs.
64      shift
65      test $# -ge 1 || usage
66      makejobs="-j $1"
67      ;;
68      --clean )
69	# Shall the build directory be deleted after processing?
70	clean=true
71	;;
72    -e )
73      # A regular expression for selecting targets
74      shift
75      test $# -ge 1 || usage
76      targexp="${targexp} -e ${1}"
77      ;;
78    --force )
79      # Force a rebuild
80      force=true ;
81      ;;
82    --bfd64)
83      # Enable 64-bit BFD
84      bfd_flag="--enable-64-bit-bfd"
85      ;;
86    -* ) usage ;;
87    *) break ;;
88  esac
89  shift
90done
91
92if test $# -ne 2
93then
94  usage
95fi
96
97### Environment.
98
99# Convert these to absolute directory paths.
100srcdir=`cd $1 && /bin/pwd` || exit 1
101builddir=`cd $2 && /bin/pwd` || exit 1
102# Version of make to use
103make=${MAKE:-make}
104MAKE=${make}
105export MAKE
106# We don't want GDB do dump cores.
107ulimit -c 0
108
109# Just make sure we're in the right directory.
110maintainers=${srcdir}/gdb/MAINTAINERS
111if [ ! -r ${maintainers} ]
112then
113    echo Maintainers file ${maintainers} not found
114    exit 1
115fi
116
117
118# Build GDB with all targets enabled.
119echo "Starting gdb_buildall.sh ..."
120
121trap "exit 1"  1 2 15
122dir=${builddir}/ALL
123
124# Should a scratch rebuild be forced, for perhaps the entire build be skipped?
125if ${force}
126then
127  echo ... forcing rebuild
128  rm -rf ${dir}
129fi
130
131# Did the previous configure attempt fail?  If it did restart from scratch
132if test -d ${dir} -a ! -r ${dir}/Makefile
133then
134  echo ... removing partially configured 
135  rm -rf ${dir}
136  if test -d ${dir}
137  then
138    echo "... ERROR: Unable to remove directory ${dir}"
139    exit 1
140  fi
141fi
142
143# Create build directory.
144mkdir -p ${dir}
145cd ${dir} || exit 1
146
147# Configure GDB.
148if test ! -r Makefile
149then
150  # Default SIMOPTS to GDBOPTS.
151  test -z "${simopts}" && simopts="${gdbopts}"
152
153  # The config options.
154  __build="--enable-targets=all"
155  __enable_gdb_build_warnings=`test -z "${gdbopts}" \
156    || echo "--enable-gdb-build-warnings=${gdbopts}"`
157  __enable_sim_build_warnings=`test -z "${simopts}" \
158    || echo "--enable-sim-build-warnings=${simopts}"`
159  __configure="${srcdir}/configure \
160    ${__build} ${bfd_flag}\
161    ${__enable_gdb_build_warnings} \
162    ${__enable_sim_build_warnings}"
163  echo ... ${__configure}
164  trap "echo Removing partially configured ${dir} directory ...; rm -rf ${dir}; exit 1" 1 2 15
165  ${__configure} > Config.log 2>&1
166  trap "exit 1"  1 2 15
167
168  # Without Makefile GDB won't build.
169  if test ! -r Makefile
170  then
171    echo "... CONFIG ERROR: GDB couldn't be configured " | tee -a Config.log
172    echo "... CONFIG ERROR: see Config.log for details "
173    exit 1
174  fi
175fi
176
177# Build GDB, if not built.
178gdb_bin="gdb/gdb"
179if test ! -x gdb/gdb -a ! -x gdb/gdb.exe
180then
181  echo ... ${make} ${makejobs}
182  ( ${make} ${makejobs} all-gdb || rm -f gdb/gdb gdb/gdb.exe
183  ) > Build.log 2>&1
184
185  # If the build fails, exit.
186  if test ! -x gdb/gdb -a ! -x gdb/gdb.exe
187  then
188    echo "... BUILD ERROR: GDB couldn't be compiled " | tee -a Build.log
189    echo "... BUILD ERROR: see Build.log for details "
190    exit 1
191  fi
192  if test -x gdb/gdb.exe
193  then
194    gdb_bin="gdb/gdb.exe"
195  fi
196fi
197
198
199# Retrieve a list of settable architectures by invoking "set architecture"
200# without parameters.
201cat <<EOF > arch
202set architecture
203quit
204EOF
205./gdb/gdb --batch -nx -x arch 2>&1 | cat > gdb_archs
206tail -n 1 gdb_archs | sed 's/auto./\n/g' | sed 's/,/\n/g' |  sed 's/Requires an argument. Valid arguments are/\n/g' | sed '/^[ ]*$/d' > arch
207mv arch gdb_archs
208
209if test "${targexp}" != ""
210then
211  alltarg=`cat gdb_archs | grep ${targexp}`
212else 
213  alltarg=`cat gdb_archs`
214fi
215rm -f gdb_archs
216
217# Test all architectures available in ALLTARG
218echo "maint print architecture for"
219echo "$alltarg" | while read target
220do
221  cat <<EOF > x
222set architecture ${target}
223maint print architecture
224quit
225EOF
226  log_file=$target.log
227  log_file=${log_file//:/_}
228  echo -n "... ${target}"
229  ./gdb/gdb -batch -nx -x x 2>&1 | cat > $log_file
230  # Check GDBs results
231  if test ! -s $log_file
232  then
233    echo " ERR: gdb printed no output" | tee -a $log_file
234  elif test `grep -o internal-error $log_file | tail -n 1`
235  then
236    echo " ERR: gdb panic" | tee -a $log_file
237  else
238    echo " OK"
239  fi
240  
241  # Create a sed script that cleans up the output from GDB.
242  rm -f mbuild.sed
243  # Rules to replace <0xNNNN> with the corresponding function's name.
244  sed -n -e '/<0x0*>/d' -e 's/^.*<0x\([0-9a-f]*\)>.*$/0x\1/p' $log_file \
245  | sort -u \
246  | while read addr
247  do
248    func="`addr2line -f -e ./$gdb_bin -s ${addr} | sed -n -e 1p`"
249    echo "s/<${addr}>/<${func}>/g"
250  done >> mbuild.sed
251  # Rules to strip the leading paths off of file names.
252  echo 's/"\/.*\/gdb\//"gdb\//g' >> mbuild.sed
253  # Run the script.
254  sed -f mbuild.sed $log_file > Mbuild.log
255
256  mv Mbuild.log ${builddir}/$log_file
257  rm -rf $log_file x mbuild.sed
258done
259echo "done."
260
261# Clean up build directory if necessary.
262if ${clean}
263then
264  echo "cleanning up $dir"
265  rm -rf ${dir}
266fi
267
268exit 0
269