scripts/functions
author Johannes Stezenbach <js@sig21.net>
Sun Oct 14 23:46:15 2012 +0000 (2012-10-14)
changeset 3082 39ec9e913d79
parent 3075 aadd4647dd91
child 3083 3a7b2eee9dcd
permissions -rw-r--r--
scripts: move backtrace marker to CT_WORK_DIR

Avoid error when commands in scripts/crosstool-NG.sh fail
before CT_BUILD_DIR is set.

So we need to remove the backtrace marker of a potential previous
build. Previously, it was implicitly removed because we did remove
the directory it was in, which is no longer the case.

Signed-off-by: Johannes Stezenbach <js@sig21.net>
[yann.morin.1998@free.fr: remove backtrace marker on start of build]
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
Message-Id: <20121015094615.GA18673@sig21.net>
Patchwork-Id: 191498
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
js@3082
    14
    if [ ! -f "${CT_WORK_DIR}/backtrace" ]; then
js@3082
    15
        touch "${CT_WORK_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
js@3082
    53
        rm -f "${CT_WORK_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@3040
   274
# Search a program: wrap "which" for those system where "which"
yann@3040
   275
# verbosely says there is no match (such as on Mandriva).
yann@210
   276
# Usage: CT_Which <filename>
yann@210
   277
CT_Which() {
yann@210
   278
  which "$1" 2>/dev/null || true
yann@210
   279
}
yann@210
   280
yann@1
   281
# Get current date with nanosecond precision
yann@1
   282
# On those system not supporting nanosecond precision, faked with rounding down
yann@1
   283
# to the highest entire second
yann@1
   284
# Usage: CT_DoDate <fmt>
yann@1
   285
CT_DoDate() {
yann@2383
   286
    date "$1" |sed -r -e 's/%?N$/000000000/;'
yann@1
   287
}
yann@1
   288
yann@1
   289
CT_STEP_COUNT=1
yann@2807
   290
CT_STEP_MESSAGE[${CT_STEP_COUNT}]="(top-level)"
yann@1
   291
# Memorise a step being done so that any error is caught
yann@1
   292
# Usage: CT_DoStep <loglevel> <message>
yann@1
   293
CT_DoStep() {
yann@523
   294
    local start=$(CT_DoDate +%s%N)
yann@1
   295
    CT_DoLog "$1" "================================================================="
yann@1
   296
    CT_DoLog "$1" "$2"
yann@1
   297
    CT_STEP_COUNT=$((CT_STEP_COUNT+1))
yann@1
   298
    CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
yann@1
   299
    CT_STEP_START[${CT_STEP_COUNT}]="${start}"
yann@1
   300
    CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
yann@1
   301
    return 0
yann@1
   302
}
yann@1
   303
yann@1
   304
# End the step just being done
yann@1
   305
# Usage: CT_EndStep
yann@1
   306
CT_EndStep() {
yann@523
   307
    local stop=$(CT_DoDate +%s%N)
yann@523
   308
    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
   309
    local elapsed=$(printf "%02d:%02d" $((SECONDS/60)) $((SECONDS%60)))
yann@1
   310
    local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
yann@1
   311
    local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
yann@1
   312
    CT_STEP_COUNT=$((CT_STEP_COUNT-1))
yann@582
   313
    CT_DoLog "${level}" "${message}: done in ${duration}s (at ${elapsed})"
yann@1
   314
    return 0
yann@1
   315
}
yann@1
   316
yann@1
   317
# Pushes into a directory, and pops back
yann@1
   318
CT_Pushd() {
yann@1
   319
    pushd "$1" >/dev/null 2>&1
yann@1
   320
}
yann@1
   321
CT_Popd() {
yann@1
   322
    popd >/dev/null 2>&1
yann@1
   323
}
yann@1
   324
yann@2898
   325
# Create a dir and cd or pushd into it
yann@2898
   326
# Usage: CT_mkdir_cd <dir/to/create>
yann@2898
   327
#        CT_mkdir_pushd <dir/to/create>
yann@2898
   328
CT_mkdir_cd() {
yann@2898
   329
    local dir="${1}"
yann@2898
   330
yann@2898
   331
    mkdir -p "${dir}"
yann@2898
   332
    cd "${dir}"
yann@2898
   333
}
yann@2898
   334
CT_mkdir_pushd() {
yann@2898
   335
    local dir="${1}"
yann@2898
   336
yann@2898
   337
    mkdir -p "${dir}"
yann@2898
   338
    CT_Pushd "${dir}"
yann@2898
   339
}
yann@2898
   340
yann@1
   341
# Creates a temporary directory
yann@1
   342
# $1: variable to assign to
yann@1
   343
# Usage: CT_MktempDir foo
yann@1
   344
CT_MktempDir() {
yann@1
   345
    # Some mktemp do not allow more than 6 Xs
yann@1113
   346
    eval "$1"=$(mktemp -q -d "${CT_BUILD_DIR}/tmp.XXXXXX")
yann@1
   347
    CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
yann@787
   348
    CT_DoLog DEBUG "Made temporary directory '${!1}'"
yann@787
   349
    return 0
yann@1
   350
}
yann@1
   351
yann@1148
   352
# Removes one or more directories, even if it is read-only, or its parent is
yann@1138
   353
# Usage: CT_DoForceRmdir dir [...]
yann@1138
   354
CT_DoForceRmdir() {
yann@1148
   355
    local dir
yann@1148
   356
    local mode
yann@1148
   357
    for dir in "${@}"; do
yann@1148
   358
        [ -d "${dir}" ] || continue
titus@1956
   359
        case "$CT_SYS_OS" in
yann@2029
   360
            Linux|CYGWIN*)
titus@1956
   361
                mode="$(stat -c '%a' "$(dirname "${dir}")")"
titus@1956
   362
                ;;
titus@1956
   363
            Darwin|*BSD)
titus@1956
   364
                mode="$(stat -f '%Lp' "$(dirname "${dir}")")"
titus@1956
   365
                ;;
titus@1956
   366
            *)
titus@1956
   367
                CT_Abort "Unhandled host OS $CT_SYS_OS"
titus@1956
   368
                ;;
titus@1956
   369
        esac
yann@1185
   370
        CT_DoExecLog ALL chmod u+w "$(dirname "${dir}")"
yann@1185
   371
        CT_DoExecLog ALL chmod -R u+w "${dir}"
yann@1148
   372
        CT_DoExecLog ALL rm -rf "${dir}"
yann@1185
   373
        CT_DoExecLog ALL chmod ${mode} "$(dirname "${dir}")"
yann@1148
   374
    done
yann@1138
   375
}
yann@1138
   376
yann@1
   377
# Echoes the specified string on stdout until the pipe breaks.
yann@1
   378
# Doesn't fail
yann@1
   379
# $1: string to echo
yann@1
   380
# Usage: CT_DoYes "" |make oldconfig
yann@1
   381
CT_DoYes() {
yann@1
   382
    yes "$1" || true
yann@1
   383
}
yann@63
   384
yann@1391
   385
# Add the specified directory to LD_LIBRARY_PATH, and export it
yann@1391
   386
# If the specified patch is already present, just export
yann@1391
   387
# $1: path to add
yann@1391
   388
# $2: add as 'first' or 'last' path, 'first' is assumed if $2 is empty
yann@1391
   389
# Usage CT_SetLibPath /some/where/lib [first|last]
yann@1391
   390
CT_SetLibPath() {
yann@1391
   391
    local path="$1"
yann@1391
   392
    local pos="$2"
yann@1391
   393
yann@1391
   394
    case ":${LD_LIBRARY_PATH}:" in
yann@1391
   395
        *:"${path}":*)  ;;
yann@1391
   396
        *)  case "${pos}" in
yann@1391
   397
                last)
yann@1391
   398
                    CT_DoLog DEBUG "Adding '${path}' at end of LD_LIBRARY_PATH"
yann@1391
   399
                    LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${path}"
yann@1391
   400
                    ;;
yann@1391
   401
                first|"")
yann@1391
   402
                    CT_DoLog DEBUG "Adding '${path}' at start of LD_LIBRARY_PATH"
yann@1391
   403
                    LD_LIBRARY_PATH="${path}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
yann@1391
   404
                    ;;
yann@1391
   405
                *)
yann@1391
   406
                    CT_Abort "Incorrect position '${pos}' to add '${path}' to LD_LIBRARY_PATH"
yann@1391
   407
                    ;;
yann@1391
   408
            esac
yann@1391
   409
            ;;
yann@1391
   410
    esac
yann@1391
   411
    CT_DoLog DEBUG "==> LD_LIBRARY_PATH='${LD_LIBRARY_PATH}'"
yann@1391
   412
    export LD_LIBRARY_PATH
yann@1391
   413
}
yann@1391
   414
yann@2608
   415
# Build up the list of allowed tarball extensions
yann@2608
   416
# Add them in the prefered order; most preferred comes first
yann@2608
   417
CT_DoListTarballExt() {
yann@2840
   418
    if [ "${CT_CONFIGURE_has_xz}" = "y" ]; then
yann@2609
   419
        printf ".tar.xz\n"
yann@2609
   420
    fi
yann@2646
   421
    if [    "${CT_CONFIGURE_has_lzma}" = "y"    \
yann@2840
   422
         -o "${CT_CONFIGURE_has_xz}" = "y" ]; then
yann@2645
   423
        printf ".tar.lzma\n"
yann@2645
   424
    fi
yann@2608
   425
    printf ".tar.bz2\n"
yann@2608
   426
    printf ".tar.gz\n.tgz\n"
yann@2608
   427
    printf ".tar\n"
yann@2608
   428
}
yann@2608
   429
yann@85
   430
# Get the file name extension of a component
yann@719
   431
# Usage: CT_GetFileExtension <component_name-component_version> [extension]
yann@1690
   432
# If found, echoes the extension to stdout, and return 0
yann@1690
   433
# If not found, echoes nothing on stdout, and return !0.
yann@85
   434
CT_GetFileExtension() {
yann@85
   435
    local ext
yann@85
   436
    local file="$1"
yann@719
   437
    shift
yann@719
   438
    local first_ext="$1"
yann@85
   439
yann@160
   440
    # we need to also check for an empty extension for those very
yann@160
   441
    # peculiar components that don't have one (such as sstrip from
yann@160
   442
    # buildroot).
yann@2608
   443
    for ext in ${first_ext} $(CT_DoListTarballExt) /.git ''; do
yann@1763
   444
        if [ -e "${CT_TARBALLS_DIR}/${file}${ext}" ]; then
yann@85
   445
            echo "${ext}"
yann@1690
   446
            exit 0
yann@85
   447
        fi
yann@85
   448
    done
yann@85
   449
yann@1690
   450
    exit 1
yann@85
   451
}
yann@85
   452
yann@2204
   453
# Try to retrieve the specified URL (HTTP or FTP)
yann@2204
   454
# Usage: CT_DoGetFile <URL>
yann@2204
   455
# This functions always returns true (0), as it can be legitimate not
yann@2204
   456
# to find the requested URL (think about snapshots, different layouts
yann@2204
   457
# for different gcc versions, etc...).
yann@2204
   458
CT_DoGetFile() {
yann@2501
   459
    local url="${1}"
yann@2501
   460
    local dest="${CT_TARBALLS_DIR}/${url##*/}"
yann@2205
   461
    local tmp="${dest}.tmp-dl"
yann@2204
   462
yann@2205
   463
    # Remove potential left-over from a previous run
yann@2205
   464
    rm -f "${tmp}"
yann@2205
   465
yann@2204
   466
    # We also retry a few times, in case there is a transient error (eg. behind
yann@2204
   467
    # a dynamic IP that changes during the transfer...)
yann@2204
   468
    # With automated download as we are doing, it can be very dangerous to
yann@2204
   469
    # continue the downloads. It's far better to simply overwrite the
yann@2204
   470
    # destination file.
yann@492
   471
    # Some company networks have firewalls to connect to the internet, but it's
yann@2660
   472
    # not easy to detect them, so force a global ${CT_CONNECT_TIMEOUT}-second
yann@2660
   473
    # timeout.
yann@2204
   474
    # For curl, no good progress indicator is available. So, be silent.
yann@2763
   475
    if CT_DoExecLog ALL wget --passive-ftp --tries=3 -nc    \
yann@2763
   476
                             --progress=dot:binary          \
yann@2763
   477
                             -T ${CT_CONNECT_TIMEOUT}       \
yann@2763
   478
                             -O "${tmp}"                    \
yann@2661
   479
                             "${url}"
yann@2661
   480
    then
yann@2661
   481
        # Success, we got it, good!
yann@2205
   482
        mv "${tmp}" "${dest}"
yann@2205
   483
    else
yann@2205
   484
        # Woops...
yann@2205
   485
        rm -f "${tmp}"
yann@2205
   486
    fi
yann@63
   487
}
yann@63
   488
yann@1113
   489
# This function tries to retrieve a tarball form a local directory
yann@1113
   490
# Usage: CT_GetLocal <basename> [.extension]
yann@1113
   491
CT_GetLocal() {
yann@1113
   492
    local basename="$1"
yann@1113
   493
    local first_ext="$2"
yann@1113
   494
    local ext
yann@1113
   495
yann@1113
   496
    # Do we already have it in *our* tarballs dir?
yann@1690
   497
    if ext="$( CT_GetFileExtension "${basename}" ${first_ext} )"; then
yann@1113
   498
        CT_DoLog DEBUG "Already have '${basename}'"
yann@1113
   499
        return 0
yann@1113
   500
    fi
yann@1113
   501
yann@1113
   502
    if [ -n "${CT_LOCAL_TARBALLS_DIR}" ]; then
yann@1113
   503
        CT_DoLog DEBUG "Trying to retrieve an already downloaded copy of '${basename}'"
yann@1113
   504
        # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
yann@1113
   505
        # or, as a failover, a file without extension.
yann@2608
   506
        for ext in ${first_ext} $(CT_DoListTarballExt) ''; do
yann@1113
   507
            CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}'"
yann@1113
   508
            if [ -r "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" -a \
yann@1113
   509
                 "${CT_FORCE_DOWNLOAD}" != "y" ]; then
yann@1113
   510
                CT_DoLog DEBUG "Got '${basename}' from local storage"
yann@1113
   511
                CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" "${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1113
   512
                return 0
yann@1113
   513
            fi
yann@1113
   514
        done
yann@1113
   515
    fi
yann@1113
   516
    return 1
yann@1113
   517
}
yann@1113
   518
david@3075
   519
# This function gets the custom source from either a tarball or directory
david@3075
   520
# Usage: CT_GetCustom <component> <custom_version> <custom_location>
david@3075
   521
CT_GetCustom() {
david@3075
   522
    local custom_component="$1"
david@3075
   523
    local custom_version="$2"
david@3075
   524
    local custom_location="$3"
david@3075
   525
    local custom_name="${custom_component}-${custom_version}"
david@3075
   526
david@3075
   527
    CT_TestAndAbort "${custom_name}: CT_CUSTOM_LOCATION_ROOT_DIR or ${custom_component}'s CUSTOM_LOCATION must be set." \
david@3075
   528
                    -z "${CT_CUSTOM_LOCATION_ROOT_DIR}" -a -z "${custom_location}"
david@3075
   529
david@3075
   530
    if [ -n "${CT_CUSTOM_LOCATION_ROOT_DIR}" \
david@3075
   531
         -a -z "${custom_location}"          ]; then
david@3075
   532
        custom_location="${CT_CUSTOM_LOCATION_ROOT_DIR}/${custom_component}"
david@3075
   533
    fi
david@3075
   534
david@3075
   535
    CT_DoLog EXTRA "Using '${custom_name}' from custom location"
david@3075
   536
    if [ ! -d "${custom_location}" ]; then
david@3075
   537
        # We need to know the custom tarball extension,
david@3075
   538
        # so we can create a properly-named symlink, which
david@3075
   539
        # we use later on in 'extract'
david@3075
   540
        case "${custom_location}" in
david@3075
   541
            *.tar.bz2)      custom_name="${custom_name}.tar.bz2";;
david@3075
   542
            *.tar.gz|*.tgz) custom_name="${custom_name}.tar.gz";;
david@3075
   543
            *.tar)          custom_name="${custom_name}.tar";;
david@3075
   544
            *)  CT_Abort "Unknown extension for custom tarball '${custom_location}'";;
david@3075
   545
        esac
david@3075
   546
        CT_DoExecLog DEBUG ln -sf "${custom_location}"  \
david@3075
   547
                                  "${CT_TARBALLS_DIR}/${custom_name}"
david@3075
   548
    else
david@3075
   549
        CT_DoExecLog DEBUG ln -snf "${custom_location}" \
david@3075
   550
                                   "${CT_SRC_DIR}/${custom_name}"
david@3075
   551
    fi
david@3075
   552
}
david@3075
   553
yann@1113
   554
# This function saves the specified to local storage if possible,
yann@1113
   555
# and if so, symlinks it for later usage
yann@1113
   556
# Usage: CT_SaveLocal </full/path/file.name>
yann@1113
   557
CT_SaveLocal() {
yann@1113
   558
    local file="$1"
yann@1113
   559
    local basename="${file##*/}"
yann@1113
   560
yann@1113
   561
    if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
yann@1134
   562
        CT_DoLog EXTRA "Saving '${basename}' to local storage"
yann@1113
   563
        # The file may already exist if downloads are forced: remove it first
yann@1113
   564
        CT_DoExecLog ALL rm -f "${CT_LOCAL_TARBALLS_DIR}/${basename}"
yann@1113
   565
        CT_DoExecLog ALL mv -f "${file}" "${CT_LOCAL_TARBALLS_DIR}"
yann@1113
   566
        CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}" "${file}"
yann@1113
   567
    fi
yann@1113
   568
}
yann@1113
   569
yann@63
   570
# Download the file from one of the URLs passed as argument
yann@1113
   571
# Usage: CT_GetFile <basename> [.extension] <url> [url ...]
yann@63
   572
CT_GetFile() {
yann@63
   573
    local ext
yann@2594
   574
    local -a URLS
yann@2594
   575
    local url
yann@63
   576
    local file="$1"
yann@1113
   577
    local first_ext
yann@63
   578
    shift
yann@712
   579
    # If next argument starts with a dot, then this is not an URL,
yann@712
   580
    # and we can consider that it is a preferred extension.
yann@242
   581
    case "$1" in
yann@712
   582
        .*) first_ext="$1"
yann@242
   583
            shift
yann@242
   584
            ;;
yann@242
   585
    esac
yann@63
   586
yann@1113
   587
    # Does it exist localy?
yann@2599
   588
    if CT_GetLocal "${file}" ${first_ext}; then
yann@2599
   589
        return 0
yann@2599
   590
    fi
yann@1113
   591
    # No, it does not...
yann@63
   592
austinpmorton@3048
   593
    # If not allowed to download from the Internet, don't
austinpmorton@3048
   594
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
austinpmorton@3048
   595
        CT_DoLog DEBUG "Not allowed to download from the Internet, aborting ${file} download"
austinpmorton@3048
   596
        return 1
austinpmorton@3048
   597
    fi
austinpmorton@3048
   598
yann@754
   599
    # Try to retrieve the file
yann@788
   600
    CT_DoLog EXTRA "Retrieving '${file}'"
yann@754
   601
yann@1022
   602
    # Add URLs on the LAN mirror
yann@1022
   603
    if [ "${CT_USE_MIRROR}" = "y" ]; then
yann@1294
   604
        CT_TestOrAbort "Please set the mirror base URL" -n "${CT_MIRROR_BASE_URL}"
yann@2594
   605
        URLS+=( "${CT_MIRROR_BASE_URL}/${file%-*}" )
yann@2594
   606
        URLS+=( "${CT_MIRROR_BASE_URL}" )
yann@2593
   607
    fi
yann@695
   608
austinpmorton@3048
   609
    if [ "${CT_FORCE_MIRROR}" != "y" ]; then
yann@2595
   610
        URLS+=( "${@}" )
yann@2595
   611
    fi
yann@1022
   612
yann@1022
   613
    # Scan all URLs in turn, and try to grab a tarball from there
yann@1763
   614
    # Do *not* try git trees (ext=/.git), this is handled in a specific
yann@1763
   615
    # wrapper, below
yann@2608
   616
    for ext in ${first_ext} $(CT_DoListTarballExt) ''; do
yann@102
   617
        # Try all urls in turn
yann@2594
   618
        for url in "${URLS[@]}"; do
yann@2589
   619
            [ -n "${url}" ] || continue
yann@523
   620
            CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
yann@265
   621
            CT_DoGetFile "${url}/${file}${ext}"
yann@2492
   622
            if [ -f "${CT_TARBALLS_DIR}/${file}${ext}" ]; then
yann@799
   623
                CT_DoLog DEBUG "Got '${file}' from the Internet"
yann@1113
   624
                CT_SaveLocal "${CT_TARBALLS_DIR}/${file}${ext}"
yann@265
   625
                return 0
yann@265
   626
            fi
yann@102
   627
        done
yann@102
   628
    done
yann@63
   629
yann@2599
   630
    # Just return error, someone may want to catch and handle the error
yann@2492
   631
    # (eg. glibc/eglibc add-ons can be missing).
yann@2492
   632
    return 1
yann@63
   633
}
yann@63
   634
yann@1113
   635
# Checkout from CVS, and build the associated tarball
yann@1113
   636
# The tarball will be called ${basename}.tar.bz2
yann@1113
   637
# Prerequisite: either the server does not require password,
yann@1113
   638
# or the user must already be logged in.
yann@1113
   639
# 'tag' is the tag to retrieve. Must be specified, but can be empty.
yann@1113
   640
# If dirname is specified, then module will be renamed to dirname
yann@1113
   641
# prior to building the tarball.
yann@1592
   642
# Usage: CT_GetCVS <basename> <url> <module> <tag> [dirname[=subdir]]
yann@1592
   643
# Note: if '=subdir' is given, then it is used instead of 'module'.
yann@1113
   644
CT_GetCVS() {
yann@1113
   645
    local basename="$1"
yann@1113
   646
    local uri="$2"
yann@1113
   647
    local module="$3"
yann@1113
   648
    local tag="${4:+-r ${4}}"
yann@1113
   649
    local dirname="$5"
yann@1113
   650
    local tmp_dir
yann@1113
   651
yann@2592
   652
    # First try locally, then the mirror
yann@2592
   653
    if CT_GetFile "${basename}"; then
yann@2592
   654
        # Got it! Return early! :-)
yann@2592
   655
        return 0
yann@2592
   656
    fi
yann@1113
   657
yann@2595
   658
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   659
        CT_DoLog WARN "Downloads forbidden, not trying cvs retrieval"
yann@2595
   660
        return 1
yann@2595
   661
    fi
yann@2595
   662
yann@1113
   663
    CT_MktempDir tmp_dir
yann@1113
   664
    CT_Pushd "${tmp_dir}"
yann@1113
   665
yann@1113
   666
    CT_DoExecLog ALL cvs -z 9 -d "${uri}" co -P ${tag} "${module}"
yann@1592
   667
    if [ -n "${dirname}" ]; then
yann@1592
   668
        case "${dirname}" in
yann@1592
   669
            *=*)
yann@1593
   670
                CT_DoExecLog DEBUG mv "${dirname#*=}" "${dirname%%=*}"
yann@1593
   671
                CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${dirname%%=*}"
yann@1592
   672
                ;;
yann@1592
   673
            *)
yann@1592
   674
                CT_DoExecLog ALL mv "${module}" "${dirname}"
yann@1592
   675
                CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${dirname:-${module}}"
yann@1592
   676
                ;;
yann@1592
   677
        esac
yann@1592
   678
    fi
yann@1113
   679
    CT_SaveLocal "${CT_TARBALLS_DIR}/${basename}.tar.bz2"
yann@1113
   680
yann@1113
   681
    CT_Popd
yann@1113
   682
    CT_DoExecLog ALL rm -rf "${tmp_dir}"
yann@1113
   683
}
yann@1113
   684
yann@1244
   685
# Check out from SVN, and build the associated tarball
yann@1244
   686
# The tarball will be called ${basename}.tar.bz2
yann@1244
   687
# Prerequisite: either the server does not require password,
yann@1244
   688
# or the user must already be logged in.
yann@1244
   689
# 'rev' is the revision to retrieve
yann@1244
   690
# Usage: CT_GetSVN <basename> <url> [rev]
yann@1244
   691
CT_GetSVN() {
yann@1244
   692
    local basename="$1"
yann@1244
   693
    local uri="$2"
yann@1244
   694
    local rev="$3"
yann@1244
   695
yann@2590
   696
    # First try locally, then the mirror
yann@2590
   697
    if CT_GetFile "${basename}"; then
yann@2590
   698
        # Got it! Return early! :-)
yann@2590
   699
        return 0
yann@2590
   700
    fi
yann@1244
   701
yann@2595
   702
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   703
        CT_DoLog WARN "Downloads forbidden, not trying svn retrieval"
yann@2595
   704
        return 1
yann@2595
   705
    fi
yann@2595
   706
yann@1244
   707
    CT_MktempDir tmp_dir
yann@1244
   708
    CT_Pushd "${tmp_dir}"
yann@1244
   709
yann@2494
   710
    if ! CT_DoExecLog ALL svn export ${rev:+-r ${rev}} "${uri}" "${basename}"; then
yann@2494
   711
        CT_DoLog WARN "Could not retrieve '${basename}'"
yann@2494
   712
        return 1
yann@2494
   713
    fi
yann@1244
   714
    CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${basename}"
yann@1244
   715
    CT_SaveLocal "${CT_TARBALLS_DIR}/${basename}.tar.bz2"
yann@1244
   716
yann@1244
   717
    CT_Popd
yann@1244
   718
    CT_DoExecLog ALL rm -rf "${tmp_dir}"
yann@1244
   719
}
yann@1244
   720
yann@1763
   721
# Clone a git tree
yann@1763
   722
# Tries the given URLs in turn until one can get cloned. No tarball will be created.
yann@1763
   723
# Prerequisites: either the server does not require password,
yann@1763
   724
# or the user has already taken any action to authenticate to the server.
yann@1763
   725
# The cloned tree will *not* be stored in the local tarballs dir!
yann@1763
   726
# Usage: CT_GetGit <basename> <url [url ...]>
yann@1763
   727
CT_GetGit() {
yann@1763
   728
    local basename="$1"; shift
yann@1763
   729
    local url
yann@1763
   730
    local cloned=0
yann@1763
   731
yann@2595
   732
    if [ "${CT_FORBID_DOWNLOAD}" = "y" ]; then
yann@2595
   733
        CT_DoLog WARN "Downloads forbidden, not trying git retrieval"
yann@2595
   734
        return 1
yann@2595
   735
    fi
yann@2595
   736
yann@1763
   737
    # Do we have it in our tarballs dir?
yann@1763
   738
    if [ -d "${CT_TARBALLS_DIR}/${basename}/.git" ]; then
yann@1763
   739
        CT_DoLog EXTRA "Updating git tree '${basename}'"
yann@1763
   740
        CT_Pushd "${CT_TARBALLS_DIR}/${basename}"
yann@1763
   741
        CT_DoExecLog ALL git pull
yann@1763
   742
        CT_Popd
yann@1763
   743
    else
yann@1763
   744
        CT_DoLog EXTRA "Retrieving git tree '${basename}'"
yann@1763
   745
        for url in "${@}"; do
yann@1763
   746
            CT_DoLog ALL "Trying to clone from '${url}'"
yann@1763
   747
            CT_DoForceRmdir "${CT_TARBALLS_DIR}/${basename}"
yann@1763
   748
            if git clone "${url}" "${CT_TARBALLS_DIR}/${basename}" 2>&1 |CT_DoLog ALL; then
yann@1763
   749
                cloned=1
yann@1763
   750
                break
yann@1763
   751
            fi
yann@1763
   752
        done
yann@1763
   753
        CT_TestOrAbort "Could not clone '${basename}'" ${cloned} -ne 0
yann@1763
   754
    fi
yann@1763
   755
}
yann@1763
   756
yann@1126
   757
# Extract a tarball
yann@63
   758
# Some tarballs need to be extracted in specific places. Eg.: glibc addons
yann@63
   759
# must be extracted in the glibc directory; uCLibc locales must be extracted
yann@1123
   760
# in the extra/locale sub-directory of uClibc. This is taken into account
yann@1123
   761
# by the caller, that did a 'cd' into the correct path before calling us
yann@1123
   762
# and sets nochdir to 'nochdir'.
yann@1763
   763
# Note also that this function handles the git trees!
yann@1763
   764
# Usage: CT_Extract <basename> [nochdir] [options]
yann@1763
   765
# where 'options' are dependent on the source (eg. git branch/tag...)
yann@1126
   766
CT_Extract() {
yann@1761
   767
    local nochdir="$1"
yann@1761
   768
    local basename
yann@1690
   769
    local ext
yann@2646
   770
    local lzma_prog
yann@2605
   771
    local -a tar_opts
yann@1690
   772
yann@1761
   773
    if [ "${nochdir}" = "nochdir" ]; then
yann@1761
   774
        shift
yann@1761
   775
        nochdir="$(pwd)"
yann@1761
   776
    else
yann@1761
   777
        nochdir="${CT_SRC_DIR}"
yann@1761
   778
    fi
yann@1761
   779
yann@1761
   780
    basename="$1"
yann@1761
   781
    shift
yann@1761
   782
yann@1690
   783
    if ! ext="$(CT_GetFileExtension "${basename}")"; then
yann@2493
   784
        CT_DoLog WARN "'${basename}' not found in '${CT_TARBALLS_DIR}'"
yann@2493
   785
        return 1
yann@1690
   786
    fi
yann@1126
   787
    local full_file="${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1126
   788
yann@1718
   789
    # Check if already extracted
yann@1718
   790
    if [ -e "${CT_SRC_DIR}/.${basename}.extracted" ]; then
yann@1718
   791
        CT_DoLog DEBUG "Already extracted '${basename}'"
yann@1718
   792
        return 0
yann@1718
   793
    fi
yann@1718
   794
yann@1691
   795
    # Check if previously partially extracted
yann@1691
   796
    if [ -e "${CT_SRC_DIR}/.${basename}.extracting" ]; then
yann@1691
   797
        CT_DoLog ERROR "The '${basename}' sources were partially extracted."
yann@1691
   798
        CT_DoLog ERROR "Please remove first:"
yann@1691
   799
        CT_DoLog ERROR " - the source dir for '${basename}', in '${CT_SRC_DIR}'"
yann@1691
   800
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${basename}.extracting'"
yann@1691
   801
        CT_Abort "I'll stop now to avoid any carnage..."
yann@1691
   802
    fi
yann@1691
   803
    CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${basename}.extracting"
yann@1691
   804
yann@1761
   805
    CT_Pushd "${nochdir}"
yann@63
   806
yann@1126
   807
    CT_DoLog EXTRA "Extracting '${basename}'"
benoit@2574
   808
    CT_DoExecLog FILE mkdir -p "${basename}"
yann@2605
   809
    tar_opts=( "--strip-components=1" )
yann@2605
   810
    tar_opts+=( "-C" "${basename}" )
yann@2605
   811
    tar_opts+=( "-xv" )
yann@2646
   812
yann@2646
   813
    # One note here:
yann@2646
   814
    # - lzma can be handled either with 'xz' or 'lzma'
yann@2646
   815
    # - we get lzma tarball only if either or both are available
yann@2646
   816
    # - so, if we get an lzma tarball, and either 'xz' or 'lzma' is
yann@2646
   817
    #   missing, we can assume the other is available
yann@2646
   818
    if [ "${CT_CONFIGURE_has_lzma}" = "y" ]; then
titus@2785
   819
        lzma_prog="lzma -fdc"
yann@2646
   820
    else
titus@2785
   821
        lzma_prog="xz -fdc"
yann@2646
   822
    fi
yann@63
   823
    case "${ext}" in
titus@2785
   824
        .tar.xz)      xz -fdc "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
titus@2785
   825
        .tar.lzma)    ${lzma_prog} "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
titus@2785
   826
        .tar.bz2)     bzip2 -dc "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
titus@2785
   827
        .tar.gz|.tgz) gzip -dc "${full_file}" | CT_DoExecLog FILE tar "${tar_opts[@]}" -f -;;
yann@2608
   828
        .tar)         CT_DoExecLog FILE tar "${tar_opts[@]}" -f "${full_file}";;
yann@1763
   829
        /.git)        CT_ExtractGit "${basename}" "${@}";;
yann@2493
   830
        *)            CT_DoLog WARN "Don't know how to handle '${basename}${ext}': unknown extension"
yann@2493
   831
                      return 1
yann@2493
   832
                      ;;
yann@63
   833
    esac
yann@63
   834
yann@1763
   835
    # Don't mark as being extracted for git
yann@1763
   836
    case "${ext}" in
yann@1763
   837
        /.git)  ;;
yann@1763
   838
        *)      CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${basename}.extracted";;
yann@1763
   839
    esac
yann@1691
   840
    CT_DoExecLog DEBUG rm -f "${CT_SRC_DIR}/.${basename}.extracting"
yann@1126
   841
yann@1761
   842
    CT_Popd
yann@1126
   843
}
yann@1126
   844
esben@2721
   845
# Create a working git clone of a local git repository
yann@1763
   846
# Usage: CT_ExtractGit <basename> [ref]
yann@1763
   847
# where 'ref' is the reference to use:
yann@1763
   848
#   the full name of a branch, like "remotes/origin/branch_name"
yann@1763
   849
#   a date as understandable by git, like "YYYY-MM-DD[ hh[:mm[:ss]]]"
yann@1763
   850
#   a tag name
esben@2721
   851
# If 'ref' is not given, the current repository HEAD will be used
yann@1763
   852
CT_ExtractGit() {
yann@1763
   853
    local basename="${1}"
yann@1763
   854
    local ref="${2}"
esben@2721
   855
    local repo
yann@1763
   856
    local ref_type
yann@1763
   857
yann@1763
   858
    # pushd now to be able to get git revlist in case ref is a date
esben@2721
   859
    repo="${CT_TARBALLS_DIR}/${basename}"
esben@2721
   860
    CT_Pushd "${repo}"
yann@1763
   861
yann@1763
   862
    # What kind of reference is ${ref} ?
yann@1763
   863
    if [ -z "${ref}" ]; then
esben@2721
   864
        ref_type=head
esben@2721
   865
        ref=$(git rev-list -n1 HEAD)
yann@1763
   866
    elif git tag |grep -E "^${ref}$" >/dev/null 2>&1; then
yann@1763
   867
        ref_type=tag
yann@1763
   868
    elif git branch -a --no-color |grep -E "^. ${ref}$" >/dev/null 2>&1; then
yann@1763
   869
        ref_type=branch
yann@1763
   870
    elif date -d "${ref}" >/dev/null 2>&1; then
yann@1763
   871
        ref_type=date
yann@1763
   872
        ref=$(git rev-list -n1 --before="${ref}")
yann@1763
   873
    else
yann@1763
   874
        CT_Abort "Reference '${ref}' is an incorrect git reference: neither tag, branch nor date"
yann@1763
   875
    fi
yann@1763
   876
esben@2721
   877
    CT_Popd
yann@1763
   878
esben@2721
   879
    CT_DoExecLog FILE rmdir "${basename}"
yann@1763
   880
    case "${ref_type}" in
esben@2721
   881
        branch) CT_DoExecLog FILE git clone -b "${ref}" "${repo}" "${basename}" ;;
esben@2721
   882
        *)      CT_DoExecLog FILE git clone "${repo}" "${basename}"
esben@2721
   883
                CT_Pushd "${basename}"
esben@2721
   884
                CT_DoExecLog FILE git checkout "${ref}"
esben@2721
   885
                CT_Popd
esben@2721
   886
                ;;
yann@1763
   887
    esac
yann@1763
   888
}
yann@1763
   889
yann@1126
   890
# Patches the specified component
yann@1761
   891
# See CT_Extract, above, for explanations on 'nochdir'
yann@1901
   892
# Usage: CT_Patch [nochdir] <packagename> <packageversion>
yann@1901
   893
# If the package directory is *not* packagename-packageversion, then
yann@1901
   894
# the caller must cd into the proper directory first, and call us
yann@1901
   895
# with nochdir
yann@1126
   896
CT_Patch() {
yann@1761
   897
    local nochdir="$1"
yann@1901
   898
    local pkgname
yann@1901
   899
    local version
yann@1901
   900
    local pkgdir
yann@1761
   901
    local base_file
yann@1761
   902
    local ver_file
yann@1507
   903
    local d
yann@1507
   904
    local -a patch_dirs
yann@1507
   905
    local bundled_patch_dir
yann@1507
   906
    local local_patch_dir
yann@1126
   907
yann@1761
   908
    if [ "${nochdir}" = "nochdir" ]; then
yann@1761
   909
        shift
yann@1906
   910
        pkgname="$1"
yann@1906
   911
        version="$2"
yann@1906
   912
        pkgdir="${pkgname}-${version}"
yann@1761
   913
        nochdir="$(pwd)"
yann@1761
   914
    else
yann@1906
   915
        pkgname="$1"
yann@1906
   916
        version="$2"
yann@1906
   917
        pkgdir="${pkgname}-${version}"
yann@1901
   918
        nochdir="${CT_SRC_DIR}/${pkgdir}"
yann@1761
   919
    fi
yann@1761
   920
yann@1126
   921
    # Check if already patched
yann@1901
   922
    if [ -e "${CT_SRC_DIR}/.${pkgdir}.patched" ]; then
yann@1901
   923
        CT_DoLog DEBUG "Already patched '${pkgdir}'"
yann@1126
   924
        return 0
yann@63
   925
    fi
yann@63
   926
yann@1271
   927
    # Check if already partially patched
yann@1901
   928
    if [ -e "${CT_SRC_DIR}/.${pkgdir}.patching" ]; then
yann@1901
   929
        CT_DoLog ERROR "The '${pkgdir}' sources were partially patched."
yann@1271
   930
        CT_DoLog ERROR "Please remove first:"
yann@1901
   931
        CT_DoLog ERROR " - the source dir for '${pkgdir}', in '${CT_SRC_DIR}'"
yann@1901
   932
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${pkgdir}.extracted'"
yann@1901
   933
        CT_DoLog ERROR " - the file '${CT_SRC_DIR}/.${pkgdir}.patching'"
yann@1271
   934
        CT_Abort "I'll stop now to avoid any carnage..."
yann@1271
   935
    fi
yann@1901
   936
    touch "${CT_SRC_DIR}/.${pkgdir}.patching"
yann@1271
   937
yann@1761
   938
    CT_Pushd "${nochdir}"
yann@1123
   939
yann@1901
   940
    CT_DoLog EXTRA "Patching '${pkgdir}'"
yann@63
   941
yann@1901
   942
    bundled_patch_dir="${CT_LIB_DIR}/patches/${pkgname}/${version}"
yann@1901
   943
    local_patch_dir="${CT_LOCAL_PATCH_DIR}/${pkgname}/${version}"
yann@1507
   944
yann@1507
   945
    case "${CT_PATCH_ORDER}" in
yann@1507
   946
        bundled)        patch_dirs=("${bundled_patch_dir}");;
yann@1507
   947
        local)          patch_dirs=("${local_patch_dir}");;
yann@1507
   948
        bundled,local)  patch_dirs=("${bundled_patch_dir}" "${local_patch_dir}");;
yann@1508
   949
        local,bundled)  patch_dirs=("${local_patch_dir}" "${bundled_patch_dir}");;
yann@1630
   950
        none)           patch_dirs=;;
yann@1507
   951
    esac
yann@1507
   952
yann@1507
   953
    for d in "${patch_dirs[@]}"; do
yann@1507
   954
        CT_DoLog DEBUG "Looking for patches in '${d}'..."
yann@1507
   955
        if [ -n "${d}" -a -d "${d}" ]; then
yann@1507
   956
            for p in "${d}"/*.patch; do
yann@63
   957
                if [ -f "${p}" ]; then
yann@523
   958
                    CT_DoLog DEBUG "Applying patch '${p}'"
yann@1765
   959
                    CT_DoExecLog ALL patch --no-backup-if-mismatch -g0 -F1 -p1 -f <"${p}"
yann@63
   960
                fi
yann@63
   961
            done
yann@1509
   962
            if [ "${CT_PATCH_SINGLE}" = "y" ]; then
yann@1509
   963
                break
yann@1509
   964
            fi
yann@63
   965
        fi
yann@63
   966
    done
yann@63
   967
yann@508
   968
    if [ "${CT_OVERIDE_CONFIG_GUESS_SUB}" = "y" ]; then
yann@508
   969
        CT_DoLog ALL "Overiding config.guess and config.sub"
yann@508
   970
        for cfg in config_guess config_sub; do
yann@1101
   971
            eval ${cfg}="${CT_LIB_DIR}/scripts/${cfg/_/.}"
yann@1101
   972
            [ -e "${CT_TOP_DIR}/scripts/${cfg/_/.}" ] && eval ${cfg}="${CT_TOP_DIR}/scripts/${cfg/_/.}"
yann@776
   973
            # Can't use CT_DoExecLog because of the '{} \;' to be passed un-mangled to find
yann@508
   974
            find . -type f -name "${cfg/_/.}" -exec cp -v "${!cfg}" {} \; |CT_DoLog ALL
yann@508
   975
        done
yann@508
   976
    fi
yann@508
   977
yann@1903
   978
    CT_DoExecLog DEBUG touch "${CT_SRC_DIR}/.${pkgdir}.patched"
yann@1903
   979
    CT_DoExecLog DEBUG rm -f "${CT_SRC_DIR}/.${pkgdir}.patching"
yann@1126
   980
yann@1761
   981
    CT_Popd
yann@63
   982
}
yann@63
   983
yann@182
   984
# Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
yann@182
   985
# Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
yann@182
   986
CT_DoConfigGuess() {
yann@1101
   987
    if [ -x "${CT_TOP_DIR}/scripts/config.guess" ]; then
yann@1101
   988
        "${CT_TOP_DIR}/scripts/config.guess"
yann@182
   989
    else
yann@1101
   990
        "${CT_LIB_DIR}/scripts/config.guess"
yann@182
   991
    fi
yann@182
   992
}
yann@182
   993
yann@182
   994
CT_DoConfigSub() {
yann@1101
   995
    if [ -x "${CT_TOP_DIR}/scripts/config.sub" ]; then
yann@1101
   996
        "${CT_TOP_DIR}/scripts/config.sub" "$@"
yann@182
   997
    else
yann@1101
   998
        "${CT_LIB_DIR}/scripts/config.sub" "$@"
yann@182
   999
    fi
yann@182
  1000
}
yann@182
  1001
yann@335
  1002
# Compute the target tuple from what is provided by the user
yann@335
  1003
# Usage: CT_DoBuildTargetTuple
yann@63
  1004
# In fact this function takes the environment variables to build the target
yann@335
  1005
# tuple. It is needed both by the normal build sequence, as well as the
yann@63
  1006
# sample saving sequence.
yann@335
  1007
CT_DoBuildTargetTuple() {
yann@383
  1008
    # Set the endianness suffix, and the default endianness gcc option
yann@2773
  1009
    case "${CT_ARCH_ENDIAN}" in
yann@2773
  1010
        big)
yann@2773
  1011
            target_endian_eb=eb
yann@383
  1012
            target_endian_el=
yann@391
  1013
            CT_ARCH_ENDIAN_CFLAG="-mbig-endian"
yann@2967
  1014
            CT_ARCH_ENDIAN_LDFLAG="-Wl,-EB"
yann@383
  1015
            ;;
yann@2773
  1016
        little)
yann@2773
  1017
            target_endian_eb=
yann@383
  1018
            target_endian_el=el
yann@391
  1019
            CT_ARCH_ENDIAN_CFLAG="-mlittle-endian"
yann@2967
  1020
            CT_ARCH_ENDIAN_LDFLAG="-Wl,-EL"
yann@383
  1021
            ;;
yann@63
  1022
    esac
yann@383
  1023
yann@965
  1024
    # Build the default architecture tuple part
yann@965
  1025
    CT_TARGET_ARCH="${CT_ARCH}"
yann@965
  1026
yann@383
  1027
    # Set defaults for the system part of the tuple. Can be overriden
yann@383
  1028
    # by architecture-specific values.
yann@383
  1029
    case "${CT_LIBC}" in
yann@787
  1030
        *glibc) CT_TARGET_SYS=gnu;;
yann@383
  1031
        uClibc) CT_TARGET_SYS=uclibc;;
yann@1591
  1032
        *)      CT_TARGET_SYS=elf;;
yann@63
  1033
    esac
yann@383
  1034
yann@391
  1035
    # Set the default values for ARCH, ABI, CPU, TUNE, FPU and FLOAT
yann@497
  1036
    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
  1037
    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
  1038
    [ "${CT_ARCH_ARCH}"     ] && { CT_ARCH_ARCH_CFLAG="-march=${CT_ARCH_ARCH}";  CT_ARCH_WITH_ARCH="--with-arch=${CT_ARCH_ARCH}"; }
yann@391
  1039
    [ "${CT_ARCH_ABI}"      ] && { CT_ARCH_ABI_CFLAG="-mabi=${CT_ARCH_ABI}";     CT_ARCH_WITH_ABI="--with-abi=${CT_ARCH_ABI}";    }
yann@391
  1040
    [ "${CT_ARCH_CPU}"      ] && { CT_ARCH_CPU_CFLAG="-mcpu=${CT_ARCH_CPU}";     CT_ARCH_WITH_CPU="--with-cpu=${CT_ARCH_CPU}";    }
yann@405
  1041
    [ "${CT_ARCH_TUNE}"     ] && { CT_ARCH_TUNE_CFLAG="-mtune=${CT_ARCH_TUNE}";  CT_ARCH_WITH_TUNE="--with-tune=${CT_ARCH_TUNE}"; }
yann@391
  1042
    [ "${CT_ARCH_FPU}"      ] && { CT_ARCH_FPU_CFLAG="-mfpu=${CT_ARCH_FPU}";     CT_ARCH_WITH_FPU="--with-fpu=${CT_ARCH_FPU}";    }
michael@2737
  1043
michael@2737
  1044
    case "${CT_ARCH_FLOAT}" in
michael@2738
  1045
        hard)
michael@2738
  1046
            CT_ARCH_FLOAT_CFLAG="-mhard-float"
michael@2738
  1047
            CT_ARCH_WITH_FLOAT="--with-float=hard"
michael@2738
  1048
            ;;
michael@2737
  1049
        soft)
michael@2737
  1050
            CT_ARCH_FLOAT_CFLAG="-msoft-float"
michael@2737
  1051
            CT_ARCH_WITH_FLOAT="--with-float=soft"
michael@2737
  1052
            ;;
michael@2739
  1053
        softfp)
michael@2739
  1054
            CT_ARCH_FLOAT_CFLAG="-mfloat-abi=softfp"
michael@2739
  1055
            CT_ARCH_WITH_FLOAT="--with-float=softfp"
michael@2739
  1056
            ;;
michael@2737
  1057
    esac
yann@391
  1058
yann@965
  1059
    # Build the default kernel tuple part
yann@965
  1060
    CT_TARGET_KERNEL="${CT_KERNEL}"
yann@964
  1061
yann@965
  1062
    # Overide the default values with the components specific settings
yann@964
  1063
    CT_DoArchTupleValues
yann@965
  1064
    CT_DoKernelTupleValues
yann@383
  1065
yann@391
  1066
    # Finish the target tuple construction
bartvdrmeulen@1896
  1067
    CT_TARGET="${CT_TARGET_ARCH}"
bartvdrmeulen@1896
  1068
    CT_TARGET="${CT_TARGET}${CT_TARGET_VENDOR:+-${CT_TARGET_VENDOR}}"
bartvdrmeulen@1896
  1069
    CT_TARGET="${CT_TARGET}${CT_TARGET_KERNEL:+-${CT_TARGET_KERNEL}}"
bartvdrmeulen@1896
  1070
    CT_TARGET="${CT_TARGET}${CT_TARGET_SYS:+-${CT_TARGET_SYS}}"
yann@1094
  1071
yann@1094
  1072
    # Sanity checks
yann@1094
  1073
    __sed_alias=""
yann@1094
  1074
    if [ -n "${CT_TARGET_ALIAS_SED_EXPR}" ]; then
yann@1094
  1075
        __sed_alias=$(echo "${CT_TARGET}" |sed -r -e "${CT_TARGET_ALIAS_SED_EXPR}")
yann@1094
  1076
    fi
yann@1094
  1077
    case ":${CT_TARGET_VENDOR}:${CT_TARGET_ALIAS}:${__sed_alias}:" in
yann@1094
  1078
      :*" "*:*:*:) CT_Abort "Don't use spaces in the vendor string, it breaks things.";;
yann@1094
  1079
      :*"-"*:*:*:) CT_Abort "Don't use dashes in the vendor string, it breaks things.";;
yann@1094
  1080
      :*:*" "*:*:) CT_Abort "Don't use spaces in the target alias, it breaks things.";;
yann@1094
  1081
      :*:*:*" "*:) CT_Abort "Don't use spaces in the target sed transform, it breaks things.";;
yann@1094
  1082
    esac
yann@1094
  1083
yann@1094
  1084
    # Canonicalise it
yann@1094
  1085
    CT_TARGET=$(CT_DoConfigSub "${CT_TARGET}")
yann@391
  1086
    # Prepare the target CFLAGS
yann@767
  1087
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ENDIAN_CFLAG}"
yann@527
  1088
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ARCH_CFLAG}"
yann@397
  1089
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ABI_CFLAG}"
yann@397
  1090
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_CPU_CFLAG}"
yann@397
  1091
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_TUNE_CFLAG}"
yann@397
  1092
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FPU_CFLAG}"
yann@397
  1093
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FLOAT_CFLAG}"
yann@527
  1094
yann@527
  1095
    # Now on for the target LDFLAGS
yann@767
  1096
    CT_ARCH_TARGET_LDFLAGS="${CT_ARCH_TARGET_LDFLAGS} ${CT_ARCH_ENDIAN_LDFLAG}"
yann@63
  1097
}
yann@121
  1098
yann@121
  1099
# This function does pause the build until the user strikes "Return"
yann@121
  1100
# Usage: CT_DoPause [optional_message]
yann@121
  1101
CT_DoPause() {
yann@121
  1102
    local foo
yann@121
  1103
    local message="${1:-Pausing for your pleasure}"
yann@121
  1104
    CT_DoLog INFO "${message}"
yann@523
  1105
    read -p "Press 'Enter' to continue, or Ctrl-C to stop..." foo >&6
yann@121
  1106
    return 0
yann@121
  1107
}
yann@121
  1108
yann@1907
  1109
# This function creates a tarball of the specified directory, but
yann@1907
  1110
# only if it exists
yann@1907
  1111
# Usage: CT_DoTarballIfExists <dir> <tarball_basename> [extra_tar_options [...]]
yann@1907
  1112
CT_DoTarballIfExists() {
yann@1907
  1113
    local dir="$1"
yann@1907
  1114
    local tarball="$2"
yann@1907
  1115
    shift 2
yann@1907
  1116
    local -a extra_tar_opts=( "$@" )
yann@1908
  1117
    local -a compress
yann@1907
  1118
yann@1907
  1119
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@1908
  1120
        y)  compress=( gzip -c -3 - ); tar_ext=.gz;;
yann@1908
  1121
        *)  compress=( cat - );        tar_ext=;;
yann@1907
  1122
    esac
yann@1907
  1123
yann@1907
  1124
    if [ -d "${dir}" ]; then
yann@1907
  1125
        CT_DoLog DEBUG "  Saving '${dir}'"
yann@1908
  1126
        { tar c -C "${dir}" -v -f - "${extra_tar_opts[@]}" .    \
yann@1908
  1127
          |"${compress[@]}" >"${tarball}.tar${tar_ext}"         ;
anthony@2155
  1128
        } 2>&1 |sed -r -e 's/^/    /;' |CT_DoLog STATE
yann@1907
  1129
    else
anthony@2155
  1130
        CT_DoLog STATE "  Not saving '${dir}': does not exist"
yann@1907
  1131
    fi
yann@1907
  1132
}
yann@1907
  1133
yann@1907
  1134
# This function extracts a tarball to the specified directory, but
yann@1907
  1135
# only if the tarball exists
anthony@2155
  1136
# Usage: CT_DoExtractTarballIfExists <tarball_basename> <dir> [extra_tar_options [...]]
yann@1907
  1137
CT_DoExtractTarballIfExists() {
yann@1907
  1138
    local tarball="$1"
yann@1907
  1139
    local dir="$2"
yann@1907
  1140
    shift 2
yann@1907
  1141
    local -a extra_tar_opts=( "$@" )
yann@1908
  1142
    local -a uncompress
yann@1907
  1143
yann@1907
  1144
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@1908
  1145
        y)  uncompress=( gzip -c -d ); tar_ext=.gz;;
yann@1908
  1146
        *)  uncompress=( cat );        tar_ext=;;
yann@1907
  1147
    esac
yann@1907
  1148
yann@1907
  1149
    if [ -f "${tarball}.tar${tar_ext}" ]; then
yann@1907
  1150
        CT_DoLog DEBUG "  Restoring '${dir}'"
yann@1907
  1151
        CT_DoForceRmdir "${dir}"
yann@1907
  1152
        CT_DoExecLog DEBUG mkdir -p "${dir}"
yann@1908
  1153
        { "${uncompress[@]}" "${tarball}.tar${tar_ext}"     \
yann@1908
  1154
          |tar x -C "${dir}" -v -f - "${extra_tar_opts[@]}" ;
anthony@2155
  1155
        } 2>&1 |sed -r -e 's/^/    /;' |CT_DoLog STATE
yann@1907
  1156
    else
anthony@2155
  1157
        CT_DoLog STATE "  Not restoring '${dir}': does not exist"
yann@1907
  1158
    fi
yann@1907
  1159
}
yann@1907
  1160
yann@121
  1161
# This function saves the state of the toolchain to be able to restart
yann@121
  1162
# at any one point
yann@121
  1163
# Usage: CT_DoSaveState <next_step_name>
yann@121
  1164
CT_DoSaveState() {
yann@121
  1165
	[ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
yann@121
  1166
    local state_name="$1"
yann@121
  1167
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@121
  1168
yann@1266
  1169
    # Log this to the log level required by the user
yann@1266
  1170
    CT_DoLog ${CT_LOG_LEVEL_MAX} "Saving state to restart at step '${state_name}'..."
yann@1134
  1171
yann@121
  1172
    rm -rf "${state_dir}"
yann@121
  1173
    mkdir -p "${state_dir}"
yann@121
  1174
anthony@2155
  1175
    CT_DoLog STATE "  Saving environment and aliases"
yann@738
  1176
    # We must omit shell functions, and some specific bash variables
yann@738
  1177
    # that break when restoring the environment, later. We could do
yann@1299
  1178
    # all the processing in the awk script, but a sed is easier...
yann@1299
  1179
    set |awk '
yann@1017
  1180
              BEGIN { _p = 1; }
yann@1017
  1181
              $0~/^[^ ]+ \(\)/ { _p = 0; }
yann@1017
  1182
              _p == 1
yann@1017
  1183
              $0 == "}" { _p = 1; }
yann@1017
  1184
              ' |sed -r -e '/^BASH_(ARGC|ARGV|LINENO|SOURCE|VERSINFO)=/d;
yann@738
  1185
                           /^(UID|EUID)=/d;
yann@738
  1186
                           /^(FUNCNAME|GROUPS|PPID|SHELLOPTS)=/d;' >"${state_dir}/env.sh"
yann@121
  1187
yann@2308
  1188
    CT_DoTarballIfExists "${CT_BUILDTOOLS_PREFIX_DIR}" "${state_dir}/buildtools_dir"
yann@1907
  1189
    CT_DoTarballIfExists "${CT_CONFIG_DIR}" "${state_dir}/config_dir"
yann@1907
  1190
    CT_DoTarballIfExists "${CT_PREFIX_DIR}" "${state_dir}/prefix_dir" --exclude '*.log'
yann@121
  1191
yann@2339
  1192
    CT_DoLog STATE "  Saving log file"
yann@2339
  1193
    exec >/dev/null
yann@2339
  1194
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@2339
  1195
        y)  gzip -3 -c "${tmp_log_file}"  >"${state_dir}/log.gz";;
yann@2339
  1196
        *)  cat "${tmp_log_file}" >"${state_dir}/log";;
yann@2339
  1197
    esac
yann@2339
  1198
    exec >>"${tmp_log_file}"
yann@121
  1199
}
yann@121
  1200
yann@136
  1201
# This function restores a previously saved state
yann@121
  1202
# Usage: CT_DoLoadState <state_name>
yann@121
  1203
CT_DoLoadState(){
yann@121
  1204
    local state_name="$1"
yann@121
  1205
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@135
  1206
    local old_RESTART="${CT_RESTART}"
yann@135
  1207
    local old_STOP="${CT_STOP}"
yann@121
  1208
yann@523
  1209
    CT_TestOrAbort "The previous build did not reach the point where it could be restarted at '${CT_RESTART}'" -d "${state_dir}"
yann@141
  1210
yann@121
  1211
    # We need to do something special with the log file!
yann@121
  1212
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
  1213
        exec >"${state_dir}/tail.log"
yann@121
  1214
    fi
yann@1266
  1215
yann@1266
  1216
    # Log this to the log level required by the user
yann@1266
  1217
    CT_DoLog ${CT_LOG_LEVEL_MAX} "Restoring state at step '${state_name}', as requested."
yann@121
  1218
yann@1907
  1219
    CT_DoExtractTarballIfExists "${state_dir}/prefix_dir" "${CT_PREFIX_DIR}"
yann@1907
  1220
    CT_DoExtractTarballIfExists "${state_dir}/config_dir" "${CT_CONFIG_DIR}"
yann@2308
  1221
    CT_DoExtractTarballIfExists "${state_dir}/buildtools_dir" "${CT_BUILDTOOLS_PREFIX_DIR}"
yann@1894
  1222
yann@121
  1223
    # Restore the environment, discarding any error message
yann@121
  1224
    # (for example, read-only bash internals)
anthony@2155
  1225
    CT_DoLog STATE "  Restoring environment"
yann@121
  1226
    . "${state_dir}/env.sh" >/dev/null 2>&1 || true
yann@121
  1227
yann@135
  1228
    # Restore the new RESTART and STOP steps
yann@135
  1229
    CT_RESTART="${old_RESTART}"
yann@135
  1230
    CT_STOP="${old_STOP}"
yann@135
  1231
    unset old_stop old_restart
yann@135
  1232
yann@2339
  1233
    CT_DoLog STATE "  Restoring log file"
yann@2339
  1234
    exec >/dev/null
yann@2339
  1235
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@2339
  1236
        y)  zcat "${state_dir}/log.gz" >"${tmp_log_file}";;
yann@2339
  1237
        *)  cat "${state_dir}/log" >"${tmp_log_file}";;
yann@2339
  1238
    esac
yann@2339
  1239
    cat "${state_dir}/tail.log" >>"${tmp_log_file}"
yann@2339
  1240
    exec >>"${tmp_log_file}"
yann@2339
  1241
    rm -f "${state_dir}/tail.log"
yann@121
  1242
}