scripts/build/cc/gcc.sh
author David Holsgrove <david.holsgrove@xilinx.com>
Thu Oct 11 14:39:41 2012 +1000 (2012-10-11)
changeset 3088 7f5a3382968f
parent 3074 bcfcd68a4e0d
child 3093 f5af323f7805
permissions -rw-r--r--
cc/gcc: Add CUSTOM version and CUSTOM_LOCATION config options and GetCustom

CUSTOM_LOCATION config options only presented in menuconfig if component
CUSTOM version selected.

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