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