scripts/build/debug/300-gdb.sh
author "Yann E. MORIN" <yann.morin.1998@free.fr>
Sun Mar 03 17:15:56 2013 +0100 (2013-03-03)
changeset 3192 d1a898bff9c6
parent 3191 992b592dc03a
child 3193 21f521809f6f
permissions -rw-r--r--
debug/gdb: fix possibly broken code

Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
     1 # Build script for the gdb debug facility
     2 
     3 # The version of ncurses to use. Yes, it's hard-coded.
     4 # It's used only internally by crosstool-NG, and is
     5 # not exposed outside, so we don't care about providing
     6 # config options for this.
     7 CT_DEBUG_GDB_NCURSES_VERSION="5.9"
     8 
     9 # Ditto for the expat library
    10 CT_DEBUG_GDB_EXPAT_VERSION="2.1.0"
    11 
    12 do_debug_gdb_parts() {
    13     need_gdb_src=
    14     need_ncurses_src=
    15     need_expat_src=
    16 
    17     if [ "${CT_GDB_CROSS}" = y ]; then
    18         need_gdb_src=y
    19     fi
    20 
    21     if [ "${CT_GDB_GDBSERVER}" = "y" ]; then
    22         need_gdb_src=y
    23     fi
    24 
    25     if [ "${CT_GDB_NATIVE}" = "y" ]; then
    26         need_gdb_src=y
    27         # GDB on Mingw depends on PDcurses, not ncurses
    28         if [ "${CT_MINGW32}" != "y" ]; then
    29             need_ncurses_src=y
    30         fi
    31         need_expat_src=y
    32     fi
    33 }
    34 
    35 do_debug_gdb_get() {
    36     local linaro_version
    37     local linaro_series
    38     local linaro_base_url="http://launchpad.net/gdb-linaro"
    39 
    40     # Account for the Linaro versioning
    41     linaro_version="$( echo "${CT_GDB_VERSION}"      \
    42                        |sed -r -e 's/^linaro-//;'   \
    43                      )"
    44     linaro_series="$( echo "${linaro_version}"      \
    45                       |sed -r -e 's/-.*//;'         \
    46                     )"
    47 
    48     do_debug_gdb_parts
    49 
    50     if [ "${need_gdb_src}" = "y" ]; then
    51         if [ "${CT_GDB_CUSTOM}" = "y" ]; then
    52             CT_GetCustom "gdb" "${CT_GDB_VERSION}" "${CT_GDB_CUSTOM_LOCATION}"
    53         else
    54             CT_GetFile "gdb-${CT_GDB_VERSION}"                          \
    55                        {ftp,http}://ftp.gnu.org/pub/gnu/gdb             \
    56                        ftp://sources.redhat.com/pub/gdb/{,old-}releases \
    57                        "${linaro_base_url}/${linaro_series}/${linaro_version}/+download"
    58         fi
    59     fi
    60 
    61     if [ "${need_ncurses_src}" = "y" ]; then
    62         CT_GetFile "ncurses-${CT_DEBUG_GDB_NCURSES_VERSION}" .tar.gz  \
    63                    {ftp,http}://ftp.gnu.org/pub/gnu/ncurses \
    64                    ftp://invisible-island.net/ncurses
    65     fi
    66 
    67     if [ "${need_expat_src}" = "y" ]; then
    68         CT_GetFile "expat-${CT_DEBUG_GDB_EXPAT_VERSION}" .tar.gz    \
    69                    http://downloads.sourceforge.net/project/expat/expat/${CT_DEBUG_GDB_EXPAT_VERSION}
    70     fi
    71 }
    72 
    73 do_debug_gdb_extract() {
    74     do_debug_gdb_parts
    75 
    76     if [ "${need_gdb_src}" = "y" ]; then
    77         # If using custom directory location, nothing to do
    78         if [    "${CT_GDB_CUSTOM}" = "y" \
    79              -a -d "${CT_SRC_DIR}/gdb-${CT_GDB_VERSION}" ]; then
    80             return 0
    81         fi
    82         CT_Extract "gdb-${CT_GDB_VERSION}"
    83         CT_Patch "gdb" "${CT_GDB_VERSION}"
    84     fi
    85 
    86     if [ "${need_ncurses_src}" = "y" ]; then
    87         CT_Extract "ncurses-${CT_DEBUG_GDB_NCURSES_VERSION}"
    88         CT_DoExecLog ALL chmod -R u+w "${CT_SRC_DIR}/ncurses-${CT_DEBUG_GDB_NCURSES_VERSION}"
    89         CT_Patch "ncurses" "${CT_DEBUG_GDB_NCURSES_VERSION}"
    90     fi
    91 
    92     if [ "${need_expat_src}" = "y" ]; then
    93         CT_Extract "expat-${CT_DEBUG_GDB_EXPAT_VERSION}"
    94         CT_Patch "expat" "${CT_DEBUG_GDB_EXPAT_VERSION}"
    95     fi
    96 }
    97 
    98 do_debug_gdb_build() {
    99     local -a extra_config
   100 
   101     do_debug_gdb_parts
   102 
   103     gdb_src_dir="${CT_SRC_DIR}/gdb-${CT_GDB_VERSION}"
   104 
   105     # Version 6.3 and below behave badly with gdbmi
   106     case "${CT_GDB_VERSION}" in
   107         6.2*|6.3)   extra_config+=("--disable-gdbmi");;
   108     esac
   109 
   110     if [ "${CT_GDB_HAS_PKGVERSION_BUGURL}" = "y" ]; then
   111         extra_config+=("--with-pkgversion=${CT_PKGVERSION}")
   112         [ -n "${CT_TOOLCHAIN_BUGURL}" ] && extra_config+=("--with-bugurl=${CT_TOOLCHAIN_BUGURL}")
   113     fi
   114 
   115     if [ "${CT_GDB_CROSS}" = "y" ]; then
   116         local -a cross_extra_config
   117         local gcc_version
   118 
   119         CT_DoStep INFO "Installing cross-gdb"
   120         CT_DoLog EXTRA "Configuring cross-gdb"
   121 
   122         mkdir -p "${CT_BUILD_DIR}/build-gdb-cross"
   123         cd "${CT_BUILD_DIR}/build-gdb-cross"
   124 
   125         cross_extra_config=("${extra_config[@]}")
   126         case "${CT_THREADS}" in
   127             none)   cross_extra_config+=("--disable-threads");;
   128             *)      cross_extra_config+=("--enable-threads");;
   129         esac
   130         if [ "${CT_GDB_CROSS_PYTHON}" = "y" ]; then
   131             cross_extra_config+=( "--with-python=yes" )
   132         else
   133             cross_extra_config+=( "--with-python=no" )
   134         fi
   135         if [ "${CT_GDB_CROSS_SIM}" = "y" ]; then
   136             cross_extra_config+=( "--enable-sim" )
   137         else
   138             cross_extra_config+=( "--disable-sim" )
   139         fi
   140 
   141         CC_for_gdb=
   142         LD_for_gdb=
   143         if [ "${CT_GDB_CROSS_STATIC}" = "y" ]; then
   144             CC_for_gdb="gcc -static"
   145             LD_for_gdb="ld -static"
   146         fi
   147 
   148         cross_extra_config+=("--enable-expat")
   149         cross_extra_config+=("--with-expat=yes")
   150 
   151         if [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ]; then
   152             cross_extra_config+=("--disable-nls")
   153         fi
   154 
   155         gdb_cross_configure="${gdb_src_dir}/configure"
   156 
   157         CT_DoLog DEBUG "Extra config passed: '${cross_extra_config[*]}'"
   158 
   159         CT_DoExecLog CFG                                \
   160         CC="${CC_for_gdb}"                              \
   161         LD="${LD_for_gdb}"                              \
   162         "${gdb_cross_configure}"                        \
   163             --build=${CT_BUILD}                         \
   164             --host=${CT_HOST}                           \
   165             --target=${CT_TARGET}                       \
   166             --prefix="${CT_PREFIX_DIR}"                 \
   167             --with-build-sysroot="${CT_SYSROOT_DIR}"    \
   168             --with-sysroot="${CT_SYSROOT_DIR}"          \
   169             --disable-werror                            \
   170             "${cross_extra_config[@]}"                  \
   171             "${CT_GDB_CROSS_EXTRA_CONFIG_ARRAY[@]}"
   172 
   173         CT_DoLog EXTRA "Building cross-gdb"
   174         CT_DoExecLog ALL make ${JOBSFLAGS}
   175 
   176         CT_DoLog EXTRA "Installing cross-gdb"
   177         CT_DoExecLog ALL make install
   178 
   179         if [ "${CT_BUILD_MANUALS}" = "y" ]; then
   180             CT_DoLog EXTRA "Building and installing the cross-GDB manuals"
   181             CT_DoExecLog ALL make ${JOBSFLAGS} pdf html
   182             CT_DoExecLog ALL make install-{pdf,html}-gdb
   183         fi
   184 
   185         if [ "${CT_GDB_INSTALL_GDBINIT}" = "y" ]; then
   186             CT_DoLog EXTRA "Install '.gdbinit' template"
   187             # See in scripts/build/internals.sh for why we do this
   188             if [ -f "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/gcc/BASE-VER" ]; then
   189                 gcc_version=$( cat "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/gcc/BASE-VER" )
   190             else
   191                 gcc_version=$( sed -r -e '/version_string/!d; s/^.+= "([^"]+)".*$/\1/;' \
   192                                    "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/gcc/version.c"   \
   193                              )
   194             fi
   195             ${sed} -r                                               \
   196                    -e "s:@@PREFIX@@:${CT_PREFIX_DIR}:;"             \
   197                    -e "s:@@VERSION@@:${gcc_version}:;"              \
   198                    "${CT_LIB_DIR}/scripts/build/debug/gdbinit.in"   \
   199                    >"${CT_PREFIX_DIR}/share/gdb/gdbinit"
   200         fi # Install gdbinit sample
   201 
   202         CT_EndStep
   203     fi
   204 
   205     if [ "${CT_GDB_NATIVE}" = "y" ]; then
   206         local -a native_extra_config
   207         local -a ncurses_opt
   208         local -a gdb_native_CFLAGS
   209 
   210         CT_DoStep INFO "Installing native gdb"
   211 
   212         native_extra_config=("${extra_config[@]}")
   213 
   214         # GDB on Mingw depends on PDcurses, not ncurses
   215         if [ "${CT_MINGW32}" != "y" ]; then
   216             CT_DoLog EXTRA "Building static target ncurses"
   217 
   218             CT_mkdir_pushd "${CT_BUILD_DIR}/build-ncurses-target-${CT_TARGET}"
   219             do_gdb_ncurses_backend host="${CT_TARGET}"                      \
   220                                    prefix="${CT_BUILD_DIR}/static-target"   \
   221                                    cflags="${CT_CFLAGS_FOR_HOST}"           \
   222                                    ldflags=""
   223             CT_Popd
   224             native_extra_config+=("--with-curses")
   225             # There's no better way to tell gdb where to find -lcurses... :-(
   226             gdb_native_CFLAGS+=("-I${CT_BUILD_DIR}/static-target/include")
   227             gdb_native_CFLAGS+=("-L${CT_BUILD_DIR}/static-target/lib")
   228         fi # need_ncurses_src
   229 
   230         # Build libexpat
   231         CT_DoLog EXTRA "Building static target expat"
   232         CT_mkdir_pushd "${CT_BUILD_DIR}/build-expat-target-${CT_TARGET}"
   233         do_gdb_expat_backend host="${CT_TARGET}"                    \
   234                              prefix="${CT_BUILD_DIR}/static-target" \
   235                              cflags=""                              \
   236                              ldflags=""
   237         CT_Popd
   238         native_extra_config+=("--with-expat")
   239         native_extra_config+=("--with-libexpat-prefix=${CT_BUILD_DIR}/static-target")
   240 
   241         CT_DoLog EXTRA "Configuring native gdb"
   242 
   243         mkdir -p "${CT_BUILD_DIR}/build-gdb-native"
   244         cd "${CT_BUILD_DIR}/build-gdb-native"
   245 
   246         case "${CT_THREADS}" in
   247             none)   native_extra_config+=("--disable-threads");;
   248             *)      native_extra_config+=("--enable-threads");;
   249         esac
   250 
   251         [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ] &&    \
   252         native_extra_config+=("--disable-nls")
   253 
   254         if [ "${CT_GDB_NATIVE_STATIC}" = "y" ]; then
   255             CC_for_gdb="${CT_TARGET}-gcc -static"
   256             LD_for_gdb="${CT_TARGET}-ld -static"
   257         else
   258             CC_for_gdb="${CT_TARGET}-gcc"
   259             LD_for_gdb="${CT_TARGET}-ld"
   260         fi
   261 
   262         export ac_cv_func_strncmp_works=yes
   263 
   264         CT_DoLog DEBUG "Extra config passed: '${native_extra_config[*]}'"
   265 
   266         CT_DoExecLog CFG                                \
   267         CC="${CC_for_gdb}"                              \
   268         LD="${LD_for_gdb}"                              \
   269         CFLAGS="${gdb_native_CFLAGS[*]}"                \
   270         "${gdb_src_dir}/configure"                      \
   271             --build=${CT_BUILD}                         \
   272             --host=${CT_TARGET}                         \
   273             --target=${CT_TARGET}                       \
   274             --prefix=/usr                               \
   275             --with-build-sysroot="${CT_SYSROOT_DIR}"    \
   276             --without-uiout                             \
   277             --disable-tui                               \
   278             --disable-gdbtk                             \
   279             --without-x                                 \
   280             --disable-sim                               \
   281             --disable-werror                            \
   282             --without-included-gettext                  \
   283             --without-develop                           \
   284             "${native_extra_config[@]}"
   285 
   286         CT_DoLog EXTRA "Building native gdb"
   287         CT_DoExecLog ALL make ${JOBSFLAGS} CC=${CT_TARGET}-${CT_CC}
   288 
   289         CT_DoLog EXTRA "Installing native gdb"
   290         CT_DoExecLog ALL make DESTDIR="${CT_DEBUGROOT_DIR}" install
   291 
   292         # Building a native gdb also builds a gdbserver
   293         find "${CT_DEBUGROOT_DIR}" -type f -name gdbserver -exec rm -fv {} \; 2>&1 |CT_DoLog ALL
   294 
   295         unset ac_cv_func_strncmp_works
   296 
   297         CT_EndStep # native gdb build
   298     fi
   299 
   300     if [ "${CT_GDB_GDBSERVER}" = "y" ]; then
   301         local -a gdbserver_extra_config
   302 
   303         CT_DoStep INFO "Installing gdbserver"
   304         CT_DoLog EXTRA "Configuring gdbserver"
   305 
   306         mkdir -p "${CT_BUILD_DIR}/build-gdb-gdbserver"
   307         cd "${CT_BUILD_DIR}/build-gdb-gdbserver"
   308 
   309         # Workaround for bad versions, where the configure
   310         # script for gdbserver is not executable...
   311         # Bah, GNU folks strike again... :-(
   312         chmod +x "${gdb_src_dir}/gdb/gdbserver/configure"
   313 
   314         gdbserver_LDFLAGS=
   315         if [ "${CT_GDB_GDBSERVER_STATIC}" = "y" ]; then
   316             gdbserver_LDFLAGS=-static
   317         fi
   318 
   319         gdbserver_extra_config=("${extra_config[@]}")
   320 
   321         if [ "${CT_GDB_GDBSERVER_HAS_IPA_LIB}" = "y" ]; then
   322             if [ "${CT_GDB_GDBSERVER_BUILD_IPA_LIB}" = "y" ]; then
   323                 gdbserver_extra_config+=( --enable-inprocess-agent )
   324             else
   325                 gdbserver_extra_config+=( --disable-inprocess-agent )
   326             fi
   327         fi
   328 
   329         CT_DoExecLog CFG                                \
   330         LDFLAGS="${gdbserver_LDFLAGS}"                  \
   331         "${gdb_src_dir}/gdb/gdbserver/configure"        \
   332             --build=${CT_BUILD}                         \
   333             --host=${CT_TARGET}                         \
   334             --target=${CT_TARGET}                       \
   335             --prefix=/usr                               \
   336             --sysconfdir=/etc                           \
   337             --localstatedir=/var                        \
   338             --includedir="${CT_HEADERS_DIR}"            \
   339             --with-build-sysroot="${CT_SYSROOT_DIR}"    \
   340             --program-prefix=                           \
   341             --without-uiout                             \
   342             --disable-tui                               \
   343             --disable-gdbtk                             \
   344             --without-x                                 \
   345             --without-included-gettext                  \
   346             --without-develop                           \
   347             --disable-werror                            \
   348             "${gdbserver_extra_config[@]}"
   349 
   350         CT_DoLog EXTRA "Building gdbserver"
   351         CT_DoExecLog ALL make ${JOBSFLAGS} CC=${CT_TARGET}-${CT_CC}
   352 
   353         CT_DoLog EXTRA "Installing gdbserver"
   354         CT_DoExecLog ALL make DESTDIR="${CT_DEBUGROOT_DIR}" install
   355 
   356         CT_EndStep
   357     fi
   358 }
   359 
   360 # Build libncurses
   361 #   Parameter     : description               : type      : default
   362 #   host          : machine to run on         : tuple     : (none)
   363 #   prefix        : prefix to install into    : dir       : (none)
   364 #   cflags        : cflags to use             : string    : (empty)
   365 #   ldflags       : ldflags to use            : string    : (empty)
   366 do_gdb_ncurses_backend() {
   367     local host
   368     local prefix
   369     local cflags
   370     local ldflags
   371     local arg
   372 
   373     for arg in "$@"; do
   374         eval "${arg// /\\ }"
   375     done
   376 
   377     [ "${CT_CC_LANG_CXX}" = "y" ] || ncurses_opts+=("--without-cxx" "--without-cxx-binding")
   378     [ "${CT_CC_LANG_ADA}" = "y" ] || ncurses_opts+=("--without-ada")
   379 
   380     CT_mkdir_pushd "build-tic"
   381 
   382     # We need a tic that runs on build, not on host nor on target
   383     # Use build = CT_REAL_BUILD so that configure thinks it is
   384     # cross-compiling, and thus will use the ${CT_BUILD}-*
   385     # tools instead of searching for the native ones...
   386     CT_DoExecLog CFG                                                    \
   387     "${CT_SRC_DIR}/ncurses-${CT_DEBUG_GDB_NCURSES_VERSION}/configure"   \
   388         --build=${CT_BUILD}                                             \
   389         --host=${CT_BUILD}                                              \
   390         --prefix=/usr                                                   \
   391         --enable-symlinks                                               \
   392         --with-build-cc=${CT_REAL_BUILD}-gcc                            \
   393         --with-build-cpp=${CT_REAL_BUILD}-gcc                           \
   394         --with-build-cflags="${CT_CFLAGS_FOR_HOST}"                     \
   395         "${ncurses_opts[@]}"
   396 
   397     # ncurses insists on linking tic statically. It does not work
   398     # on some OSes (eg. MacOS-X/Darwin/whatever-you-call-it).
   399     CT_DoExecLog DEBUG sed -r -i -e 's/-static//g;' "progs/Makefile"
   400 
   401     # Under some operating systems (eg. Winblows), there is an
   402     # extension appended to executables. Find that.
   403     tic_ext=$(grep -E '^x[[:space:]]*=' progs/Makefile |sed -r -e 's/^.*=[[:space:]]*//;')
   404 
   405     CT_DoExecLog ALL make ${JOBSFLAGS} -C include
   406     CT_DoExecLog ALL make ${JOBSFLAGS} -C progs "tic${tic_ext}"
   407 
   408     CT_DoExecLog ALL install -d -m 0755 "${CT_BUILDTOOLS_PREFIX_DIR}/bin"
   409     CT_DoExecLog ALL install -m 0755 "progs/tic${tic_ext}" "${CT_BUILDTOOLS_PREFIX_DIR}/bin"
   410 
   411     CT_Popd
   412 
   413     CT_mkdir_pushd "ncurses"
   414 
   415     CT_DoExecLog CFG                                                    \
   416     TIC_PATH="${CT_BUILDTOOLS_PREFIX_DIR}/bin/tic${tic_ext}"            \
   417     "${CT_SRC_DIR}/ncurses-${CT_DEBUG_GDB_NCURSES_VERSION}/configure"   \
   418         --build=${CT_BUILD}                                             \
   419         --host=${host}                                                  \
   420         --with-build-cc=${CT_BUILD}-gcc                                 \
   421         --with-build-cpp=${CT_BUILD}-gcc                                \
   422         --with-build-cflags="${CT_CFLAGS_FOR_HOST}"                     \
   423         --prefix="${prefix}"                                            \
   424         --without-shared                                                \
   425         --without-sysmouse                                              \
   426         --without-progs                                                 \
   427         --enable-termcap                                                \
   428         "${ncurses_opts[@]}"
   429 
   430     CT_DoExecLog ALL make ${JOBSFLAGS}
   431     CT_DoExecLog ALL make install
   432 
   433     CT_Popd
   434 }
   435 
   436 # Build libexpat
   437 #   Parameter     : description               : type      : default
   438 #   host          : machine to run on         : tuple     : (none)
   439 #   prefix        : prefix to install into    : dir       : (none)
   440 #   cflags        : cflags to use             : string    : (empty)
   441 #   ldflags       : ldflags to use            : string    : (empty)
   442 do_gdb_expat_backend() {
   443     local host
   444     local prefix
   445     local cflags
   446     local ldflags
   447     local arg
   448 
   449     for arg in "$@"; do
   450         eval "${arg// /\\ }"
   451     done
   452 
   453     CT_DoExecLog CFG                                                \
   454     "${CT_SRC_DIR}/expat-${CT_DEBUG_GDB_EXPAT_VERSION}/configure"   \
   455         --build=${CT_BUILD}                                         \
   456         --host=${host}                                              \
   457         --prefix="${prefix}"                                        \
   458         --enable-static                                             \
   459         --disable-shared
   460 
   461     CT_DoExecLog ALL make ${JOBSFLAGS}
   462     CT_DoExecLog ALL make install
   463 }