scripts/functions
author Michael Hope <michael.hope@linaro.org>
Wed Oct 19 15:27:32 2011 +1300 (2011-10-19)
changeset 2739 f320e22f2cba
parent 2738 149c33923f47
child 2763 3f1798d436da
permissions -rw-r--r--
arch: add softfp support

Some architectures support a mixed hard/soft floating point, where
the compiler emits hardware floating point instructions, but passes
the operands in core (aka integer) registers.

For example, ARM supports this mode (to come in the next changeset).

Add support for softfp cross compilers to the GCC and GLIBC
configuration. Needed for Ubuntu and other distros that are softfp.

Signed-off-by: Michael Hope <michael.hope@linaro.org>
[yann.morin.1998@anciens.enib.fr: split the original patch]
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
anthony@2155
     1
# This file contains some usefull common functions -*- sh -*-
yann@1
     2
# Copyright 2007 Yann E. MORIN
yann@1
     3
# Licensed under the GPL v2. See COPYING in the root of this package
yann@1
     4
yann@136
     5
# Prepare the fault handler
yann@1
     6
CT_OnError() {
yann@2388
     7
    local ret=$?
yann@2388
     8
    local intro
yann@2388
     9
    local file
yann@2388
    10
    local line
yann@2388
    11
    local func
yann@2388
    12
    local step_depth
yann@2388
    13
yann@726
    14
    # Bail out early in subshell, the upper level shell will act accordingly.
yann@726
    15
    [ ${BASH_SUBSHELL} -eq 0 ] || exit $ret
yann@2388
    16
yann@2388
    17
    # Print steps backtrace
yann@2388
    18
    step_depth=${CT_STEP_COUNT}
yann@2388
    19
    CT_STEP_COUNT=2
yann@2388
    20
    CT_DoLog ERROR ""
yann@2388
    21
    intro="Build failed"
yann@2388
    22
    for((step=step_depth; step>1; step--)); do
yann@2388
    23
        CT_DoLog ERROR ">>  ${intro} in step '${CT_STEP_MESSAGE[${step}]}'"
yann@2388
    24
        intro="      called"
yann@2388
    25
    done
yann@2388
    26
yann@2388
    27
    # Print functions backtrace
yann@2388
    28
    intro="Error happened in"
yann@2388
    29
    offset=1
yann@2388
    30
    CT_DoLog ERROR ">>"
yann@2388
    31
    for((depth=1; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
yann@2388
    32
        file="${BASH_SOURCE[${depth}]#${CT_LIB_DIR}/}"
yann@2388
    33
        case "${depth}" in
yann@2388
    34
            1)  line="";;
yann@2388
    35
            *)  line="@${BASH_LINENO[${depth}-1]}"
yann@2388
    36
        esac
yann@2388
    37
        func="${FUNCNAME[${depth}]}"
yann@2388
    38
        CT_DoLog ERROR ">>  ${intro}: ${func}[${file}${line}]"
yann@2388
    39
        intro="      called from"
yann@2388
    40
    done
yann@2388
    41
yann@2388
    42
    # Help diagnose the error
yann@2388
    43
    CT_DoLog ERROR ">>"
yann@2388
    44
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@2388
    45
        CT_DoLog ERROR ">>  For more info on this error, look at the file: '${tmp_log_file#${CT_TOP_DIR}/}'"
yann@2388
    46
    fi
yann@2388
    47
    CT_DoLog ERROR ">>  There is a list of known issues, some with workarounds, in:"
yann@2388
    48
    CT_DoLog ERROR ">>      '${CT_DOC_DIR#${CT_TOP_DIR}/}/B - Known issues.txt'"
yann@2388
    49
yann@2388
    50
    CT_DoLog ERROR ""
yann@523
    51
    CT_DoLog ERROR "Build failed in step '${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}'"
yann@2388
    52
yann@2388
    53
    CT_DoLog ERROR ""
yann@96
    54
    CT_DoEnd ERROR
yann@1
    55
    exit $ret
yann@1
    56
}
yann@136
    57
yann@136
    58
# Install the fault handler
yann@1
    59
trap CT_OnError ERR
yann@1
    60
yann@136
    61
# Inherit the fault handler in subshells and functions
yann@1
    62
set -E
yann@136
    63
yann@136
    64
# Make pipes fail on the _first_ failed command
yann@136
    65
# Not supported on bash < 3.x, but we need it, so drop the obsoleting bash-2.x
yann@1
    66
set -o pipefail
yann@1
    67
yann@136
    68
# Don't hash commands' locations, and search every time it is requested.
yann@136
    69
# This is slow, but needed because of the static/shared core gcc which shall
yann@136
    70
# always match to shared if it exists, and only fallback to static if the
yann@136
    71
# shared is not found
yann@136
    72
set +o hashall
yann@136
    73
yann@165
    74
# Log policy:
yann@165
    75
#  - first of all, save stdout so we can see the live logs: fd #6
yann@165
    76
exec 6>&1
yann@2339
    77
#  - then point stdout to the log file
yann@2339
    78
tmp_log_file="${CT_TOP_DIR}/build.log"
yann@2339
    79
rm -f "${tmp_log_file}"
yann@165
    80
exec >>"${tmp_log_file}"
yann@165
    81
yann@1
    82
# The different log levels:
yann@1
    83
CT_LOG_LEVEL_ERROR=0
yann@1
    84
CT_LOG_LEVEL_WARN=1
yann@1
    85
CT_LOG_LEVEL_INFO=2
yann@1
    86
CT_LOG_LEVEL_EXTRA=3
anthony@2154
    87
CT_LOG_LEVEL_CFG=4
anthony@2154
    88
CT_LOG_LEVEL_FILE=5
anthony@2155
    89
CT_LOG_LEVEL_STATE=6
anthony@2155
    90
CT_LOG_LEVEL_ALL=7
anthony@2155
    91
CT_LOG_LEVEL_DEBUG=8
yann@1
    92
yann@1083
    93
# Make it easy to use \n and !
yann@1081
    94
CR=$(printf "\n")
yann@1083
    95
BANG='!'
yann@1081
    96
yann@1
    97
# A function to log what is happening
yann@1
    98
# Different log level are available:
yann@1
    99
#   - ERROR:   A serious, fatal error occurred
yann@1
   100
#   - WARN:    A non fatal, non serious error occurred, take your responsbility with the generated build
yann@1
   101
#   - INFO:    Informational messages
yann@1
   102
#   - EXTRA:   Extra informational messages
anthony@2154
   103
#   - CFG:     Output of various "./configure"-type scripts
anthony@2154
   104
#   - FILE:    File / archive unpacking.
anthony@2155
   105
#   - STATE:   State save & restore
yann@78
   106
#   - ALL:     Component's build messages
anthony@2155
   107
#   - DEBUG:   Internal debug messages
yann@1
   108
# Usage: CT_DoLog <level> [message]
yann@1
   109
# If message is empty, then stdin will be logged.
yann@1
   110
CT_DoLog() {
yann@47
   111
    local max_level LEVEL level cur_l cur_L
yann@47
   112
    local l
yann@1
   113
    eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
yann@1
   114
    # Set the maximum log level to DEBUG if we have none
yann@78
   115
    [ -z "${max_level}" ] && max_level=${CT_LOG_LEVEL_DEBUG}
yann@1
   116
yann@47
   117
    LEVEL="$1"; shift
yann@1
   118
    eval level="\${CT_LOG_LEVEL_${LEVEL}}"
yann@1
   119
yann@1
   120
    if [ $# -eq 0 ]; then
yann@1
   121
        cat -
yann@1
   122
    else
yann@2096
   123
        printf "%s\n" "${*}"
yann@1081
   124
    fi |( IFS="${CR}" # We want the full lines, even leading spaces
yann@523
   125
          _prog_bar_cpt=0
yann@523
   126
          _prog_bar[0]='/'
yann@523
   127
          _prog_bar[1]='-'
yann@523
   128
          _prog_bar[2]='\'
yann@523
   129
          _prog_bar[3]='|'
yann@1
   130
          indent=$((2*CT_STEP_COUNT))
yann@1
   131
          while read line; do
yann@47
   132
              case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
yann@47
   133
                y,*"warning:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
yann@112
   134
                y,*"WARNING:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
yann@47
   135
                *"error:"*)             cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
yann@919
   136
                *"make["*"]: *** ["*)   cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
yann@47
   137
                *)                      cur_L="${LEVEL}"; cur_l="${level}";;
yann@47
   138
              esac
yann@523
   139
              # There will always be a log file (stdout, fd #1), be it /dev/null
yann@523
   140
              printf "[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}"
yann@47
   141
              if [ ${cur_l} -le ${max_level} ]; then
yann@523
   142
                  # Only print to console (fd #6) if log level is high enough.
javier@2382
   143
                  printf "${CT_LOG_PROGRESS_BAR:+\r}[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}" >&6
yann@82
   144
              fi
yann@82
   145
              if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
yann@523
   146
                  printf "\r[%02d:%02d] %s " $((SECONDS/60)) $((SECONDS%60)) "${_prog_bar[$((_prog_bar_cpt/10))]}" >&6
yann@523
   147
                  _prog_bar_cpt=$(((_prog_bar_cpt+1)%40))
yann@1
   148
              fi
yann@1
   149
          done
yann@1
   150
        )
yann@1
   151
yann@1
   152
    return 0
yann@1
   153
}
yann@1
   154
yann@535
   155
# Execute an action, and log its messages
yann@2340
   156
# It is possible to even log local variable assignments (a-la: var=val ./cmd opts)
yann@2340
   157
# Usage: CT_DoExecLog <level> [VAR=val...] <command> [parameters...]
yann@535
   158
CT_DoExecLog() {
yann@535
   159
    local level="$1"
yann@535
   160
    shift
yann@2340
   161
    (
yann@2340
   162
    for i in "$@"; do
yann@2340
   163
        tmp_log+="'${i}' "
yann@2340
   164
    done
yann@2340
   165
    while true; do
yann@2340
   166
        case "${1}" in
yann@2340
   167
            *=*)    eval export "'${1}'"; shift;;
yann@2340
   168
            *)      break;;
yann@2340
   169
        esac
yann@2340
   170
    done
yann@2340
   171
    CT_DoLog DEBUG "==> Executing: ${tmp_log}"
yann@668
   172
    "${@}" 2>&1 |CT_DoLog "${level}"
yann@2340
   173
    )
yann@2340
   174
    # Catch failure of the sub-shell
yann@2340
   175
    [ $? -eq 0 ]
yann@535
   176
}
yann@535
   177
yann@96
   178
# Tail message to be logged whatever happens
yann@96
   179
# Usage: CT_DoEnd <level>
yann@96
   180
CT_DoEnd()
yann@96
   181
{
yann@145
   182
    local level="$1"
yann@523
   183
    CT_STOP_DATE=$(CT_DoDate +%s%N)
yann@523
   184
    CT_STOP_DATE_HUMAN=$(CT_DoDate +%Y%m%d.%H%M%S)
yann@599
   185
    if [ "${level}" != "ERROR" ]; then
yann@582
   186
        CT_DoLog "${level:-INFO}" "Build completed at ${CT_STOP_DATE_HUMAN}"
yann@582
   187
    fi
yann@96
   188
    elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
yann@96
   189
    elapsed_min=$((elapsed/(60*1000*1000*1000)))
yann@523
   190
    elapsed_sec=$(printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000))))
yann@523
   191
    elapsed_csec=$(printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000))))
yann@145
   192
    CT_DoLog ${level:-INFO} "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
yann@96
   193
}
yann@96
   194
yann@2051
   195
# Remove entries referring to . and other relative paths
js@2044
   196
# Usage: CT_SanitizePath
js@2044
   197
CT_SanitizePath() {
js@2044
   198
    local new
yann@2051
   199
    local p
js@2044
   200
    local IFS=:
js@2044
   201
    for p in $PATH; do
yann@2051
   202
        # Only accept absolute paths;
yann@2051
   203
        # Note: as a special case the empty string in PATH is equivalent to .
yann@2051
   204
        if [ -n "${p}" -a -z "${p%%/*}" ]; then
js@2044
   205
            new="${new}${new:+:}${p}"
js@2044
   206
        fi
js@2044
   207
    done
js@2044
   208
    PATH="${new}"
js@2044
   209
}
js@2044
   210
yann@2280
   211
# Sanitise the directory name contained in the variable passed as argument:
yann@2280
   212
# - remove duplicate /
yann@2280
   213
# Usage: CT_SanitiseVarDir CT_PREFIX_DIR
yann@2280
   214
CT_SanitiseVarDir() {
yann@2280
   215
    local var
yann@2280
   216
    local old_dir
yann@2280
   217
    local new_dir
yann@2280
   218
yann@2280
   219
    for var in "$@"; do
yann@2280
   220
        eval "old_dir=\"\${${var}}\""
yann@2280
   221
        new_dir="$( printf "${old_dir}"     \
yann@2280
   222
                    |sed -r -e 's:/+:/:g;'  \
yann@2280
   223
                  )"
yann@2280
   224
        eval "${var}=\"${new_dir}\""
yann@2280
   225
        CT_DoLog DEBUG "Sanitised '${var}': '${old_dir}' -> '${new_dir}'"
yann@2280
   226
    done
yann@2280
   227
}
yann@2280
   228
yann@96
   229
# Abort the execution with an error message
yann@1
   230
# Usage: CT_Abort <message>
yann@1
   231
CT_Abort() {
yann@128
   232
    CT_DoLog ERROR "$1"
yann@1
   233
    exit 1
yann@1
   234
}
yann@1
   235
yann@1
   236
# Test a condition, and print a message if satisfied
yann@1
   237
# Usage: CT_Test <message> <tests>
yann@1
   238
CT_Test() {
yann@1
   239
    local ret
yann@1
   240
    local m="$1"
yann@1
   241
    shift
yann@1909
   242
    CT_DoLog DEBUG "Testing '! ( $* )'"
yann@1
   243
    test "$@" && CT_DoLog WARN "$m"
yann@1
   244
    return 0
yann@1
   245
}
yann@1
   246
yann@1
   247
# Test a condition, and abort with an error message if satisfied
yann@1
   248
# Usage: CT_TestAndAbort <message> <tests>
yann@1
   249
CT_TestAndAbort() {
yann@1
   250
    local m="$1"
yann@1
   251
    shift
yann@1909
   252
    CT_DoLog DEBUG "Testing '! ( $* )'"
yann@1
   253
    test "$@" && CT_Abort "$m"
yann@1
   254
    return 0
yann@1
   255
}
yann@1
   256
yann@1
   257
# Test a condition, and abort with an error message if not satisfied
yann@1
   258
# Usage: CT_TestAndAbort <message> <tests>
yann@1
   259
CT_TestOrAbort() {
yann@1
   260
    local m="$1"
yann@1
   261
    shift
yann@1909
   262
    CT_DoLog DEBUG "Testing '$*'"
yann@1
   263
    test "$@" || CT_Abort "$m"
yann@1
   264
    return 0
yann@1
   265
}
yann@1
   266
yann@1
   267
# Test the presence of a tool, or abort if not found
yann@1
   268
# Usage: CT_HasOrAbort <tool>
yann@1
   269
CT_HasOrAbort() {
yann@773
   270
    CT_TestAndAbort "'${1}' not found and needed for successful toolchain build." -z "$(CT_Which "${1}")"
yann@1
   271
    return 0
yann@1
   272
}
yann@1
   273
yann@210
   274
# Search a program: wrap "which" for those system where
yann@1226
   275
# "which" verbosely says there is no match (Mandriva is
yann@1226
   276
# such a sucker...)
yann@210
   277
# Usage: CT_Which <filename>
yann@210
   278
CT_Which() {
yann@210
   279
  which "$1" 2>/dev/null || true
yann@210
   280
}
yann@210
   281
yann@1
   282
# Get current date with nanosecond precision
yann@1
   283
# On those system not supporting nanosecond precision, faked with rounding down
yann@1
   284
# to the highest entire second
yann@1
   285
# Usage: CT_DoDate <fmt>
yann@1
   286
CT_DoDate() {
yann@2383
   287
    date "$1" |sed -r -e 's/%?N$/000000000/;'
yann@1
   288
}
yann@1
   289
yann@1
   290
CT_STEP_COUNT=1
yann@1
   291
CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
yann@1
   292
# Memorise a step being done so that any error is caught
yann@1
   293
# Usage: CT_DoStep <loglevel> <message>
yann@1
   294
CT_DoStep() {
yann@523
   295
    local start=$(CT_DoDate +%s%N)
yann@1
   296
    CT_DoLog "$1" "================================================================="
yann@1
   297
    CT_DoLog "$1" "$2"
yann@1
   298
    CT_STEP_COUNT=$((CT_STEP_COUNT+1))
yann@1
   299
    CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
yann@1
   300
    CT_STEP_START[${CT_STEP_COUNT}]="${start}"
yann@1
   301
    CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
yann@1
   302
    return 0
yann@1
   303
}
yann@1
   304
yann@1
   305
# End the step just being done
yann@1
   306
# Usage: CT_EndStep
yann@1
   307
CT_EndStep() {
yann@523
   308
    local stop=$(CT_DoDate +%s%N)
yann@523
   309
    local duration=$(printf "%032d" $((stop-${CT_STEP_START[${CT_STEP_COUNT}]})) |sed -r -e 's/([[:digit:]]{2})[[:digit:]]{7}$/\.\1/; s/^0+//; s/^\./0\./;')
yann@582
   310
    local elapsed=$(printf "%02d:%02d" $((SECONDS/60)) $((SECONDS%60)))
yann@1
   311
    local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
yann@1
   312
    local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
yann@1
   313
    CT_STEP_COUNT=$((CT_STEP_COUNT-1))
yann@582
   314
    CT_DoLog "${level}" "${message}: done in ${duration}s (at ${elapsed})"
yann@1
   315
    return 0
yann@1
   316
}
yann@1
   317
yann@1
   318
# Pushes into a directory, and pops back
yann@1
   319
CT_Pushd() {
yann@1
   320
    pushd "$1" >/dev/null 2>&1
yann@1
   321
}
yann@1
   322
CT_Popd() {
yann@1
   323
    popd >/dev/null 2>&1
yann@1
   324
}
yann@1
   325
yann@1
   326
# Creates a temporary directory
yann@1
   327
# $1: variable to assign to
yann@1
   328
# Usage: CT_MktempDir foo
yann@1
   329
CT_MktempDir() {
yann@1
   330
    # Some mktemp do not allow more than 6 Xs
yann@1113
   331
    eval "$1"=$(mktemp -q -d "${CT_BUILD_DIR}/tmp.XXXXXX")
yann@1
   332
    CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
yann@787
   333
    CT_DoLog DEBUG "Made temporary directory '${!1}'"
yann@787
   334
    return 0
yann@1
   335
}
yann@1
   336
yann@1148
   337
# Removes one or more directories, even if it is read-only, or its parent is
yann@1138
   338
# Usage: CT_DoForceRmdir dir [...]
yann@1138
   339
CT_DoForceRmdir() {
yann@1148
   340
    local dir
yann@1148
   341
    local mode
yann@1148
   342
    for dir in "${@}"; do
yann@1148
   343
        [ -d "${dir}" ] || continue
titus@1956
   344
        case "$CT_SYS_OS" in
yann@2029
   345
            Linux|CYGWIN*)
titus@1956
   346
                mode="$(stat -c '%a' "$(dirname "${dir}")")"
titus@1956
   347
                ;;
titus@1956
   348
            Darwin|*BSD)
titus@1956
   349
                mode="$(stat -f '%Lp' "$(dirname "${dir}")")"
titus@1956
   350
                ;;
titus@1956
   351
            *)
titus@1956
   352
                CT_Abort "Unhandled host OS $CT_SYS_OS"
titus@1956
   353
                ;;
titus@1956
   354
        esac
yann@1185
   355
        CT_DoExecLog ALL chmod u+w "$(dirname "${dir}")"
yann@1185
   356
        CT_DoExecLog ALL chmod -R u+w "${dir}"
yann@1148
   357
        CT_DoExecLog ALL rm -rf "${dir}"
yann@1185
   358
        CT_DoExecLog ALL chmod ${mode} "$(dirname "${dir}")"
yann@1148
   359
    done
yann@1138
   360
}
yann@1138
   361
yann@1
   362
# Echoes the specified string on stdout until the pipe breaks.
yann@1
   363
# Doesn't fail
yann@1
   364
# $1: string to echo
yann@1
   365
# Usage: CT_DoYes "" |make oldconfig
yann@1
   366
CT_DoYes() {
yann@1
   367
    yes "$1" || true
yann@1
   368
}
yann@63
   369
yann@1391
   370
# Add the specified directory to LD_LIBRARY_PATH, and export it
yann@1391
   371
# If the specified patch is already present, just export
yann@1391
   372
# $1: path to add
yann@1391
   373
# $2: add as 'first' or 'last' path, 'first' is assumed if $2 is empty
yann@1391
   374
# Usage CT_SetLibPath /some/where/lib [first|last]
yann@1391
   375
CT_SetLibPath() {
yann@1391
   376
    local path="$1"
yann@1391
   377
    local pos="$2"
yann@1391
   378
yann@1391
   379
    case ":${LD_LIBRARY_PATH}:" in
yann@1391
   380
        *:"${path}":*)  ;;
yann@1391
   381
        *)  case "${pos}" in
yann@1391
   382
                last)
yann@1391
   383
                    CT_DoLog DEBUG "Adding '${path}' at end of LD_LIBRARY_PATH"
yann@1391
   384
                    LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${path}"
yann@1391
   385
                    ;;
yann@1391
   386
                first|"")
yann@1391
   387
                    CT_DoLog DEBUG "Adding '${path}' at start of LD_LIBRARY_PATH"
yann@1391
   388
                    LD_LIBRARY_PATH="${path}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
yann@1391
   389
                    ;;
yann@1391
   390
                *)
yann@1391
   391
                    CT_Abort "Incorrect position '${pos}' to add '${path}' to LD_LIBRARY_PATH"
yann@1391
   392
                    ;;
yann@1391
   393
            esac
yann@1391
   394
            ;;
yann@1391
   395
    esac
yann@1391
   396
    CT_DoLog DEBUG "==> LD_LIBRARY_PATH='${LD_LIBRARY_PATH}'"
yann@1391
   397
    export LD_LIBRARY_PATH
yann@1391
   398
}
yann@1391
   399
yann@2608
   400
# Build up the list of allowed tarball extensions
yann@2608
   401
# Add them in the prefered order; most preferred comes first
yann@2608
   402
CT_DoListTarballExt() {
yann@2609
   403
    if [ "${CT_CONFIGURE_has_xzutils}" = "y" ]; then
yann@2609
   404
        printf ".tar.xz\n"
yann@2609
   405
    fi
yann@2646
   406
    if [    "${CT_CONFIGURE_has_lzma}" = "y"    \
yann@2646
   407
         -o "${CT_CONFIGURE_has_xzutils}" = "y" ]; then
yann@2645
   408
        printf ".tar.lzma\n"
yann@2645
   409
    fi
yann@2608
   410
    printf ".tar.bz2\n"
yann@2608
   411
    printf ".tar.gz\n.tgz\n"
yann@2608
   412
    printf ".tar\n"
yann@2608
   413
}
yann@2608
   414
yann@85
   415
# Get the file name extension of a component
yann@719
   416
# Usage: CT_GetFileExtension <component_name-component_version> [extension]
yann@1690
   417
# If found, echoes the extension to stdout, and return 0
yann@1690
   418
# If not found, echoes nothing on stdout, and return !0.
yann@85
   419
CT_GetFileExtension() {
yann@85
   420
    local ext
yann@85
   421
    local file="$1"
yann@719
   422
    shift
yann@719
   423
    local first_ext="$1"
yann@85
   424
yann@160
   425
    # we need to also check for an empty extension for those very
yann@160
   426
    # peculiar components that don't have one (such as sstrip from
yann@160
   427
    # buildroot).
yann@2608
   428
    for ext in ${first_ext} $(CT_DoListTarballExt) /.git ''; do
yann@1763
   429
        if [ -e "${CT_TARBALLS_DIR}/${file}${ext}" ]; then
yann@85
   430
            echo "${ext}"
yann@1690
   431
            exit 0
yann@85
   432
        fi
yann@85
   433
    done
yann@85
   434
yann@1690
   435
    exit 1
yann@85
   436
}
yann@85
   437
yann@2204
   438
# Try to retrieve the specified URL (HTTP or FTP)
yann@2204
   439
# Usage: CT_DoGetFile <URL>
yann@2204
   440
# This functions always returns true (0), as it can be legitimate not
yann@2204
   441
# to find the requested URL (think about snapshots, different layouts
yann@2204
   442
# for different gcc versions, etc...).
yann@2204
   443
CT_DoGetFile() {
yann@2501
   444
    local url="${1}"
yann@2501
   445
    local dest="${CT_TARBALLS_DIR}/${url##*/}"
yann@2205
   446
    local tmp="${dest}.tmp-dl"
yann@2204
   447
yann@2205
   448
    # Remove potential left-over from a previous run
yann@2205
   449
    rm -f "${tmp}"
yann@2205
   450
yann@2204
   451
    # We also retry a few times, in case there is a transient error (eg. behind
yann@2204
   452
    # a dynamic IP that changes during the transfer...)
yann@2204
   453
    # With automated download as we are doing, it can be very dangerous to
yann@2204
   454
    # continue the downloads. It's far better to simply overwrite the
yann@2204
   455
    # destination file.
yann@492
   456
    # Some company networks have firewalls to connect to the internet, but it's
yann@2660
   457
    # not easy to detect them, so force a global ${CT_CONNECT_TIMEOUT}-second
yann@2660
   458
    # timeout.
yann@2204
   459
    # For curl, no good progress indicator is available. So, be silent.
yann@2661
   460
    if CT_DoExecLog ALL curl --ftp-pasv                                 \
yann@2661
   461
                             --retry 3                                  \
yann@2661
   462
                             --connect-timeout ${CT_CONNECT_TIMEOUT}    \
yann@2661
   463
                             --location --fail --silent                 \
yann@2661
   464
                             --output "${tmp}"                          \
yann@2661
   465
                             "${url}"
yann@2661
   466
    then
yann@2661
   467
        # Success, we got it, good!
yann@2205
   468
        mv "${tmp}" "${dest}"
yann@2205
   469
    else
yann@2205
   470
        # Woops...
yann@2205
   471
        rm -f "${tmp}"
yann@2205
   472
    fi
yann@63
   473
}
yann@63
   474
yann@1113
   475
# This function tries to retrieve a tarball form a local directory
yann@1113
   476
# Usage: CT_GetLocal <basename> [.extension]
yann@1113
   477
CT_GetLocal() {
yann@1113
   478
    local basename="$1"
yann@1113
   479
    local first_ext="$2"
yann@1113
   480
    local ext
yann@1113
   481
yann@1113
   482
    # Do we already have it in *our* tarballs dir?
yann@1690
   483
    if ext="$( CT_GetFileExtension "${basename}" ${first_ext} )"; then
yann@1113
   484
        CT_DoLog DEBUG "Already have '${basename}'"
yann@1113
   485
        return 0
yann@1113
   486
    fi
yann@1113
   487
yann@1113
   488
    if [ -n "${CT_LOCAL_TARBALLS_DIR}" ]; then
yann@1113
   489
        CT_DoLog DEBUG "Trying to retrieve an already downloaded copy of '${basename}'"
yann@1113
   490
        # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
yann@1113
   491
        # or, as a failover, a file without extension.
yann@2608
   492
        for ext in ${first_ext} $(CT_DoListTarballExt) ''; do
yann@1113
   493
            CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}'"
yann@1113
   494
            if [ -r "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" -a \
yann@1113
   495
                 "${CT_FORCE_DOWNLOAD}" != "y" ]; then
yann@1113
   496
                CT_DoLog DEBUG "Got '${basename}' from local storage"
yann@1113
   497
                CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" "${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1113
   498
                return 0
yann@1113
   499
            fi
yann@1113
   500
        done
yann@1113
   501
    fi
yann@1113
   502
    return 1
yann@1113
   503
}
yann@1113
   504
yann@1113
   505
# This function saves the specified to local storage if possible,
yann@1113
   506
# and if so, symlinks it for later usage
yann@1113
   507
# Usage: CT_SaveLocal </full/path/file.name>
yann@1113
   508
CT_SaveLocal() {
yann@1113
   509
    local file="$1"
yann@1113
   510
    local basename="${file##*/}"
yann@1113
   511
yann@1113
   512
    if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
yann@1134
   513
        CT_DoLog EXTRA "Saving '${basename}' to local storage"
yann@1113
   514
        # The file may already exist if downloads are forced: remove it first
yann@1113
   515
        CT_DoExecLog ALL rm -f "${CT_LOCAL_TARBALLS_DIR}/${basename}"
yann@1113
   516
        CT_DoExecLog ALL mv -f "${file}" "${CT_LOCAL_TARBALLS_DIR}"
yann@1113
   517
        CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}" "${file}"
yann@1113
   518
    fi
yann@1113
   519
}
yann@1113
   520
yann@63
   521
# Download the file from one of the URLs passed as argument
yann@1113
   522
# Usage: CT_GetFile <basename> [.extension] <url> [url ...]
yann@63
   523
CT_GetFile() {
yann@63
   524
    local ext
yann@2594
   525
    local -a URLS
yann@2594
   526
    local url
yann@63
   527
    local file="$1"
yann@1113
   528
    local first_ext
yann@63
   529
    shift
yann@712
   530
    # If next argument starts with a dot, then this is not an URL,
yann@712
   531
    # and we can consider that it is a preferred extension.
yann@242
   532
    case "$1" in
yann@712
   533
        .*) first_ext="$1"
yann@242
   534
            shift
yann@242
   535
            ;;
yann@242
   536
    esac
yann@63
   537
yann@1113
   538
    # Does it exist localy?
yann@2599
   539
    if CT_GetLocal "${file}" ${first_ext}; then
yann@2599
   540
        return 0
yann@2599
   541
    fi
yann@1113
   542
    # No, it does not...
yann@63
   543
yann@754
   544
    # Try to retrieve the file
yann@788
   545
    CT_DoLog EXTRA "Retrieving '${file}'"
yann@754
   546
yann@1022
   547
    # Add URLs on the LAN mirror
yann@1022
   548
    if [ "${CT_USE_MIRROR}" = "y" ]; then
yann@1294
   549
        CT_TestOrAbort "Please set the mirror base URL" -n "${CT_MIRROR_BASE_URL}"
yann@2594
   550
        URLS+=( "${CT_MIRROR_BASE_URL}/${file%-*}" )
yann@2594
   551
        URLS+=( "${CT_MIRROR_BASE_URL}" )
yann@2593
   552
    fi
yann@695
   553
yann@2595
   554
    if [ "${CT_FORBID_DOWNLOAD}" != "y" ]; then
yann@2595
   555
        URLS+=( "${@}" )
yann@2595
   556
    fi
yann@1022
   557
yann@1022
   558
    # Scan all URLs in turn, and try to grab a tarball from there
yann@1763
   559
    # Do *not* try git trees (ext=/.git), this is handled in a specific
yann@1763
   560
    # wrapper, below
yann@2608
   561
    for ext in ${first_ext} $(CT_DoListTarballExt) ''; do
yann@102
   562
        # Try all urls in turn
yann@2594
   563
        for url in "${URLS[@]}"; do
yann@2589
   564
            [ -n "${url}" ] || continue
yann@523
   565
            CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
yann@265
   566
            CT_DoGetFile "${url}/${file}${ext}"
yann@2492
   567
            if [ -f "${CT_TARBALLS_DIR}/${file}${ext}" ]; then
yann@799
   568
                CT_DoLog DEBUG "Got '${file}' from the Internet"
yann@1113
   569
                CT_SaveLocal "${CT_TARBALLS_DIR}/${file}${ext}"
yann@265
   570
                return 0
yann@265
   571
            fi
yann@102
   572
        done
yann@102
   573
    done
yann@63
   574
yann@2599
   575
    # Just return error, someone may want to catch and handle the error
yann@2492
   576
    # (eg. glibc/eglibc add-ons can be missing).
yann@2492
   577
    return 1
yann@63
   578
}
yann@63
   579
yann@1113
   580
# Checkout from CVS, and build the associated tarball
yann@1113
   581
# The tarball will be called ${basename}.tar.bz2
yann@1113
   582
# Prerequisite: either the server does not require password,
yann@1113
   583
# or the user must already be logged in.
yann@1113
   584
# 'tag' is the tag to retrieve. Must be specified, but can be empty.
yann@1113
   585
# If dirname is specified, then module will be renamed to dirname
yann@1113
   586
# prior to building the tarball.
yann@1592
   587
# Usage: CT_GetCVS <basename> <url> <module> <tag> [dirname[=subdir]]
yann@1592
   588
# Note: if '=subdir' is given, then it is used instead of 'module'.
yann@1113
   589
CT_GetCVS() {
yann@1113
   590
    local basename="$1"
yann@1113
   591
    local uri="$2"
yann@1113
   592
    local module="$3"
yann@1113
   593
    local tag="${4:+-r ${4}}"
yann@1113
   594
    local dirname="$5"
yann@1113
   595
    local tmp_dir
yann@1113
   596
yann@2592
   597
    # First try locally, then the mirror
yann@2592
   598
    if CT_GetFile "${basename}"; then
yann@2592
   599
        # Got it! Return early! :-)
yann@2592
   600
        return 0
yann@2592
   601
    fi
yann@1113
   602
yann@2595
   603
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   604
        CT_DoLog WARN "Downloads forbidden, not trying cvs retrieval"
yann@2595
   605
        return 1
yann@2595
   606
    fi
yann@2595
   607
yann@1113
   608
    CT_MktempDir tmp_dir
yann@1113
   609
    CT_Pushd "${tmp_dir}"
yann@1113
   610
yann@1113
   611
    CT_DoExecLog ALL cvs -z 9 -d "${uri}" co -P ${tag} "${module}"
yann@1592
   612
    if [ -n "${dirname}" ]; then
yann@1592
   613
        case "${dirname}" in
yann@1592
   614
            *=*)
yann@1593
   615
                CT_DoExecLog DEBUG mv "${dirname#*=}" "${dirname%%=*}"
yann@1593
   616
                CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${dirname%%=*}"
yann@1592
   617
                ;;
yann@1592
   618
            *)
yann@1592
   619
                CT_DoExecLog ALL mv "${module}" "${dirname}"
yann@1592
   620
                CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${dirname:-${module}}"
yann@1592
   621
                ;;
yann@1592
   622
        esac
yann@1592
   623
    fi
yann@1113
   624
    CT_SaveLocal "${CT_TARBALLS_DIR}/${basename}.tar.bz2"
yann@1113
   625
yann@1113
   626
    CT_Popd
yann@1113
   627
    CT_DoExecLog ALL rm -rf "${tmp_dir}"
yann@1113
   628
}
yann@1113
   629
yann@1244
   630
# Check out from SVN, and build the associated tarball
yann@1244
   631
# The tarball will be called ${basename}.tar.bz2
yann@1244
   632
# Prerequisite: either the server does not require password,
yann@1244
   633
# or the user must already be logged in.
yann@1244
   634
# 'rev' is the revision to retrieve
yann@1244
   635
# Usage: CT_GetSVN <basename> <url> [rev]
yann@1244
   636
CT_GetSVN() {
yann@1244
   637
    local basename="$1"
yann@1244
   638
    local uri="$2"
yann@1244
   639
    local rev="$3"
yann@1244
   640
yann@2590
   641
    # First try locally, then the mirror
yann@2590
   642
    if CT_GetFile "${basename}"; then
yann@2590
   643
        # Got it! Return early! :-)
yann@2590
   644
        return 0
yann@2590
   645
    fi
yann@1244
   646
yann@2595
   647
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   648
        CT_DoLog WARN "Downloads forbidden, not trying svn retrieval"
yann@2595
   649
        return 1
yann@2595
   650
    fi
yann@2595
   651
yann@1244
   652
    CT_MktempDir tmp_dir
yann@1244
   653
    CT_Pushd "${tmp_dir}"
yann@1244
   654
yann@2494
   655
    if ! CT_DoExecLog ALL svn export ${rev:+-r ${rev}} "${uri}" "${basename}"; then
yann@2494
   656
        CT_DoLog WARN "Could not retrieve '${basename}'"
yann@2494
   657
        return 1
yann@2494
   658
    fi
yann@1244
   659
    CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${basename}"
yann@1244
   660
    CT_SaveLocal "${CT_TARBALLS_DIR}/${basename}.tar.bz2"
yann@1244
   661
yann@1244
   662
    CT_Popd
yann@1244
   663
    CT_DoExecLog ALL rm -rf "${tmp_dir}"
yann@1244
   664
}
yann@1244
   665
yann@1763
   666
# Clone a git tree
yann@1763
   667
# Tries the given URLs in turn until one can get cloned. No tarball will be created.
yann@1763
   668
# Prerequisites: either the server does not require password,
yann@1763
   669
# or the user has already taken any action to authenticate to the server.
yann@1763
   670
# The cloned tree will *not* be stored in the local tarballs dir!
yann@1763
   671
# Usage: CT_GetGit <basename> <url [url ...]>
yann@1763
   672
CT_GetGit() {
yann@1763
   673
    local basename="$1"; shift
yann@1763
   674
    local url
yann@1763
   675
    local cloned=0
yann@1763
   676
yann@2595
   677
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   678
        CT_DoLog WARN "Downloads forbidden, not trying git retrieval"
yann@2595
   679
        return 1
yann@2595
   680
    fi
yann@2595
   681
yann@1763
   682
    # Do we have it in our tarballs dir?
yann@1763
   683
    if [ -d "${CT_TARBALLS_DIR}/${basename}/.git" ]; then
yann@1763
   684
        CT_DoLog EXTRA "Updating git tree '${basename}'"
yann@1763
   685
        CT_Pushd "${CT_TARBALLS_DIR}/${basename}"
yann@1763
   686
        CT_DoExecLog ALL git pull
yann@1763
   687
        CT_Popd
yann@1763
   688
    else
yann@1763
   689
        CT_DoLog EXTRA "Retrieving git tree '${basename}'"
yann@1763
   690
        for url in "${@}"; do
yann@1763
   691
            CT_DoLog ALL "Trying to clone from '${url}'"
yann@1763
   692
            CT_DoForceRmdir "${CT_TARBALLS_DIR}/${basename}"
yann@1763
   693
            if git clone "${url}" "${CT_TARBALLS_DIR}/${basename}" 2>&1 |CT_DoLog ALL; then
yann@1763
   694
                cloned=1
yann@1763
   695
                break
yann@1763
   696
            fi
yann@1763
   697
        done
yann@1763
   698
        CT_TestOrAbort "Could not clone '${basename}'" ${cloned} -ne 0
yann@1763
   699
    fi
yann@1763
   700
}
yann@1763
   701
yann@1126
   702
# Extract a tarball
yann@63
   703
# Some tarballs need to be extracted in specific places. Eg.: glibc addons
yann@63
   704
# must be extracted in the glibc directory; uCLibc locales must be extracted
yann@1123
   705
# in the extra/locale sub-directory of uClibc. This is taken into account
yann@1123
   706
# by the caller, that did a 'cd' into the correct path before calling us
yann@1123
   707
# and sets nochdir to 'nochdir'.
yann@1763
   708
# Note also that this function handles the git trees!
yann@1763
   709
# Usage: CT_Extract <basename> [nochdir] [options]
yann@1763
   710
# where 'options' are dependent on the source (eg. git branch/tag...)
yann@1126
   711
CT_Extract() {
yann@1761
   712
    local nochdir="$1"
yann@1761
   713
    local basename
yann@1690
   714
    local ext
yann@2646
   715
    local lzma_prog
yann@2605
   716
    local -a tar_opts
yann@1690
   717
yann@1761
   718
    if [ "${nochdir}" = "nochdir" ]; then
yann@1761
   719
        shift
yann@1761
   720
        nochdir="$(pwd)"
yann@1761
   721
    else
yann@1761
   722
        nochdir="${CT_SRC_DIR}"
yann@1761
   723
    fi
yann@1761
   724
yann@1761
   725
    basename="$1"
yann@1761
   726
    shift
yann@1761
   727
yann@1690
   728
    if ! ext="$(CT_GetFileExtension "${basename}")"; then
yann@2493
   729
        CT_DoLog WARN "'${basename}' not found in '${CT_TARBALLS_DIR}'"
yann@2493
   730
        return 1
yann@1690
   731
    fi
yann@1126
   732
    local full_file="${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1126
   733
yann@1718
   734
    # Check if already extracted
yann@1718
   735
    if [ -e "${CT_SRC_DIR}/.${basename}.extracted" ]; then
yann@1718
   736
        CT_DoLog DEBUG "Already extracted '${basename}'"
yann@1718
   737
        return 0
yann@1718
   738
    fi
yann@1718
   739
yann@1691
   740
    # Check if previously partially extracted
yann@1691
   741
    if [ -e "${CT_SRC_DIR}/.${basename}.extracting" ]; then
yann@1691
   742
        CT_DoLog ERROR "The '${basename}' sources were partially extracted."
yann@1691
   743
        CT_DoLog ERROR "Please remove first:"
yann@1691
   744
        CT_DoLog ERROR " - the source dir for '${basename}', in '${CT_SRC_DIR}'"
yann@1691
   745
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${basename}.extracting'"
yann@1691
   746
        CT_Abort "I'll stop now to avoid any carnage..."
yann@1691
   747
    fi
yann@1691
   748
    CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${basename}.extracting"
yann@1691
   749
yann@1761
   750
    CT_Pushd "${nochdir}"
yann@63
   751
yann@1126
   752
    CT_DoLog EXTRA "Extracting '${basename}'"
benoit@2574
   753
    CT_DoExecLog FILE mkdir -p "${basename}"
yann@2605
   754
    tar_opts=( "--strip-components=1" )
yann@2605
   755
    tar_opts+=( "-C" "${basename}" )
yann@2605
   756
    tar_opts+=( "-xv" )
yann@2646
   757
yann@2646
   758
    # One note here:
yann@2646
   759
    # - lzma can be handled either with 'xz' or 'lzma'
yann@2646
   760
    # - we get lzma tarball only if either or both are available
yann@2646
   761
    # - so, if we get an lzma tarball, and either 'xz' or 'lzma' is
yann@2646
   762
    #   missing, we can assume the other is available
yann@2646
   763
    if [ "${CT_CONFIGURE_has_lzma}" = "y" ]; then
yann@2646
   764
        lzma_prog=lzma
yann@2646
   765
    else
yann@2646
   766
        lzma_prog=xz
yann@2646
   767
    fi
yann@63
   768
    case "${ext}" in
yann@2609
   769
        .tar.xz)      CT_DoExecLog FILE tar "${tar_opts[@]}" --use-compress-program=xz -f "${full_file}";;
yann@2646
   770
        .tar.lzma)    CT_DoExecLog FILE tar "${tar_opts[@]}" --use-compress-program="${lzma_prog}" -f "${full_file}";;
yann@2608
   771
        .tar.bz2)     CT_DoExecLog FILE tar "${tar_opts[@]}" -j -f "${full_file}";;
yann@2608
   772
        .tar.gz|.tgz) CT_DoExecLog FILE tar "${tar_opts[@]}" -z -f "${full_file}";;
yann@2608
   773
        .tar)         CT_DoExecLog FILE tar "${tar_opts[@]}" -f "${full_file}";;
yann@1763
   774
        /.git)        CT_ExtractGit "${basename}" "${@}";;
yann@2493
   775
        *)            CT_DoLog WARN "Don't know how to handle '${basename}${ext}': unknown extension"
yann@2493
   776
                      return 1
yann@2493
   777
                      ;;
yann@63
   778
    esac
yann@63
   779
yann@1763
   780
    # Don't mark as being extracted for git
yann@1763
   781
    case "${ext}" in
yann@1763
   782
        /.git)  ;;
yann@1763
   783
        *)      CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${basename}.extracted";;
yann@1763
   784
    esac
yann@1691
   785
    CT_DoExecLog DEBUG rm -f "${CT_SRC_DIR}/.${basename}.extracting"
yann@1126
   786
yann@1761
   787
    CT_Popd
yann@1126
   788
}
yann@1126
   789
esben@2721
   790
# Create a working git clone of a local git repository
yann@1763
   791
# Usage: CT_ExtractGit <basename> [ref]
yann@1763
   792
# where 'ref' is the reference to use:
yann@1763
   793
#   the full name of a branch, like "remotes/origin/branch_name"
yann@1763
   794
#   a date as understandable by git, like "YYYY-MM-DD[ hh[:mm[:ss]]]"
yann@1763
   795
#   a tag name
esben@2721
   796
# If 'ref' is not given, the current repository HEAD will be used
yann@1763
   797
CT_ExtractGit() {
yann@1763
   798
    local basename="${1}"
yann@1763
   799
    local ref="${2}"
esben@2721
   800
    local repo
yann@1763
   801
    local ref_type
yann@1763
   802
yann@1763
   803
    # pushd now to be able to get git revlist in case ref is a date
esben@2721
   804
    repo="${CT_TARBALLS_DIR}/${basename}"
esben@2721
   805
    CT_Pushd "${repo}"
yann@1763
   806
yann@1763
   807
    # What kind of reference is ${ref} ?
yann@1763
   808
    if [ -z "${ref}" ]; then
esben@2721
   809
        ref_type=head
esben@2721
   810
        ref=$(git rev-list -n1 HEAD)
yann@1763
   811
    elif git tag |grep -E "^${ref}$" >/dev/null 2>&1; then
yann@1763
   812
        ref_type=tag
yann@1763
   813
    elif git branch -a --no-color |grep -E "^. ${ref}$" >/dev/null 2>&1; then
yann@1763
   814
        ref_type=branch
yann@1763
   815
    elif date -d "${ref}" >/dev/null 2>&1; then
yann@1763
   816
        ref_type=date
yann@1763
   817
        ref=$(git rev-list -n1 --before="${ref}")
yann@1763
   818
    else
yann@1763
   819
        CT_Abort "Reference '${ref}' is an incorrect git reference: neither tag, branch nor date"
yann@1763
   820
    fi
yann@1763
   821
esben@2721
   822
    CT_Popd
yann@1763
   823
esben@2721
   824
    CT_DoExecLog FILE rmdir "${basename}"
yann@1763
   825
    case "${ref_type}" in
esben@2721
   826
        branch) CT_DoExecLog FILE git clone -b "${ref}" "${repo}" "${basename}" ;;
esben@2721
   827
        *)      CT_DoExecLog FILE git clone "${repo}" "${basename}"
esben@2721
   828
                CT_Pushd "${basename}"
esben@2721
   829
                CT_DoExecLog FILE git checkout "${ref}"
esben@2721
   830
                CT_Popd
esben@2721
   831
                ;;
yann@1763
   832
    esac
yann@1763
   833
}
yann@1763
   834
yann@1126
   835
# Patches the specified component
yann@1761
   836
# See CT_Extract, above, for explanations on 'nochdir'
yann@1901
   837
# Usage: CT_Patch [nochdir] <packagename> <packageversion>
yann@1901
   838
# If the package directory is *not* packagename-packageversion, then
yann@1901
   839
# the caller must cd into the proper directory first, and call us
yann@1901
   840
# with nochdir
yann@1126
   841
CT_Patch() {
yann@1761
   842
    local nochdir="$1"
yann@1901
   843
    local pkgname
yann@1901
   844
    local version
yann@1901
   845
    local pkgdir
yann@1761
   846
    local base_file
yann@1761
   847
    local ver_file
yann@1507
   848
    local d
yann@1507
   849
    local -a patch_dirs
yann@1507
   850
    local bundled_patch_dir
yann@1507
   851
    local local_patch_dir
yann@1126
   852
yann@1761
   853
    if [ "${nochdir}" = "nochdir" ]; then
yann@1761
   854
        shift
yann@1906
   855
        pkgname="$1"
yann@1906
   856
        version="$2"
yann@1906
   857
        pkgdir="${pkgname}-${version}"
yann@1761
   858
        nochdir="$(pwd)"
yann@1761
   859
    else
yann@1906
   860
        pkgname="$1"
yann@1906
   861
        version="$2"
yann@1906
   862
        pkgdir="${pkgname}-${version}"
yann@1901
   863
        nochdir="${CT_SRC_DIR}/${pkgdir}"
yann@1761
   864
    fi
yann@1761
   865
yann@1126
   866
    # Check if already patched
yann@1901
   867
    if [ -e "${CT_SRC_DIR}/.${pkgdir}.patched" ]; then
yann@1901
   868
        CT_DoLog DEBUG "Already patched '${pkgdir}'"
yann@1126
   869
        return 0
yann@63
   870
    fi
yann@63
   871
yann@1271
   872
    # Check if already partially patched
yann@1901
   873
    if [ -e "${CT_SRC_DIR}/.${pkgdir}.patching" ]; then
yann@1901
   874
        CT_DoLog ERROR "The '${pkgdir}' sources were partially patched."
yann@1271
   875
        CT_DoLog ERROR "Please remove first:"
yann@1901
   876
        CT_DoLog ERROR " - the source dir for '${pkgdir}', in '${CT_SRC_DIR}'"
yann@1901
   877
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${pkgdir}.extracted'"
yann@1901
   878
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${pkgdir}.patching'"
yann@1271
   879
        CT_Abort "I'll stop now to avoid any carnage..."
yann@1271
   880
    fi
yann@1901
   881
    touch "${CT_SRC_DIR}/.${pkgdir}.patching"
yann@1271
   882
yann@1761
   883
    CT_Pushd "${nochdir}"
yann@1123
   884
yann@1901
   885
    CT_DoLog EXTRA "Patching '${pkgdir}'"
yann@63
   886
yann@1901
   887
    bundled_patch_dir="${CT_LIB_DIR}/patches/${pkgname}/${version}"
yann@1901
   888
    local_patch_dir="${CT_LOCAL_PATCH_DIR}/${pkgname}/${version}"
yann@1507
   889
yann@1507
   890
    case "${CT_PATCH_ORDER}" in
yann@1507
   891
        bundled)        patch_dirs=("${bundled_patch_dir}");;
yann@1507
   892
        local)          patch_dirs=("${local_patch_dir}");;
yann@1507
   893
        bundled,local)  patch_dirs=("${bundled_patch_dir}" "${local_patch_dir}");;
yann@1508
   894
        local,bundled)  patch_dirs=("${local_patch_dir}" "${bundled_patch_dir}");;
yann@1630
   895
        none)           patch_dirs=;;
yann@1507
   896
    esac
yann@1507
   897
yann@1507
   898
    for d in "${patch_dirs[@]}"; do
yann@1507
   899
        CT_DoLog DEBUG "Looking for patches in '${d}'..."
yann@1507
   900
        if [ -n "${d}" -a -d "${d}" ]; then
yann@1507
   901
            for p in "${d}"/*.patch; do
yann@63
   902
                if [ -f "${p}" ]; then
yann@523
   903
                    CT_DoLog DEBUG "Applying patch '${p}'"
yann@1765
   904
                    CT_DoExecLog ALL patch --no-backup-if-mismatch -g0 -F1 -p1 -f <"${p}"
yann@63
   905
                fi
yann@63
   906
            done
yann@1509
   907
            if [ "${CT_PATCH_SINGLE}" = "y" ]; then
yann@1509
   908
                break
yann@1509
   909
            fi
yann@63
   910
        fi
yann@63
   911
    done
yann@63
   912
yann@508
   913
    if [ "${CT_OVERIDE_CONFIG_GUESS_SUB}" = "y" ]; then
yann@508
   914
        CT_DoLog ALL "Overiding config.guess and config.sub"
yann@508
   915
        for cfg in config_guess config_sub; do
yann@1101
   916
            eval ${cfg}="${CT_LIB_DIR}/scripts/${cfg/_/.}"
yann@1101
   917
            [ -e "${CT_TOP_DIR}/scripts/${cfg/_/.}" ] && eval ${cfg}="${CT_TOP_DIR}/scripts/${cfg/_/.}"
yann@776
   918
            # Can't use CT_DoExecLog because of the '{} \;' to be passed un-mangled to find
yann@508
   919
            find . -type f -name "${cfg/_/.}" -exec cp -v "${!cfg}" {} \; |CT_DoLog ALL
yann@508
   920
        done
yann@508
   921
    fi
yann@508
   922
yann@1903
   923
    CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${pkgdir}.patched"
yann@1903
   924
    CT_DoExecLog DEBUG rm -f "${CT_SRC_DIR}/.${pkgdir}.patching"
yann@1126
   925
yann@1761
   926
    CT_Popd
yann@63
   927
}
yann@63
   928
yann@182
   929
# Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
yann@182
   930
# Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
yann@182
   931
CT_DoConfigGuess() {
yann@1101
   932
    if [ -x "${CT_TOP_DIR}/scripts/config.guess" ]; then
yann@1101
   933
        "${CT_TOP_DIR}/scripts/config.guess"
yann@182
   934
    else
yann@1101
   935
        "${CT_LIB_DIR}/scripts/config.guess"
yann@182
   936
    fi
yann@182
   937
}
yann@182
   938
yann@182
   939
CT_DoConfigSub() {
yann@1101
   940
    if [ -x "${CT_TOP_DIR}/scripts/config.sub" ]; then
yann@1101
   941
        "${CT_TOP_DIR}/scripts/config.sub" "$@"
yann@182
   942
    else
yann@1101
   943
        "${CT_LIB_DIR}/scripts/config.sub" "$@"
yann@182
   944
    fi
yann@182
   945
}
yann@182
   946
yann@335
   947
# Compute the target tuple from what is provided by the user
yann@335
   948
# Usage: CT_DoBuildTargetTuple
yann@63
   949
# In fact this function takes the environment variables to build the target
yann@335
   950
# tuple. It is needed both by the normal build sequence, as well as the
yann@63
   951
# sample saving sequence.
yann@335
   952
CT_DoBuildTargetTuple() {
yann@383
   953
    # Set the endianness suffix, and the default endianness gcc option
yann@63
   954
    case "${CT_ARCH_BE},${CT_ARCH_LE}" in
yann@383
   955
        y,) target_endian_eb=eb
yann@383
   956
            target_endian_el=
yann@391
   957
            CT_ARCH_ENDIAN_CFLAG="-mbig-endian"
yann@527
   958
            CT_ARCH_ENDIAN_LDFLAG="-EB"
yann@383
   959
            ;;
yann@383
   960
        ,y) target_endian_eb=
yann@383
   961
            target_endian_el=el
yann@391
   962
            CT_ARCH_ENDIAN_CFLAG="-mlittle-endian"
yann@527
   963
            CT_ARCH_ENDIAN_LDFLAG="-EL"
yann@383
   964
            ;;
yann@63
   965
    esac
yann@383
   966
yann@965
   967
    # Build the default architecture tuple part
yann@965
   968
    CT_TARGET_ARCH="${CT_ARCH}"
yann@965
   969
yann@383
   970
    # Set defaults for the system part of the tuple. Can be overriden
yann@383
   971
    # by architecture-specific values.
yann@383
   972
    case "${CT_LIBC}" in
yann@787
   973
        *glibc) CT_TARGET_SYS=gnu;;
yann@383
   974
        uClibc) CT_TARGET_SYS=uclibc;;
yann@1591
   975
        *)      CT_TARGET_SYS=elf;;
yann@63
   976
    esac
yann@383
   977
yann@391
   978
    # Set the default values for ARCH, ABI, CPU, TUNE, FPU and FLOAT
yann@497
   979
    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
yann@497
   980
    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
yann@391
   981
    [ "${CT_ARCH_ARCH}"     ] && { CT_ARCH_ARCH_CFLAG="-march=${CT_ARCH_ARCH}";  CT_ARCH_WITH_ARCH="--with-arch=${CT_ARCH_ARCH}"; }
yann@391
   982
    [ "${CT_ARCH_ABI}"      ] && { CT_ARCH_ABI_CFLAG="-mabi=${CT_ARCH_ABI}";     CT_ARCH_WITH_ABI="--with-abi=${CT_ARCH_ABI}";    }
yann@391
   983
    [ "${CT_ARCH_CPU}"      ] && { CT_ARCH_CPU_CFLAG="-mcpu=${CT_ARCH_CPU}";     CT_ARCH_WITH_CPU="--with-cpu=${CT_ARCH_CPU}";    }
yann@405
   984
    [ "${CT_ARCH_TUNE}"     ] && { CT_ARCH_TUNE_CFLAG="-mtune=${CT_ARCH_TUNE}";  CT_ARCH_WITH_TUNE="--with-tune=${CT_ARCH_TUNE}"; }
yann@391
   985
    [ "${CT_ARCH_FPU}"      ] && { CT_ARCH_FPU_CFLAG="-mfpu=${CT_ARCH_FPU}";     CT_ARCH_WITH_FPU="--with-fpu=${CT_ARCH_FPU}";    }
michael@2737
   986
michael@2737
   987
    case "${CT_ARCH_FLOAT}" in
michael@2738
   988
        hard)
michael@2738
   989
            CT_ARCH_FLOAT_CFLAG="-mhard-float"
michael@2738
   990
            CT_ARCH_WITH_FLOAT="--with-float=hard"
michael@2738
   991
            ;;
michael@2737
   992
        soft)
michael@2737
   993
            CT_ARCH_FLOAT_CFLAG="-msoft-float"
michael@2737
   994
            CT_ARCH_WITH_FLOAT="--with-float=soft"
michael@2737
   995
            ;;
michael@2739
   996
        softfp)
michael@2739
   997
            CT_ARCH_FLOAT_CFLAG="-mfloat-abi=softfp"
michael@2739
   998
            CT_ARCH_WITH_FLOAT="--with-float=softfp"
michael@2739
   999
            ;;
michael@2737
  1000
    esac
yann@391
  1001
yann@965
  1002
    # Build the default kernel tuple part
yann@965
  1003
    CT_TARGET_KERNEL="${CT_KERNEL}"
yann@964
  1004
yann@965
  1005
    # Overide the default values with the components specific settings
yann@964
  1006
    CT_DoArchTupleValues
yann@965
  1007
    CT_DoKernelTupleValues
yann@383
  1008
yann@391
  1009
    # Finish the target tuple construction
bartvdrmeulen@1896
  1010
    CT_TARGET="${CT_TARGET_ARCH}"
bartvdrmeulen@1896
  1011
    CT_TARGET="${CT_TARGET}${CT_TARGET_VENDOR:+-${CT_TARGET_VENDOR}}"
bartvdrmeulen@1896
  1012
    CT_TARGET="${CT_TARGET}${CT_TARGET_KERNEL:+-${CT_TARGET_KERNEL}}"
bartvdrmeulen@1896
  1013
    CT_TARGET="${CT_TARGET}${CT_TARGET_SYS:+-${CT_TARGET_SYS}}"
yann@1094
  1014
yann@1094
  1015
    # Sanity checks
yann@1094
  1016
    __sed_alias=""
yann@1094
  1017
    if [ -n "${CT_TARGET_ALIAS_SED_EXPR}" ]; then
yann@1094
  1018
        __sed_alias=$(echo "${CT_TARGET}" |sed -r -e "${CT_TARGET_ALIAS_SED_EXPR}")
yann@1094
  1019
    fi
yann@1094
  1020
    case ":${CT_TARGET_VENDOR}:${CT_TARGET_ALIAS}:${__sed_alias}:" in
yann@1094
  1021
      :*" "*:*:*:) CT_Abort "Don't use spaces in the vendor string, it breaks things.";;
yann@1094
  1022
      :*"-"*:*:*:) CT_Abort "Don't use dashes in the vendor string, it breaks things.";;
yann@1094
  1023
      :*:*" "*:*:) CT_Abort "Don't use spaces in the target alias, it breaks things.";;
yann@1094
  1024
      :*:*:*" "*:) CT_Abort "Don't use spaces in the target sed transform, it breaks things.";;
yann@1094
  1025
    esac
yann@1094
  1026
yann@1094
  1027
    # Canonicalise it
yann@1094
  1028
    CT_TARGET=$(CT_DoConfigSub "${CT_TARGET}")
yann@391
  1029
    # Prepare the target CFLAGS
yann@767
  1030
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ENDIAN_CFLAG}"
yann@527
  1031
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ARCH_CFLAG}"
yann@397
  1032
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ABI_CFLAG}"
yann@397
  1033
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_CPU_CFLAG}"
yann@397
  1034
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_TUNE_CFLAG}"
yann@397
  1035
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FPU_CFLAG}"
yann@397
  1036
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FLOAT_CFLAG}"
yann@527
  1037
yann@527
  1038
    # Now on for the target LDFLAGS
yann@767
  1039
    CT_ARCH_TARGET_LDFLAGS="${CT_ARCH_TARGET_LDFLAGS} ${CT_ARCH_ENDIAN_LDFLAG}"
yann@63
  1040
}
yann@121
  1041
yann@121
  1042
# This function does pause the build until the user strikes "Return"
yann@121
  1043
# Usage: CT_DoPause [optional_message]
yann@121
  1044
CT_DoPause() {
yann@121
  1045
    local foo
yann@121
  1046
    local message="${1:-Pausing for your pleasure}"
yann@121
  1047
    CT_DoLog INFO "${message}"
yann@523
  1048
    read -p "Press 'Enter' to continue, or Ctrl-C to stop..." foo >&6
yann@121
  1049
    return 0
yann@121
  1050
}
yann@121
  1051
yann@1907
  1052
# This function creates a tarball of the specified directory, but
yann@1907
  1053
# only if it exists
yann@1907
  1054
# Usage: CT_DoTarballIfExists <dir> <tarball_basename> [extra_tar_options [...]]
yann@1907
  1055
CT_DoTarballIfExists() {
yann@1907
  1056
    local dir="$1"
yann@1907
  1057
    local tarball="$2"
yann@1907
  1058
    shift 2
yann@1907
  1059
    local -a extra_tar_opts=( "$@" )
yann@1908
  1060
    local -a compress
yann@1907
  1061
yann@1907
  1062
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@1908
  1063
        y)  compress=( gzip -c -3 - ); tar_ext=.gz;;
yann@1908
  1064
        *)  compress=( cat - );        tar_ext=;;
yann@1907
  1065
    esac
yann@1907
  1066
yann@1907
  1067
    if [ -d "${dir}" ]; then
yann@1907
  1068
        CT_DoLog DEBUG "  Saving '${dir}'"
yann@1908
  1069
        { tar c -C "${dir}" -v -f - "${extra_tar_opts[@]}" .    \
yann@1908
  1070
          |"${compress[@]}" >"${tarball}.tar${tar_ext}"         ;
anthony@2155
  1071
        } 2>&1 |sed -r -e 's/^/    /;' |CT_DoLog STATE
yann@1907
  1072
    else
anthony@2155
  1073
        CT_DoLog STATE "  Not saving '${dir}': does not exist"
yann@1907
  1074
    fi
yann@1907
  1075
}
yann@1907
  1076
yann@1907
  1077
# This function extracts a tarball to the specified directory, but
yann@1907
  1078
# only if the tarball exists
anthony@2155
  1079
# Usage: CT_DoExtractTarballIfExists <tarball_basename> <dir> [extra_tar_options [...]]
yann@1907
  1080
CT_DoExtractTarballIfExists() {
yann@1907
  1081
    local tarball="$1"
yann@1907
  1082
    local dir="$2"
yann@1907
  1083
    shift 2
yann@1907
  1084
    local -a extra_tar_opts=( "$@" )
yann@1908
  1085
    local -a uncompress
yann@1907
  1086
yann@1907
  1087
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@1908
  1088
        y)  uncompress=( gzip -c -d ); tar_ext=.gz;;
yann@1908
  1089
        *)  uncompress=( cat );        tar_ext=;;
yann@1907
  1090
    esac
yann@1907
  1091
yann@1907
  1092
    if [ -f "${tarball}.tar${tar_ext}" ]; then
yann@1907
  1093
        CT_DoLog DEBUG "  Restoring '${dir}'"
yann@1907
  1094
        CT_DoForceRmdir "${dir}"
yann@1907
  1095
        CT_DoExecLog DEBUG mkdir -p "${dir}"
yann@1908
  1096
        { "${uncompress[@]}" "${tarball}.tar${tar_ext}"     \
yann@1908
  1097
          |tar x -C "${dir}" -v -f - "${extra_tar_opts[@]}" ;
anthony@2155
  1098
        } 2>&1 |sed -r -e 's/^/    /;' |CT_DoLog STATE
yann@1907
  1099
    else
anthony@2155
  1100
        CT_DoLog STATE "  Not restoring '${dir}': does not exist"
yann@1907
  1101
    fi
yann@1907
  1102
}
yann@1907
  1103
yann@121
  1104
# This function saves the state of the toolchain to be able to restart
yann@121
  1105
# at any one point
yann@121
  1106
# Usage: CT_DoSaveState <next_step_name>
yann@121
  1107
CT_DoSaveState() {
yann@121
  1108
	[ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
yann@121
  1109
    local state_name="$1"
yann@121
  1110
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@121
  1111
yann@1266
  1112
    # Log this to the log level required by the user
yann@1266
  1113
    CT_DoLog ${CT_LOG_LEVEL_MAX} "Saving state to restart at step '${state_name}'..."
yann@1134
  1114
yann@121
  1115
    rm -rf "${state_dir}"
yann@121
  1116
    mkdir -p "${state_dir}"
yann@121
  1117
anthony@2155
  1118
    CT_DoLog STATE "  Saving environment and aliases"
yann@738
  1119
    # We must omit shell functions, and some specific bash variables
yann@738
  1120
    # that break when restoring the environment, later. We could do
yann@1299
  1121
    # all the processing in the awk script, but a sed is easier...
yann@1299
  1122
    set |awk '
yann@1017
  1123
              BEGIN { _p = 1; }
yann@1017
  1124
              $0~/^[^ ]+ \(\)/ { _p = 0; }
yann@1017
  1125
              _p == 1
yann@1017
  1126
              $0 == "}" { _p = 1; }
yann@1017
  1127
              ' |sed -r -e '/^BASH_(ARGC|ARGV|LINENO|SOURCE|VERSINFO)=/d;
yann@738
  1128
                           /^(UID|EUID)=/d;
yann@738
  1129
                           /^(FUNCNAME|GROUPS|PPID|SHELLOPTS)=/d;' >"${state_dir}/env.sh"
yann@121
  1130
yann@2308
  1131
    CT_DoTarballIfExists "${CT_BUILDTOOLS_PREFIX_DIR}" "${state_dir}/buildtools_dir"
yann@2502
  1132
    CT_DoTarballIfExists "${CT_COMPLIBS_DIR}" "${state_dir}/complibs_dir"
yann@1907
  1133
    CT_DoTarballIfExists "${CT_CONFIG_DIR}" "${state_dir}/config_dir"
yann@1907
  1134
    CT_DoTarballIfExists "${CT_CC_CORE_STATIC_PREFIX_DIR}" "${state_dir}/cc_core_static_prefix_dir"
yann@1907
  1135
    CT_DoTarballIfExists "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${state_dir}/cc_core_shared_prefix_dir"
yann@1907
  1136
    CT_DoTarballIfExists "${CT_PREFIX_DIR}" "${state_dir}/prefix_dir" --exclude '*.log'
yann@121
  1137
yann@2339
  1138
    CT_DoLog STATE "  Saving log file"
yann@2339
  1139
    exec >/dev/null
yann@2339
  1140
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@2339
  1141
        y)  gzip -3 -c "${tmp_log_file}"  >"${state_dir}/log.gz";;
yann@2339
  1142
        *)  cat "${tmp_log_file}" >"${state_dir}/log";;
yann@2339
  1143
    esac
yann@2339
  1144
    exec >>"${tmp_log_file}"
yann@121
  1145
}
yann@121
  1146
yann@136
  1147
# This function restores a previously saved state
yann@121
  1148
# Usage: CT_DoLoadState <state_name>
yann@121
  1149
CT_DoLoadState(){
yann@121
  1150
    local state_name="$1"
yann@121
  1151
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@135
  1152
    local old_RESTART="${CT_RESTART}"
yann@135
  1153
    local old_STOP="${CT_STOP}"
yann@121
  1154
yann@523
  1155
    CT_TestOrAbort "The previous build did not reach the point where it could be restarted at '${CT_RESTART}'" -d "${state_dir}"
yann@141
  1156
yann@121
  1157
    # We need to do something special with the log file!
yann@121
  1158
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
  1159
        exec >"${state_dir}/tail.log"
yann@121
  1160
    fi
yann@1266
  1161
yann@1266
  1162
    # Log this to the log level required by the user
yann@1266
  1163
    CT_DoLog ${CT_LOG_LEVEL_MAX} "Restoring state at step '${state_name}', as requested."
yann@121
  1164
yann@1907
  1165
    CT_DoExtractTarballIfExists "${state_dir}/prefix_dir" "${CT_PREFIX_DIR}"
yann@1907
  1166
    CT_DoExtractTarballIfExists "${state_dir}/cc_core_shared_prefix_dir" "${CT_CC_CORE_SHARED_PREFIX_DIR}"
yann@1907
  1167
    CT_DoExtractTarballIfExists "${state_dir}/cc_core_static_prefix_dir" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@1907
  1168
    CT_DoExtractTarballIfExists "${state_dir}/config_dir" "${CT_CONFIG_DIR}"
yann@2502
  1169
    CT_DoExtractTarballIfExists "${state_dir}/complibs_dir" "${CT_COMPLIBS_DIR}"
yann@2308
  1170
    CT_DoExtractTarballIfExists "${state_dir}/buildtools_dir" "${CT_BUILDTOOLS_PREFIX_DIR}"
yann@1894
  1171
yann@121
  1172
    # Restore the environment, discarding any error message
yann@121
  1173
    # (for example, read-only bash internals)
anthony@2155
  1174
    CT_DoLog STATE "  Restoring environment"
yann@121
  1175
    . "${state_dir}/env.sh" >/dev/null 2>&1 || true
yann@121
  1176
yann@135
  1177
    # Restore the new RESTART and STOP steps
yann@135
  1178
    CT_RESTART="${old_RESTART}"
yann@135
  1179
    CT_STOP="${old_STOP}"
yann@135
  1180
    unset old_stop old_restart
yann@135
  1181
yann@2339
  1182
    CT_DoLog STATE "  Restoring log file"
yann@2339
  1183
    exec >/dev/null
yann@2339
  1184
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@2339
  1185
        y)  zcat "${state_dir}/log.gz" >"${tmp_log_file}";;
yann@2339
  1186
        *)  cat "${state_dir}/log" >"${tmp_log_file}";;
yann@2339
  1187
    esac
yann@2339
  1188
    cat "${state_dir}/tail.log" >>"${tmp_log_file}"
yann@2339
  1189
    exec >>"${tmp_log_file}"
yann@2339
  1190
    rm -f "${state_dir}/tail.log"
yann@121
  1191
}