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
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}]}'"
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}]}'"
16 [ "${CT_LOG_TO_FILE}" = "y" ] && CT_DoLog ERROR "Look at '${CT_LOG_FILE}' for more info on this error."
22 # Install the fault handler
25 # Inherit the fault handler in subshells and functions
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
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
39 # - first of all, save stdout so we can see the live logs: fd #6
41 # - then point stdout to the log file (temporary for now)
42 tmp_log_file="${CT_TOP_DIR}/log.$$"
43 exec >>"${tmp_log_file}"
45 # The different log levels:
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.
64 local max_level LEVEL level cur_l cur_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}
71 eval level="\${CT_LOG_LEVEL_${LEVEL}}"
77 fi |( IFS="\n" # We want the full lines, even leading spaces
83 indent=$((2*CT_STEP_COUNT))
85 case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
86 y,*"warning:"*) cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
87 y,*"WARNING:"*) cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
88 *"error:"*) cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
89 *"make["?*"]:"*"Stop.") cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
90 *) cur_L="${LEVEL}"; cur_l="${level}";;
92 # There will always be a log file (stdout, fd #1), be it /dev/null
93 printf "[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}"
94 if [ ${cur_l} -le ${max_level} ]; then
95 # Only print to console (fd #6) if log level is high enough.
96 printf "\r[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}" >&6
98 if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
99 printf "\r[%02d:%02d] %s " $((SECONDS/60)) $((SECONDS%60)) "${_prog_bar[$((_prog_bar_cpt/10))]}" >&6
100 _prog_bar_cpt=$(((_prog_bar_cpt+1)%40))
108 # Execute an action, and log its messages
109 # Usage: CT_DoExecLog <level> <[VAR=val...] command [parameters...]>
113 CT_DoLog DEBUG "==> Executing: '${@}'"
114 "${@}" 2>&1 |CT_DoLog "${level}"
117 # Tail message to be logged whatever happens
118 # Usage: CT_DoEnd <level>
122 CT_STOP_DATE=$(CT_DoDate +%s%N)
123 CT_STOP_DATE_HUMAN=$(CT_DoDate +%Y%m%d.%H%M%S)
124 if [ "${level}" != "ERROR" ]; then
125 CT_DoLog "${level:-INFO}" "Build completed at ${CT_STOP_DATE_HUMAN}"
127 elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
128 elapsed_min=$((elapsed/(60*1000*1000*1000)))
129 elapsed_sec=$(printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000))))
130 elapsed_csec=$(printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000))))
131 CT_DoLog ${level:-INFO} "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
134 # Abort the execution with an error message
135 # Usage: CT_Abort <message>
141 # Test a condition, and print a message if satisfied
142 # Usage: CT_Test <message> <tests>
147 test "$@" && CT_DoLog WARN "$m"
151 # Test a condition, and abort with an error message if satisfied
152 # Usage: CT_TestAndAbort <message> <tests>
156 test "$@" && CT_Abort "$m"
160 # Test a condition, and abort with an error message if not satisfied
161 # Usage: CT_TestAndAbort <message> <tests>
165 test "$@" || CT_Abort "$m"
169 # Test the presence of a tool, or abort if not found
170 # Usage: CT_HasOrAbort <tool>
172 CT_TestAndAbort "'${1}' not found and needed for successful toolchain build." -z ""$(CT_Which "${1}")
176 # Search a program: wrap "which" for those system where
177 # "which" verbosely says there is no match (Mdk are such
179 # Usage: CT_Which <filename>
181 which "$1" 2>/dev/null || true
184 # Get current date with nanosecond precision
185 # On those system not supporting nanosecond precision, faked with rounding down
186 # to the highest entire second
187 # Usage: CT_DoDate <fmt>
189 date "$1" |sed -r -e 's/%N$/000000000/;'
193 CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
194 # Memorise a step being done so that any error is caught
195 # Usage: CT_DoStep <loglevel> <message>
197 local start=$(CT_DoDate +%s%N)
198 CT_DoLog "$1" "================================================================="
200 CT_STEP_COUNT=$((CT_STEP_COUNT+1))
201 CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
202 CT_STEP_START[${CT_STEP_COUNT}]="${start}"
203 CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
207 # End the step just being done
210 local stop=$(CT_DoDate +%s%N)
211 local duration=$(printf "%032d" $((stop-${CT_STEP_START[${CT_STEP_COUNT}]})) |sed -r -e 's/([[:digit:]]{2})[[:digit:]]{7}$/\.\1/; s/^0+//; s/^\./0\./;')
212 local elapsed=$(printf "%02d:%02d" $((SECONDS/60)) $((SECONDS%60)))
213 local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
214 local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
215 CT_STEP_COUNT=$((CT_STEP_COUNT-1))
216 CT_DoLog "${level}" "${message}: done in ${duration}s (at ${elapsed})"
220 # Pushes into a directory, and pops back
222 pushd "$1" >/dev/null 2>&1
228 # Makes a path absolute
229 # Usage: CT_MakeAbsolutePath path
230 CT_MakeAbsolutePath() {
231 # Try to cd in that directory
237 # No such directory, fail back to guessing
240 *) echo "$(pwd)/$1";;
247 # Creates a temporary directory
248 # $1: variable to assign to
249 # Usage: CT_MktempDir foo
251 # Some mktemp do not allow more than 6 Xs
252 eval "$1"=$(mktemp -q -d "${CT_BUILD_DIR}/.XXXXXX")
253 CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
256 # Echoes the specified string on stdout until the pipe breaks.
259 # Usage: CT_DoYes "" |make oldconfig
264 # Get the file name extension of a component
265 # Usage: CT_GetFileExtension <component_name-component_version> [extension]
266 # If found, echoes the extension to stdout
267 # If not found, echoes nothing on stdout.
268 CT_GetFileExtension() {
274 CT_Pushd "${CT_TARBALLS_DIR}"
275 # we need to also check for an empty extension for those very
276 # peculiar components that don't have one (such as sstrip from
278 for ext in ${first_ext} .tar.gz .tar.bz2 .tgz .tar ''; do
279 if [ -f "${file}${ext}" ]; then
289 # Download an URL using wget
290 # Usage: CT_DoGetFileWget <URL>
292 # Need to return true because it is legitimate to not find the tarball at
293 # some of the provided URLs (think about snapshots, different layouts for
294 # different gcc versions, etc...)
295 # Some (very old!) FTP server might not support the passive mode, thus
297 # With automated download as we are doing, it can be very dangerous to use
298 # -c to continue the downloads. It's far better to simply overwrite the
300 # Some company networks have firewalls to connect to the internet, but it's
301 # not easy to detect them, and wget does not timeout by default while
302 # connecting, so force a global ${CT_CONNECT_TIMEOUT}-second timeout.
303 wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 --passive-ftp "$1" \
304 || wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 "$1" \
308 # Download an URL using curl
309 # Usage: CT_DoGetFileCurl <URL>
311 # Note: comments about wget method (above) are also valid here
312 # Plus: no good progress indicator is available with curl,
313 # so output is consigned to oblivion
314 curl --ftp-pasv -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null \
315 || curl -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null \
319 _wget=$(CT_Which wget)
320 _curl=$(CT_Which curl)
321 # Wrapper function to call one of curl or wget
322 # Usage: CT_DoGetFile <URL>
324 case "${_wget},${_curl}" in
325 ,) CT_DoError "Could find neither wget nor curl";;
326 ,*) CT_DoGetFileCurl "$1" 2>&1 |CT_DoLog ALL;;
327 *) CT_DoGetFileWget "$1" 2>&1 |CT_DoLog ALL;;
331 # Download the file from one of the URLs passed as argument
332 # Usage: CT_GetFile <filename> [extension] <url> [url ...]
340 .tar.bz2|.tar.gz|.tgz|.tar)
346 # Do we already have it?
347 ext=$(CT_GetFileExtension "${file}" ${first_ext})
348 if [ -n "${ext}" ]; then
349 CT_DoLog DEBUG "Already have '${file}'"
353 CT_Pushd "${CT_TARBALLS_DIR}"
354 # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
355 # or, as a failover, a file without extension.
356 # Try local copy first, if it exists
357 for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
358 CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${file}${ext}'"
359 if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
360 "${CT_FORCE_DOWNLOAD}" != "y" ]; then
361 CT_DoLog EXTRA "Using '${file}' from local storage"
362 ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
367 # Not found locally, try from the network
368 CT_DoLog EXTRA "Retrieving '${file}' from network"
370 # Start with LAN mirror
371 if [ "${CT_USE_LAN_MIRROR}" = "y" ]; then
373 for pat in ${CT_LAN_MIRROR_PATTERNS}; do
374 # Please note: we just have the file's basename in a single piece.
375 # So we have to just try and split it back into name and version... :-(
376 pat="${pat//\%pkg/${file%-*}}"
377 pat="${pat//\%ver/${file##*-}}"
378 LAN_URLs="${LAN_URLs} ${CT_LAN_MIRROR_SCHEME}://${CT_LAN_MIRROR_HOSTNAME}/${pat}"
380 for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
381 for url in ${LAN_URLs}; do
382 CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
383 CT_DoGetFile "${url}/${file}${ext}"
384 if [ -f "${file}${ext}" ]; then
385 if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
386 # No need to test if the file already exists because
387 # it does NOT. If it did exist, we'd have been stopped
388 # above, when looking for local copies.
389 CT_DoLog EXTRA "Saving '${file}' to local storage"
390 mv "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}" |CT_DoLog ALL
391 ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
399 # OK, available neither localy, nor from the LAN mirror (if any).
400 for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
401 # Try all urls in turn
403 CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
404 CT_DoGetFile "${url}/${file}${ext}"
405 if [ -f "${file}${ext}" ]; then
406 if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
407 # No need to test if the file already exists because
408 # it does NOT. If it did exist, we'd have been stopped
409 # above, when looking for local copies.
410 CT_DoLog EXTRA "Saving '${file}' to local storage"
411 mv "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}" |CT_DoLog ALL
412 ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
420 CT_Abort "Could not download '${file}', and not present in '${CT_LOCAL_TARBALLS_DIR}'"
423 # Extract a tarball and patch the resulting sources if necessary.
424 # Some tarballs need to be extracted in specific places. Eg.: glibc addons
425 # must be extracted in the glibc directory; uCLibc locales must be extracted
426 # in the extra/locale sub-directory of uClibc.
427 CT_ExtractAndPatch() {
429 local base_file=$(echo "${file}" |cut -d - -f 1)
430 local ver_file=$(echo "${file}" |cut -d - -f 2-)
431 local official_patch_dir
432 local custom_patch_dir
434 local ext=$(CT_GetFileExtension "${file}")
435 CT_TestAndAbort "'${file}' not found in '${CT_TARBALLS_DIR}'" -z "${ext}"
436 local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
438 CT_Pushd "${CT_SRC_DIR}"
440 # Add-ons need a little love, really.
443 CT_TestAndAbort "Trying to extract the C-library addon/locales '${file}' when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
446 [ -f ".${file}.extracted" ] && return 0
447 touch ".${file}.extracted"
450 CT_TestAndAbort "Trying to extract the C-library addon/locales '${file}' when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
451 cd "${CT_LIBC_FILE}/extra/locale"
453 [ -f ".${file}.extracted" ] && return 0
454 touch ".${file}.extracted"
458 # If the directory exists, then consider extraction and patching done
459 if [ -d "${file}" ]; then
460 CT_DoLog DEBUG "Already extracted '${file}'"
464 CT_DoLog EXTRA "Extracting '${file}'"
466 .tar.bz2) tar xvjf "${full_file}" |CT_DoLog ALL;;
467 .tar.gz|.tgz) tar xvzf "${full_file}" |CT_DoLog ALL;;
468 .tar) tar xvf "${full_file}" |CT_DoLog ALL;;
469 *) CT_Abort "Don't know how to handle '${file}': unknown extension" ;;
472 # Snapshots might not have the version number in the extracted directory
473 # name. This is also the case for some (odd) packages, such as D.U.M.A.
474 # Overcome this issue by symlink'ing the directory.
475 if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
477 .tar.bz2) base=$(tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
478 .tar.gz|.tgz) base=$(tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
479 .tar) base=$(tar tf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
481 CT_TestOrAbort "There was a problem when extracting '${file}'" -d "${base}" -o "${base}" != "${file}"
482 ln -s "${base}" "${file}"
485 # Kludge: outside this function, we wouldn't know if we had just extracted
486 # a libc addon, or a plain package. Apply patches now.
487 CT_DoLog EXTRA "Patching '${file}'"
489 if [ "${libc_addon}" = "y" ]; then
490 # Some addon tarballs directly contain the correct addon directory,
491 # while others have the addon directory named after the tarball.
492 # Fix that by always using the short name (eg: linuxthreads, ports, etc...)
493 addon_short_name=$(echo "${file}" |sed -r -e 's/^[^-]+-//; s/-[^-]+$//;')
494 [ -d "${addon_short_name}" ] || ln -s "${file}" "${addon_short_name}"
495 # If libc addon, we're already in the correct place
502 [ "${CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_LIB_DIR}/patches/${base_file}/${ver_file}"
503 [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
504 for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
505 if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
506 for p in "${patch_dir}"/*.patch; do
507 if [ -f "${p}" ]; then
508 CT_DoLog DEBUG "Applying patch '${p}'"
509 patch -g0 -F1 -p1 -f <"${p}" |CT_DoLog ALL
510 CT_TestAndAbort "Failed while applying patch file '${p}'" ${PIPESTATUS[0]} -ne 0
516 if [ "${CT_OVERIDE_CONFIG_GUESS_SUB}" = "y" ]; then
517 CT_DoLog ALL "Overiding config.guess and config.sub"
518 for cfg in config_guess config_sub; do
519 eval ${cfg}="${CT_LIB_DIR}/tools/${cfg/_/.}"
520 [ -e "${CT_TOP_DIR}/tools/${cfg/_/.}" ] && eval ${cfg}="${CT_TOP_DIR}/tools/${cfg/_/.}"
521 find . -type f -name "${cfg/_/.}" -exec cp -v "${!cfg}" {} \; |CT_DoLog ALL
528 # Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
529 # Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
531 if [ -x "${CT_TOP_DIR}/tools/config.guess" ]; then
532 "${CT_TOP_DIR}/tools/config.guess"
534 "${CT_LIB_DIR}/tools/config.guess"
539 if [ -x "${CT_TOP_DIR}/tools/config.sub" ]; then
540 "${CT_TOP_DIR}/tools/config.sub" "$@"
542 "${CT_LIB_DIR}/tools/config.sub" "$@"
546 # Compute the target tuple from what is provided by the user
547 # Usage: CT_DoBuildTargetTuple
548 # In fact this function takes the environment variables to build the target
549 # tuple. It is needed both by the normal build sequence, as well as the
550 # sample saving sequence.
551 CT_DoBuildTargetTuple() {
552 # Set the endianness suffix, and the default endianness gcc option
553 case "${CT_ARCH_BE},${CT_ARCH_LE}" in
554 y,) target_endian_eb=eb
556 CT_ARCH_ENDIAN_CFLAG="-mbig-endian"
557 CT_ARCH_ENDIAN_LDFLAG="-EB"
559 ,y) target_endian_eb=
561 CT_ARCH_ENDIAN_CFLAG="-mlittle-endian"
562 CT_ARCH_ENDIAN_LDFLAG="-EL"
566 # Set defaults for the system part of the tuple. Can be overriden
567 # by architecture-specific values.
569 glibc) CT_TARGET_SYS=gnu;;
570 uClibc) CT_TARGET_SYS=uclibc;;
573 # Transform the ARCH into a kernel-understandable ARCH
574 CT_KERNEL_ARCH="${CT_ARCH}"
576 # Set the default values for ARCH, ABI, CPU, TUNE, FPU and FLOAT
577 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
578 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
579 [ "${CT_ARCH_ARCH}" ] && { CT_ARCH_ARCH_CFLAG="-march=${CT_ARCH_ARCH}"; CT_ARCH_WITH_ARCH="--with-arch=${CT_ARCH_ARCH}"; }
580 [ "${CT_ARCH_ABI}" ] && { CT_ARCH_ABI_CFLAG="-mabi=${CT_ARCH_ABI}"; CT_ARCH_WITH_ABI="--with-abi=${CT_ARCH_ABI}"; }
581 [ "${CT_ARCH_CPU}" ] && { CT_ARCH_CPU_CFLAG="-mcpu=${CT_ARCH_CPU}"; CT_ARCH_WITH_CPU="--with-cpu=${CT_ARCH_CPU}"; }
582 [ "${CT_ARCH_TUNE}" ] && { CT_ARCH_TUNE_CFLAG="-mtune=${CT_ARCH_TUNE}"; CT_ARCH_WITH_TUNE="--with-tune=${CT_ARCH_TUNE}"; }
583 [ "${CT_ARCH_FPU}" ] && { CT_ARCH_FPU_CFLAG="-mfpu=${CT_ARCH_FPU}"; CT_ARCH_WITH_FPU="--with-fpu=${CT_ARCH_FPU}"; }
584 [ "${CT_ARCH_FLOAT_SW}" ] && { CT_ARCH_FLOAT_CFLAG="-msoft-float"; CT_ARCH_WITH_FLOAT="--with-float=soft"; }
586 # Call the architecture specific settings
589 # Finish the target tuple construction
590 case "${CT_KERNEL}" in
591 linux*) CT_TARGET_KERNEL=linux;;
593 CT_TARGET=$(CT_DoConfigSub "${CT_TARGET_ARCH}-${CT_TARGET_VENDOR:-unknown}-${CT_TARGET_KERNEL}-${CT_TARGET_SYS}")
595 # Prepare the target CFLAGS
596 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_ENDIAN_CFLAG}"
597 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ARCH_CFLAG}"
598 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ABI_CFLAG}"
599 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_CPU_CFLAG}"
600 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_TUNE_CFLAG}"
601 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FPU_CFLAG}"
602 CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FLOAT_CFLAG}"
604 # Now on for the target LDFLAGS
605 CT_ARCH_TARGET_LDFLAGS="${CT_ARCH_ENDIAN_LDFLAG}"
608 # This function does pause the build until the user strikes "Return"
609 # Usage: CT_DoPause [optional_message]
612 local message="${1:-Pausing for your pleasure}"
613 CT_DoLog INFO "${message}"
614 read -p "Press 'Enter' to continue, or Ctrl-C to stop..." foo >&6
618 # This function saves the state of the toolchain to be able to restart
620 # Usage: CT_DoSaveState <next_step_name>
622 [ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
623 local state_name="$1"
624 local state_dir="${CT_STATE_DIR}/${state_name}"
626 CT_DoLog DEBUG "Saving state to restart at step '${state_name}'..."
627 rm -rf "${state_dir}"
628 mkdir -p "${state_dir}"
630 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
631 y) tar_opt=z; tar_ext=.gz;;
632 *) tar_opt=; tar_ext=;;
635 CT_DoLog DEBUG " Saving environment and aliases"
636 # We must omit shell functions
639 $0~/^[^ ]+ \(\)/ { _p = 0; }
641 $0 == "}" { _p = 1; }
642 ' >"${state_dir}/env.sh"
644 CT_DoLog DEBUG " Saving CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
645 CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
646 tar cv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}" . |CT_DoLog DEBUG
649 CT_DoLog DEBUG " Saving CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
650 CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
651 tar cv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}" . |CT_DoLog DEBUG
654 CT_DoLog DEBUG " Saving CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
655 CT_Pushd "${CT_PREFIX_DIR}"
656 tar cv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}" --exclude '*.log' . |CT_DoLog DEBUG
659 if [ "${CT_LOG_TO_FILE}" = "y" ]; then
660 CT_DoLog DEBUG " Saving log file"
662 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
663 y) gzip -3 -c "${CT_LOG_FILE}" >"${state_dir}/log.gz";;
664 *) cat "${CT_LOG_FILE}" >"${state_dir}/log";;
666 exec >>"${CT_LOG_FILE}"
670 # This function restores a previously saved state
671 # Usage: CT_DoLoadState <state_name>
673 local state_name="$1"
674 local state_dir="${CT_STATE_DIR}/${state_name}"
675 local old_RESTART="${CT_RESTART}"
676 local old_STOP="${CT_STOP}"
678 CT_TestOrAbort "The previous build did not reach the point where it could be restarted at '${CT_RESTART}'" -d "${state_dir}"
680 # We need to do something special with the log file!
681 if [ "${CT_LOG_TO_FILE}" = "y" ]; then
682 exec >"${state_dir}/tail.log"
684 CT_DoLog INFO "Restoring state at step '${state_name}', as requested."
686 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
687 y) tar_opt=z; tar_ext=.gz;;
688 *) tar_opt=; tar_ext=;;
691 CT_DoLog DEBUG " Removing previous build directories"
692 chmod -R u+rwX "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
693 rm -rf "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
694 mkdir -p "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
696 CT_DoLog DEBUG " Restoring CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
697 CT_Pushd "${CT_PREFIX_DIR}"
698 tar xv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}" |CT_DoLog DEBUG
701 CT_DoLog DEBUG " Restoring CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
702 CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
703 tar xv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}" |CT_DoLog DEBUG
706 CT_DoLog DEBUG " Restoring CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
707 CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
708 tar xv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}" |CT_DoLog DEBUG
711 # Restore the environment, discarding any error message
712 # (for example, read-only bash internals)
713 CT_DoLog DEBUG " Restoring environment"
714 . "${state_dir}/env.sh" >/dev/null 2>&1 || true
716 # Restore the new RESTART and STOP steps
717 CT_RESTART="${old_RESTART}"
718 CT_STOP="${old_STOP}"
719 unset old_stop old_restart
721 if [ "${CT_LOG_TO_FILE}" = "y" ]; then
722 CT_DoLog DEBUG " Restoring log file"
724 case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
725 y) zcat "${state_dir}/log.gz" >"${CT_LOG_FILE}";;
726 *) cat "${state_dir}/log" >"${CT_LOG_FILE}";;
728 cat "${state_dir}/tail.log" >>"${CT_LOG_FILE}"
729 exec >>"${CT_LOG_FILE}"
730 rm -f "${state_dir}/tail.log"