scripts/build/cc/gcc.sh
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Jun 13 23:38:37 2010 +0200 (2010-06-13)
changeset 1983 198a5a6e5239
parent 1981 7130357d712b
child 1991 1974075aa641
permissions -rw-r--r--
cc/gcc: baremetal requires a two-pass process

Here, we implement a highly ugly hack. I'm not proud of that one...

To build the libstdc++ library, the compiler requires the C library. In
case we build for non-baremetal, this is normally handled by the final
step, later.

But in the case of bare-metal, we never go through the final step (because
it does not work, and it seems complex enough to make it work), so the
baremetal compilers are issued out of the core step.
     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     # Ah! gcc folks are kind of 'different': they store the tarballs in
     8     # subdirectories of the same name! That's because gcc is such /crap/ that
     9     # it is such /big/ that it needs being splitted for distribution! Sad. :-(
    10     # Arrgghh! Some of those versions does not follow this convention:
    11     # gcc-3.3.3 lives in releases/gcc-3.3.3, while gcc-2.95.* isn't in a
    12     # subdirectory! You bastard!
    13     CT_GetFile "gcc-${CT_CC_VERSION}"                                                       \
    14                {ftp,http}://ftp.gnu.org/gnu/gcc{,{,/releases}/gcc-${CT_CC_VERSION}}         \
    15                ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/releases/gcc-${CT_CC_VERSION} \
    16                ftp://ftp.uvsq.fr/pub/gcc/snapshots/${CT_CC_VERSION}
    17 
    18     # Starting with GCC 4.3, ecj is used for Java, and will only be
    19     # built if the configure script finds ecj.jar at the top of the
    20     # GCC source tree, which will not be there unless we get it and
    21     # put it there ourselves
    22     if [ "${CT_CC_LANG_JAVA_USE_ECJ}" = "y" ]; then
    23         CT_GetFile ecj-latest .jar ftp://gcc.gnu.org/pub/java   \
    24                                    ftp://sourceware.org/pub/java
    25     fi
    26 }
    27 
    28 # Extract gcc
    29 do_cc_extract() {
    30     CT_Extract "gcc-${CT_CC_VERSION}"
    31     CT_Patch "gcc" "${CT_CC_VERSION}"
    32 
    33     # Copy ecj-latest.jar to ecj.jar at the top of the GCC source tree
    34     if [ "${CT_CC_LANG_JAVA_USE_ECJ}" = "y"                     \
    35          -a ! -f "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/ecj.jar"   \
    36        ]; then
    37         CT_DoExecLog ALL cp -v "${CT_TARBALLS_DIR}/ecj-latest.jar" "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/ecj.jar"
    38     fi
    39 }
    40 
    41 #------------------------------------------------------------------------------
    42 # Core gcc pass 1
    43 do_cc_core_pass_1() {
    44     # If we're building for bare metal, build the static core gcc,
    45     # with libgcc.
    46     # In case we're not bare metal and building a canadian compiler, do nothing
    47     # In case we're not bare metal, and we're NPTL, build the static core gcc.
    48     # In any other case, do nothing.
    49     case "${CT_BARE_METAL},${CT_CANADIAN},${CT_THREADS}" in
    50         y,*,*)  do_cc_core mode=static;;
    51         ,y,*)   ;;
    52         ,,nptl) do_cc_core mode=static;;
    53         *)      ;;
    54     esac
    55 }
    56 
    57 # Core gcc pass 2
    58 do_cc_core_pass_2() {
    59     # In case we're building for bare metal, do nothing, we already have
    60     # our compiler.
    61     # In case we're not bare metal and building a canadian compiler, do nothing
    62     # In case we're NPTL, build the shared core gcc and the target libgcc.
    63     # In any other case, build the static core gcc and, if using gcc-4.3+,
    64     # also build the target libgcc.
    65     case "${CT_BARE_METAL},${CT_CANADIAN},${CT_THREADS}" in
    66         y,*,*)  do_cc_core mode=baremetal build_libgcc=yes build_libstdcxx=yes;;
    67         ,y,*)   ;;
    68         ,,nptl)
    69             do_cc_core mode=shared build_libgcc=yes
    70             ;;
    71         *)  if [ "${CT_CC_GCC_4_3_or_later}" = "y" ]; then
    72                 do_cc_core mode=static build_libgcc=yes
    73             else
    74                 do_cc_core mode=static
    75             fi
    76             ;;
    77     esac
    78 }
    79 
    80 #------------------------------------------------------------------------------
    81 # Build core gcc
    82 # This function is used to build both the static and the shared core C conpiler,
    83 # with or without the target libgcc. We need to know wether:
    84 #  - we're building static, shared or bare metal: mode=[static|shared|baremetal]
    85 #  - we need to build libgcc or not             : build_libgcc=[yes|no]     (default: no)
    86 #  - we need to build libstdc++ or not          : build_libstdcxx=[yes|no]  (default: no)
    87 # Usage: do_cc_core_static mode=[static|shared|baremetal] build_libgcc=[yes|no]
    88 do_cc_core() {
    89     local mode
    90     local build_libgcc=no
    91     local build_libstdcxx=no
    92     local core_prefix_dir
    93     local lang_opt
    94     local tmp
    95     local -a extra_config
    96     local core_LDFLAGS
    97     local -a core_targets
    98 
    99     while [ $# -ne 0 ]; do
   100         eval "${1}"
   101         shift
   102     done
   103 
   104     lang_opt=c
   105     case "${mode}" in
   106         static)
   107             core_prefix_dir="${CT_CC_CORE_STATIC_PREFIX_DIR}"
   108             extra_config+=("--with-newlib")
   109             extra_config+=("--enable-threads=no")
   110             extra_config+=("--disable-shared")
   111             copy_headers=y  # For baremetal, as there's no headers to copy,
   112                             # we copy an empty directory. So, who cares?
   113             ;;
   114         shared)
   115             core_prefix_dir="${CT_CC_CORE_SHARED_PREFIX_DIR}"
   116             extra_config+=("--enable-shared")
   117             copy_headers=y
   118             ;;
   119         baremetal)
   120             core_prefix_dir="${CT_PREFIX_DIR}"
   121             extra_config+=("--with-newlib")
   122             extra_config+=("--enable-threads=no")
   123             extra_config+=("--disable-shared")
   124             [ "${CT_CC_LANG_CXX}" = "y" ] && lang_opt="${lang_opt},c++"
   125             copy_headers=n
   126             ;;
   127         *)
   128             CT_Abort "Internal Error: 'mode' must be one of: 'static', 'shared' or 'baremetal', not '${mode:-(empty)}'"
   129             ;;
   130     esac
   131 
   132     CT_DoStep INFO "Installing ${mode} core C compiler"
   133     mkdir -p "${CT_BUILD_DIR}/build-cc-core-${mode}"
   134     cd "${CT_BUILD_DIR}/build-cc-core-${mode}"
   135 
   136     # Bare metal delivers the core compiler as final compiler, so add version info and bugurl
   137     [ -n "${CT_CC_BUGURL}" ]     && extra_config+=("--with-bugurl=${CT_CC_BUGURL}")
   138     [ -n "${CT_CC_PKGVERSION}" ] && extra_config+=("--with-pkgversion=${CT_CC_PKGVERSION}")
   139 
   140     if [ "${copy_headers}" = "y" ]; then
   141         CT_DoLog DEBUG "Copying headers to install area of bootstrap gcc, so it can build libgcc2"
   142         CT_DoExecLog ALL cp -a "${CT_HEADERS_DIR}" "${core_prefix_dir}/${CT_TARGET}/include"
   143     fi
   144 
   145     CT_DoLog EXTRA "Configuring ${mode} core C compiler"
   146 
   147     for tmp in ARCH ABI CPU TUNE FPU FLOAT; do
   148         eval tmp="\${CT_ARCH_WITH_${tmp}}"
   149         if [ -n "${tmp}" ]; then
   150             extra_config+=("${tmp}")
   151         fi
   152     done
   153     if [ "${CT_CC_CXA_ATEXIT}" = "y" ]; then
   154         extra_config+=("--enable-__cxa_atexit")
   155     else
   156         extra_config+=("--disable-__cxa_atexit")
   157     fi
   158 
   159     # When companion libraries are build static (eg !shared),
   160     # the libstdc++ is not pulled automatically, although it
   161     # is needed. Shoe-horn it in our LDFLAGS
   162     if [ "${CT_COMPLIBS_SHARED}" != "y" ]; then
   163         core_LDFLAGS='-lstdc++'
   164     fi
   165     if [ "${CT_CC_GCC_USE_GMP_MPFR}" = "y" ]; then
   166         extra_config+=("--with-gmp=${CT_COMPLIBS_DIR}")
   167         extra_config+=("--with-mpfr=${CT_COMPLIBS_DIR}")
   168     fi
   169     if [ "${CT_CC_GCC_USE_PPL_CLOOG_MPC}" = "y" ]; then
   170         extra_config+=("--with-ppl=${CT_COMPLIBS_DIR}")
   171         extra_config+=("--with-cloog=${CT_COMPLIBS_DIR}")
   172         extra_config+=("--with-mpc=${CT_COMPLIBS_DIR}")
   173     fi
   174     if [ "${CT_CC_GCC_USE_LIBELF}" = "y" ]; then
   175         extra_config+=("--with-libelf=${CT_COMPLIBS_DIR}")
   176     fi
   177 
   178     if [ "${CT_CC_GCC_ENABLE_TARGET_OPTSPACE}" = "y" ]; then
   179         extra_config+=("--enable-target-optspace")
   180     fi
   181 
   182     CT_DoLog DEBUG "Extra config passed: '${extra_config[*]}'"
   183 
   184     # Use --with-local-prefix so older gccs don't look in /usr/local (http://gcc.gnu.org/PR10532)
   185     CC_FOR_BUILD="${CT_BUILD}-gcc"                  \
   186     CFLAGS="${CT_CFLAGS_FOR_HOST}"                  \
   187     LDFLAGS="${core_LDFLAGS}"                       \
   188     CT_DoExecLog ALL                                \
   189     "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/configure"  \
   190         --build=${CT_BUILD}                         \
   191         --host=${CT_HOST}                           \
   192         --target=${CT_TARGET}                       \
   193         --prefix="${core_prefix_dir}"               \
   194         --with-local-prefix="${CT_SYSROOT_DIR}"     \
   195         --disable-multilib                          \
   196         ${CC_CORE_SYSROOT_ARG}                      \
   197         "${extra_config[@]}"                        \
   198         --disable-nls                               \
   199         --enable-symvers=gnu                        \
   200         --enable-languages="${lang_opt}"            \
   201         ${CT_CC_CORE_EXTRA_CONFIG}
   202 
   203     if [ "${build_libgcc}" = "yes" ]; then
   204         # HACK: we need to override SHLIB_LC from gcc/config/t-slibgcc-elf-ver or
   205         # gcc/config/t-libunwind so -lc is removed from the link for
   206         # libgcc_s.so, as we do not have a target -lc yet.
   207         # This is not as ugly as it appears to be ;-) All symbols get resolved
   208         # during the glibc build, and we provide a proper libgcc_s.so for the
   209         # cross toolchain during the final gcc build.
   210         #
   211         # As we cannot modify the source tree, nor override SHLIB_LC itself
   212         # during configure or make, we have to edit the resultant
   213         # gcc/libgcc.mk itself to remove -lc from the link.
   214         # This causes us to have to jump through some hoops...
   215         #
   216         # To produce libgcc.mk to edit we firstly require libiberty.a,
   217         # so we configure then build it.
   218         # Next we have to configure gcc, create libgcc.mk then edit it...
   219         # So much easier if we just edit the source tree, but hey...
   220         if [ ! -f "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/gcc/BASE-VER" ]; then
   221             CT_DoExecLog ALL make configure-libiberty
   222             CT_DoExecLog ALL make ${PARALLELMFLAGS} -C libiberty libiberty.a
   223             CT_DoExecLog ALL make configure-gcc configure-libcpp
   224             CT_DoExecLog ALL make ${PARALLELMFLAGS} all-libcpp
   225         else
   226             CT_DoExecLog ALL make configure-gcc configure-libcpp configure-build-libiberty
   227             CT_DoExecLog ALL make ${PARALLELMFLAGS} all-libcpp all-build-libiberty
   228         fi
   229         # HACK: gcc-4.2 uses libdecnumber to build libgcc.mk, so build it here.
   230         if [ -d "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/libdecnumber" ]; then
   231             CT_DoExecLog ALL make configure-libdecnumber
   232             CT_DoExecLog ALL make ${PARALLELMFLAGS} -C libdecnumber libdecnumber.a
   233         fi
   234 
   235         # Starting with GCC 4.3, libgcc.mk is no longer built,
   236         # and libgcc.mvars is used instead.
   237 
   238         if [ "${CT_CC_GCC_4_3_or_later}" = "y" ]; then
   239             libgcc_rule="libgcc.mvars"
   240             core_targets=( gcc target-libgcc )
   241         else
   242             libgcc_rule="libgcc.mk"
   243             core_targets=( gcc )
   244         fi
   245 
   246         # On bare metal and canadian build the host-compiler is used when
   247         # actually the build-system compiler is required. Choose the correct
   248         # compilers for canadian build and use the defaults on other
   249         # configurations.
   250         if [ "${CT_BARE_METAL},${CT_CANADIAN}" = "y,y" ]; then
   251             repair_cc="CC_FOR_BUILD=${CT_BUILD}-gcc \
   252                        GCC_FOR_TARGET=${CT_TARGET}-gcc"
   253         else
   254             repair_cc=""
   255         fi
   256 
   257         CT_DoExecLog ALL make ${PARALLELMFLAGS} -C gcc ${libgcc_rule} \
   258                               ${repair_cc}
   259         sed -r -i -e 's@-lc@@g' gcc/${libgcc_rule}
   260     else # build_libgcc
   261         core_targets=( gcc )
   262     fi   # ! build libgcc
   263     if [    "${build_libstdcxx}" = "yes"    \
   264          -a "${CT_CC_LANG_CXX}"  = "y"      \
   265        ]; then
   266         core_targets+=( target-libstdc++-v3 )
   267     fi
   268 
   269     CT_DoLog EXTRA "Building ${mode} core C compiler"
   270     CT_DoExecLog ALL make ${PARALLELMFLAGS} "${core_targets[@]/#/all-}"
   271 
   272     CT_DoLog EXTRA "Installing ${mode} core C compiler"
   273     CT_DoExecLog ALL make "${core_targets[@]/#/install-}"
   274 
   275     # Create a symlink ${CT_TARGET}-cc to ${CT_TARGET}-gcc to always be able
   276     # to call the C compiler with the same, somewhat canonical name.
   277     # check whether compiler has an extension
   278     file="$( ls -1 "${core_prefix_dir}/bin/${CT_TARGET}-gcc."* 2>/dev/null || true )"
   279     [ -z "${file}" ] || ext=".${file##*.}"
   280     CT_DoExecLog ALL ln -sv "${CT_TARGET}-gcc${ext}" "${core_prefix_dir}/bin/${CT_TARGET}-cc${ext}"
   281 
   282     CT_EndStep
   283 }
   284 
   285 #------------------------------------------------------------------------------
   286 # Build final gcc
   287 do_cc() {
   288     local -a extra_config
   289     local tmp
   290     local final_LDFLAGS
   291 
   292     # If building for bare metal, nothing to be done here, the static core conpiler is enough!
   293     [ "${CT_BARE_METAL}" = "y" ] && return 0
   294 
   295     CT_DoStep INFO "Installing final compiler"
   296 
   297     mkdir -p "${CT_BUILD_DIR}/build-cc"
   298     cd "${CT_BUILD_DIR}/build-cc"
   299 
   300     CT_DoLog EXTRA "Configuring final compiler"
   301 
   302     # Enable selected languages
   303     lang_opt="c"
   304     [ "${CT_CC_LANG_CXX}" = "y"      ] && lang_opt="${lang_opt},c++"
   305     [ "${CT_CC_LANG_FORTRAN}" = "y"  ] && lang_opt="${lang_opt},fortran"
   306     [ "${CT_CC_LANG_ADA}" = "y"      ] && lang_opt="${lang_opt},ada"
   307     [ "${CT_CC_LANG_JAVA}" = "y"     ] && lang_opt="${lang_opt},java"
   308     [ "${CT_CC_LANG_OBJC}" = "y"     ] && lang_opt="${lang_opt},objc"
   309     [ "${CT_CC_LANG_OBJCXX}" = "y"   ] && lang_opt="${lang_opt},obj-c++"
   310     CT_Test "Building ADA language is not yet supported. Will try..." "${CT_CC_LANG_ADA}" = "y"
   311     CT_Test "Building Objective-C language is not yet supported. Will try..." "${CT_CC_LANG_OBJC}" = "y"
   312     CT_Test "Building Objective-C++ language is not yet supported. Will try..." "${CT_CC_LANG_OBJCXX}" = "y"
   313     CT_Test "Building ${CT_CC_LANG_OTHERS//,/ } language(s) is not yet supported. Will try..." -n "${CT_CC_LANG_OTHERS}"
   314     lang_opt=$(echo "${lang_opt},${CT_CC_LANG_OTHERS}" |sed -r -e 's/,+/,/g; s/,*$//;')
   315 
   316     extra_config+=("--enable-languages=${lang_opt}")
   317     extra_config+=("--disable-multilib")
   318     for tmp in ARCH ABI CPU TUNE FPU FLOAT; do
   319         eval tmp="\${CT_ARCH_WITH_${tmp}}"
   320         if [ -n "${tmp}" ]; then
   321             extra_config+=("${tmp}")
   322         fi
   323     done
   324 
   325     [ "${CT_SHARED_LIBS}" = "y" ]                   || extra_config+=("--disable-shared")
   326     [ -n "${CT_CC_PKGVERSION}" ]                    && extra_config+=("--with-pkgversion=${CT_CC_PKGVERSION}")
   327     [ -n "${CT_CC_BUGURL}" ]                        && extra_config+=("--with-bugurl=${CT_CC_BUGURL}")
   328     [ "${CT_CC_SJLJ_EXCEPTIONS_USE}" = "y" ]        && extra_config+=("--enable-sjlj-exceptions")
   329     [ "${CT_CC_SJLJ_EXCEPTIONS_DONT_USE}" = "y" ]   && extra_config+=("--disable-sjlj-exceptions")
   330     if [ "${CT_CC_CXA_ATEXIT}" = "y" ]; then
   331         extra_config+=("--enable-__cxa_atexit")
   332     else
   333         extra_config+=("--disable-__cxa_atexit")
   334     fi
   335     if [ -n "${CC_ENABLE_CXX_FLAGS}" ]; then
   336         extra_config+=("--enable-cxx-flags=${CC_ENABLE_CXX_FLAGS}")
   337     fi
   338 
   339     # When companion libraries are build static (eg !shared),
   340     # the libstdc++ is not pulled automatically, although it
   341     # is needed. Shoe-horn it in our LDFLAGS
   342     if [ "${CT_COMPLIBS_SHARED}" != "y" ]; then
   343         final_LDFLAGS='-lstdc++'
   344     fi
   345     if [ "${CT_CC_GCC_USE_GMP_MPFR}" = "y" ]; then
   346         extra_config+=("--with-gmp=${CT_COMPLIBS_DIR}")
   347         extra_config+=("--with-mpfr=${CT_COMPLIBS_DIR}")
   348     fi
   349     if [ "${CT_CC_GCC_USE_PPL_CLOOG_MPC}" = "y" ]; then
   350         extra_config+=("--with-ppl=${CT_COMPLIBS_DIR}")
   351         extra_config+=("--with-cloog=${CT_COMPLIBS_DIR}")
   352         extra_config+=("--with-mpc=${CT_COMPLIBS_DIR}")
   353     fi
   354     if [ "${CT_CC_GCC_USE_LIBELF}" = "y" ]; then
   355         extra_config+=("--with-libelf=${CT_COMPLIBS_DIR}")
   356     fi
   357 
   358     if [ "${CT_THREADS}" = "none" ]; then
   359         extra_config+=("--disable-threads")
   360         if [ "${CT_CC_GCC_4_2_or_later}" = y ]; then
   361             extra_config+=("--disable-libgomp")
   362         fi
   363     else
   364         extra_config+=("--enable-threads=posix")
   365     fi
   366 
   367     if [ "${CT_CC_GCC_ENABLE_TARGET_OPTSPACE}" = "y" ]; then
   368         extra_config+=("--enable-target-optspace")
   369     fi
   370 
   371     CT_DoLog DEBUG "Extra config passed: '${extra_config[*]}'"
   372 
   373     # --enable-symvers=gnu really only needed for sh4 to work around a
   374     # detection problem only matters for gcc-3.2.x and later, I think.
   375     # --disable-nls to work around crash bug on ppc405, but also because
   376     # embedded systems don't really need message catalogs...
   377     CC_FOR_BUILD="${CT_BUILD}-gcc"                  \
   378     CFLAGS="${CT_CFLAGS_FOR_HOST}"                  \
   379     LDFLAGS="${final_LDFLAGS}"                      \
   380     CFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}"         \
   381     CXXFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}"       \
   382     LDFLAGS_FOR_TARGET="${CT_TARGET_LDFLAGS}"       \
   383     CT_DoExecLog ALL                                \
   384     "${CT_SRC_DIR}/gcc-${CT_CC_VERSION}/configure"  \
   385         --build=${CT_BUILD}                         \
   386         --host=${CT_HOST}                           \
   387         --target=${CT_TARGET}                       \
   388         --prefix="${CT_PREFIX_DIR}"                 \
   389         ${CC_SYSROOT_ARG}                           \
   390         "${extra_config[@]}"                        \
   391         --with-local-prefix="${CT_SYSROOT_DIR}"     \
   392         --disable-nls                               \
   393         --enable-symvers=gnu                        \
   394         --enable-c99                                \
   395         --enable-long-long                          \
   396         ${CT_CC_EXTRA_CONFIG}
   397 
   398     if [ "${CT_CANADIAN}" = "y" ]; then
   399         CT_DoLog EXTRA "Building libiberty"
   400         CT_DoExecLog ALL make ${PARALLELMFLAGS} all-build-libiberty
   401     fi
   402 
   403     CT_DoLog EXTRA "Building final compiler"
   404     CT_DoExecLog ALL make ${PARALLELMFLAGS} all
   405 
   406     CT_DoLog EXTRA "Installing final compiler"
   407     CT_DoExecLog ALL make install
   408 
   409     # Create a symlink ${CT_TARGET}-cc to ${CT_TARGET}-gcc to always be able
   410     # to call the C compiler with the same, somewhat canonical name.
   411     # check whether compiler has an extension
   412     file="$( ls -1 "${CT_TARGET}/bin/${CT_TARGET}-gcc."* 2>/dev/null || true )"
   413     [ -z "${file}" ] || ext=".${file##*.}"
   414     CT_DoExecLog ALL ln -sv "${CT_TARGET}-gcc${ext}" "${CT_PREFIX_DIR}/bin/${CT_TARGET}-cc${ext}"
   415 
   416     CT_EndStep
   417 }