scripts/build/binutils/binutils.sh
author "Yann E. MORIN" <yann.morin.1998@free.fr>
Sun May 11 23:43:52 2014 +0200 (2014-05-11)
changeset 3320 78af1c99bc6d
parent 3316 42590ff8521c
permissions -rw-r--r--
scripts/functions: add target_endian_le and target_endian_be

We currently define target_endian_el and target_endian_eb to be the
tuple extension depending on endianness, defined to be respectively
'el' or 'eb' according to the endianness.

Some architecture do not use 'el' or 'eb', but use 'le' or 'be'.

Provide that as well, as two new variables: target_endian_le and
target_endian_be.

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