scripts/build/binutils/binutils.sh
author Daniel Price <daniel.price@gmail.com>
Tue Nov 20 16:59:17 2012 -0800 (2012-11-20)
changeset 3126 333d3e40cbd1
parent 3119 1c56c03b7ed5
child 3290 bddc642fc8eb
permissions -rw-r--r--
scripts: refine static linking check to better guide the user

The current mechanism to check if static linking is possible, and the mesage
displayed on failure, can be puzzling to the unsuspecting user.

Also, the current implementation is not using the existing infrastructure,
and is thus difficult to enhance with new tests.

So, switch to using the standard CT_DoExecLog infra, and use four tests to
check for the host compiler:
- check we can run it
- check it can build a trivial program
- check it can statically link that program
- check if it statically link with libstdc++

That should cover most of the problems. Hopefully.

(At the same time, fix a typo in a comment)

Signed-off-by: Daniel Price <daniel.price@gmail.com>
[yann.morin.1998@free.fr: split original patch for self-contained changes]
[yann.morin.1998@free.fr: use steps to better see gcc's output]
[yann.morin.1998@free.fr: commit log]
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
Message-Id: <163f86b5216fc08c672a.1353459722@nipigon.dssd.com>
Patchwork-Id: 200536
     1 # This file adds functions to build binutils
     2 # Copyright 2007 Yann E. MORIN
     3 # Licensed under the GPL v2. See COPYING in the root of this package
     4 
     5 # Download binutils
     6 do_binutils_get() {
     7     if [ "${CT_BINUTILS_CUSTOM}" = "y" ]; then
     8         CT_GetCustom "binutils" "${CT_BINUTILS_VERSION}" \
     9                      "${CT_BINUTILS_CUSTOM_LOCATION}"
    10     else
    11         CT_GetFile "binutils-${CT_BINUTILS_VERSION}"                                        \
    12                    {ftp,http}://{ftp.gnu.org/gnu,ftp.kernel.org/pub/linux/devel}/binutils   \
    13                    ftp://gcc.gnu.org/pub/binutils/{releases,snapshots}
    14     fi
    15 
    16     if [ -n "${CT_ARCH_BINFMT_FLAT}" ]; then
    17         CT_GetCVS "elf2flt-${CT_ELF2FLT_VERSION}"               \
    18                   ":pserver:anonymous@cvs.uclinux.org:/var/cvs" \
    19                   "elf2flt"                                     \
    20                   ""                                            \
    21                   "elf2flt-${CT_ELF2FLT_VERSION}"
    22     fi
    23 }
    24 
    25 # Extract binutils
    26 do_binutils_extract() {
    27     # If using custom directory location, nothing to do
    28     if [ "${CT_BINUTILS_CUSTOM}" = "y" \
    29          -a -d "${CT_SRC_DIR}/binutils-${CT_BINUTILS_VERSION}" ]; then
    30         return 0
    31     fi
    32 
    33     CT_Extract "binutils-${CT_BINUTILS_VERSION}"
    34     CT_Patch "binutils" "${CT_BINUTILS_VERSION}"
    35 
    36     if [ -n "${CT_ARCH_BINFMT_FLAT}" ]; then
    37         CT_Extract "elf2flt-${CT_ELF2FLT_VERSION}"
    38         CT_Patch "elf2flt" "${CT_ELF2FLT_VERSION}"
    39     fi
    40 }
    41 
    42 # Build binutils for build -> target
    43 do_binutils_for_build() {
    44     local -a binutils_opts
    45 
    46     case "${CT_TOOLCHAIN_TYPE}" in
    47         native|cross)   return 0;;
    48     esac
    49 
    50     CT_DoStep INFO "Installing binutils for build"
    51     CT_mkdir_pushd "${CT_BUILD_DIR}/build-binutils-build-${CT_BUILD}"
    52 
    53     binutils_opts+=( "host=${CT_BUILD}" )
    54     binutils_opts+=( "prefix=${CT_BUILDTOOLS_PREFIX_DIR}" )
    55     binutils_opts+=( "cflags=${CT_CFLAGS_FOR_BUILD}" )
    56     binutils_opts+=( "ldflags=${CT_LDFLAGS_FOR_BUILD}" )
    57 
    58     do_binutils_backend "${binutils_opts[@]}"
    59 
    60     CT_Popd
    61 
    62     if [ -n "${CT_ARCH_BINFMT_FLAT}" ]; then
    63         # We re-use binutils' options, plus our owns
    64         binutils_opts+=( "binutils_src=${CT_SRC_DIR}/binutils-${CT_BINUTILS_VERSION}" )
    65         binutils_opts+=( "binutils_bld=${CT_BUILD_DIR}/build-binutils-build-${CT_BUILD}" )
    66 
    67         CT_mkdir_pushd "${CT_BUILD_DIR}/build-elf2flt-build-${CT_BUILD}"
    68 
    69         do_elf2flt_backend "${binutils_opts[@]}"
    70 
    71         CT_Popd
    72     fi
    73 
    74     CT_EndStep
    75 }
    76 
    77 # Build binutils for host -> target
    78 do_binutils_for_host() {
    79     local -a binutils_tools
    80     local -a binutils_opts
    81 
    82     CT_DoStep INFO "Installing binutils for host"
    83     CT_mkdir_pushd "${CT_BUILD_DIR}/build-binutils-host-${CT_HOST}"
    84 
    85     binutils_opts+=( "host=${CT_HOST}" )
    86     binutils_opts+=( "prefix=${CT_PREFIX_DIR}" )
    87     binutils_opts+=( "static_build=${CT_STATIC_TOOLCHAIN}" )
    88     binutils_opts+=( "cflags=${CT_CFLAGS_FOR_HOST}" )
    89     binutils_opts+=( "ldflags=${CT_LDFLAGS_FOR_HOST}" )
    90     binutils_opts+=( "build_manuals=${CT_BUILD_MANUALS}" )
    91 
    92     do_binutils_backend "${binutils_opts[@]}"
    93 
    94     CT_Popd
    95 
    96     if [ -n "${CT_ARCH_BINFMT_FLAT}" ]; then
    97         # We re-use binutils' options, plus our owns
    98         binutils_opts+=( "binutils_src=${CT_SRC_DIR}/binutils-${CT_BINUTILS_VERSION}" )
    99         binutils_opts+=( "binutils_bld=${CT_BUILD_DIR}/build-binutils-host-${CT_HOST}" )
   100 
   101         CT_mkdir_pushd "${CT_BUILD_DIR}/build-elf2flt-host-${CT_HOST}"
   102 
   103         do_elf2flt_backend "${binutils_opts[@]}"
   104 
   105         CT_Popd
   106     fi
   107 
   108     # Make those new tools available to the core C compilers to come.
   109     # Note: some components want the ${TARGET}-{ar,as,ld,strip} commands as
   110     # well. Create that.
   111     # Don't do it for canadian or cross-native, because the binutils
   112     # are not executable on the build machine.
   113     case "${CT_TOOLCHAIN_TYPE}" in
   114         cross|native)
   115             binutils_tools=( ar as ld strip )
   116             if [ -n "${CT_ARCH_BINFMT_FLAT}" ]; then
   117                 binutils_tools+=( elf2flt flthdr )
   118             fi
   119             case "${CT_BINUTILS_LINKERS_LIST}" in
   120                 ld)         binutils_tools+=( ld.bfd ) ;;
   121                 gold)       binutils_tools+=( ld.gold ) ;;
   122                 ld,gold)    binutils_tools+=( ld.bfd ld.gold ) ;;
   123                 gold,ld)    binutils_tools+=( ld.bfd ld.gold ) ;;
   124             esac
   125             mkdir -p "${CT_BUILDTOOLS_PREFIX_DIR}/${CT_TARGET}/bin"
   126             mkdir -p "${CT_BUILDTOOLS_PREFIX_DIR}/bin"
   127             for t in "${binutils_tools[@]}"; do
   128                 CT_DoExecLog ALL ln -sv                                         \
   129                                     "${CT_PREFIX_DIR}/bin/${CT_TARGET}-${t}"    \
   130                                     "${CT_BUILDTOOLS_PREFIX_DIR}/${CT_TARGET}/bin/${t}"
   131                 CT_DoExecLog ALL ln -sv                                         \
   132                                     "${CT_PREFIX_DIR}/bin/${CT_TARGET}-${t}"    \
   133                                     "${CT_BUILDTOOLS_PREFIX_DIR}/bin/${CT_TARGET}-${t}"
   134             done
   135             ;;
   136         *)  ;;
   137     esac
   138 
   139     CT_EndStep
   140 }
   141 
   142 # Build binutils for X -> target
   143 #     Parameter     : description               : type      : default
   144 #     host          : machine to run on         : tuple     : (none)
   145 #     prefix        : prefix to install into    : dir       : (none)
   146 #     static_build  : build statcially          : bool      : no
   147 #     cflags        : cflags to use             : string    : (empty)
   148 #     ldflags       : ldflags to use            : string    : (empty)
   149 #     build_manuals : whether to build manuals  : bool      : no
   150 do_binutils_backend() {
   151     local host
   152     local prefix
   153     local static_build
   154     local cflags
   155     local ldflags
   156     local build_manuals=no
   157     local -a extra_config
   158     local -a extra_make_flags
   159     local -a manuals_for
   160     local -a manuals_install
   161     local arg
   162 
   163     for arg in "$@"; do
   164         eval "${arg// /\\ }"
   165     done
   166 
   167     CT_DoLog EXTRA "Configuring binutils"
   168 
   169     if [ "${CT_BINUTILS_HAS_GOLD}" = "y" ]; then
   170         case "${CT_BINUTILS_LINKERS_LIST}" in
   171             ld)
   172                 extra_config+=( --enable-ld=yes --enable-gold=no )
   173                 ;;
   174             gold)
   175                 extra_config+=( --enable-ld=no --enable-gold=yes )
   176                 ;;
   177             ld,gold)
   178                 extra_config+=( --enable-ld=default --enable-gold=yes )
   179                 ;;
   180             gold,ld)
   181                 extra_config+=( --enable-ld=yes --enable-gold=default )
   182                 ;;
   183         esac
   184         if [ "${CT_BINUTILS_GOLD_THREADS}" = "y" ]; then
   185             extra_config+=( --enable-threads )
   186         fi
   187     fi
   188     if [ "${CT_BINUTILS_PLUGINS}" = "y" ]; then
   189         extra_config+=( --enable-plugins )
   190     fi
   191     if [ "${CT_BINUTILS_HAS_PKGVERSION_BUGURL}" = "y" ]; then
   192         extra_config+=("--with-pkgversion=${CT_PKGVERSION}")
   193         [ -n "${CT_TOOLCHAIN_BUGURL}" ] && extra_config+=("--with-bugurl=${CT_TOOLCHAIN_BUGURL}")
   194     fi
   195     if [ "${CT_MULTILIB}" = "y" ]; then
   196         extra_config+=("--enable-multilib")
   197     else
   198         extra_config+=("--disable-multilib")
   199     fi
   200 
   201     [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ] && extra_config+=("--disable-nls")
   202 
   203     CT_DoLog DEBUG "Extra config passed: '${extra_config[*]}'"
   204 
   205     CT_DoExecLog CFG                                            \
   206     CFLAGS="${cflags}"                                          \
   207     CXXFLAGS="${cflags}"                                        \
   208     LDFLAGS="${ldflags}"                                        \
   209     "${CT_SRC_DIR}/binutils-${CT_BINUTILS_VERSION}/configure"   \
   210         --build=${CT_BUILD}                                     \
   211         --host=${host}                                          \
   212         --target=${CT_TARGET}                                   \
   213         --prefix=${prefix}                                      \
   214         --disable-werror                                        \
   215         "${extra_config[@]}"                                    \
   216         ${CT_ARCH_WITH_FLOAT}                                   \
   217         ${BINUTILS_SYSROOT_ARG}                                 \
   218         "${CT_BINUTILS_EXTRA_CONFIG_ARRAY[@]}"
   219 
   220     if [ "${static_build}" = "y" ]; then
   221         extra_make_flags+=("LDFLAGS=-static -all-static")
   222         CT_DoLog EXTRA "Prepare binutils for static build"
   223         CT_DoExecLog ALL make ${JOBSFLAGS} configure-host
   224     fi
   225 
   226     CT_DoLog EXTRA "Building binutils"
   227     CT_DoExecLog ALL make "${extra_make_flags[@]}" ${JOBSFLAGS}
   228 
   229     CT_DoLog EXTRA "Installing binutils"
   230     CT_DoExecLog ALL make install
   231 
   232     if [ "${build_manuals}" = "y" ]; then
   233         CT_DoLog EXTRA "Building and installing the binutils manuals"
   234         manuals_for=( gas binutils ld gprof )
   235         if [ "${CT_BINUTILS_LINKER_GOLD}" = "y" ]; then
   236             manuals_for+=( gold )
   237         fi
   238         manuals_install=( "${manuals_for[@]/#/install-pdf-}" )
   239         manuals_install+=( "${manuals_for[@]/#/install-html-}" )
   240         CT_DoExecLog ALL make ${JOBSFLAGS} pdf html
   241         CT_DoExecLog ALL make "${manuals_install[@]}"
   242     fi
   243 
   244     # Install the wrapper if needed
   245     if [ "${CT_BINUTILS_LD_WRAPPER}" = "y" ]; then
   246         CT_DoLog EXTRA "Installing ld wrapper"
   247         rm -f "${prefix}/bin/${CT_TARGET}-ld"
   248         rm -f "${prefix}/${CT_TARGET}/bin/ld"
   249         sed -r -e "s/@@DEFAULT_LD@@/${CT_BINUTILS_LINKER_DEFAULT}/" \
   250             "${CT_LIB_DIR}/scripts/build/binutils/binutils-ld.in"   \
   251             >"${prefix}/bin/${CT_TARGET}-ld"
   252         chmod +x "${prefix}/bin/${CT_TARGET}-ld"
   253         cp -a "${prefix}/bin/${CT_TARGET}-ld"   \
   254               "${prefix}/${CT_TARGET}/bin/ld"
   255 
   256         # If needed, force using ld.bfd during the toolchain build
   257         if [ "${CT_BINUTILS_FORCE_LD_BFD}" = "y" ]; then
   258             export CTNG_LD_IS=bfd
   259         fi
   260     fi
   261 }
   262 
   263 # Build elf2flt for X -> target
   264 #     Parameter     : description               : type      : default
   265 #     host          : machine to run on         : tuple     : (none)
   266 #     prefix        : prefix to install into    : dir       : (none)
   267 #     static_build  : build statcially          : bool      : no
   268 #     cflags        : cflags to use             : string    : (empty)
   269 #     ldflags       : ldflags to use            : string    : (empty)
   270 #     binutils_src  : source dir of binutils    : dir       : (none)
   271 #     binutils_bld  : build dir of binutils     : dir       : (none)
   272 #     build_manuals : whether to build manuals  : bool      : no
   273 do_elf2flt_backend() {
   274     local host
   275     local prefix
   276     local static_build
   277     local cflags
   278     local ldflags
   279     local binutils_bld
   280     local binutils_src
   281     local build_manuals
   282     local arg
   283 
   284     for arg in "$@"; do
   285         eval "${arg// /\\ }"
   286     done
   287 
   288     CT_DoLog EXTRA "Configuring elf2flt"
   289     CT_DoExecLog CFG                                            \
   290     CFLAGS="${cflags}"                                          \
   291     LDFLAGS="${ldflags}"                                        \
   292     "${CT_SRC_DIR}/elf2flt-${CT_ELF2FLT_VERSION}/configure"     \
   293         --build=${CT_BUILD}                                     \
   294         --host=${host}                                          \
   295         --target=${CT_TARGET}                                   \
   296         --prefix=${prefix}                                      \
   297         --with-bfd-include-dir=${binutils_bld}/bfd              \
   298         --with-binutils-include-dir=${binutils_src}/include     \
   299         --with-libbfd=${binutils_bld}/bfd/libbfd.a              \
   300         --with-libiberty=${binutils_bld}/libiberty/libiberty.a  \
   301         ${elf2flt_opts}                                         \
   302         "${CT_ELF2FLT_EXTRA_CONFIG_ARRAY[@]}"
   303 
   304     CT_DoLog EXTRA "Building elf2flt"
   305     CT_DoExecLog ALL make ${JOBSFLAGS}
   306 
   307     CT_DoLog EXTRA "Installing elf2flt"
   308     CT_DoExecLog ALL make install
   309 }
   310 
   311 # Now on for the target libraries
   312 do_binutils_for_target() {
   313     local -a extra_config
   314     local -a targets
   315     local -a build_targets
   316     local -a install_targets
   317     local t
   318 
   319     [ "${CT_BINUTILS_FOR_TARGET_IBERTY}" = "y" ] && targets+=("libiberty")
   320     [ "${CT_BINUTILS_FOR_TARGET_BFD}"    = "y" ] && targets+=("bfd")
   321     for t in "${targets[@]}"; do
   322         build_targets+=("all-${t}")
   323         install_targets+=("install-${t}")
   324     done
   325 
   326     if [ "${#targets[@]}" -ne 0 ]; then
   327         CT_DoStep INFO "Installing binutils for target"
   328         mkdir -p "${CT_BUILD_DIR}/build-binutils-for-target"
   329         CT_Pushd "${CT_BUILD_DIR}/build-binutils-for-target"
   330 
   331         CT_DoLog EXTRA "Configuring binutils for target"
   332 
   333         if [ "${CT_BINUTILS_HAS_PKGVERSION_BUGURL}" = "y" ]; then
   334             extra_config+=("--with-pkgversion=${CT_PKGVERSION}")
   335             [ -n "${CT_TOOLCHAIN_BUGURL}" ] && extra_config+=("--with-bugurl=${CT_TOOLCHAIN_BUGURL}")
   336         fi
   337         if [ "${CT_MULTILIB}" = "y" ]; then
   338             extra_config+=("--enable-multilib")
   339         else
   340             extra_config+=("--disable-multilib")
   341         fi
   342 
   343         [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ] && extra_config+=("--disable-nls")
   344 
   345         CT_DoExecLog CFG                                            \
   346         "${CT_SRC_DIR}/binutils-${CT_BINUTILS_VERSION}/configure"   \
   347             --build=${CT_BUILD}                                     \
   348             --host=${CT_TARGET}                                     \
   349             --target=${CT_TARGET}                                   \
   350             --prefix=/usr                                           \
   351             --disable-werror                                        \
   352             --enable-shared                                         \
   353             --enable-static                                         \
   354             "${extra_config[@]}"                                    \
   355             ${CT_ARCH_WITH_FLOAT}                                   \
   356             "${CT_BINUTILS_EXTRA_CONFIG_ARRAY[@]}"
   357 
   358         CT_DoLog EXTRA "Building binutils' libraries (${targets[*]}) for target"
   359         CT_DoExecLog ALL make ${JOBSFLAGS} "${build_targets[@]}"
   360         CT_DoLog EXTRA "Installing binutils' libraries (${targets[*]}) for target"
   361         CT_DoExecLog ALL make DESTDIR="${CT_SYSROOT_DIR}" "${install_targets[@]}"
   362 
   363         CT_Popd
   364         CT_EndStep
   365     fi
   366 }