scripts/build/cc/gcc.sh
author Daniel Price <daniel.price@gmail.com>
Tue Nov 20 16:59:17 2012 -0800 (2012-11-20)
changeset 3126 333d3e40cbd1
parent 3093 f5af323f7805
child 3131 bd172b161ff8
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 the function to build the gcc C compiler
     2 # Copyright 2007 Yann E. MORIN
     3 # Licensed under the GPL v2. See COPYING in the root of this package
     4 
     5 # Download gcc
     6 do_cc_get() {
     7     local linaro_version
     8     local linaro_series
     9     local linaro_base_url="http://launchpad.net/gcc-linaro"
    10 
    11     if [ "${CT_CC_CUSTOM}" = "y" ]; then
    12         CT_GetCustom "gcc" "${CT_CC_VERSION}" "${CT_CC_CUSTOM_LOCATION}"
    13     else
    14         # Account for the Linaro versioning
    15         linaro_version="$( echo "${CT_CC_VERSION}"      \
    16                            |sed -r -e 's/^linaro-//;'   \
    17                          )"
    18         linaro_series="$( echo "${linaro_version}"      \
    19                           |sed -r -e 's/-.*//;'         \
    20                         )"
    21 
    22         # Ah! gcc folks are kind of 'different': they store the tarballs in
    23         # subdirectories of the same name!
    24         # Arrgghh! Some of those versions does not follow this convention:
    25         # gcc-3.3.3 lives in releases/gcc-3.3.3, while gcc-2.95.* isn't in a
    26         # subdirectory!
    27         CT_GetFile "gcc-${CT_CC_VERSION}"                                                       \
    28                    {ftp,http}://ftp.gnu.org/gnu/gcc{,{,/releases}/gcc-${CT_CC_VERSION}}         \
    29                    ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/releases/gcc-${CT_CC_VERSION} \
    30                    ftp://ftp.uvsq.fr/pub/gcc/snapshots/${CT_CC_VERSION}                         \
    31                    "${linaro_base_url}/${linaro_series}/${linaro_version}/+download"
    32 
    33     fi # ! custom location
    34     # Starting with GCC 4.3, ecj is used for Java, and will only be
    35     # built if the configure script finds ecj.jar at the top of the
    36     # GCC source tree, which will not be there unless we get it and
    37     # put it there ourselves
    38     if [ "${CT_CC_LANG_JAVA_USE_ECJ}" = "y" ]; then
    39         CT_GetFile ecj-latest .jar ftp://gcc.gnu.org/pub/java   \
    40                                    ftp://sourceware.org/pub/java
    41     fi
    42 }
    43 
    44 # Extract gcc
    45 do_cc_extract() {
    46     # If using custom directory location, nothing to do
    47     if [ "${CT_CC_CUSTOM}" = "y"                    \
    48          -a -d "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}" ]; then
    49         return 0
    50     fi
    51 
    52     CT_Extract "gcc-${CT_CC_VERSION}"
    53     CT_Patch "gcc" "${CT_CC_VERSION}"
    54 
    55     # Copy ecj-latest.jar to ecj.jar at the top of the GCC source tree
    56     if [ "${CT_CC_LANG_JAVA_USE_ECJ}" = "y"                     \
    57          -a ! -f "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/ecj.jar"   \
    58        ]; then
    59         CT_DoExecLog ALL cp -v "${CT_TARBALLS_DIR}/ecj-latest.jar" "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/ecj.jar"
    60     fi
    61 }
    62 
    63 #------------------------------------------------------------------------------
    64 # This function builds up the set of languages to enable
    65 # No argument expected, returns the comma-separated language list on stdout
    66 cc_gcc_lang_list() {
    67     local lang_list
    68 
    69     lang_list="c"
    70     [ "${CT_CC_LANG_CXX}" = "y"      ] && lang_list+=",c++"
    71     [ "${CT_CC_LANG_FORTRAN}" = "y"  ] && lang_list+=",fortran"
    72     [ "${CT_CC_LANG_ADA}" = "y"      ] && lang_list+=",ada"
    73     [ "${CT_CC_LANG_JAVA}" = "y"     ] && lang_list+=",java"
    74     [ "${CT_CC_LANG_OBJC}" = "y"     ] && lang_list+=",objc"
    75     [ "${CT_CC_LANG_OBJCXX}" = "y"   ] && lang_list+=",obj-c++"
    76     lang_list+="${CT_CC_LANG_OTHERS:+,${CT_CC_LANG_OTHERS}}"
    77 
    78     printf "%s" "${lang_list}"
    79 }
    80 
    81 #------------------------------------------------------------------------------
    82 # Core gcc pass 1
    83 do_cc_core_pass_1() {
    84     local -a core_opts
    85 
    86     core_opts+=( "mode=static" )
    87     core_opts+=( "host=${CT_BUILD}" )
    88     core_opts+=( "complibs=${CT_BUILDTOOLS_PREFIX_DIR}" )
    89     core_opts+=( "prefix=${CT_BUILDTOOLS_PREFIX_DIR}" )
    90     core_opts+=( "cflags=${CT_CFLAGS_FOR_HOST}" )
    91     core_opts+=( "ldflags=${CT_LDFLAGS_FOR_HOST}" )
    92     core_opts+=( "lang_list=c" )
    93 
    94     CT_DoStep INFO "Installing pass-1 core C compiler"
    95     CT_mkdir_pushd "${CT_BUILD_DIR}/build-cc-core-pass-1"
    96 
    97     do_cc_core_backend "${core_opts[@]}"
    98 
    99     CT_Popd
   100     CT_EndStep
   101 }
   102 
   103 # Core gcc pass 2
   104 do_cc_core_pass_2() {
   105     local -a core_opts
   106 
   107     # Common options:
   108     core_opts+=( "host=${CT_BUILD}" )
   109     core_opts+=( "prefix=${CT_BUILDTOOLS_PREFIX_DIR}" )
   110     core_opts+=( "complibs=${CT_BUILDTOOLS_PREFIX_DIR}" )
   111     core_opts+=( "cflags=${CT_CFLAGS_FOR_HOST}" )
   112     core_opts+=( "ldflags=${CT_LDFLAGS_FOR_HOST}" )
   113     core_opts+=( "lang_list=c" )
   114 
   115     # Different conditions are at stake here:
   116     #   - In case the threading model is NPTL, we need a shared-capable core
   117     #     gcc; in all other cases, we need a static-only core gcc.
   118     #   - In case the threading model is NPTL or win32, or gcc is 4.3 or
   119     #     later, we need to build libgcc
   120     case "${CT_THREADS}" in
   121         nptl)
   122             core_opts+=( "mode=shared" )
   123             core_opts+=( "build_libgcc=yes" )
   124             ;;
   125         win32)
   126             core_opts+=( "mode=static" )
   127             core_opts+=( "build_libgcc=yes" )
   128             ;;
   129         *)
   130             core_opts+=( "mode=static" )
   131             if [ "${CT_CC_GCC_4_3_or_later}" = "y" ]; then
   132                 core_opts+=( "build_libgcc=yes" )
   133             fi
   134             ;;
   135     esac
   136 
   137     CT_DoStep INFO "Installing pass-2 core C compiler"
   138     CT_mkdir_pushd "${CT_BUILD_DIR}/build-cc-core-pass-2"
   139 
   140     do_cc_core_backend "${core_opts[@]}"
   141 
   142     CT_Popd
   143     CT_EndStep
   144 }
   145 
   146 #------------------------------------------------------------------------------
   147 # Build core gcc
   148 # This function is used to build the core C compiler.
   149 # Usage: do_cc_core_backend param=value [...]
   150 #   Parameter           : Definition                                : Type      : Default
   151 #   mode                : build a 'static', 'shared' or 'baremetal' : string    : (none)
   152 #   host                : the machine the core will run on          : tuple     : (none)
   153 #   prefix              : dir prefix to install into                : dir       : (none)
   154 #   complibs            : dir where complibs are isntalled          : dir       : (none)
   155 #   lang_list           : the list of languages to build            : string    : (empty)
   156 #   build_libgcc        : build libgcc or not                       : bool      : no
   157 #   build_libstdcxx     : build libstdc++ or not                    : bool      : no
   158 #   build_staticlinked  : build statically linked or not            : bool      : no
   159 #   build_manuals       : whether to build manuals or not           : bool      : no
   160 #   cflags              : cflags to use                             : string    : (empty)
   161 #   ldflags             : ldflags to use                            : string    : (empty)
   162 # Usage: do_cc_core_backend mode=[static|shared|baremetal] build_libgcc=[yes|no] build_staticlinked=[yes|no]
   163 do_cc_core_backend() {
   164     local mode
   165     local build_libgcc=no
   166     local build_libstdcxx=no
   167     local build_staticlinked=no
   168     local build_manuals=no
   169     local host
   170     local prefix
   171     local complibs
   172     local lang_list
   173     local cflags
   174     local ldflags
   175     local tmp
   176     local -a host_libstdcxx_flags
   177     local -a extra_config
   178     local -a core_LDFLAGS
   179     local -a core_targets
   180     local arg
   181 
   182     for arg in "$@"; do
   183         eval "${arg// /\\ }"
   184     done
   185 
   186     CT_DoLog EXTRA "Configuring core C compiler"
   187 
   188     case "${mode}" in
   189         static)
   190             extra_config+=("--with-newlib")
   191             extra_config+=("--enable-threads=no")
   192             extra_config+=("--disable-shared")
   193             copy_headers=y  # For baremetal, as there's no headers to copy,
   194                             # we copy an empty directory. So, who cares?
   195             ;;
   196         shared)
   197             extra_config+=("--enable-shared")
   198             copy_headers=y
   199             ;;
   200         baremetal)
   201             extra_config+=("--with-newlib")
   202             extra_config+=("--enable-threads=no")
   203             extra_config+=("--disable-shared")
   204             copy_headers=n
   205             ;;
   206         *)
   207             CT_Abort "Internal Error: 'mode' must be one of: 'static', 'shared' or 'baremetal', not '${mode:-(empty)}'"
   208             ;;
   209     esac
   210 
   211     if [ "${CT_CC_GCC_HAS_PKGVERSION_BUGURL}" = "y" ]; then
   212         # Bare metal delivers the core compiler as final compiler, so add version info and bugurl
   213         extra_config+=("--with-pkgversion=${CT_PKGVERSION}")
   214         [ -n "${CT_TOOLCHAIN_BUGURL}" ] && extra_config+=("--with-bugurl=${CT_TOOLCHAIN_BUGURL}")
   215     fi
   216 
   217     if [ "${copy_headers}" = "y" ]; then
   218         CT_DoLog DEBUG "Copying headers to install area of bootstrap gcc, so it can build libgcc2"
   219         CT_DoExecLog ALL cp -a "${CT_HEADERS_DIR}" "${prefix}/${CT_TARGET}/include"
   220     fi
   221 
   222     for tmp in ARCH ABI CPU TUNE FPU FLOAT; do
   223         eval tmp="\${CT_ARCH_WITH_${tmp}}"
   224         if [ -n "${tmp}" ]; then
   225             extra_config+=("${tmp}")
   226         fi
   227     done
   228     if [ "${CT_CC_CXA_ATEXIT}" = "y" ]; then
   229         extra_config+=("--enable-__cxa_atexit")
   230     else
   231         extra_config+=("--disable-__cxa_atexit")
   232     fi
   233 
   234     core_LDFLAGS+=("${ldflags}")
   235 
   236     # *** WARNING ! ***
   237     # Keep this full if-else-if-elif-fi-fi block in sync
   238     # with the same block in do_cc, below.
   239     if [ "${build_staticlinked}" = "yes" ]; then
   240         core_LDFLAGS+=("-static")
   241         host_libstdcxx_flags+=("-static-libgcc")
   242         host_libstdcxx_flags+=("-Wl,-Bstatic,-lstdc++")
   243         host_libstdcxx_flags+=("-lm")
   244         # Companion libraries are build static (eg !shared), so
   245         # the libstdc++ is not pulled automatically, although it
   246         # is needed. Shoe-horn it in our LDFLAGS
   247         # Ditto libm on some Fedora boxen
   248         core_LDFLAGS+=("-lstdc++")
   249         core_LDFLAGS+=("-lm")
   250     else
   251         if [ "${CT_CC_STATIC_LIBSTDCXX}" = "y" ]; then
   252             # this is from CodeSourcery arm-2010q1-202-arm-none-linux-gnueabi.src.tar.bz2
   253             # build script
   254             # INFO: if the host gcc is gcc-4.5 then presumably we could use -static-libstdc++,
   255             #       see http://gcc.gnu.org/ml/gcc-patches/2009-06/msg01635.html
   256             host_libstdcxx_flags+=("-static-libgcc")
   257             host_libstdcxx_flags+=("-Wl,-Bstatic,-lstdc++,-Bdynamic")
   258             host_libstdcxx_flags+=("-lm")
   259         elif [ "${CT_COMPLIBS_SHARED}" != "y" ]; then
   260             # When companion libraries are build static (eg !shared),
   261             # the libstdc++ is not pulled automatically, although it
   262             # is needed. Shoe-horn it in our LDFLAGS
   263             # Ditto libm on some Fedora boxen
   264             core_LDFLAGS+=("-lstdc++")
   265             core_LDFLAGS+=("-lm")
   266         fi
   267     fi
   268 
   269     if [ "${CT_CC_GCC_USE_GMP_MPFR}" = "y" ]; then
   270         extra_config+=("--with-gmp=${complibs}")
   271         extra_config+=("--with-mpfr=${complibs}")
   272     fi
   273     if [ "${CT_CC_GCC_USE_MPC}" = "y" ]; then
   274         extra_config+=("--with-mpc=${complibs}")
   275     fi
   276     if [ "${CT_CC_GCC_USE_GRAPHITE}" = "y" ]; then
   277         extra_config+=("--with-ppl=${complibs}")
   278         # With PPL 0.11+, also pull libpwl if needed
   279         if [ "${CT_PPL_NEEDS_LIBPWL}" = "y" ]; then
   280             host_libstdcxx_flags+=("-L${complibs}/lib")
   281             host_libstdcxx_flags+=("-lpwl")
   282         fi
   283         extra_config+=("--with-cloog=${complibs}")
   284     elif [ "${CT_CC_GCC_HAS_GRAPHITE}" = "y" ]; then
   285         extra_config+=("--with-ppl=no")
   286         extra_config+=("--with-cloog=no")
   287     fi
   288     if [ "${CT_CC_GCC_USE_LTO}" = "y" ]; then
   289         extra_config+=("--with-libelf=${complibs}")
   290         extra_config+=("--enable-lto")
   291     elif [ "${CT_CC_GCC_HAS_LTO}" = "y" ]; then
   292         extra_config+=("--with-libelf=no")
   293         extra_config+=("--disable-lto")
   294     fi
   295 
   296     if [ ${#host_libstdcxx_flags[@]} -ne 0 ]; then
   297         extra_config+=("--with-host-libstdcxx=${host_libstdcxx_flags[*]}")
   298     fi
   299 
   300     if [ "${CT_CC_GCC_ENABLE_TARGET_OPTSPACE}" = "y" ]; then
   301         extra_config+=("--enable-target-optspace")
   302     fi
   303 
   304     case "${CT_CC_GCC_LDBL_128}" in
   305         y)  extra_config+=("--with-long-double-128");;
   306         m)  ;;
   307         "") extra_config+=("--without-long-double-128");;
   308     esac
   309 
   310     if [ "${CT_CC_GCC_BUILD_ID}" = "y" ]; then
   311         extra_config+=( --enable-linker-build-id )
   312     fi
   313 
   314     case "${CT_CC_GCC_LNK_HASH_STYLE}" in
   315         "") ;;
   316         *)  extra_config+=( "--with-linker-hash-style=${CT_CC_GCC_LNK_HASH_STYLE}" );;
   317     esac
   318 
   319     case "${CT_ARCH}" in
   320         mips)
   321             case "${CT_CC_GCC_mips_llsc}" in
   322                 y)  extra_config+=( --with-llsc );;
   323                 m)  ;;
   324                 *)  extra_config+=( --without-llsc );;
   325             esac
   326             case "${CT_CC_GCC_mips_synci}" in
   327                 y)  extra_config+=( --with-synci );;
   328                 m)  ;;
   329                 *)  extra_config+=( --without-synci );;
   330             esac
   331             if [ "${CT_CC_GCC_mips_plt}" ]; then
   332                 extra_config+=( --with-mips-plt )
   333             fi
   334             ;; # ARCH is mips
   335     esac
   336 
   337     extra_config+=(--disable-libgomp)
   338     extra_config+=(--disable-libmudflap)
   339 
   340     [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ] && extra_config+=("--disable-nls")
   341 
   342     [ "${CT_CC_GCC_DISABLE_PCH}" = "y" ] && extra_config+=("--disable-libstdcxx-pch")
   343 
   344     if [ "${CT_CC_GCC_SYSTEM_ZLIB}" = "y" ]; then
   345         extra_config+=("--with-system-zlib")
   346     fi
   347 
   348     if [ "${CT_MULTILIB}" = "y" ]; then
   349         extra_config+=("--enable-multilib")
   350     else
   351         extra_config+=("--disable-multilib")
   352     fi
   353 
   354     CT_DoLog DEBUG "Extra config passed: '${extra_config[*]}'"
   355 
   356     # Use --with-local-prefix so older gccs don't look in /usr/local (http://gcc.gnu.org/PR10532)
   357     CT_DoExecLog CFG                                \
   358     CC_FOR_BUILD="${CT_BUILD}-gcc"                  \
   359     CFLAGS="${cflags}"                              \
   360     LDFLAGS="${core_LDFLAGS[*]}"                    \
   361     "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/configure"  \
   362         --build=${CT_BUILD}                         \
   363         --host=${host}                              \
   364         --target=${CT_TARGET}                       \
   365         --prefix="${prefix}"                        \
   366         --with-local-prefix="${CT_SYSROOT_DIR}"     \
   367         --disable-libmudflap                        \
   368         ${CC_CORE_SYSROOT_ARG}                      \
   369         "${extra_config[@]}"                        \
   370         --enable-languages="${lang_list}"           \
   371         "${CT_CC_CORE_EXTRA_CONFIG_ARRAY[@]}"
   372 
   373     if [ "${build_libgcc}" = "yes" ]; then
   374         # HACK: we need to override SHLIB_LC from gcc/config/t-slibgcc-elf-ver or
   375         # gcc/config/t-libunwind so -lc is removed from the link for
   376         # libgcc_s.so, as we do not have a target -lc yet.
   377         # This is not as ugly as it appears to be ;-) All symbols get resolved
   378         # during the glibc build, and we provide a proper libgcc_s.so for the
   379         # cross toolchain during the final gcc build.
   380         #
   381         # As we cannot modify the source tree, nor override SHLIB_LC itself
   382         # during configure or make, we have to edit the resultant
   383         # gcc/libgcc.mk itself to remove -lc from the link.
   384         # This causes us to have to jump through some hoops...
   385         #
   386         # To produce libgcc.mk to edit we firstly require libiberty.a,
   387         # so we configure then build it.
   388         # Next we have to configure gcc, create libgcc.mk then edit it...
   389         # So much easier if we just edit the source tree, but hey...
   390         if [ ! -f "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/gcc/BASE-VER" ]; then
   391             CT_DoExecLog CFG make ${JOBSFLAGS} configure-libiberty
   392             CT_DoExecLog ALL make ${JOBSFLAGS} -C libiberty libiberty.a
   393             CT_DoExecLog CFG make ${JOBSFLAGS} configure-gcc configure-libcpp
   394             CT_DoExecLog ALL make ${JOBSFLAGS} all-libcpp
   395         else
   396             CT_DoExecLog CFG make ${JOBSFLAGS} configure-gcc configure-libcpp configure-build-libiberty
   397             CT_DoExecLog ALL make ${JOBSFLAGS} all-libcpp all-build-libiberty
   398         fi
   399         # HACK: gcc-4.2 uses libdecnumber to build libgcc.mk, so build it here.
   400         if [ -d "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/libdecnumber" ]; then
   401             CT_DoExecLog CFG make ${JOBSFLAGS} configure-libdecnumber
   402             CT_DoExecLog ALL make ${JOBSFLAGS} -C libdecnumber libdecnumber.a
   403         fi
   404 
   405         # Starting with GCC 4.3, libgcc.mk is no longer built,
   406         # and libgcc.mvars is used instead.
   407 
   408         if [ "${CT_CC_GCC_4_3_or_later}" = "y" ]; then
   409             libgcc_rule="libgcc.mvars"
   410             core_targets=( gcc target-libgcc )
   411         else
   412             libgcc_rule="libgcc.mk"
   413             core_targets=( gcc )
   414         fi
   415 
   416         # On bare metal and canadian build the host-compiler is used when
   417         # actually the build-system compiler is required. Choose the correct
   418         # compilers for canadian build and use the defaults on other
   419         # configurations.
   420         if [ "${CT_BARE_METAL},${CT_CANADIAN}" = "y,y" ]; then
   421             repair_cc="CC_FOR_BUILD=${CT_BUILD}-gcc \
   422                        GCC_FOR_TARGET=${CT_TARGET}-gcc"
   423         else
   424             repair_cc=""
   425         fi
   426 
   427         CT_DoExecLog ALL make ${JOBSFLAGS} -C gcc ${libgcc_rule} \
   428                               ${repair_cc}
   429         sed -r -i -e 's@-lc@@g' gcc/${libgcc_rule}
   430     else # build_libgcc
   431         core_targets=( gcc )
   432     fi   # ! build libgcc
   433     if [    "${build_libstdcxx}" = "yes"    \
   434          -a "${CT_CC_LANG_CXX}"  = "y"      \
   435        ]; then
   436         core_targets+=( target-libstdc++-v3 )
   437     fi
   438 
   439     CT_DoLog EXTRA "Building core C compiler"
   440     CT_DoExecLog ALL make ${JOBSFLAGS} "${core_targets[@]/#/all-}"
   441 
   442     CT_DoLog EXTRA "Installing core C compiler"
   443     CT_DoExecLog ALL make ${JOBSFLAGS} "${core_targets[@]/#/install-}"
   444 
   445     if [ "${build_manuals}" = "yes" ]; then
   446         CT_DoLog EXTRA "Building the GCC manuals"
   447         CT_DoExecLog ALL make pdf html
   448         CT_DoLog EXTRA "Installing the GCC manuals"
   449         CT_DoExecLog ALL make install-{pdf,html}-gcc
   450     fi
   451 
   452     # Create a symlink ${CT_TARGET}-cc to ${CT_TARGET}-gcc to always be able
   453     # to call the C compiler with the same, somewhat canonical name.
   454     # check whether compiler has an extension
   455     file="$( ls -1 "${prefix}/bin/${CT_TARGET}-gcc."* 2>/dev/null || true )"
   456     [ -z "${file}" ] || ext=".${file##*.}"
   457     CT_DoExecLog ALL ln -sfv "${CT_TARGET}-gcc${ext}" "${prefix}/bin/${CT_TARGET}-cc${ext}"
   458 
   459     if [ "${CT_MULTILIB}" = "y" ]; then
   460         if [ "${CT_CANADIAN}" = "y" -a "${mode}" = "baremetal" \
   461              -a "${host}" = "${CT_HOST}" ]; then
   462             CT_DoLog WARN "Canadian Cross unable to confirm multilibs configured correctly"
   463         else
   464             multilibs=( $( "${prefix}/bin/${CT_TARGET}-gcc" -print-multi-lib   \
   465                            |tail -n +2 ) )
   466             if [ ${#multilibs[@]} -ne 0 ]; then
   467                 CT_DoLog EXTRA "gcc configured with these multilibs (besides the default):"
   468                 for i in "${multilibs[@]}"; do
   469                     dir="${i%%;*}"
   470                     flags="${i#*;}"
   471                     CT_DoLog EXTRA "   ${flags//@/ -}  -->  ${dir}/"
   472                 done
   473             else
   474                 CT_DoLog WARN "gcc configured for multilib, but none available"
   475            fi
   476         fi
   477     fi
   478 }
   479 
   480 #------------------------------------------------------------------------------
   481 # Build complete gcc to run on build
   482 do_cc_for_build() {
   483     local -a build_final_opts
   484     local build_final_backend
   485 
   486     # In case we're canadian or cross-native, it seems that a
   487     # real, complete compiler is needed?!? WTF? Sigh...
   488     # Otherwise, there is nothing to do.
   489     case "${CT_TOOLCHAIN_TYPE}" in
   490         native|cross)   return 0;;
   491     esac
   492 
   493     build_final_opts+=( "host=${CT_BUILD}" )
   494     build_final_opts+=( "prefix=${CT_BUILDTOOLS_PREFIX_DIR}" )
   495     build_final_opts+=( "complibs=${CT_BUILDTOOLS_PREFIX_DIR}" )
   496     build_final_opts+=( "lang_list=$( cc_gcc_lang_list )" )
   497     if [ "${CT_BARE_METAL}" = "y" ]; then
   498         # In the tests I've done, bare-metal was not impacted by the
   499         # lack of such a compiler, but better safe than sorry...
   500         build_final_opts+=( "mode=baremetal" )
   501         build_final_opts+=( "build_libgcc=yes" )
   502         build_final_opts+=( "build_libstdcxx=yes" )
   503         if [ "${CT_STATIC_TOOLCHAIN}" = "y" ]; then
   504             build_final_opts+=( "build_staticlinked=yes" )
   505         fi
   506         build_final_backend=do_cc_core_backend
   507     else
   508         build_final_backend=do_cc_backend
   509     fi
   510 
   511     CT_DoStep INFO "Installing final compiler for build"
   512     CT_mkdir_pushd "${CT_BUILD_DIR}/build-cc-final-build-${CT_BUILD}"
   513 
   514     "${build_final_backend}" "${build_final_opts[@]}"
   515 
   516     CT_Popd
   517     CT_EndStep
   518 }
   519 
   520 #------------------------------------------------------------------------------
   521 # Build final gcc to run on host
   522 do_cc_for_host() {
   523     local -a final_opts
   524     local final_backend
   525 
   526     final_opts+=( "host=${CT_HOST}" )
   527     final_opts+=( "prefix=${CT_PREFIX_DIR}" )
   528     final_opts+=( "complibs=${CT_HOST_COMPLIBS_DIR}" )
   529     final_opts+=( "cflags=${CT_CFLAGS_FOR_HOST}" )
   530     final_opts+=( "ldflags=${CT_LDFLAGS_FOR_HOST}" )
   531     final_opts+=( "lang_list=$( cc_gcc_lang_list )" )
   532     if [ "${CT_BUILD_MANUALS}" = "y" ]; then
   533         final_opts+=( "build_manuals=yes" )
   534     fi
   535     if [ "${CT_BARE_METAL}" = "y" ]; then
   536         final_opts+=( "mode=baremetal" )
   537         final_opts+=( "build_libgcc=yes" )
   538         final_opts+=( "build_libstdcxx=yes" )
   539         if [ "${CT_STATIC_TOOLCHAIN}" = "y" ]; then
   540             final_opts+=( "build_staticlinked=yes" )
   541         fi
   542         final_backend=do_cc_core_backend
   543     else
   544         final_backend=do_cc_backend
   545     fi
   546 
   547     CT_DoStep INFO "Installing final compiler"
   548     CT_mkdir_pushd "${CT_BUILD_DIR}/build-cc-final"
   549 
   550     "${final_backend}" "${final_opts[@]}"
   551 
   552     CT_Popd
   553     CT_EndStep
   554 }
   555 
   556 #------------------------------------------------------------------------------
   557 # Build the final gcc
   558 # Usage: do_cc_backend param=value [...]
   559 #   Parameter     : Definition                          : Type      : Default
   560 #   host          : the host we run onto                : tuple     : (none)
   561 #   prefix        : the runtime prefix                  : dir       : (none)
   562 #   complibs      : the companion libraries prefix      : dir       : (none)
   563 #   cflags        : cflags to use                       : string    : (empty)
   564 #   ldflags       : ldflags to use                      : string    : (empty)
   565 #   lang_list     : the list of languages to build      : string    : (empty)
   566 #   build_manuals : whether to build manuals or not     : bool      : no
   567 do_cc_backend() {
   568     local host
   569     local prefix
   570     local complibs
   571     local cflags
   572     local ldflags
   573     local lang_list
   574     local build_manuals
   575     local -a host_libstdcxx_flags
   576     local -a extra_config
   577     local -a final_LDFLAGS
   578     local tmp
   579     local arg
   580 
   581     for arg in "$@"; do
   582         eval "${arg// /\\ }"
   583     done
   584 
   585     CT_DoLog EXTRA "Configuring final compiler"
   586 
   587     # Enable selected languages
   588     extra_config+=("--enable-languages=${lang_list}")
   589 
   590     for tmp in ARCH ABI CPU TUNE FPU FLOAT; do
   591         eval tmp="\${CT_ARCH_WITH_${tmp}}"
   592         if [ -n "${tmp}" ]; then
   593             extra_config+=("${tmp}")
   594         fi
   595     done
   596 
   597     [ "${CT_SHARED_LIBS}" = "y" ] || extra_config+=("--disable-shared")
   598     if [ "${CT_CC_GCC_HAS_PKGVERSION_BUGURL}" = "y" ]; then
   599         extra_config+=("--with-pkgversion=${CT_PKGVERSION}")
   600         [ -n "${CT_TOOLCHAIN_BUGURL}" ] && extra_config+=("--with-bugurl=${CT_TOOLCHAIN_BUGURL}")
   601     fi
   602     case "${CT_CC_GCC_SJLJ_EXCEPTIONS}" in
   603         y)  extra_config+=("--enable-sjlj-exceptions");;
   604         m)  ;;
   605         "") extra_config+=("--disable-sjlj-exceptions");;
   606     esac
   607     if [ "${CT_CC_CXA_ATEXIT}" = "y" ]; then
   608         extra_config+=("--enable-__cxa_atexit")
   609     else
   610         extra_config+=("--disable-__cxa_atexit")
   611     fi
   612     if [ -n "${CT_CC_ENABLE_CXX_FLAGS}" ]; then
   613         extra_config+=("--enable-cxx-flags=${CT_CC_ENABLE_CXX_FLAGS}")
   614     fi
   615     if [ "${CT_CC_GCC_LIBMUDFLAP}" = "y" ]; then
   616         extra_config+=(--enable-libmudflap)
   617     else
   618         extra_config+=(--disable-libmudflap)
   619     fi
   620     if [ "${CT_CC_GCC_LIBGOMP}" = "y" ]; then
   621         extra_config+=(--enable-libgomp)
   622     else
   623         extra_config+=(--disable-libgomp)
   624     fi
   625     if [ "${CT_CC_GCC_LIBSSP}" = "y" ]; then
   626         extra_config+=(--enable-libssp)
   627     else
   628         extra_config+=(--disable-libssp)
   629     fi
   630     if [ "${CT_CC_GCC_HAS_LIBQUADMATH}" = "y" ]; then
   631         if [ "${CT_CC_GCC_LIBQUADMATH}" = "y" ]; then
   632             extra_config+=(--enable-libquadmath)
   633             extra_config+=(--enable-libquadmath-support)
   634         else
   635             extra_config+=(--disable-libquadmath)
   636             extra_config+=(--disable-libquadmath-support)
   637         fi
   638     fi
   639 
   640     final_LDFLAGS+=("${ldflags}")
   641 
   642     # *** WARNING ! ***
   643     # Keep this full if-else-if-elif-fi-fi block in sync
   644     # with the same block in do_cc_core, above.
   645     if [ "${CT_STATIC_TOOLCHAIN}" = "y" ]; then
   646         final_LDFLAGS+=("-static")
   647         host_libstdcxx_flags+=("-static-libgcc")
   648         host_libstdcxx_flags+=("-Wl,-Bstatic,-lstdc++")
   649         host_libstdcxx_flags+=("-lm")
   650         # Companion libraries are build static (eg !shared), so
   651         # the libstdc++ is not pulled automatically, although it
   652         # is needed. Shoe-horn it in our LDFLAGS
   653         # Ditto libm on some Fedora boxen
   654         final_LDFLAGS+=("-lstdc++")
   655         final_LDFLAGS+=("-lm")
   656     else
   657         if [ "${CT_CC_STATIC_LIBSTDCXX}" = "y" ]; then
   658             # this is from CodeSourcery arm-2010q1-202-arm-none-linux-gnueabi.src.tar.bz2
   659             # build script
   660             # INFO: if the host gcc is gcc-4.5 then presumably we could use -static-libstdc++,
   661             #       see http://gcc.gnu.org/ml/gcc-patches/2009-06/msg01635.html
   662             host_libstdcxx_flags+=("-static-libgcc")
   663             host_libstdcxx_flags+=("-Wl,-Bstatic,-lstdc++,-Bdynamic")
   664             host_libstdcxx_flags+=("-lm")
   665         elif [ "${CT_COMPLIBS_SHARED}" != "y" ]; then
   666             # When companion libraries are build static (eg !shared),
   667             # the libstdc++ is not pulled automatically, although it
   668             # is needed. Shoe-horn it in our LDFLAGS
   669             # Ditto libm on some Fedora boxen
   670             final_LDFLAGS+=("-lstdc++")
   671             final_LDFLAGS+=("-lm")
   672         fi
   673     fi
   674 
   675     if [ "${CT_CC_GCC_USE_GMP_MPFR}" = "y" ]; then
   676         extra_config+=("--with-gmp=${complibs}")
   677         extra_config+=("--with-mpfr=${complibs}")
   678     fi
   679     if [ "${CT_CC_GCC_USE_MPC}" = "y" ]; then
   680         extra_config+=("--with-mpc=${complibs}")
   681     fi
   682     if [ "${CT_CC_GCC_USE_GRAPHITE}" = "y" ]; then
   683         extra_config+=("--with-ppl=${complibs}")
   684         # With PPL 0.11+, also pull libpwl if needed
   685         if [ "${CT_PPL_NEEDS_LIBPWL}" = "y" ]; then
   686             host_libstdcxx_flags+=("-L${complibs}/lib")
   687             host_libstdcxx_flags+=("-lpwl")
   688         fi
   689         extra_config+=("--with-cloog=${complibs}")
   690     elif [ "${CT_CC_GCC_HAS_GRAPHITE}" = "y" ]; then
   691         extra_config+=("--with-ppl=no")
   692         extra_config+=("--with-cloog=no")
   693     fi
   694     if [ "${CT_CC_GCC_USE_LTO}" = "y" ]; then
   695         extra_config+=("--with-libelf=${complibs}")
   696     elif [ "${CT_CC_GCC_HAS_LTO}" = "y" ]; then
   697         extra_config+=("--with-libelf=no")
   698     fi
   699 
   700     if [ ${#host_libstdcxx_flags[@]} -ne 0 ]; then
   701         extra_config+=("--with-host-libstdcxx=${host_libstdcxx_flags[*]}")
   702     fi
   703 
   704     if [ "${CT_THREADS}" = "none" ]; then
   705         extra_config+=("--disable-threads")
   706         if [ "${CT_CC_GCC_4_2_or_later}" = y ]; then
   707             CT_Test "Disabling libgomp for no-thread gcc>=4.2" "${CT_CC_GCC_LIBGOMP}" = "Y"
   708             extra_config+=("--disable-libgomp")
   709         fi
   710     else
   711         if [ "${CT_THREADS}" = "win32" ]; then
   712             extra_config+=("--enable-threads=win32")
   713             extra_config+=("--disable-win32-registry")
   714         else
   715             extra_config+=("--enable-threads=posix")
   716         fi
   717     fi
   718 
   719     if [ "${CT_CC_GCC_ENABLE_TARGET_OPTSPACE}" = "y" ]; then
   720         extra_config+=("--enable-target-optspace")
   721     fi
   722     if [ "${CT_CC_GCC_DISABLE_PCH}" = "y" ]; then
   723         extra_config+=("--disable-libstdcxx-pch")
   724     fi
   725 
   726     case "${CT_CC_GCC_LDBL_128}" in
   727         y)  extra_config+=("--with-long-double-128");;
   728         m)  ;;
   729         "") extra_config+=("--without-long-double-128");;
   730     esac
   731 
   732     if [ "${CT_CC_GCC_BUILD_ID}" = "y" ]; then
   733         extra_config+=( --enable-linker-build-id )
   734     fi
   735 
   736     case "${CT_CC_GCC_LNK_HASH_STYLE}" in
   737         "") ;;
   738         *)  extra_config+=( "--with-linker-hash-style=${CT_CC_GCC_LNK_HASH_STYLE}" );;
   739     esac
   740 
   741     if [ "${CT_CC_GCC_ENABLE_PLUGINS}" = "y" ]; then
   742         extra_config+=( --enable-plugin )
   743     fi
   744     if [ "${CT_CC_GCC_GOLD}" = "y" ]; then
   745         extra_config+=( --enable-gold )
   746     fi
   747 
   748     case "${CT_ARCH}" in
   749         mips)
   750             case "${CT_CC_GCC_mips_llsc}" in
   751                 y)  extra_config+=( --with-llsc );;
   752                 m)  ;;
   753                 *)  extra_config+=( --without-llsc );;
   754             esac
   755             case "${CT_CC_GCC_mips_synci}" in
   756                 y)  extra_config+=( --with-synci );;
   757                 m)  ;;
   758                 *)  extra_config+=( --without-synci );;
   759             esac
   760             if [ "${CT_CC_GCC_mips_plt}" ]; then
   761                 extra_config+=( --with-mips-plt )
   762             fi
   763             ;; # ARCH is mips
   764     esac
   765 
   766     [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ] && extra_config+=("--disable-nls")
   767 
   768     if [ "${CT_CC_GCC_SYSTEM_ZLIB}" = "y" ]; then
   769         extra_config+=("--with-system-zlib")
   770     fi
   771 
   772     if [ "${CT_MULTILIB}" = "y" ]; then
   773         extra_config+=("--enable-multilib")
   774     else
   775         extra_config+=("--disable-multilib")
   776     fi
   777 
   778     CT_DoLog DEBUG "Extra config passed: '${extra_config[*]}'"
   779 
   780     CT_DoExecLog CFG                                \
   781     CC_FOR_BUILD="${CT_BUILD}-gcc"                  \
   782     CFLAGS="${cflags}"                              \
   783     LDFLAGS="${final_LDFLAGS[*]}"                   \
   784     CFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}"         \
   785     CXXFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}"       \
   786     LDFLAGS_FOR_TARGET="${CT_TARGET_LDFLAGS}"       \
   787     "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/configure"  \
   788         --build=${CT_BUILD}                         \
   789         --host=${host}                              \
   790         --target=${CT_TARGET}                       \
   791         --prefix="${prefix}"                        \
   792         ${CC_SYSROOT_ARG}                           \
   793         "${extra_config[@]}"                        \
   794         --with-local-prefix="${CT_SYSROOT_DIR}"     \
   795         --enable-c99                                \
   796         --enable-long-long                          \
   797         "${CT_CC_EXTRA_CONFIG_ARRAY[@]}"
   798 
   799     if [ "${CT_CANADIAN}" = "y" ]; then
   800         CT_DoLog EXTRA "Building libiberty"
   801         CT_DoExecLog ALL make ${JOBSFLAGS} all-build-libiberty
   802     fi
   803 
   804     CT_DoLog EXTRA "Building final compiler"
   805     CT_DoExecLog ALL make ${JOBSFLAGS} all
   806 
   807     CT_DoLog EXTRA "Installing final compiler"
   808     CT_DoExecLog ALL make ${JOBSFLAGS} install
   809 
   810     if [ "${build_manuals}" = "yes" ]; then
   811         CT_DoLog EXTRA "Building the GCC manuals"
   812         CT_DoExecLog ALL make pdf html
   813         CT_DoLog EXTRA "Installing the GCC manuals"
   814         CT_DoExecLog ALL make install-{pdf,html}-gcc
   815     fi
   816 
   817     # Create a symlink ${CT_TARGET}-cc to ${CT_TARGET}-gcc to always be able
   818     # to call the C compiler with the same, somewhat canonical name.
   819     # check whether compiler has an extension
   820     file="$( ls -1 "${CT_PREFIX_DIR}/bin/${CT_TARGET}-gcc."* 2>/dev/null || true )"
   821     [ -z "${file}" ] || ext=".${file##*.}"
   822     CT_DoExecLog ALL ln -sfv "${CT_TARGET}-gcc${ext}" "${CT_PREFIX_DIR}/bin/${CT_TARGET}-cc${ext}"
   823 
   824     if [ "${CT_MULTILIB}" = "y" ]; then
   825         if [ "${CT_CANADIAN}" = "y" ]; then
   826             CT_DoLog WARN "Canadian Cross unable to confirm multilibs configured correctly"
   827         else
   828             multilibs=( $( "${prefix}/bin/${CT_TARGET}-gcc" -print-multi-lib \
   829                            |tail -n +2 ) )
   830             if [ ${#multilibs[@]} -ne 0 ]; then
   831                 CT_DoLog EXTRA "gcc configured with these multilibs (besides the default):"
   832                 for i in "${multilibs[@]}"; do
   833                     dir="${i%%;*}"
   834                     flags="${i#*;}"
   835                     CT_DoLog EXTRA "   ${flags//@/ -}  -->  ${dir}/"
   836                 done
   837             else
   838                 CT_DoLog WARN "gcc configured for multilib, but none available"
   839             fi
   840         fi
   841     fi
   842 }