scripts/functions
author "Benoît THÉBAUDEAU" <benoit.thebaudeau@advansee.com>
Thu Aug 18 16:05:48 2011 +0200 (2011-08-18)
changeset 2612 a52574521bea
parent 2608 aa09a36c3d36
child 2645 9cb3554bebeb
permissions -rw-r--r--
debug/gdb: update GDB revisions

Update GDB with the latest available revisions.

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