scripts/functions
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Jan 20 20:37:43 2009 +0000 (2009-01-20)
changeset 1149 df32ef8825f9
parent 1148 081205fd3790
child 1177 748c418d3b6a
permissions -rw-r--r--
On 20090115.0012+0100, "Andy Johnson" <ajohnson@aecno.com> wrote:
... I added a step after
"debug" called "finish", and moved the code in crosstool.sh
after the loop that processes the steps from crosstool.sh
into a do_finish function in functions. Thus, it is now
possible to restart after the "debug" step to re-do the
final few things (clean and compress).

/trunk/scripts/crosstool-NG.sh | 38 0 38 0 --------------------------------------
/trunk/scripts/functions | 42 42 0 0 ++++++++++++++++++++++++++++++++++++++++++
/trunk/steps.mk | 3 2 1 0 ++-
3 files changed, 44 insertions(+), 39 deletions(-)
yann@1
     1
# This file contains some usefull common functions
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@1
     7
    ret=$?
yann@726
     8
    # Bail out early in subshell, the upper level shell will act accordingly.
yann@726
     9
    [ ${BASH_SUBSHELL} -eq 0 ] || exit $ret
yann@523
    10
    CT_DoLog ERROR "Build failed in step '${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}'"
yann@1
    11
    for((step=(CT_STEP_COUNT-1); step>1; step--)); do
yann@523
    12
        CT_DoLog ERROR "      called in step '${CT_STEP_MESSAGE[${step}]}'"
yann@1
    13
    done
yann@523
    14
    CT_DoLog ERROR "Error happened in '${BASH_SOURCE[1]}' in function '${FUNCNAME[1]}' (line unknown, sorry)"
yann@1
    15
    for((depth=2; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
yann@523
    16
        CT_DoLog ERROR "      called from '${BASH_SOURCE[${depth}]}' at line # ${BASH_LINENO[${depth}-1]} in function '${FUNCNAME[${depth}]}'"
yann@1
    17
    done
yann@523
    18
    [ "${CT_LOG_TO_FILE}" = "y" ] && CT_DoLog ERROR "Look at '${CT_LOG_FILE}' for more info on this error."
yann@96
    19
    CT_STEP_COUNT=1
yann@96
    20
    CT_DoEnd ERROR
yann@1
    21
    exit $ret
yann@1
    22
}
yann@136
    23
yann@136
    24
# Install the fault handler
yann@1
    25
trap CT_OnError ERR
yann@1
    26
yann@136
    27
# Inherit the fault handler in subshells and functions
yann@1
    28
set -E
yann@136
    29
yann@136
    30
# Make pipes fail on the _first_ failed command
yann@136
    31
# Not supported on bash < 3.x, but we need it, so drop the obsoleting bash-2.x
yann@1
    32
set -o pipefail
yann@1
    33
yann@136
    34
# Don't hash commands' locations, and search every time it is requested.
yann@136
    35
# This is slow, but needed because of the static/shared core gcc which shall
yann@136
    36
# always match to shared if it exists, and only fallback to static if the
yann@136
    37
# shared is not found
yann@136
    38
set +o hashall
yann@136
    39
yann@165
    40
# Log policy:
yann@165
    41
#  - first of all, save stdout so we can see the live logs: fd #6
yann@165
    42
exec 6>&1
yann@165
    43
#  - then point stdout to the log file (temporary for now)
yann@165
    44
tmp_log_file="${CT_TOP_DIR}/log.$$"
yann@165
    45
exec >>"${tmp_log_file}"
yann@165
    46
yann@1
    47
# The different log levels:
yann@1
    48
CT_LOG_LEVEL_ERROR=0
yann@1
    49
CT_LOG_LEVEL_WARN=1
yann@1
    50
CT_LOG_LEVEL_INFO=2
yann@1
    51
CT_LOG_LEVEL_EXTRA=3
yann@1
    52
CT_LOG_LEVEL_DEBUG=4
yann@78
    53
CT_LOG_LEVEL_ALL=5
yann@1
    54
yann@1083
    55
# Make it easy to use \n and !
yann@1081
    56
CR=$(printf "\n")
yann@1083
    57
BANG='!'
yann@1081
    58
yann@1
    59
# A function to log what is happening
yann@1
    60
# Different log level are available:
yann@1
    61
#   - ERROR:   A serious, fatal error occurred
yann@1
    62
#   - WARN:    A non fatal, non serious error occurred, take your responsbility with the generated build
yann@1
    63
#   - INFO:    Informational messages
yann@1
    64
#   - EXTRA:   Extra informational messages
yann@1
    65
#   - DEBUG:   Debug messages
yann@78
    66
#   - ALL:     Component's build messages
yann@1
    67
# Usage: CT_DoLog <level> [message]
yann@1
    68
# If message is empty, then stdin will be logged.
yann@1
    69
CT_DoLog() {
yann@47
    70
    local max_level LEVEL level cur_l cur_L
yann@47
    71
    local l
yann@1
    72
    eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
yann@1
    73
    # Set the maximum log level to DEBUG if we have none
yann@78
    74
    [ -z "${max_level}" ] && max_level=${CT_LOG_LEVEL_DEBUG}
yann@1
    75
yann@47
    76
    LEVEL="$1"; shift
yann@1
    77
    eval level="\${CT_LOG_LEVEL_${LEVEL}}"
yann@1
    78
yann@1
    79
    if [ $# -eq 0 ]; then
yann@1
    80
        cat -
yann@1
    81
    else
yann@646
    82
        echo "${@}"
yann@1081
    83
    fi |( IFS="${CR}" # We want the full lines, even leading spaces
yann@523
    84
          _prog_bar_cpt=0
yann@523
    85
          _prog_bar[0]='/'
yann@523
    86
          _prog_bar[1]='-'
yann@523
    87
          _prog_bar[2]='\'
yann@523
    88
          _prog_bar[3]='|'
yann@1
    89
          indent=$((2*CT_STEP_COUNT))
yann@1
    90
          while read line; do
yann@47
    91
              case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
yann@47
    92
                y,*"warning:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
yann@112
    93
                y,*"WARNING:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
yann@47
    94
                *"error:"*)             cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
yann@919
    95
                *"make["*"]: *** ["*)   cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
yann@47
    96
                *)                      cur_L="${LEVEL}"; cur_l="${level}";;
yann@47
    97
              esac
yann@523
    98
              # There will always be a log file (stdout, fd #1), be it /dev/null
yann@523
    99
              printf "[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}"
yann@47
   100
              if [ ${cur_l} -le ${max_level} ]; then
yann@523
   101
                  # Only print to console (fd #6) if log level is high enough.
yann@523
   102
                  printf "\r[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}" >&6
yann@82
   103
              fi
yann@82
   104
              if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
yann@523
   105
                  printf "\r[%02d:%02d] %s " $((SECONDS/60)) $((SECONDS%60)) "${_prog_bar[$((_prog_bar_cpt/10))]}" >&6
yann@523
   106
                  _prog_bar_cpt=$(((_prog_bar_cpt+1)%40))
yann@1
   107
              fi
yann@1
   108
          done
yann@1
   109
        )
yann@1
   110
yann@1
   111
    return 0
yann@1
   112
}
yann@1
   113
yann@535
   114
# Execute an action, and log its messages
yann@535
   115
# Usage: CT_DoExecLog <level> <[VAR=val...] command [parameters...]>
yann@535
   116
CT_DoExecLog() {
yann@535
   117
    local level="$1"
yann@535
   118
    shift
yann@657
   119
    CT_DoLog DEBUG "==> Executing: '${@}'"
yann@668
   120
    "${@}" 2>&1 |CT_DoLog "${level}"
yann@535
   121
}
yann@535
   122
yann@96
   123
# Tail message to be logged whatever happens
yann@96
   124
# Usage: CT_DoEnd <level>
yann@96
   125
CT_DoEnd()
yann@96
   126
{
yann@145
   127
    local level="$1"
yann@523
   128
    CT_STOP_DATE=$(CT_DoDate +%s%N)
yann@523
   129
    CT_STOP_DATE_HUMAN=$(CT_DoDate +%Y%m%d.%H%M%S)
yann@599
   130
    if [ "${level}" != "ERROR" ]; then
yann@582
   131
        CT_DoLog "${level:-INFO}" "Build completed at ${CT_STOP_DATE_HUMAN}"
yann@582
   132
    fi
yann@96
   133
    elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
yann@96
   134
    elapsed_min=$((elapsed/(60*1000*1000*1000)))
yann@523
   135
    elapsed_sec=$(printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000))))
yann@523
   136
    elapsed_csec=$(printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000))))
yann@145
   137
    CT_DoLog ${level:-INFO} "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
yann@96
   138
}
yann@96
   139
yann@96
   140
# Abort the execution with an error message
yann@1
   141
# Usage: CT_Abort <message>
yann@1
   142
CT_Abort() {
yann@128
   143
    CT_DoLog ERROR "$1"
yann@1
   144
    exit 1
yann@1
   145
}
yann@1
   146
yann@1
   147
# Test a condition, and print a message if satisfied
yann@1
   148
# Usage: CT_Test <message> <tests>
yann@1
   149
CT_Test() {
yann@1
   150
    local ret
yann@1
   151
    local m="$1"
yann@1
   152
    shift
yann@1
   153
    test "$@" && CT_DoLog WARN "$m"
yann@1
   154
    return 0
yann@1
   155
}
yann@1
   156
yann@1
   157
# Test a condition, and abort with an error message if satisfied
yann@1
   158
# Usage: CT_TestAndAbort <message> <tests>
yann@1
   159
CT_TestAndAbort() {
yann@1
   160
    local m="$1"
yann@1
   161
    shift
yann@1
   162
    test "$@" && CT_Abort "$m"
yann@1
   163
    return 0
yann@1
   164
}
yann@1
   165
yann@1
   166
# Test a condition, and abort with an error message if not satisfied
yann@1
   167
# Usage: CT_TestAndAbort <message> <tests>
yann@1
   168
CT_TestOrAbort() {
yann@1
   169
    local m="$1"
yann@1
   170
    shift
yann@1
   171
    test "$@" || CT_Abort "$m"
yann@1
   172
    return 0
yann@1
   173
}
yann@1
   174
yann@1
   175
# Test the presence of a tool, or abort if not found
yann@1
   176
# Usage: CT_HasOrAbort <tool>
yann@1
   177
CT_HasOrAbort() {
yann@773
   178
    CT_TestAndAbort "'${1}' not found and needed for successful toolchain build." -z "$(CT_Which "${1}")"
yann@1
   179
    return 0
yann@1
   180
}
yann@1
   181
yann@210
   182
# Search a program: wrap "which" for those system where
yann@210
   183
# "which" verbosely says there is no match (Mdk are such
yann@210
   184
# suckers...)
yann@210
   185
# Usage: CT_Which <filename>
yann@210
   186
CT_Which() {
yann@210
   187
  which "$1" 2>/dev/null || true
yann@210
   188
}
yann@210
   189
yann@1
   190
# Get current date with nanosecond precision
yann@1
   191
# On those system not supporting nanosecond precision, faked with rounding down
yann@1
   192
# to the highest entire second
yann@1
   193
# Usage: CT_DoDate <fmt>
yann@1
   194
CT_DoDate() {
yann@1
   195
    date "$1" |sed -r -e 's/%N$/000000000/;'
yann@1
   196
}
yann@1
   197
yann@1
   198
CT_STEP_COUNT=1
yann@1
   199
CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
yann@1
   200
# Memorise a step being done so that any error is caught
yann@1
   201
# Usage: CT_DoStep <loglevel> <message>
yann@1
   202
CT_DoStep() {
yann@523
   203
    local start=$(CT_DoDate +%s%N)
yann@1
   204
    CT_DoLog "$1" "================================================================="
yann@1
   205
    CT_DoLog "$1" "$2"
yann@1
   206
    CT_STEP_COUNT=$((CT_STEP_COUNT+1))
yann@1
   207
    CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
yann@1
   208
    CT_STEP_START[${CT_STEP_COUNT}]="${start}"
yann@1
   209
    CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
yann@1
   210
    return 0
yann@1
   211
}
yann@1
   212
yann@1
   213
# End the step just being done
yann@1
   214
# Usage: CT_EndStep
yann@1
   215
CT_EndStep() {
yann@523
   216
    local stop=$(CT_DoDate +%s%N)
yann@523
   217
    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
   218
    local elapsed=$(printf "%02d:%02d" $((SECONDS/60)) $((SECONDS%60)))
yann@1
   219
    local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
yann@1
   220
    local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
yann@1
   221
    CT_STEP_COUNT=$((CT_STEP_COUNT-1))
yann@582
   222
    CT_DoLog "${level}" "${message}: done in ${duration}s (at ${elapsed})"
yann@1
   223
    return 0
yann@1
   224
}
yann@1
   225
yann@1
   226
# Pushes into a directory, and pops back
yann@1
   227
CT_Pushd() {
yann@1
   228
    pushd "$1" >/dev/null 2>&1
yann@1
   229
}
yann@1
   230
CT_Popd() {
yann@1
   231
    popd >/dev/null 2>&1
yann@1
   232
}
yann@1
   233
yann@1
   234
# Creates a temporary directory
yann@1
   235
# $1: variable to assign to
yann@1
   236
# Usage: CT_MktempDir foo
yann@1
   237
CT_MktempDir() {
yann@1
   238
    # Some mktemp do not allow more than 6 Xs
yann@1113
   239
    eval "$1"=$(mktemp -q -d "${CT_BUILD_DIR}/tmp.XXXXXX")
yann@1
   240
    CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
yann@787
   241
    CT_DoLog DEBUG "Made temporary directory '${!1}'"
yann@787
   242
    return 0
yann@1
   243
}
yann@1
   244
yann@1148
   245
# Removes one or more directories, even if it is read-only, or its parent is
yann@1138
   246
# Usage: CT_DoForceRmdir dir [...]
yann@1138
   247
CT_DoForceRmdir() {
yann@1148
   248
    local dir
yann@1148
   249
    local mode
yann@1148
   250
    for dir in "${@}"; do
yann@1148
   251
        [ -d "${dir}" ] || continue
yann@1148
   252
        mode="$(stat -c '%a' "${dir}")"
yann@1148
   253
        CT_DoExecLog ALL chmod -R u+w "$(dirname "${dir}")"
yann@1148
   254
        CT_DoExecLog ALL rm -rf "${dir}"
yann@1148
   255
        CT_DoExecLog ALL chmod -R ${mode} "$(dirname "${dir}")"
yann@1148
   256
    done
yann@1138
   257
}
yann@1138
   258
yann@1
   259
# Echoes the specified string on stdout until the pipe breaks.
yann@1
   260
# Doesn't fail
yann@1
   261
# $1: string to echo
yann@1
   262
# Usage: CT_DoYes "" |make oldconfig
yann@1
   263
CT_DoYes() {
yann@1
   264
    yes "$1" || true
yann@1
   265
}
yann@63
   266
yann@85
   267
# Get the file name extension of a component
yann@719
   268
# Usage: CT_GetFileExtension <component_name-component_version> [extension]
yann@85
   269
# If found, echoes the extension to stdout
yann@85
   270
# If not found, echoes nothing on stdout.
yann@85
   271
CT_GetFileExtension() {
yann@85
   272
    local ext
yann@85
   273
    local file="$1"
yann@719
   274
    shift
yann@719
   275
    local first_ext="$1"
yann@85
   276
yann@160
   277
    # we need to also check for an empty extension for those very
yann@160
   278
    # peculiar components that don't have one (such as sstrip from
yann@160
   279
    # buildroot).
yann@719
   280
    for ext in ${first_ext} .tar.gz .tar.bz2 .tgz .tar ''; do
yann@1123
   281
        if [ -f "${CT_TARBALLS_DIR}/${file}${ext}" ]; then
yann@85
   282
            echo "${ext}"
yann@85
   283
            break
yann@85
   284
        fi
yann@85
   285
    done
yann@85
   286
yann@85
   287
    return 0
yann@85
   288
}
yann@85
   289
yann@754
   290
# Set environment for proxy access
yann@754
   291
# Usage: CT_DoSetProxy <proxy_type>
yann@775
   292
# where proxy_type is one of 'http', 'sockssys', 'socks4' or 'socks5',
yann@775
   293
# or empty (to not change proxy settings).
yann@754
   294
CT_DoSetProxy() {
yann@775
   295
    case "${1}" in
yann@754
   296
        http)
yann@754
   297
            http_proxy="http://"
yann@754
   298
            case  "${CT_PROXY_USER}:${CT_PROXY_PASS}" in
yann@754
   299
                :)      ;;
yann@754
   300
                :*)     http_proxy="${http_proxy}:${CT_PROXY_PASS}@";;
yann@754
   301
                *:)     http_proxy="${http_proxy}${CT_PROXY_USER}@";;
yann@754
   302
                *:*)    http_proxy="${http_proxy}${CT_PROXY_USER}:${CT_PROXY_PASS}@";;
yann@754
   303
            esac
yann@754
   304
            export http_proxy="${http_proxy}${CT_PROXY_HOST}:${CT_PROXY_PORT}/"
yann@754
   305
            export https_proxy="${http_proxy}"
yann@754
   306
            export ftp_proxy="${http_proxy}"
yann@754
   307
            CT_DoLog DEBUG "http_proxy='${http_proxy}'"
yann@754
   308
            ;;
yann@754
   309
        sockssys)
yann@754
   310
            CT_HasOrAbort tsocks
yann@754
   311
            . tsocks -on
yann@754
   312
            ;;
yann@754
   313
        socks*)
yann@754
   314
            # Remove any lingering config file from any previous run
yann@754
   315
            rm -f "${CT_BUILD_DIR}/tsocks.conf"
yann@754
   316
            # Find all interfaces and build locally accessible networks
yann@754
   317
            server_ip=$(ping -c 1 -W 2 "${CT_PROXY_HOST}" |head -n 1 |sed -r -e 's/^[^\(]+\(([^\)]+)\).*$/\1/;' || true)
yann@754
   318
            CT_TestOrAbort "SOCKS proxy '${CT_PROXY_HOST}' has no IP." -n "${server_ip}"
yann@1017
   319
            /sbin/ifconfig |gawk -v server_ip="${server_ip}" '
yann@754
   320
                BEGIN {
yann@754
   321
                    split( server_ip, tmp, "\\." );
yann@754
   322
                    server_ip_num = tmp[1] * 2^24 + tmp[2] * 2^16 + tmp[3] * 2^8 + tmp[4] * 2^0;
yann@754
   323
                    pairs = 0;
yann@754
   324
                }
yann@754
   325
yann@754
   326
                $0 ~ /^[[:space:]]*inet addr:/ {
yann@754
   327
                    split( $2, tmp, ":|\\." );
yann@754
   328
                    if( ( tmp[2] == 127 ) && ( tmp[3] == 0 ) && ( tmp[4] == 0 ) && ( tmp[5] == 1 ) ) {
yann@754
   329
                        /* Skip 127.0.0.1, it'\''s taken care of by tsocks itself */
yann@754
   330
                        next;
yann@754
   331
                    }
yann@754
   332
                    ip_num = tmp[2] * 2^24 + tmp[3] * 2^16 + tmp[4] * 2 ^8 + tmp[5] * 2^0;
yann@754
   333
                    i = 32;
yann@754
   334
                    do {
yann@754
   335
                        i--;
yann@754
   336
                        mask = 2^32 - 2^i;
yann@754
   337
                    } while( (i!=0) && ( and( server_ip_num, mask ) == and( ip_num, mask ) ) );
yann@754
   338
                    mask = and( 0xFFFFFFFF, lshift( mask, 1 ) );
yann@754
   339
                    if( (i!=0) && (mask!=0) ) {
yann@754
   340
                        masked_ip = and( ip_num, mask );
yann@754
   341
                        for( i=0; i<pairs; i++ ) {
yann@754
   342
                            if( ( masked_ip == ips[i] ) && ( mask == masks[i] ) ) {
yann@754
   343
                                next;
yann@754
   344
                            }
yann@754
   345
                        }
yann@754
   346
                        ips[pairs] = masked_ip;
yann@754
   347
                        masks[pairs] = mask;
yann@754
   348
                        pairs++;
yann@754
   349
                        printf( "local = %d.%d.%d.%d/%d.%d.%d.%d\n",
yann@754
   350
                                and( 0xFF, masked_ip / 2^24 ),
yann@754
   351
                                and( 0xFF, masked_ip / 2^16 ),
yann@754
   352
                                and( 0xFF, masked_ip / 2^8 ),
yann@754
   353
                                and( 0xFF, masked_ip / 2^0 ),
yann@754
   354
                                and( 0xFF, mask / 2^24 ),
yann@754
   355
                                and( 0xFF, mask / 2^16 ),
yann@754
   356
                                and( 0xFF, mask / 2^8 ),
yann@754
   357
                                and( 0xFF, mask / 2^0 ) );
yann@754
   358
                    }
yann@754
   359
                }
yann@754
   360
            ' >"${CT_BUILD_DIR}/tsocks.conf"
yann@754
   361
            ( echo "server = ${server_ip}";
yann@754
   362
              echo "server_port = ${CT_PROXY_PORT}";
yann@754
   363
              [ -n "${CT_PROXY_USER}"   ] && echo "default_user=${CT_PROXY_USER}";
yann@754
   364
              [ -n "${CT_PROXY_PASS}" ] && echo "default_pass=${CT_PROXY_PASS}";
yann@754
   365
            ) >>"${CT_BUILD_DIR}/tsocks.conf"
yann@754
   366
            case "${CT_PROXY_TYPE/socks}" in
yann@754
   367
                4|5) proxy_type="${CT_PROXY_TYPE/socks}";;
yann@754
   368
                auto)
yann@754
   369
                    reply=$(inspectsocks "${server_ip}" "${CT_PROXY_PORT}" 2>&1 || true)
yann@754
   370
                    case "${reply}" in
yann@754
   371
                        *"server is a version 4 socks server") proxy_type=4;;
yann@754
   372
                        *"server is a version 5 socks server") proxy_type=5;;
yann@754
   373
                        *) CT_Abort "Unable to determine SOCKS proxy type for '${CT_PROXY_HOST}:${CT_PROXY_PORT}'"
yann@754
   374
                    esac
yann@754
   375
                    ;;
yann@754
   376
            esac
yann@754
   377
            echo "server_type = ${proxy_type}" >> "${CT_BUILD_DIR}/tsocks.conf"
yann@754
   378
            CT_HasOrAbort tsocks
yann@754
   379
            # If tsocks was found, then validateconf is present (distributed with tsocks).
yann@754
   380
            CT_DoExecLog DEBUG validateconf -f "${CT_BUILD_DIR}/tsocks.conf"
yann@754
   381
            export TSOCKS_CONF_FILE="${CT_BUILD_DIR}/tsocks.conf"
yann@754
   382
            . tsocks -on
yann@754
   383
            ;;
yann@754
   384
    esac
yann@754
   385
}
yann@754
   386
yann@63
   387
# Download an URL using wget
yann@63
   388
# Usage: CT_DoGetFileWget <URL>
yann@63
   389
CT_DoGetFileWget() {
yann@63
   390
    # Need to return true because it is legitimate to not find the tarball at
yann@63
   391
    # some of the provided URLs (think about snapshots, different layouts for
yann@63
   392
    # different gcc versions, etc...)
yann@63
   393
    # Some (very old!) FTP server might not support the passive mode, thus
yann@63
   394
    # retry without
yann@63
   395
    # With automated download as we are doing, it can be very dangerous to use
yann@63
   396
    # -c to continue the downloads. It's far better to simply overwrite the
yann@63
   397
    # destination file
yann@492
   398
    # Some company networks have firewalls to connect to the internet, but it's
yann@1113
   399
    # not easy to detect them, and wget does not timeout by default while
yann@492
   400
    # connecting, so force a global ${CT_CONNECT_TIMEOUT}-second timeout.
yann@492
   401
    wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 --passive-ftp "$1"    \
yann@492
   402
    || wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 "$1"               \
yann@439
   403
    || true
yann@63
   404
}
yann@63
   405
yann@63
   406
# Download an URL using curl
yann@63
   407
# Usage: CT_DoGetFileCurl <URL>
yann@63
   408
CT_DoGetFileCurl() {
yann@492
   409
    # Note: comments about wget method (above) are also valid here
yann@439
   410
    # Plus: no good progress indicator is available with curl,
yann@439
   411
    #       so output is consigned to oblivion
yann@492
   412
    curl --ftp-pasv -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null    \
yann@492
   413
    || curl -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null            \
yann@439
   414
    || true
yann@63
   415
}
yann@63
   416
yann@523
   417
_wget=$(CT_Which wget)
yann@523
   418
_curl=$(CT_Which curl)
yann@63
   419
# Wrapper function to call one of curl or wget
yann@63
   420
# Usage: CT_DoGetFile <URL>
yann@63
   421
CT_DoGetFile() {
yann@63
   422
    case "${_wget},${_curl}" in
yann@1120
   423
        ,)  CT_Abort "Could find neither wget nor curl";;
yann@776
   424
        ,*) CT_DoExecLog ALL CT_DoGetFileCurl "$1" 2>&1;;
yann@776
   425
        *)  CT_DoExecLog ALL CT_DoGetFileWget "$1" 2>&1;;
yann@63
   426
    esac
yann@63
   427
}
yann@63
   428
yann@1113
   429
# This function tries to retrieve a tarball form a local directory
yann@1113
   430
# Usage: CT_GetLocal <basename> [.extension]
yann@1113
   431
CT_GetLocal() {
yann@1113
   432
    local basename="$1"
yann@1113
   433
    local first_ext="$2"
yann@1113
   434
    local ext
yann@1113
   435
yann@1113
   436
    # Do we already have it in *our* tarballs dir?
yann@1113
   437
    ext=$(CT_GetFileExtension "${basename}" ${first_ext})
yann@1113
   438
    if [ -n "${ext}" ]; then
yann@1113
   439
        CT_DoLog DEBUG "Already have '${basename}'"
yann@1113
   440
        return 0
yann@1113
   441
    fi
yann@1113
   442
yann@1113
   443
    if [ -n "${CT_LOCAL_TARBALLS_DIR}" ]; then
yann@1113
   444
        CT_DoLog DEBUG "Trying to retrieve an already downloaded copy of '${basename}'"
yann@1113
   445
        # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
yann@1113
   446
        # or, as a failover, a file without extension.
yann@1113
   447
        # Try local copy first, if it exists
yann@1113
   448
        for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
yann@1113
   449
            CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}'"
yann@1113
   450
            if [ -r "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" -a \
yann@1113
   451
                 "${CT_FORCE_DOWNLOAD}" != "y" ]; then
yann@1113
   452
                CT_DoLog DEBUG "Got '${basename}' from local storage"
yann@1113
   453
                CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}${ext}" "${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1113
   454
                return 0
yann@1113
   455
            fi
yann@1113
   456
        done
yann@1113
   457
    fi
yann@1113
   458
    return 1
yann@1113
   459
}
yann@1113
   460
yann@1113
   461
# This function saves the specified to local storage if possible,
yann@1113
   462
# and if so, symlinks it for later usage
yann@1113
   463
# Usage: CT_SaveLocal </full/path/file.name>
yann@1113
   464
CT_SaveLocal() {
yann@1113
   465
    local file="$1"
yann@1113
   466
    local basename="${file##*/}"
yann@1113
   467
yann@1113
   468
    if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
yann@1134
   469
        CT_DoLog EXTRA "Saving '${basename}' to local storage"
yann@1113
   470
        # The file may already exist if downloads are forced: remove it first
yann@1113
   471
        CT_DoExecLog ALL rm -f "${CT_LOCAL_TARBALLS_DIR}/${basename}"
yann@1113
   472
        CT_DoExecLog ALL mv -f "${file}" "${CT_LOCAL_TARBALLS_DIR}"
yann@1113
   473
        CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${basename}" "${file}"
yann@1113
   474
    fi
yann@1113
   475
}
yann@1113
   476
yann@63
   477
# Download the file from one of the URLs passed as argument
yann@1113
   478
# Usage: CT_GetFile <basename> [.extension] <url> [url ...]
yann@63
   479
CT_GetFile() {
yann@63
   480
    local ext
yann@63
   481
    local url
yann@63
   482
    local file="$1"
yann@1113
   483
    local first_ext
yann@63
   484
    shift
yann@712
   485
    # If next argument starts with a dot, then this is not an URL,
yann@712
   486
    # and we can consider that it is a preferred extension.
yann@242
   487
    case "$1" in
yann@712
   488
        .*) first_ext="$1"
yann@242
   489
            shift
yann@242
   490
            ;;
yann@242
   491
    esac
yann@63
   492
yann@1113
   493
    # Does it exist localy?
yann@1113
   494
    CT_GetLocal "${file}" ${first_ext} && return 0 || true
yann@1113
   495
    # No, it does not...
yann@63
   496
yann@1131
   497
    # Are downloads allowed ?
yann@1131
   498
    CT_TestAndAbort "File '${file}' not present locally, and downloads are not allowed" "${CT_FORBID_DOWNLOAD}" = "y"
yann@1131
   499
yann@754
   500
    # Try to retrieve the file
yann@788
   501
    CT_DoLog EXTRA "Retrieving '${file}'"
yann@63
   502
    CT_Pushd "${CT_TARBALLS_DIR}"
yann@754
   503
yann@1022
   504
    # Add URLs on the LAN mirror
yann@1022
   505
    LAN_URLS=
yann@1022
   506
    if [ "${CT_USE_MIRROR}" = "y" ]; then
yann@1022
   507
        CT_TestOrAbort "Please set the LAN mirror hostname" -n "${CT_MIRROR_HOSTNAME}"
yann@1022
   508
        CT_TestOrAbort "Please tell me where to find tarballs on the LAN mirror '${CT_MIRROR_HOSTNAME}'" -n "${CT_MIRROR_BASE}"
yann@1022
   509
        LAN_URLS="${LAN_URLS} ${CT_MIRROR_SCHEME}://${CT_MIRROR_HOSTNAME}/${CT_MIRROR_BASE}/${file%-*}"
yann@1022
   510
        LAN_URLS="${LAN_URLS} ${CT_MIRROR_SCHEME}://${CT_MIRROR_HOSTNAME}/${CT_MIRROR_BASE}"
yann@695
   511
yann@1134
   512
        if [ "${CT_PREFER_MIRROR}" = "y" ]; then
yann@1134
   513
            CT_DoLog DEBUG "Pre-pending LAN mirror URLs"
yann@1134
   514
            URLS="${LAN_URLS} ${@}"
yann@1134
   515
        else
yann@1134
   516
            CT_DoLog DEBUG "Appending LAN mirror URLs"
yann@1134
   517
            URLS="${@} ${LAN_URLS}"
yann@1134
   518
        fi
yann@1022
   519
    fi
yann@1022
   520
yann@1022
   521
    # Scan all URLs in turn, and try to grab a tarball from there
yann@754
   522
    CT_DoSetProxy ${CT_PROXY_TYPE}
yann@242
   523
    for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
yann@102
   524
        # Try all urls in turn
yann@1022
   525
        for url in ${URLS}; do
yann@523
   526
            CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
yann@265
   527
            CT_DoGetFile "${url}/${file}${ext}"
yann@265
   528
            if [ -f "${file}${ext}" ]; then
yann@799
   529
                CT_DoLog DEBUG "Got '${file}' from the Internet"
yann@1113
   530
                CT_SaveLocal "${CT_TARBALLS_DIR}/${file}${ext}"
yann@265
   531
                return 0
yann@265
   532
            fi
yann@102
   533
        done
yann@102
   534
    done
yann@63
   535
    CT_Popd
yann@63
   536
yann@754
   537
    CT_Abort "Could not retrieve '${file}'."
yann@63
   538
}
yann@63
   539
yann@1113
   540
# Checkout from CVS, and build the associated tarball
yann@1113
   541
# The tarball will be called ${basename}.tar.bz2
yann@1113
   542
# Prerequisite: either the server does not require password,
yann@1113
   543
# or the user must already be logged in.
yann@1113
   544
# 'tag' is the tag to retrieve. Must be specified, but can be empty.
yann@1113
   545
# If dirname is specified, then module will be renamed to dirname
yann@1113
   546
# prior to building the tarball.
yann@1113
   547
# Usage: CT_GetCVS <basename> <url> <module> <tag> [dirname]
yann@1113
   548
CT_GetCVS() {
yann@1113
   549
    local basename="$1"
yann@1113
   550
    local uri="$2"
yann@1113
   551
    local module="$3"
yann@1113
   552
    local tag="${4:+-r ${4}}"
yann@1113
   553
    local dirname="$5"
yann@1113
   554
    local tmp_dir
yann@1113
   555
yann@1113
   556
    # Does it exist localy?
yann@1113
   557
    CT_GetLocal "${basename}" && return 0 || true
yann@1113
   558
    # No, it does not...
yann@1113
   559
yann@1131
   560
    # Are downloads allowed ?
yann@1131
   561
    CT_TestAndAbort "File '${file}' not present locally, and downloads are not allowed" "${CT_FORBID_DOWNLOAD}" = "y"
yann@1131
   562
yann@1113
   563
    CT_DoLog EXTRA "Retrieving '${basename}'"
yann@1113
   564
yann@1113
   565
    CT_MktempDir tmp_dir
yann@1113
   566
    CT_Pushd "${tmp_dir}"
yann@1113
   567
yann@1113
   568
    CT_DoSetProxy ${CT_PROXY_TYPE}
yann@1113
   569
    CT_DoExecLog ALL cvs -z 9 -d "${uri}" co -P ${tag} "${module}"
yann@1113
   570
    [ -n "${dirname}" ] && CT_DoExecLog ALL mv "${module}" "${dirname}"
yann@1113
   571
    CT_DoExecLog ALL tar cjf "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${dirname:-${module}}"
yann@1113
   572
    CT_SaveLocal "${CT_TARBALLS_DIR}/${basename}.tar.bz2"
yann@1113
   573
yann@1113
   574
    CT_Popd
yann@1113
   575
    CT_DoExecLog ALL rm -rf "${tmp_dir}"
yann@1113
   576
}
yann@1113
   577
yann@1126
   578
# Extract a tarball
yann@63
   579
# Some tarballs need to be extracted in specific places. Eg.: glibc addons
yann@63
   580
# must be extracted in the glibc directory; uCLibc locales must be extracted
yann@1123
   581
# in the extra/locale sub-directory of uClibc. This is taken into account
yann@1123
   582
# by the caller, that did a 'cd' into the correct path before calling us
yann@1123
   583
# and sets nochdir to 'nochdir'.
yann@1126
   584
# Usage: CT_Extract <basename> [nochdir]
yann@1126
   585
CT_Extract() {
yann@1126
   586
    local basename="$1"
yann@1123
   587
    local nochdir="$2"
yann@1126
   588
    local ext=$(CT_GetFileExtension "${basename}")
yann@1126
   589
    CT_TestAndAbort "'${basename}' not found in '${CT_TARBALLS_DIR}'" -z "${ext}"
yann@1126
   590
    local full_file="${CT_TARBALLS_DIR}/${basename}${ext}"
yann@1126
   591
yann@1126
   592
    # Check if already extracted
yann@1126
   593
    if [ -e "${CT_SRC_DIR}/.${basename}.extracted" ]; then
yann@1126
   594
        CT_DoLog DEBUG "Already extracted '${basename}'"
yann@1126
   595
        return 0
yann@1126
   596
    fi
yann@63
   597
yann@1123
   598
    [ "${nochdir}" = "nochdir" ] || CT_Pushd "${CT_SRC_DIR}"
yann@63
   599
yann@1126
   600
    CT_DoLog EXTRA "Extracting '${basename}'"
yann@63
   601
    case "${ext}" in
yann@776
   602
        .tar.bz2)     CT_DoExecLog ALL tar xvjf "${full_file}";;
yann@776
   603
        .tar.gz|.tgz) CT_DoExecLog ALL tar xvzf "${full_file}";;
yann@776
   604
        .tar)         CT_DoExecLog ALL tar xvf  "${full_file}";;
yann@1126
   605
        *)            CT_Abort "Don't know how to handle '${basename}${ext}': unknown extension" ;;
yann@63
   606
    esac
yann@63
   607
yann@1126
   608
    touch "${CT_SRC_DIR}/.${basename}.extracted"
yann@1126
   609
yann@1126
   610
    [ "${nochdir}" = "nochdir" ] || CT_Popd
yann@1126
   611
}
yann@1126
   612
yann@1126
   613
# Patches the specified component
yann@1126
   614
# Usage: CT_Patch <basename> [nochdir]
yann@1126
   615
CT_Patch() {
yann@1126
   616
    local basename="$1"
yann@1126
   617
    local nochdir="$2"
yann@1126
   618
    local base_file="${basename%%-*}"
yann@1126
   619
    local ver_file="${basename#*-}"
yann@1126
   620
    local official_patch_dir
yann@1126
   621
    local custom_patch_dir
yann@1126
   622
yann@1126
   623
    # Check if already patched
yann@1126
   624
    if [ -e "${CT_SRC_DIR}/.${basename}.patched" ]; then
yann@1126
   625
        CT_DoLog DEBUG "Already patched '${basename}'"
yann@1126
   626
        return 0
yann@63
   627
    fi
yann@63
   628
yann@1126
   629
    [ "${nochdir}" = "nochdir" ] || CT_Pushd "${CT_SRC_DIR}/${basename}"
yann@1123
   630
yann@1126
   631
    CT_DoLog EXTRA "Patching '${basename}'"
yann@63
   632
yann@182
   633
    official_patch_dir=
yann@182
   634
    custom_patch_dir=
yann@956
   635
    [ "${CT_CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_LIB_DIR}/patches/${base_file}/${ver_file}"
yann@63
   636
    [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
yann@63
   637
    for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
yann@63
   638
        if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
yann@63
   639
            for p in "${patch_dir}"/*.patch; do
yann@63
   640
                if [ -f "${p}" ]; then
yann@523
   641
                    CT_DoLog DEBUG "Applying patch '${p}'"
yann@776
   642
                    CT_DoExecLog ALL patch -g0 -F1 -p1 -f <"${p}"
yann@523
   643
                    CT_TestAndAbort "Failed while applying patch file '${p}'" ${PIPESTATUS[0]} -ne 0
yann@63
   644
                fi
yann@63
   645
            done
yann@63
   646
        fi
yann@63
   647
    done
yann@63
   648
yann@508
   649
    if [ "${CT_OVERIDE_CONFIG_GUESS_SUB}" = "y" ]; then
yann@508
   650
        CT_DoLog ALL "Overiding config.guess and config.sub"
yann@508
   651
        for cfg in config_guess config_sub; do
yann@1101
   652
            eval ${cfg}="${CT_LIB_DIR}/scripts/${cfg/_/.}"
yann@1101
   653
            [ -e "${CT_TOP_DIR}/scripts/${cfg/_/.}" ] && eval ${cfg}="${CT_TOP_DIR}/scripts/${cfg/_/.}"
yann@776
   654
            # Can't use CT_DoExecLog because of the '{} \;' to be passed un-mangled to find
yann@508
   655
            find . -type f -name "${cfg/_/.}" -exec cp -v "${!cfg}" {} \; |CT_DoLog ALL
yann@508
   656
        done
yann@508
   657
    fi
yann@508
   658
yann@1126
   659
    touch "${CT_SRC_DIR}/.${basename}.patched"
yann@1126
   660
yann@1123
   661
    [ "${nochdir}" = "nochdir" ] || CT_Popd
yann@63
   662
}
yann@63
   663
yann@182
   664
# Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
yann@182
   665
# Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
yann@182
   666
CT_DoConfigGuess() {
yann@1101
   667
    if [ -x "${CT_TOP_DIR}/scripts/config.guess" ]; then
yann@1101
   668
        "${CT_TOP_DIR}/scripts/config.guess"
yann@182
   669
    else
yann@1101
   670
        "${CT_LIB_DIR}/scripts/config.guess"
yann@182
   671
    fi
yann@182
   672
}
yann@182
   673
yann@182
   674
CT_DoConfigSub() {
yann@1101
   675
    if [ -x "${CT_TOP_DIR}/scripts/config.sub" ]; then
yann@1101
   676
        "${CT_TOP_DIR}/scripts/config.sub" "$@"
yann@182
   677
    else
yann@1101
   678
        "${CT_LIB_DIR}/scripts/config.sub" "$@"
yann@182
   679
    fi
yann@182
   680
}
yann@182
   681
yann@335
   682
# Compute the target tuple from what is provided by the user
yann@335
   683
# Usage: CT_DoBuildTargetTuple
yann@63
   684
# In fact this function takes the environment variables to build the target
yann@335
   685
# tuple. It is needed both by the normal build sequence, as well as the
yann@63
   686
# sample saving sequence.
yann@335
   687
CT_DoBuildTargetTuple() {
yann@383
   688
    # Set the endianness suffix, and the default endianness gcc option
yann@63
   689
    case "${CT_ARCH_BE},${CT_ARCH_LE}" in
yann@383
   690
        y,) target_endian_eb=eb
yann@383
   691
            target_endian_el=
yann@391
   692
            CT_ARCH_ENDIAN_CFLAG="-mbig-endian"
yann@527
   693
            CT_ARCH_ENDIAN_LDFLAG="-EB"
yann@383
   694
            ;;
yann@383
   695
        ,y) target_endian_eb=
yann@383
   696
            target_endian_el=el
yann@391
   697
            CT_ARCH_ENDIAN_CFLAG="-mlittle-endian"
yann@527
   698
            CT_ARCH_ENDIAN_LDFLAG="-EL"
yann@383
   699
            ;;
yann@63
   700
    esac
yann@383
   701
yann@965
   702
    # Build the default architecture tuple part
yann@965
   703
    CT_TARGET_ARCH="${CT_ARCH}"
yann@965
   704
yann@383
   705
    # Set defaults for the system part of the tuple. Can be overriden
yann@383
   706
    # by architecture-specific values.
yann@383
   707
    case "${CT_LIBC}" in
yann@850
   708
        none)   CT_TARGET_SYS=elf;;
yann@787
   709
        *glibc) CT_TARGET_SYS=gnu;;
yann@383
   710
        uClibc) CT_TARGET_SYS=uclibc;;
yann@63
   711
    esac
yann@383
   712
yann@387
   713
    # Transform the ARCH into a kernel-understandable ARCH
yann@387
   714
    CT_KERNEL_ARCH="${CT_ARCH}"
yann@387
   715
yann@391
   716
    # Set the default values for ARCH, ABI, CPU, TUNE, FPU and FLOAT
yann@497
   717
    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
   718
    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
   719
    [ "${CT_ARCH_ARCH}"     ] && { CT_ARCH_ARCH_CFLAG="-march=${CT_ARCH_ARCH}";  CT_ARCH_WITH_ARCH="--with-arch=${CT_ARCH_ARCH}"; }
yann@391
   720
    [ "${CT_ARCH_ABI}"      ] && { CT_ARCH_ABI_CFLAG="-mabi=${CT_ARCH_ABI}";     CT_ARCH_WITH_ABI="--with-abi=${CT_ARCH_ABI}";    }
yann@391
   721
    [ "${CT_ARCH_CPU}"      ] && { CT_ARCH_CPU_CFLAG="-mcpu=${CT_ARCH_CPU}";     CT_ARCH_WITH_CPU="--with-cpu=${CT_ARCH_CPU}";    }
yann@405
   722
    [ "${CT_ARCH_TUNE}"     ] && { CT_ARCH_TUNE_CFLAG="-mtune=${CT_ARCH_TUNE}";  CT_ARCH_WITH_TUNE="--with-tune=${CT_ARCH_TUNE}"; }
yann@391
   723
    [ "${CT_ARCH_FPU}"      ] && { CT_ARCH_FPU_CFLAG="-mfpu=${CT_ARCH_FPU}";     CT_ARCH_WITH_FPU="--with-fpu=${CT_ARCH_FPU}";    }
yann@412
   724
    [ "${CT_ARCH_FLOAT_SW}" ] && { CT_ARCH_FLOAT_CFLAG="-msoft-float";           CT_ARCH_WITH_FLOAT="--with-float=soft";          }
yann@391
   725
yann@965
   726
    # Build the default kernel tuple part
yann@965
   727
    CT_TARGET_KERNEL="${CT_KERNEL}"
yann@964
   728
yann@965
   729
    # Overide the default values with the components specific settings
yann@964
   730
    CT_DoArchTupleValues
yann@965
   731
    CT_DoKernelTupleValues
yann@383
   732
yann@391
   733
    # Finish the target tuple construction
yann@1094
   734
    CT_TARGET="${CT_TARGET_ARCH}-${CT_TARGET_VENDOR:-unknown}-${CT_TARGET_KERNEL}${CT_TARGET_KERNEL:+-}${CT_TARGET_SYS}"
yann@1094
   735
yann@1094
   736
    # Sanity checks
yann@1094
   737
    __sed_alias=""
yann@1094
   738
    if [ -n "${CT_TARGET_ALIAS_SED_EXPR}" ]; then
yann@1094
   739
        __sed_alias=$(echo "${CT_TARGET}" |sed -r -e "${CT_TARGET_ALIAS_SED_EXPR}")
yann@1094
   740
    fi
yann@1094
   741
    case ":${CT_TARGET_VENDOR}:${CT_TARGET_ALIAS}:${__sed_alias}:" in
yann@1094
   742
      :*" "*:*:*:) CT_Abort "Don't use spaces in the vendor string, it breaks things.";;
yann@1094
   743
      :*"-"*:*:*:) CT_Abort "Don't use dashes in the vendor string, it breaks things.";;
yann@1094
   744
      :*:*" "*:*:) CT_Abort "Don't use spaces in the target alias, it breaks things.";;
yann@1094
   745
      :*:*:*" "*:) CT_Abort "Don't use spaces in the target sed transform, it breaks things.";;
yann@1094
   746
    esac
yann@1094
   747
yann@1094
   748
    # Canonicalise it
yann@1094
   749
    CT_TARGET=$(CT_DoConfigSub "${CT_TARGET}")
yann@391
   750
yann@391
   751
    # Prepare the target CFLAGS
yann@767
   752
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ENDIAN_CFLAG}"
yann@527
   753
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ARCH_CFLAG}"
yann@397
   754
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ABI_CFLAG}"
yann@397
   755
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_CPU_CFLAG}"
yann@397
   756
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_TUNE_CFLAG}"
yann@397
   757
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FPU_CFLAG}"
yann@397
   758
    CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FLOAT_CFLAG}"
yann@527
   759
yann@527
   760
    # Now on for the target LDFLAGS
yann@767
   761
    CT_ARCH_TARGET_LDFLAGS="${CT_ARCH_TARGET_LDFLAGS} ${CT_ARCH_ENDIAN_LDFLAG}"
yann@63
   762
}
yann@121
   763
yann@121
   764
# This function does pause the build until the user strikes "Return"
yann@121
   765
# Usage: CT_DoPause [optional_message]
yann@121
   766
CT_DoPause() {
yann@121
   767
    local foo
yann@121
   768
    local message="${1:-Pausing for your pleasure}"
yann@121
   769
    CT_DoLog INFO "${message}"
yann@523
   770
    read -p "Press 'Enter' to continue, or Ctrl-C to stop..." foo >&6
yann@121
   771
    return 0
yann@121
   772
}
yann@121
   773
yann@121
   774
# This function saves the state of the toolchain to be able to restart
yann@121
   775
# at any one point
yann@121
   776
# Usage: CT_DoSaveState <next_step_name>
yann@121
   777
CT_DoSaveState() {
yann@121
   778
	[ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
yann@121
   779
    local state_name="$1"
yann@121
   780
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@121
   781
yann@523
   782
    CT_DoLog DEBUG "Saving state to restart at step '${state_name}'..."
yann@1134
   783
yann@121
   784
    rm -rf "${state_dir}"
yann@121
   785
    mkdir -p "${state_dir}"
yann@121
   786
yann@121
   787
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@327
   788
        y)  tar_opt=z; tar_ext=.gz;;
yann@327
   789
        *)  tar_opt=;  tar_ext=;;
yann@121
   790
    esac
yann@121
   791
yann@121
   792
    CT_DoLog DEBUG "  Saving environment and aliases"
yann@738
   793
    # We must omit shell functions, and some specific bash variables
yann@738
   794
    # that break when restoring the environment, later. We could do
yann@1017
   795
    # all the processing in the gawk script, but a sed is easier...
yann@1017
   796
    set |gawk '
yann@1017
   797
              BEGIN { _p = 1; }
yann@1017
   798
              $0~/^[^ ]+ \(\)/ { _p = 0; }
yann@1017
   799
              _p == 1
yann@1017
   800
              $0 == "}" { _p = 1; }
yann@1017
   801
              ' |sed -r -e '/^BASH_(ARGC|ARGV|LINENO|SOURCE|VERSINFO)=/d;
yann@738
   802
                           /^(UID|EUID)=/d;
yann@738
   803
                           /^(FUNCNAME|GROUPS|PPID|SHELLOPTS)=/d;' >"${state_dir}/env.sh"
yann@121
   804
yann@523
   805
    CT_DoLog DEBUG "  Saving CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
yann@136
   806
    CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@776
   807
    CT_DoExecLog DEBUG tar cv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}" .
yann@136
   808
    CT_Popd
yann@136
   809
yann@523
   810
    CT_DoLog DEBUG "  Saving CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
yann@136
   811
    CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
yann@776
   812
    CT_DoExecLog DEBUG tar cv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}" .
yann@121
   813
    CT_Popd
yann@121
   814
yann@523
   815
    CT_DoLog DEBUG "  Saving CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
yann@121
   816
    CT_Pushd "${CT_PREFIX_DIR}"
yann@776
   817
    CT_DoExecLog DEBUG tar cv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}" --exclude '*.log' .
yann@121
   818
    CT_Popd
yann@121
   819
yann@121
   820
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
   821
        CT_DoLog DEBUG "  Saving log file"
yann@121
   822
        exec >/dev/null
yann@121
   823
        case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@121
   824
            y)  gzip -3 -c "${CT_LOG_FILE}"  >"${state_dir}/log.gz";;
yann@121
   825
            *)  cat "${CT_LOG_FILE}" >"${state_dir}/log";;
yann@121
   826
        esac
yann@121
   827
        exec >>"${CT_LOG_FILE}"
yann@121
   828
    fi
yann@121
   829
}
yann@121
   830
yann@136
   831
# This function restores a previously saved state
yann@121
   832
# Usage: CT_DoLoadState <state_name>
yann@121
   833
CT_DoLoadState(){
yann@121
   834
    local state_name="$1"
yann@121
   835
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@135
   836
    local old_RESTART="${CT_RESTART}"
yann@135
   837
    local old_STOP="${CT_STOP}"
yann@121
   838
yann@523
   839
    CT_TestOrAbort "The previous build did not reach the point where it could be restarted at '${CT_RESTART}'" -d "${state_dir}"
yann@141
   840
yann@121
   841
    # We need to do something special with the log file!
yann@121
   842
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
   843
        exec >"${state_dir}/tail.log"
yann@121
   844
    fi
yann@523
   845
    CT_DoLog INFO "Restoring state at step '${state_name}', as requested."
yann@121
   846
yann@121
   847
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@327
   848
        y)  tar_opt=z; tar_ext=.gz;;
yann@327
   849
        *)  tar_opt=;  tar_ext=;;
yann@121
   850
    esac
yann@121
   851
yann@121
   852
    CT_DoLog DEBUG "  Removing previous build directories"
yann@136
   853
    chmod -R u+rwX "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@136
   854
    rm -rf         "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@136
   855
    mkdir -p       "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@121
   856
yann@523
   857
    CT_DoLog DEBUG "  Restoring CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
yann@121
   858
    CT_Pushd "${CT_PREFIX_DIR}"
yann@776
   859
    CT_DoExecLog DEBUG tar xv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}"
yann@121
   860
    CT_Popd
yann@121
   861
yann@523
   862
    CT_DoLog DEBUG "  Restoring CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
yann@136
   863
    CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
yann@776
   864
    CT_DoExecLog DEBUG tar xv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}"
yann@136
   865
    CT_Popd
yann@136
   866
yann@523
   867
    CT_DoLog DEBUG "  Restoring CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
yann@136
   868
    CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@776
   869
    CT_DoExecLog DEBUG tar xv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}"
yann@121
   870
    CT_Popd
yann@121
   871
yann@121
   872
    # Restore the environment, discarding any error message
yann@121
   873
    # (for example, read-only bash internals)
yann@121
   874
    CT_DoLog DEBUG "  Restoring environment"
yann@121
   875
    . "${state_dir}/env.sh" >/dev/null 2>&1 || true
yann@121
   876
yann@135
   877
    # Restore the new RESTART and STOP steps
yann@135
   878
    CT_RESTART="${old_RESTART}"
yann@135
   879
    CT_STOP="${old_STOP}"
yann@135
   880
    unset old_stop old_restart
yann@135
   881
yann@121
   882
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
   883
        CT_DoLog DEBUG "  Restoring log file"
yann@121
   884
        exec >/dev/null
yann@121
   885
        case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@121
   886
            y)  zcat "${state_dir}/log.gz" >"${CT_LOG_FILE}";;
yann@121
   887
            *)  cat "${state_dir}/log" >"${CT_LOG_FILE}";;
yann@121
   888
        esac
yann@121
   889
        cat "${state_dir}/tail.log" >>"${CT_LOG_FILE}"
yann@121
   890
        exec >>"${CT_LOG_FILE}"
yann@121
   891
        rm -f "${state_dir}/tail.log"
yann@121
   892
    fi
yann@121
   893
}
yann@1149
   894
yann@1149
   895
do_finish() {
yann@1149
   896
    CT_DoStep INFO "Cleaning-up the toolchain's directory"
yann@1149
   897
yann@1149
   898
    CT_DoLog EXTRA "Removing access to the build system tools"
yann@1149
   899
    find "${CT_PREFIX_DIR}/bin" -name "${CT_BUILD}-"'*' -exec rm -fv {} \; |CT_DoLog DEBUG
yann@1149
   900
    find "${CT_PREFIX_DIR}/bin" -name "${CT_HOST}-"'*' -exec rm -fv {} \; |CT_DoLog DEBUG
yann@1149
   901
    CT_DoExecLog DEBUG rm -fv "${CT_PREFIX_DIR}/bin/makeinfo"
yann@1149
   902
yann@1149
   903
    if [ "${CT_BARE_METAL}" != "y" ]; then
yann@1149
   904
        CT_DoLog EXTRA "Installing the populate helper"
yann@1149
   905
        sed -r -e 's|@@CT_TARGET@@|'"${CT_TARGET}"'|g;' \
yann@1149
   906
            "${CT_LIB_DIR}/scripts/populate.in"           \
yann@1149
   907
            >"${CT_PREFIX_DIR}/bin/${CT_TARGET}-populate"
yann@1149
   908
        CT_DoExecLog ALL chmod 755 "${CT_PREFIX_DIR}/bin/${CT_TARGET}-populate"
yann@1149
   909
    fi
yann@1149
   910
yann@1149
   911
    # Create the aliases to the target tools
yann@1149
   912
    CT_DoLog EXTRA "Creating toolchain aliases"
yann@1149
   913
    CT_Pushd "${CT_PREFIX_DIR}/bin"
yann@1149
   914
    for t in "${CT_TARGET}-"*; do
yann@1149
   915
        if [ -n "${CT_TARGET_ALIAS}" ]; then
yann@1149
   916
            _t=$(echo "$t" |sed -r -e 's/^'"${CT_TARGET}"'-/'"${CT_TARGET_ALIAS}"'-/;')
yann@1149
   917
            CT_DoExecLog ALL ln -sv "${t}" "${_t}"
yann@1149
   918
        fi
yann@1149
   919
        if [ -n "${CT_TARGET_ALIAS_SED_EXPR}" ]; then
yann@1149
   920
            _t=$(echo "$t" |sed -r -e "${CT_TARGET_ALIAS_SED_EXPR}")
yann@1149
   921
            CT_DoExecLog ALL ln -sv "${t}" "${_t}"
yann@1149
   922
        fi
yann@1149
   923
    done
yann@1149
   924
    CT_Popd
yann@1149
   925
yann@1149
   926
    # Remove the generated documentation files
yann@1149
   927
    if [ "${CT_REMOVE_DOCS}" = "y" ]; then
yann@1149
   928
        CT_DoLog EXTRA "Removing installed documentation"
yann@1149
   929
        CT_DoForceRmdir "${CT_PREFIX_DIR}/"{,usr/}{man,info}
yann@1149
   930
        CT_DoForceRmdir "${CT_SYSROOT_DIR}/"{,usr/}{man,info}
yann@1149
   931
        CT_DoForceRmdir "${CT_DEBUG_INSTALL_DIR}/"{,usr/}{man,info}
yann@1149
   932
    fi
yann@1149
   933
yann@1149
   934
    CT_EndStep
yann@1149
   935
}