scripts/crosstool.sh
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue May 08 20:24:26 2007 +0000 (2007-05-08)
changeset 80 d62d61c9de7b
parent 78 c3868084d81a
child 81 a20be197429a
permissions -rwxr-xr-x
Beautify the progress bar.
     1 #!/bin/bash
     2 # Copyright 2007 Yann E. MORIN
     3 # Licensed under the GPL v2. See COPYING in the root of this package.
     4 
     5 # This is the main entry point to crosstool
     6 # This will:
     7 #   - download, extract and patch the toolchain components
     8 #   - build and install each components in turn
     9 #   - and eventually test the resulting toolchain
    10 
    11 # What this file does is prepare the environment, based upon the user-choosen
    12 # options. It also checks the existing environment for un-friendly variables,
    13 # and checks for needed tools. It eventually calls the main build script.
    14 
    15 # User must set CT_TOP_DIR in is environment!
    16 # Once we can build out-of-tree, then this will have to go.
    17 if [ -z "${CT_TOP_DIR}" -o ! -d "${CT_TOP_DIR}" ]; then
    18     # We don't have the functions right now, because we don't have CT_TOP_DIR.
    19     # Do the print stuff by hand:
    20     echo "CT_TOP_DIR not set. You must set CT_TOP_DIR to the top directory where crosstool is installed."
    21     exit 1
    22 fi
    23 
    24 # Parse the common functions
    25 . "${CT_TOP_DIR}/scripts/functions"
    26 
    27 CT_STAR_DATE=`CT_DoDate +%s%N`
    28 CT_STAR_DATE_HUMAN=`CT_DoDate +%Y%m%d.%H%M%S`
    29 
    30 # Log to a temporary file until we have built our environment
    31 CT_ACTUAL_LOG_FILE="${CT_TOP_DIR}/$$.log"
    32 
    33 # CT_TOP_DIR should be an absolute path.
    34 CT_TOP_DIR="`CT_MakeAbsolutePath \"${CT_TOP_DIR}\"`"
    35 
    36 # Parse the configuration file
    37 CT_TestOrAbort "Configuration file not found. Please create one." -f "${CT_TOP_DIR}/.config"
    38 . "${CT_TOP_DIR}/.config"
    39 
    40 # The progress bar indicator is asked for
    41 if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
    42     _CT_PROG_BAR_DATE() {
    43         local str=`CT_DoDate +%s`
    44         local elapsed=$((str-(CT_STAR_DATE/(1000*1000*1000))))
    45         printf "[%02d:%02d]" $((elapsed/60)) $((elapsed%60))
    46     }
    47     _CT_PROG_BAR() {
    48         [ $((cpt/10)) -eq 0 ] && echo -en "\r`_CT_PROG_BAR_DATE` /"
    49         [ $((cpt/10)) -eq 1 ] && echo -en "\r`_CT_PROG_BAR_DATE` -"
    50         [ $((cpt/10)) -eq 2 ] && echo -en "\r`_CT_PROG_BAR_DATE` \\"
    51         [ $((cpt/10)) -eq 3 ] && echo -en "\r`_CT_PROG_BAR_DATE` |"
    52         cpt=$(((cpt+1)%40))
    53     }
    54     CT_PROG_BAR=_CT_PROG_BAR
    55     export -f _CT_PROG_BAR
    56 else
    57     CT_PROG_BAR=
    58 fi
    59 
    60 # Apply the color scheme if needed
    61 if [ "${CT_LOG_USE_COLORS}" = "y" ]; then
    62     CT_ERROR_COLOR="${_A_NOR}${_A_BRI}${_F_RED}"
    63     CT_WARN_COLOR="${_A_NOR}${_A_BRI}${_F_YEL}"
    64     CT_INFO_COLOR="${_A_NOR}${_A_BRI}${_F_GRN}"
    65     CT_EXTRA_COLOR="${_A_NOR}${_A_DIM}${_F_GRN}"
    66     CT_DEBUG_COLOR="${_A_NOR}${_A_BRI}${_F_BLU}"
    67     CT_ALL_COLOR="${_A_NOR}${_A_DIM}${_F_WHI}"
    68     CT_NORMAL_COLOR="${_A_NOR}"
    69 else
    70     CT_ERROR_COLOR=
    71     CT_WARN_COLOR=
    72     CT_INFO_COLOR=
    73     CT_EXTRA_COLOR=
    74     CT_DEBUG_COLOR=
    75     CT_ALL_COLOR=
    76     CT_NORMAL_COLOR=
    77 fi
    78 
    79 # Yes! We can do full logging from now on!
    80 CT_DoLog INFO "Build started ${CT_STAR_DATE_HUMAN}"
    81 
    82 # renice oursleves
    83 renice ${CT_NICE} $$ |CT_DoLog DEBUG
    84 
    85 # Some sanity checks in the environment and needed tools
    86 CT_DoLog INFO "Checking environment sanity"
    87 
    88 # Enable known ordering of files in directory listings:
    89 CT_Test "Crosstool-NG might not work as expected with LANG=\"${LANG}\"" -n "${LANG}"
    90 case "${LC_COLLATE},${LC_ALL}" in
    91   # These four combinations are known to sort files in the correct order:
    92   fr_FR*,)  ;;
    93   en_US*,)  ;;
    94   *,fr_FR*) ;;
    95   *,en_US*) ;;
    96   # Anything else is destined to be borked if not gracefuly handled:
    97   *) CT_DoLog WARN "Either LC_COLLATE=\"${LC_COLLATE}\" or LC_ALL=\"${LC_ALL}\" is not supported."
    98      export LC_ALL=`locale -a |egrep "^(fr_FR|en_US)" |head -n 1`
    99      CT_TestOrAbort "Neither en_US* nor fr_FR* locales found on your system." -n "${LC_ALL}"
   100      CT_DoLog WARN "Forcing to known working LC_ALL=\"${LC_ALL}\"."
   101      ;;
   102 esac
   103 
   104 # Other environment sanity checks
   105 CT_TestAndAbort "Don't set LD_LIBRARY_PATH. It screws up the build." -n "${LD_LIBRARY_PATH}"
   106 CT_TestAndAbort "Don't set CFLAGS. It screws up the build." -n "${CFLAGS}"
   107 CT_TestAndAbort "Don't set CXXFLAGS. It screws up the build." -n "${CXXFLAGS}"
   108 CT_Test "GREP_OPTIONS screws up the build. Resetting." -n "${GREP_OPTIONS}"
   109 GREP_OPTIONS=
   110 CT_HasOrAbort awk
   111 CT_HasOrAbort sed
   112 CT_HasOrAbort bison
   113 CT_HasOrAbort flex
   114 
   115 CT_DoStep DEBUG "Dumping crosstool-NG configuration"
   116 cat ${CT_TOP_DIR}/.config |egrep '^(# |)CT_' |CT_DoLog DEBUG
   117 CT_EndStep
   118 
   119 CT_DoLog INFO "Building environment variables"
   120 
   121 # Target triplet: CT_TARGET needs a little love:
   122 CT_DoBuildTargetTriplet
   123 
   124 # Now, build up the variables from the user-configured options.
   125 CT_KERNEL_FILE="${CT_KERNEL}-${CT_KERNEL_VERSION}"
   126 CT_BINUTILS_FILE="binutils-${CT_BINUTILS_VERSION}"
   127 if [ "${CT_CC_USE_CORE}" != "y" ]; then
   128     CT_CC_CORE="${CT_CC}"
   129     CT_CC_CORE_VERSION="${CT_CC_VERSION}"
   130     CT_CC_CORE_EXTRA_CONFIG="${CT_CC_EXTRA_CONFIG}"
   131 fi
   132 CT_CC_CORE_FILE="${CT_CC_CORE}-${CT_CC_CORE_VERSION}"
   133 CT_CC_FILE="${CT_CC}-${CT_CC_VERSION}"
   134 CT_LIBC_FILE="${CT_LIBC}-${CT_LIBC_VERSION}"
   135 [ "${CT_ARCH_FLOAT_SW_LIBFLOAT}" = "y" ] && CT_LIBFLOAT_FILE="libfloat-990616"
   136 
   137 # Kludge: If any of the configured options needs CT_TARGET or CT_TOP_DIR,
   138 # then rescan the options file now:
   139 . "${CT_TOP_DIR}/.config"
   140 
   141 # Some more sanity checks now that we have all paths set up
   142 case "${CT_TARBALLS_DIR},${CT_SRC_DIR},${CT_BUILD_DIR},${CT_PREFIX_DIR},${CT_INSTALL_DIR}" in
   143     *" "*) CT_Abort "Don't use spaces in paths, it breaks things.";;
   144 esac
   145 
   146 # Note: we'll always install the core compiler in its own directory, so as to
   147 # not mix the two builds: core and final. Anyway, its generic, wether we use
   148 # a different compiler as core, or not.
   149 CT_CC_CORE_PREFIX_DIR="${CT_BUILD_DIR}/${CT_CC}-core"
   150 
   151 # Good, now grab a bit of informations on the system we're being run,
   152 # just in case something goes awok, and it's not our fault:
   153 CT_SYS_HOSTNAME=`hostname -f 2>/dev/null || true`
   154 # Hmmm. Some non-DHCP-enabled machines do not have an FQDN... Fall back to node name.
   155 CT_SYS_HOSTNAME="${CT_SYS_HOSTNAME:-`uname -n`}"
   156 CT_SYS_KERNEL=`uname -s`
   157 CT_SYS_REVISION=`uname -r`
   158 # MacOS X lacks '-o' :
   159 CT_SYS_OS=`uname -o || echo "Unknown (maybe MacOS-X)"`
   160 CT_SYS_MACHINE=`uname -m`
   161 CT_SYS_PROCESSOR=`uname -p`
   162 CT_SYS_USER="`id -un`"
   163 CT_SYS_DATE=`CT_DoDate +%Y%m%d.%H%M%S`
   164 CT_SYS_GCC=`gcc -dumpversion`
   165 CT_TOOLCHAIN_ID="crosstool-${CT_VERSION} build ${CT_SYS_DATE} by ${CT_SYS_USER}@${CT_SYS_HOSTNAME} for ${CT_TARGET}"
   166 
   167 # Check now if we can write to the destination directory:
   168 if [ -d "${CT_INSTALL_DIR}" ]; then
   169     CT_TestAndAbort "Destination directory \"${CT_INSTALL_DIR}\" is not removable" ! -w `dirname "${CT_INSTALL_DIR}"`
   170 fi
   171 
   172 # Get rid of pre-existing installed toolchain and previous build directories.
   173 # We need to do that _before_ we can safely log, because the log file will
   174 # most probably be in the toolchain directory.
   175 if [ -d "${CT_INSTALL_DIR}" ]; then
   176     mv "${CT_INSTALL_DIR}" "${CT_INSTALL_DIR}.$$"
   177     nohup rm -rf "${CT_INSTALL_DIR}.$$" >/dev/null 2>&1 &
   178 fi
   179 if [ -d "${CT_BUILD_DIR}" ]; then
   180     mv "${CT_BUILD_DIR}" "${CT_BUILD_DIR}.$$"
   181     nohup rm -rf "${CT_BUILD_DIR}.$$" >/dev/null 2>&1 &
   182 fi
   183 if [ "${CT_FORCE_EXTRACT}" = "y" -a -d "${CT_SRC_DIR}" ]; then
   184     mv "${CT_SRC_DIR}" "${CT_SRC_DIR}.$$"
   185     nohup rm -rf "${CT_SRC_DIR}.$$" >/dev/null 2>&1 &
   186 fi
   187 mkdir -p "${CT_INSTALL_DIR}"
   188 mkdir -p "${CT_BUILD_DIR}"
   189 mkdir -p "${CT_TARBALLS_DIR}"
   190 mkdir -p "${CT_SRC_DIR}"
   191 
   192 # Make all path absolute, it so much easier!
   193 # Now we have had the directories created, we even will get rid of embedded .. in paths:
   194 CT_SRC_DIR="`CT_MakeAbsolutePath \"${CT_SRC_DIR}\"`"
   195 CT_TARBALLS_DIR="`CT_MakeAbsolutePath \"${CT_TARBALLS_DIR}\"`"
   196 
   197 # Redirect log to the actual log file now we can
   198 # It's quite understandable that the log file will be installed in the install
   199 # directory, so we must first ensure it exists and is writeable (above) before
   200 # we can log there
   201 case "${CT_LOG_TO_FILE},${CT_LOG_FILE}" in
   202     ,*)   rm -f "${CT_ACTUAL_LOG_FILE}"
   203           CT_ACTUAL_LOG_FILE=/dev/null
   204           ;;
   205     y,/*) mkdir -p "`dirname \"${CT_LOG_FILE}\"`"
   206           mv "${CT_ACTUAL_LOG_FILE}" "${CT_LOG_FILE}"
   207           CT_ACTUAL_LOG_FILE="${CT_LOG_FILE}"
   208           ;;
   209     y,*)  mkdir -p "`pwd`/`dirname \"${CT_LOG_FILE}\"`"
   210           mv "${CT_ACTUAL_LOG_FILE}" "`pwd`/${CT_LOG_FILE}"
   211           CT_ACTUAL_LOG_FILE="`pwd`/${CT_LOG_FILE}"
   212           ;;
   213 esac
   214 
   215 # Determine build system if not set by the user
   216 CT_Test "You did not specify the build system. Guessing." -z "${CT_BUILD}"
   217 CT_BUILD="`${CT_TOP_DIR}/tools/config.sub \"${CT_BUILD:-\`${CT_TOP_DIR}/tools/config.guess\`}\"`"
   218 
   219 # Arrange paths depending on wether we use sys-root or not.
   220 if [ "${CT_USE_SYSROOT}" = "y" ]; then
   221     CT_SYSROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}/sys-root"
   222     CT_HEADERS_DIR="${CT_SYSROOT_DIR}/usr/include"
   223     BINUTILS_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
   224     CC_CORE_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
   225     CC_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
   226     LIBC_SYSROOT_ARG=""
   227     # glibc's prefix must be exactly /usr, else --with-sysroot'd gcc will get
   228     # confused when $sysroot/usr/include is not present.
   229     # Note: --prefix=/usr is magic!
   230     # See http://www.gnu.org/software/libc/FAQ.html#s-2.2
   231 else
   232     # plain old way. All libraries in prefix/target/lib
   233     CT_SYSROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}"
   234     CT_HEADERS_DIR="${CT_SYSROOT_DIR}/include"
   235     # hack!  Always use --with-sysroot for binutils.
   236     # binutils 2.14 and later obey it, older binutils ignore it.
   237     # Lets you build a working 32->64 bit cross gcc
   238     BINUTILS_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
   239     # Use --with-headers, else final gcc will define disable_glibc while
   240     # building libgcc, and you'll have no profiling
   241     CC_CORE_SYSROOT_ARG="--without-headers"
   242     CC_SYSROOT_ARG="--with-headers=${CT_HEADERS_DIR}"
   243     LIBC_SYSROOT_ARG="prefix="
   244 fi
   245 
   246 # Prepare the 'lib' directories in sysroot, else the ../lib64 hack used by
   247 # 32 -> 64 bit crosscompilers won't work, and build of final gcc will fail with
   248 #  "ld: cannot open crti.o: No such file or directory"
   249 mkdir -p "${CT_SYSROOT_DIR}/lib"
   250 mkdir -p "${CT_SYSROOT_DIR}/usr/lib"
   251 
   252 # Canadian-cross are really picky on the way they are built. Tweak the values.
   253 if [ "${CT_CANADIAN}" = "y" ]; then
   254     # Arrange so that gcc never, ever think that build system == host system
   255     CT_CANADIAN_OPT="--build=`echo \"${CT_BUILD}\" |sed -r -e 's/-/-build_/'`"
   256     # We shall have a compiler for this target!
   257     # Do test here...
   258 else
   259     CT_HOST="${CT_BUILD}"
   260     CT_CANADIAN_OPT=
   261     # Add the target toolchain in the path so that we can build the C library
   262     export PATH="${CT_PREFIX_DIR}/bin:${CT_CC_CORE_PREFIX_DIR}/bin:${PATH}"
   263 fi
   264 
   265 # Modify GCC_HOST to never be equal to $BUILD or $TARGET
   266 # This strange operation causes gcc to always generate a cross-compiler
   267 # even if the build machine is the same kind as the host.
   268 # This is why CC has to be set when doing a canadian cross; you can't find a
   269 # host compiler by appending -gcc to our whacky $GCC_HOST
   270 # Kludge: it is reported that the above causes canadian crosses with cygwin
   271 # hosts to fail, so avoid it just in that one case.  It would be cleaner to
   272 # just move this into the non-canadian case above, but I'm afraid that might
   273 # cause some configure script somewhere to decide that since build==host, they
   274 # could run host binaries.
   275 # (Copied almost as-is from original crosstool):
   276 case "${CT_KERNEL},${CT_CANADIAN}" in
   277     cygwin,y) ;;
   278     *)        CT_HOST="`echo \"${CT_HOST}\" |sed -r -e 's/-/-host_/;'`";;
   279 esac
   280 
   281 # Ah! Recent versions of binutils need some of the build and/or host system
   282 # (read CT_BUILD and CT_HOST) tools to be accessible (ar is but an example).
   283 # Do that:
   284 CT_DoLog EXTRA "Making build system tools available"
   285 mkdir -p "${CT_PREFIX_DIR}/bin"
   286 for tool in ar; do
   287     ln -s "`which ${tool}`" "${CT_PREFIX_DIR}/bin/${CT_BUILD}-${tool}"
   288     ln -s "`which ${tool}`" "${CT_PREFIX_DIR}/bin/${CT_HOST}-${tool}"
   289 done
   290 
   291 # Ha. cygwin host have an .exe suffix (extension) for executables.
   292 [ "${CT_KERNEL}" = "cygwin" ] && EXEEXT=".exe" || EXEEXT=""
   293 
   294 # Transform the ARCH into a kernel-understandable ARCH
   295 case "${CT_ARCH}" in
   296     x86) CT_KERNEL_ARCH=i386;;
   297     ppc) CT_KERNEL_ARCH=powerpc;;
   298     *)   CT_KERNEL_ARCH="${CT_ARCH}";;
   299 esac
   300 
   301 # Build up the TARGET_CFLAGS from user-provided options
   302 # Override with user-specified CFLAGS
   303 [ -n "${CT_ARCH_CPU}" ]  && CT_TARGET_CFLAGS="-mcpu=${CT_ARCH_CPU} ${CT_TARGET_CFLAGS}"
   304 [ -n "${CT_ARCH_TUNE}" ] && CT_TARGET_CFLAGS="-mtune=${CT_ARCH_TUNE} ${CT_TARGET_CFLAGS}"
   305 [ -n "${CT_ARCH_ARCH}" ] && CT_TARGET_CFLAGS="-march=${CT_ARCH_ARCH} ${CT_TARGET_CFLAGS}"
   306 [ -n "${CT_ARCH_FPU}" ]  && CT_TARGET_CFLAGS="-mfpu=${CT_ARCH_FPU} ${CT_TARGET_CFLAGS}"
   307 
   308 # Help gcc
   309 CT_CFLAGS_FOR_HOST=
   310 [ "${CT_USE_PIPES}" = "y" ] && CT_CFLAGS_FOR_HOST="${CT_CFLAGS_FOR_HOST} -pipe"
   311 
   312 # And help make go faster
   313 PARALLELMFLAGS=
   314 [ ${CT_PARALLEL_JOBS} -ne 0 ] && PARALLELMFLAGS="${PARALLELMFLAGS} -j${CT_PARALLEL_JOBS}"
   315 [ ${CT_LOAD} -ne 0 ] && PARALLELMFLAGS="${PARALLELMFLAGS} -l${CT_LOAD}"
   316 
   317 CT_DoStep EXTRA "Dumping internal crosstool-NG configuration"
   318 CT_DoLog EXTRA "Building a toolchain for:"
   319 CT_DoLog EXTRA "  build  = ${CT_BUILD}"
   320 CT_DoLog EXTRA "  host   = ${CT_HOST}"
   321 CT_DoLog EXTRA "  target = ${CT_TARGET}"
   322 set |egrep '^CT_.+=' |sort |CT_DoLog DEBUG
   323 CT_EndStep
   324 
   325 # Include sub-scripts instead of calling them: that way, we do not have to
   326 # export any variable, nor re-parse the configuration and functions files.
   327 . "${CT_TOP_DIR}/scripts/build/kernel_${CT_KERNEL}.sh"
   328 . "${CT_TOP_DIR}/scripts/build/binutils.sh"
   329 . "${CT_TOP_DIR}/scripts/build/libc_libfloat.sh"
   330 . "${CT_TOP_DIR}/scripts/build/libc_${CT_LIBC}.sh"
   331 . "${CT_TOP_DIR}/scripts/build/cc_core_${CT_CC_CORE}.sh"
   332 . "${CT_TOP_DIR}/scripts/build/cc_${CT_CC}.sh"
   333 
   334 # Now for the job by itself. Go have a coffee!
   335 if [ "${CT_NO_DOWNLOAD}" != "y" ]; then
   336 	CT_DoStep INFO "Retrieving needed toolchain components' tarballs"
   337     do_kernel_get
   338     do_binutils_get
   339     do_cc_core_get
   340     do_libfloat_get
   341     do_libc_get
   342     do_cc_get
   343     CT_EndStep
   344 fi
   345 
   346 if [ "${CT_ONLY_DOWNLOAD}" != "y" ]; then
   347     if [ "${CT_FORCE_EXTRACT}" = "y" ]; then
   348         mv "${CT_SRC_DIR}" "${CT_SRC_DIR}.$$"
   349         nohup rm -rf "${CT_SRC_DIR}.$$" >/dev/null 2>&1
   350     fi
   351     CT_DoStep INFO "Extracting and patching toolchain components"
   352     do_kernel_extract
   353     do_binutils_extract
   354     do_libc_extract
   355     do_libfloat_extract
   356     do_cc_core_extract
   357     do_cc_extract
   358     CT_EndStep
   359 
   360     if [ "${CT_ONLY_EXTRACT}" != "y" ]; then
   361         do_libc_check_config
   362         do_kernel_check_config
   363         do_kernel_headers
   364         do_binutils
   365         do_libc_headers
   366         do_cc_core
   367         do_libfloat
   368         do_libc
   369         do_cc
   370         do_libc_finish
   371     fi
   372 fi
   373 
   374 if [ -n "${CT_TARGET_ALIAS}" ]; then
   375     CT_DoLog EXTRA "Creating symlinks from \"${CT_TARGET}-*\" to \"${CT_TARGET_ALIAS}-*\""
   376     CT_Pushd "${CT_PREFIX_DIR}/bin"
   377     for t in "${CT_TARGET}-"*; do
   378         _t="`echo \"$t\" |sed -r -e 's/^'\"${CT_TARGET}\"'-/'\"${CT_TARGET_ALIAS}\"'-/;'`"
   379         CT_DoLog DEBUG "Linking \"${_t}\" -> \"${t}\""
   380         ln -s "${t}" "${_t}"
   381     done
   382     CT_Popd
   383 fi
   384 
   385 if [ "${CT_REMOVE_DOCS}" = "y" ]; then
   386 	CT_DoLog INFO "Removing installed documentation"
   387     rm -rf "${CT_PREFIX_DIR}/"{man,info}
   388 fi
   389 
   390 CT_STOP_DATE=`CT_DoDate +%s%N`
   391 CT_STOP_DATE_HUMAN=`CT_DoDate +%Y%m%d.%H%M%S`
   392 CT_DoLog INFO "Build completed at ${CT_STOP_DATE_HUMAN}"
   393 elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
   394 elapsed_min=$((elapsed/(60*1000*1000*1000)))
   395 elapsed_sec=`printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000)))`
   396 elapsed_csec=`printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000)))`
   397 CT_DoLog INFO "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
   398 
   399 # Restore a 'normal' color setting
   400 echo -en "${CT_NORMAL_COLOR}"
   401 
   402 trap - EXIT