scripts/functions
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Jul 17 17:56:22 2011 +0200 (2011-07-17)
changeset 2889 f3b2199620f1
parent 2807 4193d6e6a174
child 2898 7e0af0188fc3
permissions -rw-r--r--
cc/gcc: pass the install prefix to the core passes

Currently, the discrimination on the core compilers prefixes depends on
the type of core compiler to build.

This is not correct, and the caller of the core backend should specify
the prefix.

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@2807
     9
    local file line func
yann@2807
    10
    local step step_depth
yann@2388
    11
yann@2807
    12
    # To avoid printing the backtace for each sub-shell
yann@2807
    13
    # up to the top-level, just remember we've dumped it
yann@2807
    14
    if [ ! -f "${CT_BUILD_DIR}/backtrace" ]; then
yann@2807
    15
        touch "${CT_BUILD_DIR}/backtrace"
yann@2388
    16
yann@2807
    17
        # Print steps backtrace
yann@2807
    18
        step_depth=${CT_STEP_COUNT}
yann@2807
    19
        CT_STEP_COUNT=1     # To have a zero-indentation
yann@2807
    20
        CT_DoLog ERROR ""
yann@2807
    21
        CT_DoLog ERROR ">>"
yann@2807
    22
        intro="Build failed"
yann@2807
    23
        for((step=step_depth; step>0; step--)); do
yann@2807
    24
            CT_DoLog ERROR ">>  ${intro} in step '${CT_STEP_MESSAGE[${step}]}'"
yann@2807
    25
            intro="      called"
yann@2807
    26
        done
yann@2388
    27
yann@2807
    28
        # Print functions backtrace
yann@2807
    29
        intro="Error happened in"
yann@2807
    30
        CT_DoLog ERROR ">>"
yann@2807
    31
        for((depth=1; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
yann@2807
    32
            file="${BASH_SOURCE[${depth}]#${CT_LIB_DIR}/}"
yann@2807
    33
            func="${FUNCNAME[${depth}]}"
yann@2807
    34
            line="@${BASH_LINENO[${depth}-1]:-?}"
yann@2807
    35
            CT_DoLog ERROR ">>  ${intro}: ${func}[${file}${line}]"
yann@2807
    36
            intro="      called from"
yann@2807
    37
        done
yann@2807
    38
    fi
yann@2388
    39
yann@2807
    40
    # And finally, in top-level shell, print some hints
yann@2807
    41
    if [ ${BASH_SUBSHELL} -eq 0 ]; then
yann@2807
    42
        # Help diagnose the error
yann@2807
    43
        CT_STEP_COUNT=1     # To have a zero-indentation
yann@2807
    44
        CT_DoLog ERROR ">>"
yann@2807
    45
        if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@2807
    46
            CT_DoLog ERROR ">>  For more info on this error, look at the file: '${tmp_log_file#${CT_TOP_DIR}/}'"
yann@2807
    47
        fi
yann@2807
    48
        CT_DoLog ERROR ">>  There is a list of known issues, some with workarounds, in:"
yann@2807
    49
        CT_DoLog ERROR ">>      '${CT_DOC_DIR#${CT_TOP_DIR}/}/B - Known issues.txt'"
yann@2807
    50
yann@2807
    51
        CT_DoLog ERROR ""
yann@2807
    52
        CT_DoEnd ERROR
yann@2807
    53
        rm -f "${CT_BUILD_DIR}/backtrace"
yann@2388
    54
    fi
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@2807
   291
CT_STEP_MESSAGE[${CT_STEP_COUNT}]="(top-level)"
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@2840
   403
    if [ "${CT_CONFIGURE_has_xz}" = "y" ]; then
yann@2609
   404
        printf ".tar.xz\n"
yann@2609
   405
    fi
yann@2646
   406
    if [    "${CT_CONFIGURE_has_lzma}" = "y"    \
yann@2840
   407
         -o "${CT_CONFIGURE_has_xz}" = "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@2763
   460
    if CT_DoExecLog ALL wget --passive-ftp --tries=3 -nc    \
yann@2763
   461
                             --progress=dot:binary          \
yann@2763
   462
                             -T ${CT_CONNECT_TIMEOUT}       \
yann@2763
   463
                             -O "${tmp}"                    \
yann@2661
   464
                             "${url}"
yann@2661
   465
    then
yann@2661
   466
        # Success, we got it, good!
yann@2205
   467
        mv "${tmp}" "${dest}"
yann@2205
   468
    else
yann@2205
   469
        # Woops...
yann@2205
   470
        rm -f "${tmp}"
yann@2205
   471
    fi
yann@63
   472
}
yann@63
   473
yann@1113
   474
# This function tries to retrieve a tarball form a local directory
yann@1113
   475
# Usage: CT_GetLocal <basename> [.extension]
yann@1113
   476
CT_GetLocal() {
yann@1113
   477
    local basename="$1"
yann@1113
   478
    local first_ext="$2"
yann@1113
   479
    local ext
yann@1113
   480
yann@1113
   481
    # Do we already have it in *our* tarballs dir?
yann@1690
   482
    if ext="$( CT_GetFileExtension "${basename}" ${first_ext} )"; then
yann@1113
   483
        CT_DoLog DEBUG "Already have '${basename}'"
yann@1113
   484
        return 0
yann@1113
   485
    fi
yann@1113
   486
yann@1113
   487
    if [ -n "${CT_LOCAL_TARBALLS_DIR}" ]; then
yann@1113
   488
        CT_DoLog DEBUG "Trying to retrieve an already downloaded copy of '${basename}'"
yann@1113
   489
        # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
yann@1113
   490
        # or, as a failover, a file without extension.
yann@2608
   491
        for ext in ${first_ext} $(CT_DoListTarballExt) ''; do
yann@1113
   492
            CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}'"
yann@1113
   493
            if [ -r "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" -a \
yann@1113
   494
                 "${CT_FORCE_DOWNLOAD}" != "y" ]; then
yann@1113
   495
                CT_DoLog DEBUG "Got '${basename}' from local storage"
yann@1113
   496
                CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" "${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1113
   497
                return 0
yann@1113
   498
            fi
yann@1113
   499
        done
yann@1113
   500
    fi
yann@1113
   501
    return 1
yann@1113
   502
}
yann@1113
   503
yann@1113
   504
# This function saves the specified to local storage if possible,
yann@1113
   505
# and if so, symlinks it for later usage
yann@1113
   506
# Usage: CT_SaveLocal </full/path/file.name>
yann@1113
   507
CT_SaveLocal() {
yann@1113
   508
    local file="$1"
yann@1113
   509
    local basename="${file##*/}"
yann@1113
   510
yann@1113
   511
    if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
yann@1134
   512
        CT_DoLog EXTRA "Saving '${basename}' to local storage"
yann@1113
   513
        # The file may already exist if downloads are forced: remove it first
yann@1113
   514
        CT_DoExecLog ALL rm -f "${CT_LOCAL_TARBALLS_DIR}/${basename}"
yann@1113
   515
        CT_DoExecLog ALL mv -f "${file}" "${CT_LOCAL_TARBALLS_DIR}"
yann@1113
   516
        CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}" "${file}"
yann@1113
   517
    fi
yann@1113
   518
}
yann@1113
   519
yann@63
   520
# Download the file from one of the URLs passed as argument
yann@1113
   521
# Usage: CT_GetFile <basename> [.extension] <url> [url ...]
yann@63
   522
CT_GetFile() {
yann@63
   523
    local ext
yann@2594
   524
    local -a URLS
yann@2594
   525
    local url
yann@63
   526
    local file="$1"
yann@1113
   527
    local first_ext
yann@63
   528
    shift
yann@712
   529
    # If next argument starts with a dot, then this is not an URL,
yann@712
   530
    # and we can consider that it is a preferred extension.
yann@242
   531
    case "$1" in
yann@712
   532
        .*) first_ext="$1"
yann@242
   533
            shift
yann@242
   534
            ;;
yann@242
   535
    esac
yann@63
   536
yann@1113
   537
    # Does it exist localy?
yann@2599
   538
    if CT_GetLocal "${file}" ${first_ext}; then
yann@2599
   539
        return 0
yann@2599
   540
    fi
yann@1113
   541
    # No, it does not...
yann@63
   542
yann@754
   543
    # Try to retrieve the file
yann@788
   544
    CT_DoLog EXTRA "Retrieving '${file}'"
yann@754
   545
yann@1022
   546
    # Add URLs on the LAN mirror
yann@1022
   547
    if [ "${CT_USE_MIRROR}" = "y" ]; then
yann@1294
   548
        CT_TestOrAbort "Please set the mirror base URL" -n "${CT_MIRROR_BASE_URL}"
yann@2594
   549
        URLS+=( "${CT_MIRROR_BASE_URL}/${file%-*}" )
yann@2594
   550
        URLS+=( "${CT_MIRROR_BASE_URL}" )
yann@2593
   551
    fi
yann@695
   552
yann@2595
   553
    if [ "${CT_FORBID_DOWNLOAD}" != "y" ]; then
yann@2595
   554
        URLS+=( "${@}" )
yann@2595
   555
    fi
yann@1022
   556
yann@1022
   557
    # Scan all URLs in turn, and try to grab a tarball from there
yann@1763
   558
    # Do *not* try git trees (ext=/.git), this is handled in a specific
yann@1763
   559
    # wrapper, below
yann@2608
   560
    for ext in ${first_ext} $(CT_DoListTarballExt) ''; do
yann@102
   561
        # Try all urls in turn
yann@2594
   562
        for url in "${URLS[@]}"; do
yann@2589
   563
            [ -n "${url}" ] || continue
yann@523
   564
            CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
yann@265
   565
            CT_DoGetFile "${url}/${file}${ext}"
yann@2492
   566
            if [ -f "${CT_TARBALLS_DIR}/${file}${ext}" ]; then
yann@799
   567
                CT_DoLog DEBUG "Got '${file}' from the Internet"
yann@1113
   568
                CT_SaveLocal "${CT_TARBALLS_DIR}/${file}${ext}"
yann@265
   569
                return 0
yann@265
   570
            fi
yann@102
   571
        done
yann@102
   572
    done
yann@63
   573
yann@2599
   574
    # Just return error, someone may want to catch and handle the error
yann@2492
   575
    # (eg. glibc/eglibc add-ons can be missing).
yann@2492
   576
    return 1
yann@63
   577
}
yann@63
   578
yann@1113
   579
# Checkout from CVS, and build the associated tarball
yann@1113
   580
# The tarball will be called ${basename}.tar.bz2
yann@1113
   581
# Prerequisite: either the server does not require password,
yann@1113
   582
# or the user must already be logged in.
yann@1113
   583
# 'tag' is the tag to retrieve. Must be specified, but can be empty.
yann@1113
   584
# If dirname is specified, then module will be renamed to dirname
yann@1113
   585
# prior to building the tarball.
yann@1592
   586
# Usage: CT_GetCVS <basename> <url> <module> <tag> [dirname[=subdir]]
yann@1592
   587
# Note: if '=subdir' is given, then it is used instead of 'module'.
yann@1113
   588
CT_GetCVS() {
yann@1113
   589
    local basename="$1"
yann@1113
   590
    local uri="$2"
yann@1113
   591
    local module="$3"
yann@1113
   592
    local tag="${4:+-r ${4}}"
yann@1113
   593
    local dirname="$5"
yann@1113
   594
    local tmp_dir
yann@1113
   595
yann@2592
   596
    # First try locally, then the mirror
yann@2592
   597
    if CT_GetFile "${basename}"; then
yann@2592
   598
        # Got it! Return early! :-)
yann@2592
   599
        return 0
yann@2592
   600
    fi
yann@1113
   601
yann@2595
   602
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   603
        CT_DoLog WARN "Downloads forbidden, not trying cvs retrieval"
yann@2595
   604
        return 1
yann@2595
   605
    fi
yann@2595
   606
yann@1113
   607
    CT_MktempDir tmp_dir
yann@1113
   608
    CT_Pushd "${tmp_dir}"
yann@1113
   609
yann@1113
   610
    CT_DoExecLog ALL cvs -z 9 -d "${uri}" co -P ${tag} "${module}"
yann@1592
   611
    if [ -n "${dirname}" ]; then
yann@1592
   612
        case "${dirname}" in
yann@1592
   613
            *=*)
yann@1593
   614
                CT_DoExecLog DEBUG mv "${dirname#*=}" "${dirname%%=*}"
yann@1593
   615
                CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${dirname%%=*}"
yann@1592
   616
                ;;
yann@1592
   617
            *)
yann@1592
   618
                CT_DoExecLog ALL mv "${module}" "${dirname}"
yann@1592
   619
                CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${dirname:-${module}}"
yann@1592
   620
                ;;
yann@1592
   621
        esac
yann@1592
   622
    fi
yann@1113
   623
    CT_SaveLocal "${CT_TARBALLS_DIR}/${basename}.tar.bz2"
yann@1113
   624
yann@1113
   625
    CT_Popd
yann@1113
   626
    CT_DoExecLog ALL rm -rf "${tmp_dir}"
yann@1113
   627
}
yann@1113
   628
yann@1244
   629
# Check out from SVN, and build the associated tarball
yann@1244
   630
# The tarball will be called ${basename}.tar.bz2
yann@1244
   631
# Prerequisite: either the server does not require password,
yann@1244
   632
# or the user must already be logged in.
yann@1244
   633
# 'rev' is the revision to retrieve
yann@1244
   634
# Usage: CT_GetSVN <basename> <url> [rev]
yann@1244
   635
CT_GetSVN() {
yann@1244
   636
    local basename="$1"
yann@1244
   637
    local uri="$2"
yann@1244
   638
    local rev="$3"
yann@1244
   639
yann@2590
   640
    # First try locally, then the mirror
yann@2590
   641
    if CT_GetFile "${basename}"; then
yann@2590
   642
        # Got it! Return early! :-)
yann@2590
   643
        return 0
yann@2590
   644
    fi
yann@1244
   645
yann@2595
   646
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   647
        CT_DoLog WARN "Downloads forbidden, not trying svn retrieval"
yann@2595
   648
        return 1
yann@2595
   649
    fi
yann@2595
   650
yann@1244
   651
    CT_MktempDir tmp_dir
yann@1244
   652
    CT_Pushd "${tmp_dir}"
yann@1244
   653
yann@2494
   654
    if ! CT_DoExecLog ALL svn export ${rev:+-r ${rev}} "${uri}" "${basename}"; then
yann@2494
   655
        CT_DoLog WARN "Could not retrieve '${basename}'"
yann@2494
   656
        return 1
yann@2494
   657
    fi
yann@1244
   658
    CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${basename}"
yann@1244
   659
    CT_SaveLocal "${CT_TARBALLS_DIR}/${basename}.tar.bz2"
yann@1244
   660
yann@1244
   661
    CT_Popd
yann@1244
   662
    CT_DoExecLog ALL rm -rf "${tmp_dir}"
yann@1244
   663
}
yann@1244
   664
yann@1763
   665
# Clone a git tree
yann@1763
   666
# Tries the given URLs in turn until one can get cloned. No tarball will be created.
yann@1763
   667
# Prerequisites: either the server does not require password,
yann@1763
   668
# or the user has already taken any action to authenticate to the server.
yann@1763
   669
# The cloned tree will *not* be stored in the local tarballs dir!
yann@1763
   670
# Usage: CT_GetGit <basename> <url [url ...]>
yann@1763
   671
CT_GetGit() {
yann@1763
   672
    local basename="$1"; shift
yann@1763
   673
    local url
yann@1763
   674
    local cloned=0
yann@1763
   675
yann@2595
   676
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   677
        CT_DoLog WARN "Downloads forbidden, not trying git retrieval"
yann@2595
   678
        return 1
yann@2595
   679
    fi
yann@2595
   680
yann@1763
   681
    # Do we have it in our tarballs dir?
yann@1763
   682
    if [ -d "${CT_TARBALLS_DIR}/${basename}/.git" ]; then
yann@1763
   683
        CT_DoLog EXTRA "Updating git tree '${basename}'"
yann@1763
   684
        CT_Pushd "${CT_TARBALLS_DIR}/${basename}"
yann@1763
   685
        CT_DoExecLog ALL git pull
yann@1763
   686
        CT_Popd
yann@1763
   687
    else
yann@1763
   688
        CT_DoLog EXTRA "Retrieving git tree '${basename}'"
yann@1763
   689
        for url in "${@}"; do
yann@1763
   690
            CT_DoLog ALL "Trying to clone from '${url}'"
yann@1763
   691
            CT_DoForceRmdir "${CT_TARBALLS_DIR}/${basename}"
yann@1763
   692
            if git clone "${url}" "${CT_TARBALLS_DIR}/${basename}" 2>&1 |CT_DoLog ALL; then
yann@1763
   693
                cloned=1
yann@1763
   694
                break
yann@1763
   695
            fi
yann@1763
   696
        done
yann@1763
   697
        CT_TestOrAbort "Could not clone '${basename}'" ${cloned} -ne 0
yann@1763
   698
    fi
yann@1763
   699
}
yann@1763
   700
yann@1126
   701
# Extract a tarball
yann@63
   702
# Some tarballs need to be extracted in specific places. Eg.: glibc addons
yann@63
   703
# must be extracted in the glibc directory; uCLibc locales must be extracted
yann@1123
   704
# in the extra/locale sub-directory of uClibc. This is taken into account
yann@1123
   705
# by the caller, that did a 'cd' into the correct path before calling us
yann@1123
   706
# and sets nochdir to 'nochdir'.
yann@1763
   707
# Note also that this function handles the git trees!
yann@1763
   708
# Usage: CT_Extract <basename> [nochdir] [options]
yann@1763
   709
# where 'options' are dependent on the source (eg. git branch/tag...)
yann@1126
   710
CT_Extract() {
yann@1761
   711
    local nochdir="$1"
yann@1761
   712
    local basename
yann@1690
   713
    local ext
yann@2646
   714
    local lzma_prog
yann@2605
   715
    local -a tar_opts
yann@1690
   716
yann@1761
   717
    if [ "${nochdir}" = "nochdir" ]; then
yann@1761
   718
        shift
yann@1761
   719
        nochdir="$(pwd)"
yann@1761
   720
    else
yann@1761
   721
        nochdir="${CT_SRC_DIR}"
yann@1761
   722
    fi
yann@1761
   723
yann@1761
   724
    basename="$1"
yann@1761
   725
    shift
yann@1761
   726
yann@1690
   727
    if ! ext="$(CT_GetFileExtension "${basename}")"; then
yann@2493
   728
        CT_DoLog WARN "'${basename}' not found in '${CT_TARBALLS_DIR}'"
yann@2493
   729
        return 1
yann@1690
   730
    fi
yann@1126
   731
    local full_file="${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1126
   732
yann@1718
   733
    # Check if already extracted
yann@1718
   734
    if [ -e "${CT_SRC_DIR}/.${basename}.extracted" ]; then
yann@1718
   735
        CT_DoLog DEBUG "Already extracted '${basename}'"
yann@1718
   736
        return 0
yann@1718
   737
    fi
yann@1718
   738
yann@1691
   739
    # Check if previously partially extracted
yann@1691
   740
    if [ -e "${CT_SRC_DIR}/.${basename}.extracting" ]; then
yann@1691
   741
        CT_DoLog ERROR "The '${basename}' sources were partially extracted."
yann@1691
   742
        CT_DoLog ERROR "Please remove first:"
yann@1691
   743
        CT_DoLog ERROR " - the source dir for '${basename}', in '${CT_SRC_DIR}'"
yann@1691
   744
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${basename}.extracting'"
yann@1691
   745
        CT_Abort "I'll stop now to avoid any carnage..."
yann@1691
   746
    fi
yann@1691
   747
    CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${basename}.extracting"
yann@1691
   748
yann@1761
   749
    CT_Pushd "${nochdir}"
yann@63
   750
yann@1126
   751
    CT_DoLog EXTRA "Extracting '${basename}'"
benoit@2574
   752
    CT_DoExecLog FILE mkdir -p "${basename}"
yann@2605
   753
    tar_opts=( "--strip-components=1" )
yann@2605
   754
    tar_opts+=( "-C" "${basename}" )
yann@2605
   755
    tar_opts+=( "-xv" )
yann@2646
   756
yann@2646
   757
    # One note here:
yann@2646
   758
    # - lzma can be handled either with 'xz' or 'lzma'
yann@2646
   759
    # - we get lzma tarball only if either or both are available
yann@2646
   760
    # - so, if we get an lzma tarball, and either 'xz' or 'lzma' is
yann@2646
   761
    #   missing, we can assume the other is available
yann@2646
   762
    if [ "${CT_CONFIGURE_has_lzma}" = "y" ]; then
titus@2785
   763
        lzma_prog="lzma -fdc"
yann@2646
   764
    else
titus@2785
   765
        lzma_prog="xz -fdc"
yann@2646
   766
    fi
yann@63
   767
    case "${ext}" in
titus@2785
   768
        .tar.xz)      xz -fdc "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
titus@2785
   769
        .tar.lzma)    ${lzma_prog} "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
titus@2785
   770
        .tar.bz2)     bzip2 -dc "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
titus@2785
   771
        .tar.gz|.tgz) gzip -dc "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
yann@2608
   772
        .tar)         CT_DoExecLog FILE tar "${tar_opts[@]}" -f "${full_file}";;
yann@1763
   773
        /.git)        CT_ExtractGit "${basename}" "${@}";;
yann@2493
   774
        *)            CT_DoLog WARN "Don't know how to handle '${basename}${ext}': unknown extension"
yann@2493
   775
                      return 1
yann@2493
   776
                      ;;
yann@63
   777
    esac
yann@63
   778
yann@1763
   779
    # Don't mark as being extracted for git
yann@1763
   780
    case "${ext}" in
yann@1763
   781
        /.git)  ;;
yann@1763
   782
        *)      CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${basename}.extracted";;
yann@1763
   783
    esac
yann@1691
   784
    CT_DoExecLog DEBUG rm -f "${CT_SRC_DIR}/.${basename}.extracting"
yann@1126
   785
yann@1761
   786
    CT_Popd
yann@1126
   787
}
yann@1126
   788
esben@2721
   789
# Create a working git clone of a local git repository
yann@1763
   790
# Usage: CT_ExtractGit <basename> [ref]
yann@1763
   791
# where 'ref' is the reference to use:
yann@1763
   792
#   the full name of a branch, like "remotes/origin/branch_name"
yann@1763
   793
#   a date as understandable by git, like "YYYY-MM-DD[ hh[:mm[:ss]]]"
yann@1763
   794
#   a tag name
esben@2721
   795
# If 'ref' is not given, the current repository HEAD will be used
yann@1763
   796
CT_ExtractGit() {
yann@1763
   797
    local basename="${1}"
yann@1763
   798
    local ref="${2}"
esben@2721
   799
    local repo
yann@1763
   800
    local ref_type
yann@1763
   801
yann@1763
   802
    # pushd now to be able to get git revlist in case ref is a date
esben@2721
   803
    repo="${CT_TARBALLS_DIR}/${basename}"
esben@2721
   804
    CT_Pushd "${repo}"
yann@1763
   805
yann@1763
   806
    # What kind of reference is ${ref} ?
yann@1763
   807
    if [ -z "${ref}" ]; then
esben@2721
   808
        ref_type=head
esben@2721
   809
        ref=$(git rev-list -n1 HEAD)
yann@1763
   810
    elif git tag |grep -E "^${ref}$" >/dev/null 2>&1; then
yann@1763
   811
        ref_type=tag
yann@1763
   812
    elif git branch -a --no-color |grep -E "^. ${ref}$" >/dev/null 2>&1; then
yann@1763
   813
        ref_type=branch
yann@1763
   814
    elif date -d "${ref}" >/dev/null 2>&1; then
yann@1763
   815
        ref_type=date
yann@1763
   816
        ref=$(git rev-list -n1 --before="${ref}")
yann@1763
   817
    else
yann@1763
   818
        CT_Abort "Reference '${ref}' is an incorrect git reference: neither tag, branch nor date"
yann@1763
   819
    fi
yann@1763
   820
esben@2721
   821
    CT_Popd
yann@1763
   822
esben@2721
   823
    CT_DoExecLog FILE rmdir "${basename}"
yann@1763
   824
    case "${ref_type}" in
esben@2721
   825
        branch) CT_DoExecLog FILE git clone -b "${ref}" "${repo}" "${basename}" ;;
esben@2721
   826
        *)      CT_DoExecLog FILE git clone "${repo}" "${basename}"
esben@2721
   827
                CT_Pushd "${basename}"
esben@2721
   828
                CT_DoExecLog FILE git checkout "${ref}"
esben@2721
   829
                CT_Popd
esben@2721
   830
                ;;
yann@1763
   831
    esac
yann@1763
   832
}
yann@1763
   833
yann@1126
   834
# Patches the specified component
yann@1761
   835
# See CT_Extract, above, for explanations on 'nochdir'
yann@1901
   836
# Usage: CT_Patch [nochdir] <packagename> <packageversion>
yann@1901
   837
# If the package directory is *not* packagename-packageversion, then
yann@1901
   838
# the caller must cd into the proper directory first, and call us
yann@1901
   839
# with nochdir
yann@1126
   840
CT_Patch() {
yann@1761
   841
    local nochdir="$1"
yann@1901
   842
    local pkgname
yann@1901
   843
    local version
yann@1901
   844
    local pkgdir
yann@1761
   845
    local base_file
yann@1761
   846
    local ver_file
yann@1507
   847
    local d
yann@1507
   848
    local -a patch_dirs
yann@1507
   849
    local bundled_patch_dir
yann@1507
   850
    local local_patch_dir
yann@1126
   851
yann@1761
   852
    if [ "${nochdir}" = "nochdir" ]; then
yann@1761
   853
        shift
yann@1906
   854
        pkgname="$1"
yann@1906
   855
        version="$2"
yann@1906
   856
        pkgdir="${pkgname}-${version}"
yann@1761
   857
        nochdir="$(pwd)"
yann@1761
   858
    else
yann@1906
   859
        pkgname="$1"
yann@1906
   860
        version="$2"
yann@1906
   861
        pkgdir="${pkgname}-${version}"
yann@1901
   862
        nochdir="${CT_SRC_DIR}/${pkgdir}"
yann@1761
   863
    fi
yann@1761
   864
yann@1126
   865
    # Check if already patched
yann@1901
   866
    if [ -e "${CT_SRC_DIR}/.${pkgdir}.patched" ]; then
yann@1901
   867
        CT_DoLog DEBUG "Already patched '${pkgdir}'"
yann@1126
   868
        return 0
yann@63
   869
    fi
yann@63
   870
yann@1271
   871
    # Check if already partially patched
yann@1901
   872
    if [ -e "${CT_SRC_DIR}/.${pkgdir}.patching" ]; then
yann@1901
   873
        CT_DoLog ERROR "The '${pkgdir}' sources were partially patched."
yann@1271
   874
        CT_DoLog ERROR "Please remove first:"
yann@1901
   875
        CT_DoLog ERROR " - the source dir for '${pkgdir}', in '${CT_SRC_DIR}'"
yann@1901
   876
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${pkgdir}.extracted'"
yann@1901
   877
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${pkgdir}.patching'"
yann@1271
   878
        CT_Abort "I'll stop now to avoid any carnage..."
yann@1271
   879
    fi
yann@1901
   880
    touch "${CT_SRC_DIR}/.${pkgdir}.patching"
yann@1271
   881
yann@1761
   882
    CT_Pushd "${nochdir}"
yann@1123
   883
yann@1901
   884
    CT_DoLog EXTRA "Patching '${pkgdir}'"
yann@63
   885
yann@1901
   886
    bundled_patch_dir="${CT_LIB_DIR}/patches/${pkgname}/${version}"
yann@1901
   887
    local_patch_dir="${CT_LOCAL_PATCH_DIR}/${pkgname}/${version}"
yann@1507
   888
yann@1507
   889
    case "${CT_PATCH_ORDER}" in
yann@1507
   890
        bundled)        patch_dirs=("${bundled_patch_dir}");;
yann@1507
   891
        local)          patch_dirs=("${local_patch_dir}");;
yann@1507
   892
        bundled,local)  patch_dirs=("${bundled_patch_dir}" "${local_patch_dir}");;
yann@1508
   893
        local,bundled)  patch_dirs=("${local_patch_dir}" "${bundled_patch_dir}");;
yann@1630
   894
        none)           patch_dirs=;;
yann@1507
   895
    esac
yann@1507
   896
yann@1507
   897
    for d in "${patch_dirs[@]}"; do
yann@1507
   898
        CT_DoLog DEBUG "Looking for patches in '${d}'..."
yann@1507
   899
        if [ -n "${d}" -a -d "${d}" ]; then
yann@1507
   900
            for p in "${d}"/*.patch; do
yann@63
   901
                if [ -f "${p}" ]; then
yann@523
   902
                    CT_DoLog DEBUG "Applying patch '${p}'"
yann@1765
   903
                    CT_DoExecLog ALL patch --no-backup-if-mismatch -g0 -F1 -p1 -f <"${p}"
yann@63
   904
                fi
yann@63
   905
            done
yann@1509
   906
            if [ "${CT_PATCH_SINGLE}" = "y" ]; then
yann@1509
   907
                break
yann@1509
   908
            fi
yann@63
   909
        fi
yann@63
   910
    done
yann@63
   911
yann@508
   912
    if [ "${CT_OVERIDE_CONFIG_GUESS_SUB}" = "y" ]; then
yann@508
   913
        CT_DoLog ALL "Overiding config.guess and config.sub"
yann@508
   914
        for cfg in config_guess config_sub; do
yann@1101
   915
            eval ${cfg}="${CT_LIB_DIR}/scripts/${cfg/_/.}"
yann@1101
   916
            [ -e "${CT_TOP_DIR}/scripts/${cfg/_/.}" ] && eval ${cfg}="${CT_TOP_DIR}/scripts/${cfg/_/.}"
yann@776
   917
            # Can't use CT_DoExecLog because of the '{} \;' to be passed un-mangled to find
yann@508
   918
            find . -type f -name "${cfg/_/.}" -exec cp -v "${!cfg}" {} \; |CT_DoLog ALL
yann@508
   919
        done
yann@508
   920
    fi
yann@508
   921
yann@1903
   922
    CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${pkgdir}.patched"
yann@1903
   923
    CT_DoExecLog DEBUG rm -f "${CT_SRC_DIR}/.${pkgdir}.patching"
yann@1126
   924
yann@1761
   925
    CT_Popd
yann@63
   926
}
yann@63
   927
yann@182
   928
# Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
yann@182
   929
# Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
yann@182
   930
CT_DoConfigGuess() {
yann@1101
   931
    if [ -x "${CT_TOP_DIR}/scripts/config.guess" ]; then
yann@1101
   932
        "${CT_TOP_DIR}/scripts/config.guess"
yann@182
   933
    else
yann@1101
   934
        "${CT_LIB_DIR}/scripts/config.guess"
yann@182
   935
    fi
yann@182
   936
}
yann@182
   937
yann@182
   938
CT_DoConfigSub() {
yann@1101
   939
    if [ -x "${CT_TOP_DIR}/scripts/config.sub" ]; then
yann@1101
   940
        "${CT_TOP_DIR}/scripts/config.sub" "$@"
yann@182
   941
    else
yann@1101
   942
        "${CT_LIB_DIR}/scripts/config.sub" "$@"
yann@182
   943
    fi
yann@182
   944
}
yann@182
   945
yann@335
   946
# Compute the target tuple from what is provided by the user
yann@335
   947
# Usage: CT_DoBuildTargetTuple
yann@63
   948
# In fact this function takes the environment variables to build the target
yann@335
   949
# tuple. It is needed both by the normal build sequence, as well as the
yann@63
   950
# sample saving sequence.
yann@335
   951
CT_DoBuildTargetTuple() {
yann@383
   952
    # Set the endianness suffix, and the default endianness gcc option
yann@2773
   953
    case "${CT_ARCH_ENDIAN}" in
yann@2773
   954
        big)
yann@2773
   955
            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@2773
   960
        little)
yann@2773
   961
            target_endian_eb=
yann@383
   962
            target_endian_el=el
yann@391
   963
            CT_ARCH_ENDIAN_CFLAG="-mlittle-endian"
yann@527
   964
            CT_ARCH_ENDIAN_LDFLAG="-EL"
yann@383
   965
            ;;
yann@63
   966
    esac
yann@383
   967
yann@965
   968
    # Build the default architecture tuple part
yann@965
   969
    CT_TARGET_ARCH="${CT_ARCH}"
yann@965
   970
yann@383
   971
    # Set defaults for the system part of the tuple. Can be overriden
yann@383
   972
    # by architecture-specific values.
yann@383
   973
    case "${CT_LIBC}" in
yann@787
   974
        *glibc) CT_TARGET_SYS=gnu;;
yann@383
   975
        uClibc) CT_TARGET_SYS=uclibc;;
yann@1591
   976
        *)      CT_TARGET_SYS=elf;;
yann@63
   977
    esac
yann@383
   978
yann@391
   979
    # Set the default values for ARCH, ABI, CPU, TUNE, FPU and FLOAT
yann@497
   980
    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
   981
    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
   982
    [ "${CT_ARCH_ARCH}"     ] && { CT_ARCH_ARCH_CFLAG="-march=${CT_ARCH_ARCH}";  CT_ARCH_WITH_ARCH="--with-arch=${CT_ARCH_ARCH}"; }
yann@391
   983
    [ "${CT_ARCH_ABI}"      ] && { CT_ARCH_ABI_CFLAG="-mabi=${CT_ARCH_ABI}";     CT_ARCH_WITH_ABI="--with-abi=${CT_ARCH_ABI}";    }
yann@391
   984
    [ "${CT_ARCH_CPU}"      ] && { CT_ARCH_CPU_CFLAG="-mcpu=${CT_ARCH_CPU}";     CT_ARCH_WITH_CPU="--with-cpu=${CT_ARCH_CPU}";    }
yann@405
   985
    [ "${CT_ARCH_TUNE}"     ] && { CT_ARCH_TUNE_CFLAG="-mtune=${CT_ARCH_TUNE}";  CT_ARCH_WITH_TUNE="--with-tune=${CT_ARCH_TUNE}"; }
yann@391
   986
    [ "${CT_ARCH_FPU}"      ] && { CT_ARCH_FPU_CFLAG="-mfpu=${CT_ARCH_FPU}";     CT_ARCH_WITH_FPU="--with-fpu=${CT_ARCH_FPU}";    }
michael@2737
   987
michael@2737
   988
    case "${CT_ARCH_FLOAT}" in
michael@2738
   989
        hard)
michael@2738
   990
            CT_ARCH_FLOAT_CFLAG="-mhard-float"
michael@2738
   991
            CT_ARCH_WITH_FLOAT="--with-float=hard"
michael@2738
   992
            ;;
michael@2737
   993
        soft)
michael@2737
   994
            CT_ARCH_FLOAT_CFLAG="-msoft-float"
michael@2737
   995
            CT_ARCH_WITH_FLOAT="--with-float=soft"
michael@2737
   996
            ;;
michael@2739
   997
        softfp)
michael@2739
   998
            CT_ARCH_FLOAT_CFLAG="-mfloat-abi=softfp"
michael@2739
   999
            CT_ARCH_WITH_FLOAT="--with-float=softfp"
michael@2739
  1000
            ;;
michael@2737
  1001
    esac
yann@391
  1002
yann@965
  1003
    # Build the default kernel tuple part
yann@965
  1004
    CT_TARGET_KERNEL="${CT_KERNEL}"
yann@964
  1005
yann@965
  1006
    # Overide the default values with the components specific settings
yann@964
  1007
    CT_DoArchTupleValues
yann@965
  1008
    CT_DoKernelTupleValues
yann@383
  1009
yann@391
  1010
    # Finish the target tuple construction
bartvdrmeulen@1896
  1011
    CT_TARGET="${CT_TARGET_ARCH}"
bartvdrmeulen@1896
  1012
    CT_TARGET="${CT_TARGET}${CT_TARGET_VENDOR:+-${CT_TARGET_VENDOR}}"
bartvdrmeulen@1896
  1013
    CT_TARGET="${CT_TARGET}${CT_TARGET_KERNEL:+-${CT_TARGET_KERNEL}}"
bartvdrmeulen@1896
  1014
    CT_TARGET="${CT_TARGET}${CT_TARGET_SYS:+-${CT_TARGET_SYS}}"
yann@1094
  1015
yann@1094
  1016
    # Sanity checks
yann@1094
  1017
    __sed_alias=""
yann@1094
  1018
    if [ -n "${CT_TARGET_ALIAS_SED_EXPR}" ]; then
yann@1094
  1019
        __sed_alias=$(echo "${CT_TARGET}" |sed -r -e "${CT_TARGET_ALIAS_SED_EXPR}")
yann@1094
  1020
    fi
yann@1094
  1021
    case ":${CT_TARGET_VENDOR}:${CT_TARGET_ALIAS}:${__sed_alias}:" in
yann@1094
  1022
      :*" "*:*:*:) CT_Abort "Don't use spaces in the vendor string, it breaks things.";;
yann@1094
  1023
      :*"-"*:*:*:) CT_Abort "Don't use dashes in the vendor string, it breaks things.";;
yann@1094
  1024
      :*:*" "*:*:) CT_Abort "Don't use spaces in the target alias, it breaks things.";;
yann@1094
  1025
      :*:*:*" "*:) CT_Abort "Don't use spaces in the target sed transform, it breaks things.";;
yann@1094
  1026
    esac
yann@1094
  1027
yann@1094
  1028
    # Canonicalise it
yann@1094
  1029
    CT_TARGET=$(CT_DoConfigSub "${CT_TARGET}")
yann@391
  1030
    # Prepare the target CFLAGS
yann@767
  1031
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ENDIAN_CFLAG}"
yann@527
  1032
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ARCH_CFLAG}"
yann@397
  1033
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ABI_CFLAG}"
yann@397
  1034
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_CPU_CFLAG}"
yann@397
  1035
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_TUNE_CFLAG}"
yann@397
  1036
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FPU_CFLAG}"
yann@397
  1037
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FLOAT_CFLAG}"
yann@527
  1038
yann@527
  1039
    # Now on for the target LDFLAGS
yann@767
  1040
    CT_ARCH_TARGET_LDFLAGS="${CT_ARCH_TARGET_LDFLAGS} ${CT_ARCH_ENDIAN_LDFLAG}"
yann@63
  1041
}
yann@121
  1042
yann@121
  1043
# This function does pause the build until the user strikes "Return"
yann@121
  1044
# Usage: CT_DoPause [optional_message]
yann@121
  1045
CT_DoPause() {
yann@121
  1046
    local foo
yann@121
  1047
    local message="${1:-Pausing for your pleasure}"
yann@121
  1048
    CT_DoLog INFO "${message}"
yann@523
  1049
    read -p "Press 'Enter' to continue, or Ctrl-C to stop..." foo >&6
yann@121
  1050
    return 0
yann@121
  1051
}
yann@121
  1052
yann@1907
  1053
# This function creates a tarball of the specified directory, but
yann@1907
  1054
# only if it exists
yann@1907
  1055
# Usage: CT_DoTarballIfExists <dir> <tarball_basename> [extra_tar_options [...]]
yann@1907
  1056
CT_DoTarballIfExists() {
yann@1907
  1057
    local dir="$1"
yann@1907
  1058
    local tarball="$2"
yann@1907
  1059
    shift 2
yann@1907
  1060
    local -a extra_tar_opts=( "$@" )
yann@1908
  1061
    local -a compress
yann@1907
  1062
yann@1907
  1063
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@1908
  1064
        y)  compress=( gzip -c -3 - ); tar_ext=.gz;;
yann@1908
  1065
        *)  compress=( cat - );        tar_ext=;;
yann@1907
  1066
    esac
yann@1907
  1067
yann@1907
  1068
    if [ -d "${dir}" ]; then
yann@1907
  1069
        CT_DoLog DEBUG "  Saving '${dir}'"
yann@1908
  1070
        { tar c -C "${dir}" -v -f - "${extra_tar_opts[@]}" .    \
yann@1908
  1071
          |"${compress[@]}" >"${tarball}.tar${tar_ext}"         ;
anthony@2155
  1072
        } 2>&1 |sed -r -e 's/^/    /;' |CT_DoLog STATE
yann@1907
  1073
    else
anthony@2155
  1074
        CT_DoLog STATE "  Not saving '${dir}': does not exist"
yann@1907
  1075
    fi
yann@1907
  1076
}
yann@1907
  1077
yann@1907
  1078
# This function extracts a tarball to the specified directory, but
yann@1907
  1079
# only if the tarball exists
anthony@2155
  1080
# Usage: CT_DoExtractTarballIfExists <tarball_basename> <dir> [extra_tar_options [...]]
yann@1907
  1081
CT_DoExtractTarballIfExists() {
yann@1907
  1082
    local tarball="$1"
yann@1907
  1083
    local dir="$2"
yann@1907
  1084
    shift 2
yann@1907
  1085
    local -a extra_tar_opts=( "$@" )
yann@1908
  1086
    local -a uncompress
yann@1907
  1087
yann@1907
  1088
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@1908
  1089
        y)  uncompress=( gzip -c -d ); tar_ext=.gz;;
yann@1908
  1090
        *)  uncompress=( cat );        tar_ext=;;
yann@1907
  1091
    esac
yann@1907
  1092
yann@1907
  1093
    if [ -f "${tarball}.tar${tar_ext}" ]; then
yann@1907
  1094
        CT_DoLog DEBUG "  Restoring '${dir}'"
yann@1907
  1095
        CT_DoForceRmdir "${dir}"
yann@1907
  1096
        CT_DoExecLog DEBUG mkdir -p "${dir}"
yann@1908
  1097
        { "${uncompress[@]}" "${tarball}.tar${tar_ext}"     \
yann@1908
  1098
          |tar x -C "${dir}" -v -f - "${extra_tar_opts[@]}" ;
anthony@2155
  1099
        } 2>&1 |sed -r -e 's/^/    /;' |CT_DoLog STATE
yann@1907
  1100
    else
anthony@2155
  1101
        CT_DoLog STATE "  Not restoring '${dir}': does not exist"
yann@1907
  1102
    fi
yann@1907
  1103
}
yann@1907
  1104
yann@121
  1105
# This function saves the state of the toolchain to be able to restart
yann@121
  1106
# at any one point
yann@121
  1107
# Usage: CT_DoSaveState <next_step_name>
yann@121
  1108
CT_DoSaveState() {
yann@121
  1109
	[ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
yann@121
  1110
    local state_name="$1"
yann@121
  1111
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@121
  1112
yann@1266
  1113
    # Log this to the log level required by the user
yann@1266
  1114
    CT_DoLog ${CT_LOG_LEVEL_MAX} "Saving state to restart at step '${state_name}'..."
yann@1134
  1115
yann@121
  1116
    rm -rf "${state_dir}"
yann@121
  1117
    mkdir -p "${state_dir}"
yann@121
  1118
anthony@2155
  1119
    CT_DoLog STATE "  Saving environment and aliases"
yann@738
  1120
    # We must omit shell functions, and some specific bash variables
yann@738
  1121
    # that break when restoring the environment, later. We could do
yann@1299
  1122
    # all the processing in the awk script, but a sed is easier...
yann@1299
  1123
    set |awk '
yann@1017
  1124
              BEGIN { _p = 1; }
yann@1017
  1125
              $0~/^[^ ]+ \(\)/ { _p = 0; }
yann@1017
  1126
              _p == 1
yann@1017
  1127
              $0 == "}" { _p = 1; }
yann@1017
  1128
              ' |sed -r -e '/^BASH_(ARGC|ARGV|LINENO|SOURCE|VERSINFO)=/d;
yann@738
  1129
                           /^(UID|EUID)=/d;
yann@738
  1130
                           /^(FUNCNAME|GROUPS|PPID|SHELLOPTS)=/d;' >"${state_dir}/env.sh"
yann@121
  1131
yann@2308
  1132
    CT_DoTarballIfExists "${CT_BUILDTOOLS_PREFIX_DIR}" "${state_dir}/buildtools_dir"
yann@2502
  1133
    CT_DoTarballIfExists "${CT_COMPLIBS_DIR}" "${state_dir}/complibs_dir"
yann@1907
  1134
    CT_DoTarballIfExists "${CT_CONFIG_DIR}" "${state_dir}/config_dir"
yann@1907
  1135
    CT_DoTarballIfExists "${CT_CC_CORE_STATIC_PREFIX_DIR}" "${state_dir}/cc_core_static_prefix_dir"
yann@1907
  1136
    CT_DoTarballIfExists "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${state_dir}/cc_core_shared_prefix_dir"
yann@1907
  1137
    CT_DoTarballIfExists "${CT_PREFIX_DIR}" "${state_dir}/prefix_dir" --exclude '*.log'
yann@121
  1138
yann@2339
  1139
    CT_DoLog STATE "  Saving log file"
yann@2339
  1140
    exec >/dev/null
yann@2339
  1141
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@2339
  1142
        y)  gzip -3 -c "${tmp_log_file}"  >"${state_dir}/log.gz";;
yann@2339
  1143
        *)  cat "${tmp_log_file}" >"${state_dir}/log";;
yann@2339
  1144
    esac
yann@2339
  1145
    exec >>"${tmp_log_file}"
yann@121
  1146
}
yann@121
  1147
yann@136
  1148
# This function restores a previously saved state
yann@121
  1149
# Usage: CT_DoLoadState <state_name>
yann@121
  1150
CT_DoLoadState(){
yann@121
  1151
    local state_name="$1"
yann@121
  1152
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@135
  1153
    local old_RESTART="${CT_RESTART}"
yann@135
  1154
    local old_STOP="${CT_STOP}"
yann@121
  1155
yann@523
  1156
    CT_TestOrAbort "The previous build did not reach the point where it could be restarted at '${CT_RESTART}'" -d "${state_dir}"
yann@141
  1157
yann@121
  1158
    # We need to do something special with the log file!
yann@121
  1159
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
  1160
        exec >"${state_dir}/tail.log"
yann@121
  1161
    fi
yann@1266
  1162
yann@1266
  1163
    # Log this to the log level required by the user
yann@1266
  1164
    CT_DoLog ${CT_LOG_LEVEL_MAX} "Restoring state at step '${state_name}', as requested."
yann@121
  1165
yann@1907
  1166
    CT_DoExtractTarballIfExists "${state_dir}/prefix_dir" "${CT_PREFIX_DIR}"
yann@1907
  1167
    CT_DoExtractTarballIfExists "${state_dir}/cc_core_shared_prefix_dir" "${CT_CC_CORE_SHARED_PREFIX_DIR}"
yann@1907
  1168
    CT_DoExtractTarballIfExists "${state_dir}/cc_core_static_prefix_dir" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@1907
  1169
    CT_DoExtractTarballIfExists "${state_dir}/config_dir" "${CT_CONFIG_DIR}"
yann@2502
  1170
    CT_DoExtractTarballIfExists "${state_dir}/complibs_dir" "${CT_COMPLIBS_DIR}"
yann@2308
  1171
    CT_DoExtractTarballIfExists "${state_dir}/buildtools_dir" "${CT_BUILDTOOLS_PREFIX_DIR}"
yann@1894
  1172
yann@121
  1173
    # Restore the environment, discarding any error message
yann@121
  1174
    # (for example, read-only bash internals)
anthony@2155
  1175
    CT_DoLog STATE "  Restoring environment"
yann@121
  1176
    . "${state_dir}/env.sh" >/dev/null 2>&1 || true
yann@121
  1177
yann@135
  1178
    # Restore the new RESTART and STOP steps
yann@135
  1179
    CT_RESTART="${old_RESTART}"
yann@135
  1180
    CT_STOP="${old_STOP}"
yann@135
  1181
    unset old_stop old_restart
yann@135
  1182
yann@2339
  1183
    CT_DoLog STATE "  Restoring log file"
yann@2339
  1184
    exec >/dev/null
yann@2339
  1185
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@2339
  1186
        y)  zcat "${state_dir}/log.gz" >"${tmp_log_file}";;
yann@2339
  1187
        *)  cat "${state_dir}/log" >"${tmp_log_file}";;
yann@2339
  1188
    esac
yann@2339
  1189
    cat "${state_dir}/tail.log" >>"${tmp_log_file}"
yann@2339
  1190
    exec >>"${tmp_log_file}"
yann@2339
  1191
    rm -f "${state_dir}/tail.log"
yann@121
  1192
}