Don't print double-faults.
/trunk/scripts/functions | 3 3 0 0 +++
1 file changed, 3 insertions(+)
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
5 # Prepare the fault handler
9 [ ${CT_IN_FAULT_HANDLER} -ne 0 ] && return 0
11 CT_DoLog ERROR "Build failed in step '${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}'"
12 for((step=(CT_STEP_COUNT-1); step>1; step--)); do
13 CT_DoLog ERROR " called in step '${CT_STEP_MESSAGE[${step}]}'"
15 CT_DoLog ERROR "Error happened in '${BASH_SOURCE[1]}' in function '${FUNCNAME[1]}' (line unknown, sorry)"
16 for((depth=2; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
17 CT_DoLog ERROR " called from '${BASH_SOURCE[${depth}]}' at line # ${BASH_LINENO[${depth}-1]} in function '${FUNCNAME[${depth}]}'"
19 [ "${CT_LOG_TO_FILE}" = "y" ] && CT_DoLog ERROR "Look at '${CT_LOG_FILE}' for more info on this error."
25 # Install the fault handler
28 # Inherit the fault handler in subshells and functions
31 # Make pipes fail on the _first_ failed command
32 # Not supported on bash < 3.x, but we need it, so drop the obsoleting bash-2.x
35 # Don't hash commands' locations, and search every time it is requested.
36 # This is slow, but needed because of the static/shared core gcc which shall
37 # always match to shared if it exists, and only fallback to static if the
42 # - first of all, save stdout so we can see the live logs: fd #6
44 # - then point stdout to the log file (temporary for now)
45 tmp_log_file="${CT_TOP_DIR}/log.$$"
46 exec >>"${tmp_log_file}"
48 # The different log levels:
56 # A function to log what is happening
57 # Different log level are available:
58 # - ERROR: A serious, fatal error occurred
59 # - WARN: A non fatal, non serious error occurred, take your responsbility with the generated build
60 # - INFO: Informational messages
61 # - EXTRA: Extra informational messages
62 # - DEBUG: Debug messages
63 # - ALL: Component's build messages
64 # Usage: CT_DoLog <level> [message]
65 # If message is empty, then stdin will be logged.
67 local max_level LEVEL level cur_l cur_L
69 eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
70 # Set the maximum log level to DEBUG if we have none
71 [ -z "${max_level}" ] && max_level=${CT_LOG_LEVEL_DEBUG}
74 eval level="\${CT_LOG_LEVEL_${LEVEL}}"
80 fi |( IFS="\n" # We want the full lines, even leading spaces
86 indent=$((2*CT_STEP_COUNT))
88 case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
89 y,*"warning:"*) cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
90 y,*"WARNING:"*) cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
91 *"error:"*) cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
92 *"make["?*"]:"*"Stop.") cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
93 *) cur_L="${LEVEL}"; cur_l="${level}";;
95 # There will always be a log file (stdout, fd #1), be it /dev/null
96 printf "[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}"
97 if [ ${cur_l} -le ${max_level} ]; then
98 # Only print to console (fd #6) if log level is high enough.
99 printf "\r[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}" >&6
101 if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
102 printf "\r[%02d:%02d] %s " $((SECONDS/60)) $((SECONDS%60)) "${_prog_bar[$((_prog_bar_cpt/10))]}" >&6
103 _prog_bar_cpt=$(((_prog_bar_cpt+1)%40))
111 # Execute an action, and log its messages
112 # Usage: CT_DoExecLog <level> <[VAR=val...] command [parameters...]>
116 CT_DoLog DEBUG "==> Executing: '${@}'"
117 eval "${@}" 2>&1 |CT_DoLog "${level}"
120 # Tail message to be logged whatever happens
121 # Usage: CT_DoEnd <level>
125 CT_STOP_DATE=$(CT_DoDate +%s%N)
126 CT_STOP_DATE_HUMAN=$(CT_DoDate +%Y%m%d.%H%M%S)
127 if [ "${level}" != "ERROR" ]; then
128 CT_DoLog "${level:-INFO}" "Build completed at ${CT_STOP_DATE_HUMAN}"
130 elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
131 elapsed_min=$((elapsed/(60*1000*1000*1000)))
132 elapsed_sec=$(printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000))))
133 elapsed_csec=$(printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000))))
134 CT_DoLog ${level:-INFO} "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
137 # Abort the execution with an error message
138 # Usage: CT_Abort <message>
144 # Test a condition, and print a message if satisfied
145 # Usage: CT_Test <message> <tests>
150 test "$@" && CT_DoLog WARN "$m"
154 # Test a condition, and abort with an error message if satisfied
155 # Usage: CT_TestAndAbort <message> <tests>
159 test "$@" && CT_Abort "$m"
163 # Test a condition, and abort with an error message if not satisfied
164 # Usage: CT_TestAndAbort <message> <tests>
168 test "$@" || CT_Abort "$m"
172 # Test the presence of a tool, or abort if not found
173 # Usage: CT_HasOrAbort <tool>
175 CT_TestAndAbort "'${1}' not found and needed for successful toolchain build." -z ""$(CT_Which "${1}")
179 # Search a program: wrap "which" for those system where
180 # "which" verbosely says there is no match (Mdk are such
182 # Usage: CT_Which <filename>
184 which "$1" 2>/dev/null || true
187 # Get current date with nanosecond precision
188 # On those system not supporting nanosecond precision, faked with rounding down
189 # to the highest entire second
190 # Usage: CT_DoDate <fmt>
192 date "$1" |sed -r -e 's/%N$/000000000/;'
196 CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
197 # Memorise a step being done so that any error is caught
198 # Usage: CT_DoStep <loglevel> <message>
200 local start=$(CT_DoDate +%s%N)
201 CT_DoLog "$1" "================================================================="
203 CT_STEP_COUNT=$((CT_STEP_COUNT+1))
204 CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
205 CT_STEP_START[${CT_STEP_COUNT}]="${start}"
206 CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
210 # End the step just being done
213 local stop=$(CT_DoDate +%s%N)
214 local duration=$(printf "%032d" $((stop-${CT_STEP_START[${CT_STEP_COUNT}]})) |sed -r -e 's/([[:digit:]]{2})[[:digit:]]{7}$/\.\1/; s/^0+//; s/^\./0\./;')
215 local elapsed=$(printf "%02d:%02d" $((SECONDS/60)) $((SECONDS%60)))
216 local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
217 local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
218 CT_STEP_COUNT=$((CT_STEP_COUNT-1))
219 CT_DoLog "${level}" "${message}: done in ${duration}s (at ${elapsed})"
223 # Pushes into a directory, and pops back
225 pushd "$1" >/dev/null 2>&1
231 # Makes a path absolute
232 # Usage: CT_MakeAbsolutePath path
233 CT_MakeAbsolutePath() {
234 # Try to cd in that directory
240 # No such directory, fail back to guessing
243 *) echo "$(pwd)/$1";;
250 # Creates a temporary directory
251 # $1: variable to assign to
252 # Usage: CT_MktempDir foo
254 # Some mktemp do not allow more than 6 Xs
255 eval "$1"=$(mktemp -q -d "${CT_BUILD_DIR}/.XXXXXX")
256 CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
259 # Echoes the specified string on stdout until the pipe breaks.
262 # Usage: CT_DoYes "" |make oldconfig
267 # Get the file name extension of a component
268 # Usage: CT_GetFileExtension <component_name-component_version>
269 # If found, echoes the extension to stdout
270 # If not found, echoes nothing on stdout.
271 CT_GetFileExtension() {
275 CT_Pushd "${CT_TARBALLS_DIR}"
276 # we need to also check for an empty extension for those very
277 # peculiar components that don't have one (such as sstrip from
279 for ext in .tar.gz .tar.bz2 .tgz .tar ''; do
280 if [ -f "${file}${ext}" ]; then
290 # Download an URL using wget
291 # Usage: CT_DoGetFileWget <URL>
293 # Need to return true because it is legitimate to not find the tarball at
294 # some of the provided URLs (think about snapshots, different layouts for
295 # different gcc versions, etc...)
296 # Some (very old!) FTP server might not support the passive mode, thus
298 # With automated download as we are doing, it can be very dangerous to use
299 # -c to continue the downloads. It's far better to simply overwrite the
301 # Some company networks have firewalls to connect to the internet, but it's
302 # not easy to detect them, and wget does not timeout by default while
303 # connecting, so force a global ${CT_CONNECT_TIMEOUT}-second timeout.
304 wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 --passive-ftp "$1" \
305 || wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 "$1" \
309 # Download an URL using curl
310 # Usage: CT_DoGetFileCurl <URL>
312 # Note: comments about wget method (above) are also valid here
313 # Plus: no good progress indicator is available with curl,
314 # so output is consigned to oblivion
315 curl --ftp-pasv -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null \
316 || curl -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null \
320 _wget=$(CT_Which wget)
321 _curl=$(CT_Which curl)
322 # Wrapper function to call one of curl or wget
323 # Usage: CT_DoGetFile <URL>
325 case "${_wget},${_curl}" in
326 ,) CT_DoError "Could find neither wget nor curl";;
327 ,*) CT_DoGetFileCurl "$1" 2>&1 |CT_DoLog ALL;;
328 *) CT_DoGetFileWget "$1" 2>&1 |CT_DoLog ALL;;
332 # Download the file from one of the URLs passed as argument
333 # Usage: CT_GetFile <filename> [extension] <url> [url ...]
341 .tar.bz2|.tar.gz|.tgz|.tar)
347 # Do we already have it?
348 ext=$(CT_GetFileExtension "${file}")
349 if [ -n "${ext}" ]; then
350 CT_DoLog DEBUG "Already have '${file}'"
354 CT_Pushd "${CT_TARBALLS_DIR}"
355 # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
356 # or, as a failover, a file without extension.
357 # Try local copy first, if it exists
358 for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
359 CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${file}${ext}'"
360 if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
361 "${CT_FORCE_DOWNLOAD}" != "y" ]; then
362 CT_DoLog EXTRA "Using '${file}' from local storage"
363 ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
368 CT_DoLog EXTRA "Retrieving '${file}' from network"
369 for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
370 # Try all urls in turn
372 CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
373 CT_DoGetFile "${url}/${file}${ext}"
374 if [ -f "${file}${ext}" ]; then
375 # No need to test if the file already exists because
376 # it does NOT. If it did exist, we'd have been stopped
377 # above, when looking for local copies.
378 if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
379 CT_DoLog EXTRA "Saving '${file}' to local storage"
380 mv "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}" |CT_DoLog ALL
381 ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
389 CT_Abort "Could not download '${file}', and not present in '${CT_LOCAL_TARBALLS_DIR}'"
392 # Extract a tarball and patch the resulting sources if necessary.
393 # Some tarballs need to be extracted in specific places. Eg.: glibc addons
394 # must be extracted in the glibc directory; uCLibc locales must be extracted
395 # in the extra/locale sub-directory of uClibc.
396 CT_ExtractAndPatch() {
398 local base_file=$(echo "${file}" |cut -d - -f 1)
399 local ver_file=$(echo "${file}" |cut -d - -f 2-)
400 local official_patch_dir
401 local custom_patch_dir
403 local ext=$(CT_GetFileExtension "${file}")
404 CT_TestAndAbort "'${file}' not found in '${CT_TARBALLS_DIR}'" -z "${ext}"
405 local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
407 CT_Pushd "${CT_SRC_DIR}"
409 # Add-ons need a little love, really.
412 CT_TestAndAbort "Trying to extract the C-library addon/locales '${file}' when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
415 [ -f ".${file}.extracted" ] && return 0
416 touch ".${file}.extracted"
419 CT_TestAndAbort "Trying to extract the C-library addon/locales '${file}' when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
420 cd "${CT_LIBC_FILE}/extra/locale"
422 [ -f ".${file}.extracted" ] && return 0
423 touch ".${file}.extracted"
427 # If the directory exists, then consider extraction and patching done
428 if [ -d "${file}" ]; then
429 CT_DoLog DEBUG "Already extracted '${file}'"
433 CT_DoLog EXTRA "Extracting '${file}'"
435 .tar.bz2) tar xvjf "${full_file}" |CT_DoLog ALL;;
436 .tar.gz|.tgz) tar xvzf "${full_file}" |CT_DoLog ALL;;
437 .tar) tar xvf "${full_file}" |CT_DoLog ALL;;
438 *) CT_Abort "Don't know how to handle '${file}': unknown extension" ;;
441 # Snapshots might not have the version number in the extracted directory
442 # name. This is also the case for some (odd) packages, such as D.U.M.A.
443 # Overcome this issue by symlink'ing the directory.
444 if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
446 .tar.bz2) base=$(tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
447 .tar.gz|.tgz) base=$(tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
448 .tar) base=$(tar tf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
450 CT_TestOrAbort "There was a problem when extracting '${file}'" -d "${base}" -o "${base}" != "${file}"
451 ln -s "${base}" "${file}"
454 # Kludge: outside this function, we wouldn't know if we had just extracted
455 # a libc addon, or a plain package. Apply patches now.
456 CT_DoLog EXTRA "Patching '${file}'"
458 if [ "${libc_addon}" = "y" ]; then
459 # Some addon tarballs directly contain the correct addon directory,
460 # while others have the addon directory named after the tarball.
461 # Fix that by always using the short name (eg: linuxthreads, ports, etc...)
462 addon_short_name=$(echo "${file}" |sed -r -e 's/^[^-]+-//; s/-[^-]+$//;')
463 [ -d "${addon_short_name}" ] || ln -s "${file}" "${addon_short_name}"
464 # If libc addon, we're already in the correct place
471 [ "${CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_LIB_DIR}/patches/${base_file}/${ver_file}"
472 [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
473 for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
474 if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
475 for p in "${patch_dir}"/*.patch; do
476 if [ -f "${p}" ]; then
477 CT_DoLog DEBUG "Applying patch '${p}'"
478 patch -g0 -F1 -p1 -f <"${p}" |CT_DoLog ALL
479 CT_TestAndAbort "Failed while applying patch file '${p}'" ${PIPESTATUS[0]} -ne 0
485 if [ "${CT_OVERIDE_CONFIG_GUESS_SUB}" = "y" ]; then
486 CT_DoLog ALL "Overiding config.guess and config.sub"
487 for cfg in config_guess config_sub; do
488 eval ${cfg}="${CT_LIB_DIR}/tools/${cfg/_/.}"
489 [ -e "${CT_TOP_DIR}/tools/${cfg/_/.}" ] && eval ${cfg}="${CT_TOP_DIR}/tools/${cfg/_/.}"
490 find . -type f -name "${cfg/_/.}" -exec cp -v "${!cfg}" {} \; |CT_DoLog ALL
497 # Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
498 # Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
500 if [ -x "${CT_TOP_DIR}/tools/config.guess" ]; then
501 "${CT_TOP_DIR}/tools/config.guess"
503 "${CT_LIB_DIR}/tools/config.guess"
508 if [ -x "${CT_TOP_DIR}/tools/config.sub" ]; then
509 "${CT_TOP_DIR}/tools/config.sub" "$@"
511 "${CT_LIB_DIR}/tools/config.sub" "$@"
515 # Compute the target tuple from what is provided by the user
516 # Usage: CT_DoBuildTargetTuple
517 # In fact this function takes the environment variables to build the target
518 # tuple. It is needed both by the normal build sequence, as well as the
519 # sample saving sequence.
520 CT_DoBuildTargetTuple() {
521 # Set the endianness suffix, and the default endianness gcc option
522 case "${CT_ARCH_BE},${CT_ARCH_LE}" in
523 y,) target_endian_eb=eb
525 CT_ARCH_ENDIAN_CFLAG="-mbig-endian"
526 CT_ARCH_ENDIAN_LDFLAG="-EB"
528 ,y) target_endian_eb=
530 CT_ARCH_ENDIAN_CFLAG="-mlittle-endian"
531 CT_ARCH_ENDIAN_LDFLAG="-EL"
535 # Set defaults for the system part of the tuple. Can be overriden
536 # by architecture-specific values.
538 glibc) CT_TARGET_SYS=gnu;;
539 uClibc) CT_TARGET_SYS=uclibc;;
542 # Transform the ARCH into a kernel-understandable ARCH
543 CT_KERNEL_ARCH="${CT_ARCH}"
545 # Set the default values for ARCH, ABI, CPU, TUNE, FPU and FLOAT
546 unset CT_ARCH_ARCH_CFLAG CT_ARCH_ABI_CFLAG CT_ARCH_CPU_CFLAG CT_ARCH_TUNE_CFLAG CT_ARCH_FPU_CFLAG CT_ARCH_FLOAT_CFLAG
547 unset CT_ARCH_WITH_ARCH CT_ARCH_WITH_ABI CT_ARCH_WITH_CPU CT_ARCH_WITH_TUNE CT_ARCH_WITH_FPU CT_ARCH_WITH_FLOAT
548 [ "${CT_ARCH_ARCH}" ] && { CT_ARCH_ARCH_CFLAG="-march=${CT_ARCH_ARCH}"; CT_ARCH_WITH_ARCH="--with-arch=${CT_ARCH_ARCH}"; }
549 [ "${CT_ARCH_ABI}" ] && { CT_ARCH_ABI_CFLAG="-mabi=${CT_ARCH_ABI}"; CT_ARCH_WITH_ABI="--with-abi=${CT_ARCH_ABI}"; }
550 [ "${CT_ARCH_CPU}" ] && { CT_ARCH_CPU_CFLAG="-mcpu=${CT_ARCH_CPU}"; CT_ARCH_WITH_CPU="--with-cpu=${CT_ARCH_CPU}"; }
551 [ "${CT_ARCH_TUNE}" ] && { CT_ARCH_TUNE_CFLAG="-mtune=${CT_ARCH_TUNE}"; CT_ARCH_WITH_TUNE="--with-tune=${CT_ARCH_TUNE}"; }
552 [ "${CT_ARCH_FPU}" ] && { CT_ARCH_FPU_CFLAG="-mfpu=${CT_ARCH_FPU}"; CT_ARCH_WITH_FPU="--with-fpu=${CT_ARCH_FPU}"; }
553 [ "${CT_ARCH_FLOAT_SW}" ] && { CT_ARCH_FLOAT_CFLAG="-msoft-float"; CT_ARCH_WITH_FLOAT="--with-float=soft"; }
555 # Call the architecture specific settings
558 # Finish the target tuple construction
559 case "${CT_KERNEL}" in
560 linux*) CT_TARGET_KERNEL=linux;;
562 CT_TARGET=$(CT_DoConfigSub "${CT_TARGET_ARCH}-${CT_TARGET_VENDOR:-unknown}-${CT_TARGET_KERNEL}-${CT_TARGET_SYS}")
564 # Prepare the target CFLAGS
565 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_ENDIAN_CFLAG}"
566 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ARCH_CFLAG}"
567 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ABI_CFLAG}"
568 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_CPU_CFLAG}"
569 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_TUNE_CFLAG}"
570 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FPU_CFLAG}"
571 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FLOAT_CFLAG}"
573 # Now on for the target LDFLAGS
574 CT_ARCH_TARGET_LDFLAGS="${CT_ARCH_ENDIAN_LDFLAG}"
577 # This function does pause the build until the user strikes "Return"
578 # Usage: CT_DoPause [optional_message]
581 local message="${1:-Pausing for your pleasure}"
582 CT_DoLog INFO "${message}"
583 read -p "Press 'Enter' to continue, or Ctrl-C to stop..." foo >&6
587 # This function saves the state of the toolchain to be able to restart
589 # Usage: CT_DoSaveState <next_step_name>
591 [ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
592 local state_name="$1"
593 local state_dir="${CT_STATE_DIR}/${state_name}"
595 CT_DoLog DEBUG "Saving state to restart at step '${state_name}'..."
596 rm -rf "${state_dir}"
597 mkdir -p "${state_dir}"
599 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
600 y) tar_opt=z; tar_ext=.gz;;
601 *) tar_opt=; tar_ext=;;
604 CT_DoLog DEBUG " Saving environment and aliases"
605 # We must omit shell functions
608 $0~/^[^ ]+ \(\)/ { _p = 0; }
610 $0 == "}" { _p = 1; }
611 ' >"${state_dir}/env.sh"
613 CT_DoLog DEBUG " Saving CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
614 CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
615 tar cv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}" . |CT_DoLog DEBUG
618 CT_DoLog DEBUG " Saving CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
619 CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
620 tar cv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}" . |CT_DoLog DEBUG
623 CT_DoLog DEBUG " Saving CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
624 CT_Pushd "${CT_PREFIX_DIR}"
625 tar cv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}" --exclude '*.log' . |CT_DoLog DEBUG
628 if [ "${CT_LOG_TO_FILE}" = "y" ]; then
629 CT_DoLog DEBUG " Saving log file"
631 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
632 y) gzip -3 -c "${CT_LOG_FILE}" >"${state_dir}/log.gz";;
633 *) cat "${CT_LOG_FILE}" >"${state_dir}/log";;
635 exec >>"${CT_LOG_FILE}"
639 # This function restores a previously saved state
640 # Usage: CT_DoLoadState <state_name>
642 local state_name="$1"
643 local state_dir="${CT_STATE_DIR}/${state_name}"
644 local old_RESTART="${CT_RESTART}"
645 local old_STOP="${CT_STOP}"
647 CT_TestOrAbort "The previous build did not reach the point where it could be restarted at '${CT_RESTART}'" -d "${state_dir}"
649 # We need to do something special with the log file!
650 if [ "${CT_LOG_TO_FILE}" = "y" ]; then
651 exec >"${state_dir}/tail.log"
653 CT_DoLog INFO "Restoring state at step '${state_name}', as requested."
655 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
656 y) tar_opt=z; tar_ext=.gz;;
657 *) tar_opt=; tar_ext=;;
660 CT_DoLog DEBUG " Removing previous build directories"
661 chmod -R u+rwX "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
662 rm -rf "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
663 mkdir -p "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
665 CT_DoLog DEBUG " Restoring CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
666 CT_Pushd "${CT_PREFIX_DIR}"
667 tar xv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}" |CT_DoLog DEBUG
670 CT_DoLog DEBUG " Restoring CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
671 CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
672 tar xv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}" |CT_DoLog DEBUG
675 CT_DoLog DEBUG " Restoring CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
676 CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
677 tar xv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}" |CT_DoLog DEBUG
680 # Restore the environment, discarding any error message
681 # (for example, read-only bash internals)
682 CT_DoLog DEBUG " Restoring environment"
683 . "${state_dir}/env.sh" >/dev/null 2>&1 || true
685 # Restore the new RESTART and STOP steps
686 CT_RESTART="${old_RESTART}"
687 CT_STOP="${old_STOP}"
688 unset old_stop old_restart
690 if [ "${CT_LOG_TO_FILE}" = "y" ]; then
691 CT_DoLog DEBUG " Restoring log file"
693 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
694 y) zcat "${state_dir}/log.gz" >"${CT_LOG_FILE}";;
695 *) cat "${state_dir}/log" >"${CT_LOG_FILE}";;
697 cat "${state_dir}/tail.log" >>"${CT_LOG_FILE}"
698 exec >>"${CT_LOG_FILE}"
699 rm -f "${state_dir}/tail.log"