scripts/functions
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Jul 01 19:04:20 2007 +0000 (2007-07-01)
changeset 182 223c84ec2d90
parent 165 a291bfa17715
child 210 98baeb928964
permissions -rw-r--r--
Merge the build system to trunk: ct-ng is now installable:
- ./configure --prefix=/some/place
- make
- make install
- export PATH="${PATH}:/some/place/bin"
- ct-ng <action>
     1 # This file contains some usefull common functions
     2 # Copyright 2007 Yann E. MORIN
     3 # Licensed under the GPL v2. See COPYING in the root of this package
     4 
     5 # Prepare the fault handler
     6 CT_OnError() {
     7     ret=$?
     8     CT_DoLog ERROR "Build failed in step \"${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}\""
     9     for((step=(CT_STEP_COUNT-1); step>1; step--)); do
    10         CT_DoLog ERROR "      called in step \"${CT_STEP_MESSAGE[${step}]}\""
    11     done
    12     CT_DoLog ERROR "Error happened in \"${BASH_SOURCE[1]}\" in function \"${FUNCNAME[1]}\" (line unknown, sorry)"
    13     for((depth=2; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
    14         CT_DoLog ERROR "      called from \"${BASH_SOURCE[${depth}]}\" at line # ${BASH_LINENO[${depth}-1]} in function \"${FUNCNAME[${depth}]}\""
    15     done
    16     [ "${CT_LOG_TO_FILE}" = "y" ] && CT_DoLog ERROR "Look at \"${CT_LOG_FILE}\" for more info on this error."
    17     CT_STEP_COUNT=1
    18     CT_DoEnd ERROR
    19     exit $ret
    20 }
    21 
    22 # Install the fault handler
    23 trap CT_OnError ERR
    24 
    25 # Inherit the fault handler in subshells and functions
    26 set -E
    27 
    28 # Make pipes fail on the _first_ failed command
    29 # Not supported on bash < 3.x, but we need it, so drop the obsoleting bash-2.x
    30 set -o pipefail
    31 
    32 # Don't hash commands' locations, and search every time it is requested.
    33 # This is slow, but needed because of the static/shared core gcc which shall
    34 # always match to shared if it exists, and only fallback to static if the
    35 # shared is not found
    36 set +o hashall
    37 
    38 # Log policy:
    39 #  - first of all, save stdout so we can see the live logs: fd #6
    40 exec 6>&1
    41 #  - then point stdout to the log file (temporary for now)
    42 tmp_log_file="${CT_TOP_DIR}/log.$$"
    43 exec >>"${tmp_log_file}"
    44 
    45 # The different log levels:
    46 CT_LOG_LEVEL_ERROR=0
    47 CT_LOG_LEVEL_WARN=1
    48 CT_LOG_LEVEL_INFO=2
    49 CT_LOG_LEVEL_EXTRA=3
    50 CT_LOG_LEVEL_DEBUG=4
    51 CT_LOG_LEVEL_ALL=5
    52 
    53 # A function to log what is happening
    54 # Different log level are available:
    55 #   - ERROR:   A serious, fatal error occurred
    56 #   - WARN:    A non fatal, non serious error occurred, take your responsbility with the generated build
    57 #   - INFO:    Informational messages
    58 #   - EXTRA:   Extra informational messages
    59 #   - DEBUG:   Debug messages
    60 #   - ALL:     Component's build messages
    61 # Usage: CT_DoLog <level> [message]
    62 # If message is empty, then stdin will be logged.
    63 CT_DoLog() {
    64     local max_level LEVEL level cur_l cur_L
    65     local l
    66     eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
    67     # Set the maximum log level to DEBUG if we have none
    68     [ -z "${max_level}" ] && max_level=${CT_LOG_LEVEL_DEBUG}
    69 
    70     LEVEL="$1"; shift
    71     eval level="\${CT_LOG_LEVEL_${LEVEL}}"
    72 
    73     if [ $# -eq 0 ]; then
    74         cat -
    75     else
    76         echo "${1}"
    77     fi |( IFS="\n" # We want the full lines, even leading spaces
    78           CT_PROG_BAR_CPT=0
    79           indent=$((2*CT_STEP_COUNT))
    80           while read line; do
    81               case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
    82                 y,*"warning:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
    83                 y,*"WARNING:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
    84                 *"error:"*)             cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
    85                 *"make["?*"]:"*"Stop.") cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
    86                 *)                      cur_L="${LEVEL}"; cur_l="${level}";;
    87               esac
    88               l="`printf \"[%-5s]%*s%s%s\" \"${cur_L}\" \"${indent}\" \" \" \"${line}\"`"
    89               # There will always be a log file, be it /dev/null
    90               echo -e "${l}"
    91               if [ ${cur_l} -le ${max_level} ]; then
    92                   echo -e "\r${l}" >&6
    93               fi
    94               if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
    95                   [ ${CT_PROG_BAR_CPT} -eq 0  ] && bar="/"
    96                   [ ${CT_PROG_BAR_CPT} -eq 10 ] && bar="-"
    97                   [ ${CT_PROG_BAR_CPT} -eq 20 ] && bar="\\"
    98                   [ ${CT_PROG_BAR_CPT} -eq 30 ] && bar="|"
    99                   printf "\r[%02d:%02d] %s " $((SECONDS/60)) $((SECONDS%60)) "${bar}" >&6
   100                   CT_PROG_BAR_CPT=$(((CT_PROG_BAR_CPT+1)%40))
   101               fi
   102           done
   103         )
   104 
   105     return 0
   106 }
   107 
   108 # Tail message to be logged whatever happens
   109 # Usage: CT_DoEnd <level>
   110 CT_DoEnd()
   111 {
   112     local level="$1"
   113     CT_STOP_DATE=`CT_DoDate +%s%N`
   114     CT_STOP_DATE_HUMAN=`CT_DoDate +%Y%m%d.%H%M%S`
   115     CT_DoLog "${level:-INFO}" "Build completed at ${CT_STOP_DATE_HUMAN}"
   116     elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
   117     elapsed_min=$((elapsed/(60*1000*1000*1000)))
   118     elapsed_sec=`printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000)))`
   119     elapsed_csec=`printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000)))`
   120     CT_DoLog ${level:-INFO} "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
   121 }
   122 
   123 # Abort the execution with an error message
   124 # Usage: CT_Abort <message>
   125 CT_Abort() {
   126     CT_DoLog ERROR "$1"
   127     exit 1
   128 }
   129 
   130 # Test a condition, and print a message if satisfied
   131 # Usage: CT_Test <message> <tests>
   132 CT_Test() {
   133     local ret
   134     local m="$1"
   135     shift
   136     test "$@" && CT_DoLog WARN "$m"
   137     return 0
   138 }
   139 
   140 # Test a condition, and abort with an error message if satisfied
   141 # Usage: CT_TestAndAbort <message> <tests>
   142 CT_TestAndAbort() {
   143     local m="$1"
   144     shift
   145     test "$@" && CT_Abort "$m"
   146     return 0
   147 }
   148 
   149 # Test a condition, and abort with an error message if not satisfied
   150 # Usage: CT_TestAndAbort <message> <tests>
   151 CT_TestOrAbort() {
   152     local m="$1"
   153     shift
   154     test "$@" || CT_Abort "$m"
   155     return 0
   156 }
   157 
   158 # Test the presence of a tool, or abort if not found
   159 # Usage: CT_HasOrAbort <tool>
   160 CT_HasOrAbort() {
   161     CT_TestAndAbort "\"${1}\" not found and needed for successfull toolchain build." -z "`which \"${1}\"`"
   162     return 0
   163 }
   164 
   165 # Get current date with nanosecond precision
   166 # On those system not supporting nanosecond precision, faked with rounding down
   167 # to the highest entire second
   168 # Usage: CT_DoDate <fmt>
   169 CT_DoDate() {
   170     date "$1" |sed -r -e 's/%N$/000000000/;'
   171 }
   172 
   173 CT_STEP_COUNT=1
   174 CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
   175 # Memorise a step being done so that any error is caught
   176 # Usage: CT_DoStep <loglevel> <message>
   177 CT_DoStep() {
   178     local start=`CT_DoDate +%s%N`
   179     CT_DoLog "$1" "================================================================="
   180     CT_DoLog "$1" "$2"
   181     CT_STEP_COUNT=$((CT_STEP_COUNT+1))
   182     CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
   183     CT_STEP_START[${CT_STEP_COUNT}]="${start}"
   184     CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
   185     return 0
   186 }
   187 
   188 # End the step just being done
   189 # Usage: CT_EndStep
   190 CT_EndStep() {
   191     local stop=`CT_DoDate +%s%N`
   192     local duration=`printf "%032d" $((stop-${CT_STEP_START[${CT_STEP_COUNT}]})) |sed -r -e 's/([[:digit:]]{2})[[:digit:]]{7}$/\.\1/; s/^0+//; s/^\./0\./;'`
   193     local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
   194     local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
   195     CT_STEP_COUNT=$((CT_STEP_COUNT-1))
   196     CT_DoLog "${level}" "${message}: done in ${duration}s"
   197     return 0
   198 }
   199 
   200 # Pushes into a directory, and pops back
   201 CT_Pushd() {
   202     pushd "$1" >/dev/null 2>&1
   203 }
   204 CT_Popd() {
   205     popd >/dev/null 2>&1
   206 }
   207 
   208 # Makes a path absolute
   209 # Usage: CT_MakeAbsolutePath path
   210 CT_MakeAbsolutePath() {
   211     # Try to cd in that directory
   212     if [ -d "$1" ]; then
   213         CT_Pushd "$1"
   214         pwd
   215         CT_Popd
   216     else
   217         # No such directory, fail back to guessing
   218         case "$1" in
   219             /*)  echo "$1";;
   220             *)   echo "`pwd`/$1";;
   221         esac
   222     fi
   223     
   224     return 0
   225 }
   226 
   227 # Creates a temporary directory
   228 # $1: variable to assign to
   229 # Usage: CT_MktempDir foo
   230 CT_MktempDir() {
   231     # Some mktemp do not allow more than 6 Xs
   232     eval "$1"="`mktemp -q -d \"${CT_BUILD_DIR}/.XXXXXX\"`"
   233     CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
   234 }
   235 
   236 # Echoes the specified string on stdout until the pipe breaks.
   237 # Doesn't fail
   238 # $1: string to echo
   239 # Usage: CT_DoYes "" |make oldconfig
   240 CT_DoYes() {
   241     yes "$1" || true
   242 }
   243 
   244 # Get the file name extension of a component
   245 # Usage: CT_GetFileExtension <component_name-component_version>
   246 # If found, echoes the extension to stdout
   247 # If not found, echoes nothing on stdout.
   248 CT_GetFileExtension() {
   249     local ext
   250     local file="$1"
   251     local got_it=1
   252 
   253     CT_Pushd "${CT_TARBALLS_DIR}"
   254     # we need to also check for an empty extension for those very
   255     # peculiar components that don't have one (such as sstrip from
   256     # buildroot).
   257     for ext in .tar.gz .tar.bz2 .tgz .tar ''; do
   258         if [ -f "${file}${ext}" ]; then
   259             echo "${ext}"
   260             got_it=0
   261             break
   262         fi
   263     done
   264     CT_Popd
   265 
   266     return 0
   267 }
   268 
   269 # Download an URL using wget
   270 # Usage: CT_DoGetFileWget <URL>
   271 CT_DoGetFileWget() {
   272     # Need to return true because it is legitimate to not find the tarball at
   273     # some of the provided URLs (think about snapshots, different layouts for
   274     # different gcc versions, etc...)
   275     # Some (very old!) FTP server might not support the passive mode, thus
   276     # retry without
   277     # With automated download as we are doing, it can be very dangerous to use
   278     # -c to continue the downloads. It's far better to simply overwrite the
   279     # destination file
   280     wget -nc --progress=dot:binary --tries=3 --passive-ftp "$1" || wget -nc --progress=dot:binary --tries=3 "$1" || true
   281 }
   282 
   283 # Download an URL using curl
   284 # Usage: CT_DoGetFileCurl <URL>
   285 CT_DoGetFileCurl() {
   286 	# Note: comments about wget method are also valid here
   287 	# Plus: no good progreess indicator is available with curl,
   288 	#       so output is consigned to oblivion
   289 	curl --ftp-pasv -O --retry 3 "$1" >/dev/null || curl -O --retry 3 "$1" >/dev/null || true
   290 }
   291 
   292 # Wrapper function to call one of curl or wget
   293 # Usage: CT_DoGetFile <URL>
   294 CT_DoGetFile() {
   295     local _wget=`which wget`
   296     local _curl=`which curl`
   297     case "${_wget},${_curl}" in
   298         ,)  CT_DoError "Could find neither wget nor curl";;
   299         ,*) CT_DoGetFileCurl "$1" 2>&1 |CT_DoLog ALL;;
   300         *)  CT_DoGetFileWget "$1" 2>&1 |CT_DoLog ALL;;
   301     esac
   302 }
   303 
   304 # Download the file from one of the URLs passed as argument
   305 # Usage: CT_GetFile <filename> <url> [<url> ...]
   306 CT_GetFile() {
   307     local got_it
   308     local ext
   309     local url
   310     local file="$1"
   311     shift
   312 
   313     # Do we already have it?
   314     ext=`CT_GetFileExtension "${file}"`
   315     if [ -n "${ext}" ]; then
   316         CT_DoLog DEBUG "Already have \"${file}\""
   317         return 0
   318     fi
   319 
   320     CT_Pushd "${CT_TARBALLS_DIR}"
   321     # File not yet downloaded, try to get it
   322     got_it=0
   323     # We'd rather have a bzip2'ed tarball, then gzipped, and finally plain tar.
   324     # Try local copy first, if it exists
   325     for ext in .tar.bz2 .tar.gz .tgz .tar; do
   326         if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
   327              "${CT_FORCE_DOWNLOAD}" != "y" ]; then
   328             CT_DoLog EXTRA "Copying \"${file}\" from local copy"
   329             cp -v "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
   330             return 0
   331         fi
   332     done
   333     # Try to download it
   334     CT_DoLog EXTRA "Retrieving \"${file}\""
   335     for ext in .tar.bz2 .tar.gz .tgz .tar; do
   336         # Try all urls in turn
   337         for url in "$@"; do
   338             case "${url}" in
   339                 *)  CT_DoLog DEBUG "Trying \"${url}/${file}${ext}\""
   340                     CT_DoGetFile "${url}/${file}${ext}"
   341                     ;;
   342             esac
   343             [ -f "${file}${ext}" ] && return 0 || true
   344         done
   345     done
   346     CT_Popd
   347 
   348     CT_Abort "Could not download \"${file}\", and not present in \"${CT_LOCAL_TARBALLS_DIR}\""
   349 }
   350 
   351 # Extract a tarball and patch the resulting sources if necessary.
   352 # Some tarballs need to be extracted in specific places. Eg.: glibc addons
   353 # must be extracted in the glibc directory; uCLibc locales must be extracted
   354 # in the extra/locale sub-directory of uClibc.
   355 CT_ExtractAndPatch() {
   356     local file="$1"
   357     local base_file=`echo "${file}" |cut -d - -f 1`
   358     local ver_file=`echo "${file}" |cut -d - -f 2-`
   359     local official_patch_dir
   360     local custom_patch_dir
   361     local libc_addon
   362     local ext=`CT_GetFileExtension "${file}"`
   363     CT_TestAndAbort "\"${file}\" not found in \"${CT_TARBALLS_DIR}\"" -z "${ext}"
   364     local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
   365 
   366     CT_Pushd "${CT_SRC_DIR}"
   367 
   368     # Add-ons need a little love, really.
   369     case "${file}" in
   370         glibc-[a-z]*-*)
   371             CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
   372             cd "${CT_LIBC_FILE}"
   373             libc_addon=y
   374             [ -f ".${file}.extracted" ] && return 0
   375             touch ".${file}.extracted"
   376             ;;
   377         uClibc-locale-*)
   378             CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
   379             cd "${CT_LIBC_FILE}/extra/locale"
   380             libc_addon=y
   381             [ -f ".${file}.extracted" ] && return 0
   382             touch ".${file}.extracted"
   383             ;;
   384     esac
   385 
   386     # If the directory exists, then consider extraction and patching done
   387     if [ -d "${file}" ]; then
   388         CT_DoLog DEBUG "Already extracted \"${file}\""
   389         return 0
   390     fi
   391 
   392     CT_DoLog EXTRA "Extracting \"${file}\""
   393     case "${ext}" in
   394         .tar.bz2)     tar xvjf "${full_file}" |CT_DoLog ALL;;
   395         .tar.gz|.tgz) tar xvzf "${full_file}" |CT_DoLog ALL;;
   396         .tar)         tar xvf  "${full_file}" |CT_DoLog ALL;;
   397         *)            CT_Abort "Don't know how to handle \"${file}\": unknown extension" ;;
   398     esac
   399 
   400     # Snapshots might not have the version number in the extracted directory
   401     # name. This is also the case for some (old) packages, such as libfloat.
   402     # Overcome this issue by symlink'ing the directory.
   403     if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
   404         case "${ext}" in
   405             .tar.bz2)     base=`tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   406             .tar.gz|.tgz) base=`tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   407             .tar)         base=`tar tf  "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   408         esac
   409         CT_TestOrAbort "There was a problem when extracting \"${file}\"" -d "${base}" -o "${base}" != "${file}"
   410         ln -s "${base}" "${file}"
   411     fi
   412 
   413     # Kludge: outside this function, we wouldn't know if we had just extracted
   414     # a libc addon, or a plain package. Apply patches now.
   415     CT_DoLog EXTRA "Patching \"${file}\""
   416 
   417     if [ "${libc_addon}" = "y" ]; then
   418         # Some addons tarball directly contian the correct addon directory,
   419         # while others have the addon directory named ofter the tarball.
   420         # Fix that bu always using the short name (eg: linuxthreads, ports, etc...)
   421         addon_short_name=`echo "${file}" |sed -r -e 's/^[^-]+-//; s/-[^-]+$//;'`
   422         [ -d "${addon_short_name}" ] || ln -s "${file}" "${addon_short_name}"
   423         # If libc addon, we're already in the correct place
   424     else
   425         cd "${file}"
   426     fi
   427 
   428     official_patch_dir=
   429     custom_patch_dir=
   430     [ "${CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_LIB_DIR}/patches/${base_file}/${ver_file}"
   431     [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
   432     for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
   433         if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
   434             for p in "${patch_dir}"/*.patch; do
   435                 if [ -f "${p}" ]; then
   436                     CT_DoLog DEBUG "Applying patch \"${p}\""
   437                     patch -g0 -F1 -p1 -f <"${p}" |CT_DoLog ALL
   438                     CT_TestAndAbort "Failed while applying patch file \"${p}\"" ${PIPESTATUS[0]} -ne 0
   439                 fi
   440             done
   441         fi
   442     done
   443 
   444     CT_Popd
   445 }
   446 
   447 # Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
   448 # Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
   449 CT_DoConfigGuess() {
   450     if [ -x "${CT_TOP_DIR}/tools/config.guess" ]; then
   451         "${CT_TOP_DIR}/tools/config.guess"
   452     else
   453         "${CT_LIB_DIR}/tools/config.guess"
   454     fi
   455 }
   456 
   457 CT_DoConfigSub() {
   458     if [ -x "${CT_TOP_DIR}/tools/config.sub" ]; then
   459         "${CT_TOP_DIR}/tools/config.sub" "$@"
   460     else
   461         "${CT_LIB_DIR}/tools/config.sub" "$@"
   462     fi
   463 }
   464 
   465 # Compute the target triplet from what is provided by the user
   466 # Usage: CT_DoBuildTargetTriplet
   467 # In fact this function takes the environment variables to build the target
   468 # triplet. It is needed both by the normal build sequence, as well as the
   469 # sample saving sequence.
   470 CT_DoBuildTargetTriplet() {
   471     case "${CT_ARCH_BE},${CT_ARCH_LE}" in
   472         y,) target_endian_eb=eb; target_endian_el=;;
   473         ,y) target_endian_eb=; target_endian_el=el;;
   474     esac
   475     case "${CT_ARCH}" in
   476         arm)  CT_TARGET="${CT_ARCH}${target_endian_eb}";;
   477         mips) CT_TARGET="${CT_ARCH}${target_endian_el}";;
   478         x86*) # Much love for this one :-(
   479               arch="${CT_ARCH_ARCH}"
   480               [ -z "${arch}" ] && arch="${CT_ARCH_TUNE}"
   481               case "${CT_ARCH}" in
   482                   x86_64)  CT_TARGET=x86_64;;
   483               	  *)  case "${arch}" in
   484                           "")                                       CT_TARGET=i386;;
   485                           i386|i486|i586|i686)                      CT_TARGET="${arch}";;
   486                           winchip*)                                 CT_TARGET=i486;;
   487                           pentium|pentium-mmx|c3*)                  CT_TARGET=i586;;
   488                           nocona|athlon*64|k8|athlon-fx|opteron)    CT_TARGET=x86_64;;
   489                           pentiumpro|pentium*|athlon*)              CT_TARGET=i686;;
   490                           *)                                        CT_TARGET=i586;;
   491                       esac;;
   492               esac;;
   493     esac
   494     case "${CT_TARGET_VENDOR}" in
   495         "") CT_TARGET="${CT_TARGET}-unknown";;
   496         *)  CT_TARGET="${CT_TARGET}-${CT_TARGET_VENDOR}";;
   497     esac
   498     case "${CT_KERNEL}" in
   499         linux*)  CT_TARGET="${CT_TARGET}-linux";;
   500     esac
   501     case "${CT_LIBC}" in
   502         glibc)  CT_TARGET="${CT_TARGET}-gnu";;
   503         uClibc) CT_TARGET="${CT_TARGET}-uclibc";;
   504     esac
   505     CT_TARGET=`CT_DoConfigSub "${CT_TARGET}"`
   506 }
   507 
   508 # This function does pause the build until the user strikes "Return"
   509 # Usage: CT_DoPause [optional_message]
   510 CT_DoPause() {
   511     local foo
   512     local message="${1:-Pausing for your pleasure}"
   513     CT_DoLog INFO "${message}"
   514     read -p "Press \"Enter\" to continue, or Ctrl-C to stop..." foo >&6
   515     return 0
   516 }
   517 
   518 # This function saves the state of the toolchain to be able to restart
   519 # at any one point
   520 # Usage: CT_DoSaveState <next_step_name>
   521 CT_DoSaveState() {
   522 	[ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
   523     local state_name="$1"
   524     local state_dir="${CT_STATE_DIR}/${state_name}"
   525 
   526     CT_DoLog DEBUG "Saving state to restart at step \"${state_name}\"..."
   527     rm -rf "${state_dir}"
   528     mkdir -p "${state_dir}"
   529 
   530     case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
   531         y)  tar_opt=czf; tar_ext=".tar.gz";;
   532         *)  tar_opt=cf;  tar_ext=".tar";;
   533     esac
   534 
   535     CT_DoLog DEBUG "  Saving environment and aliases"
   536     # We must omit shell functions
   537     # 'isgrep' is here because I don't seem to
   538     # be able to remove the functions names.
   539     set |awk '
   540          BEGIN { _p = 1; }
   541          $0~/^[^ ] ()/ { _p = 0; }
   542          _p == 1
   543          $0 == "}" { _p = 1; }
   544          ' |egrep -v '^[^ ]+ \(\)' >"${state_dir}/env.sh"
   545 
   546     CT_DoLog DEBUG "  Saving CT_CC_CORE_STATIC_PREFIX_DIR=\"${CT_CC_CORE_STATIC_PREFIX_DIR}\""
   547     CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
   548     tar ${tar_opt} "${state_dir}/cc_core_static_prefix_dir${tar_ext}" .
   549     CT_Popd
   550 
   551     CT_DoLog DEBUG "  Saving CT_CC_CORE_SHARED_PREFIX_DIR=\"${CT_CC_CORE_SHARED_PREFIX_DIR}\""
   552     CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
   553     tar ${tar_opt} "${state_dir}/cc_core_shared_prefix_dir${tar_ext}" .
   554     CT_Popd
   555 
   556     CT_DoLog DEBUG "  Saving CT_PREFIX_DIR=\"${CT_PREFIX_DIR}\""
   557     CT_Pushd "${CT_PREFIX_DIR}"
   558     tar ${tar_opt} "${state_dir}/prefix_dir${tar_ext}" --exclude '*.log' .
   559     CT_Popd
   560 
   561     if [ "${CT_LOG_TO_FILE}" = "y" ]; then
   562         CT_DoLog DEBUG "  Saving log file"
   563         exec >/dev/null
   564         case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
   565             y)  gzip -3 -c "${CT_LOG_FILE}"  >"${state_dir}/log.gz";;
   566             *)  cat "${CT_LOG_FILE}" >"${state_dir}/log";;
   567         esac
   568         exec >>"${CT_LOG_FILE}"
   569     fi
   570 }
   571 
   572 # This function restores a previously saved state
   573 # Usage: CT_DoLoadState <state_name>
   574 CT_DoLoadState(){
   575     local state_name="$1"
   576     local state_dir="${CT_STATE_DIR}/${state_name}"
   577     local old_RESTART="${CT_RESTART}"
   578     local old_STOP="${CT_STOP}"
   579 
   580     CT_TestOrAbort "The previous build did not reach the point where it could be restarted at \"${CT_RESTART}\"" -d "${state_dir}"
   581 
   582     # We need to do something special with the log file!
   583     if [ "${CT_LOG_TO_FILE}" = "y" ]; then
   584         exec >"${state_dir}/tail.log"
   585     fi
   586     CT_DoLog INFO "Restoring state at step \"${state_name}\", as requested."
   587 
   588     case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
   589         y)  tar_opt=xzf; tar_ext=".tar.gz";;
   590         *)  tar_opt=cf;  tar_ext=".tar";;
   591     esac
   592 
   593     CT_DoLog DEBUG "  Removing previous build directories"
   594     chmod -R u+rwX "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
   595     rm -rf         "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
   596     mkdir -p       "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
   597 
   598     CT_DoLog DEBUG "  Restoring CT_PREFIX_DIR=\"${CT_PREFIX_DIR}\""
   599     CT_Pushd "${CT_PREFIX_DIR}"
   600     tar ${tar_opt} "${state_dir}/prefix_dir${tar_ext}"
   601     CT_Popd
   602 
   603     CT_DoLog DEBUG "  Restoring CT_CC_CORE_SHARED_PREFIX_DIR=\"${CT_CC_CORE_SHARED_PREFIX_DIR}\""
   604     CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
   605     tar ${tar_opt} "${state_dir}/cc_core_shared_prefix_dir${tar_ext}"
   606     CT_Popd
   607 
   608     CT_DoLog DEBUG "  Restoring CT_CC_CORE_STATIC_PREFIX_DIR=\"${CT_CC_CORE_STATIC_PREFIX_DIR}\""
   609     CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
   610     tar ${tar_opt} "${state_dir}/cc_core_static_prefix_dir${tar_ext}"
   611     CT_Popd
   612 
   613     # Restore the environment, discarding any error message
   614     # (for example, read-only bash internals)
   615     CT_DoLog DEBUG "  Restoring environment"
   616     . "${state_dir}/env.sh" >/dev/null 2>&1 || true
   617 
   618     # Restore the new RESTART and STOP steps
   619     CT_RESTART="${old_RESTART}"
   620     CT_STOP="${old_STOP}"
   621     unset old_stop old_restart
   622 
   623     if [ "${CT_LOG_TO_FILE}" = "y" ]; then
   624         CT_DoLog DEBUG "  Restoring log file"
   625         exec >/dev/null
   626         case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
   627             y)  zcat "${state_dir}/log.gz" >"${CT_LOG_FILE}";;
   628             *)  cat "${state_dir}/log" >"${CT_LOG_FILE}";;
   629         esac
   630         cat "${state_dir}/tail.log" >>"${CT_LOG_FILE}"
   631         exec >>"${CT_LOG_FILE}"
   632         rm -f "${state_dir}/tail.log"
   633     fi
   634 }