scripts/functions
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun May 27 20:22:06 2007 +0000 (2007-05-27)
changeset 136 22b5ef41df97
parent 135 b2695c2f1919
child 141 860f8380538d
permissions -rw-r--r--
Merge the NPTL stuff.
That still leaves the linuxthreads stuff broken, but it was just before. I don't care anyway. Time to fix that later...
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@1
     8
    CT_DoLog ERROR "Build failed in step \"${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}\""
yann@1
     9
    for((step=(CT_STEP_COUNT-1); step>1; step--)); do
yann@1
    10
        CT_DoLog ERROR "      called in step \"${CT_STEP_MESSAGE[${step}]}\""
yann@1
    11
    done
yann@1
    12
    CT_DoLog ERROR "Error happened in \"${BASH_SOURCE[1]}\" in function \"${FUNCNAME[1]}\" (line unknown, sorry)"
yann@1
    13
    for((depth=2; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
yann@1
    14
        CT_DoLog ERROR "      called from \"${BASH_SOURCE[${depth}]}\" at line # ${BASH_LINENO[${depth}-1]} in function \"${FUNCNAME[${depth}]}\""
yann@1
    15
    done
yann@112
    16
    [ "${CT_LOG_TO_FILE}" = "y" ] && CT_DoLog ERROR "Look at \"${CT_LOG_FILE}\" for more info on this error."
yann@96
    17
    CT_STEP_COUNT=1
yann@96
    18
    CT_DoEnd ERROR
yann@1
    19
    exit $ret
yann@1
    20
}
yann@136
    21
yann@136
    22
# Install the fault handler
yann@1
    23
trap CT_OnError ERR
yann@1
    24
yann@136
    25
# Inherit the fault handler in subshells and functions
yann@1
    26
set -E
yann@136
    27
yann@136
    28
# Make pipes fail on the _first_ failed command
yann@136
    29
# Not supported on bash < 3.x, but we need it, so drop the obsoleting bash-2.x
yann@1
    30
set -o pipefail
yann@1
    31
yann@136
    32
# Don't hash commands' locations, and search every time it is requested.
yann@136
    33
# This is slow, but needed because of the static/shared core gcc which shall
yann@136
    34
# always match to shared if it exists, and only fallback to static if the
yann@136
    35
# shared is not found
yann@136
    36
set +o hashall
yann@136
    37
yann@1
    38
# The different log levels:
yann@1
    39
CT_LOG_LEVEL_ERROR=0
yann@1
    40
CT_LOG_LEVEL_WARN=1
yann@1
    41
CT_LOG_LEVEL_INFO=2
yann@1
    42
CT_LOG_LEVEL_EXTRA=3
yann@1
    43
CT_LOG_LEVEL_DEBUG=4
yann@78
    44
CT_LOG_LEVEL_ALL=5
yann@1
    45
yann@1
    46
# A function to log what is happening
yann@1
    47
# Different log level are available:
yann@1
    48
#   - ERROR:   A serious, fatal error occurred
yann@1
    49
#   - WARN:    A non fatal, non serious error occurred, take your responsbility with the generated build
yann@1
    50
#   - INFO:    Informational messages
yann@1
    51
#   - EXTRA:   Extra informational messages
yann@1
    52
#   - DEBUG:   Debug messages
yann@78
    53
#   - ALL:     Component's build messages
yann@1
    54
# Usage: CT_DoLog <level> [message]
yann@1
    55
# If message is empty, then stdin will be logged.
yann@1
    56
CT_DoLog() {
yann@47
    57
    local max_level LEVEL level cur_l cur_L
yann@47
    58
    local l
yann@1
    59
    eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
yann@1
    60
    # Set the maximum log level to DEBUG if we have none
yann@78
    61
    [ -z "${max_level}" ] && max_level=${CT_LOG_LEVEL_DEBUG}
yann@1
    62
yann@47
    63
    LEVEL="$1"; shift
yann@1
    64
    eval level="\${CT_LOG_LEVEL_${LEVEL}}"
yann@1
    65
yann@1
    66
    if [ $# -eq 0 ]; then
yann@1
    67
        cat -
yann@1
    68
    else
yann@1
    69
        echo "${1}"
yann@112
    70
    fi |( offset=$((`CT_DoDate +%s`+(CT_STAR_DATE/(1000*1000*1000))))
yann@112
    71
          IFS="\n" # We want the full lines, even leading spaces
yann@82
    72
          CT_PROG_BAR_CPT=0
yann@1
    73
          indent=$((2*CT_STEP_COUNT))
yann@1
    74
          while read line; do
yann@47
    75
              case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
yann@47
    76
                y,*"warning:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
yann@112
    77
                y,*"WARNING:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
yann@47
    78
                *"error:"*)             cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
yann@78
    79
                *"make["?*"]:"*"Stop.") cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
yann@47
    80
                *)                      cur_L="${LEVEL}"; cur_l="${level}";;
yann@47
    81
              esac
yann@47
    82
              l="`printf \"[%-5s]%*s%s%s\" \"${cur_L}\" \"${indent}\" \" \" \"${line}\"`"
yann@1
    83
              # There will always be a log file, be it /dev/null
yann@112
    84
              echo -e "${l}"
yann@47
    85
              if [ ${cur_l} -le ${max_level} ]; then
yann@112
    86
                  echo -e "\r${l}" >&6
yann@82
    87
              fi
yann@82
    88
              if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
yann@112
    89
                  elapsed=$((SECONDS+OFFSET))
yann@82
    90
                  [ ${CT_PROG_BAR_CPT} -eq 0  ] && bar="/"
yann@82
    91
                  [ ${CT_PROG_BAR_CPT} -eq 10 ] && bar="-"
yann@82
    92
                  [ ${CT_PROG_BAR_CPT} -eq 20 ] && bar="\\"
yann@82
    93
                  [ ${CT_PROG_BAR_CPT} -eq 30 ] && bar="|"
yann@112
    94
                  printf "\r[%02d:%02d] %s " $((elapsed/60)) $((elapsed%60)) "${bar}" >&6
yann@82
    95
                  CT_PROG_BAR_CPT=$(((CT_PROG_BAR_CPT+1)%40))
yann@1
    96
              fi
yann@1
    97
          done
yann@1
    98
        )
yann@1
    99
yann@1
   100
    return 0
yann@1
   101
}
yann@1
   102
yann@96
   103
# Tail message to be logged whatever happens
yann@96
   104
# Usage: CT_DoEnd <level>
yann@96
   105
CT_DoEnd()
yann@96
   106
{
yann@96
   107
    CT_STOP_DATE=`CT_DoDate +%s%N`
yann@96
   108
    CT_STOP_DATE_HUMAN=`CT_DoDate +%Y%m%d.%H%M%S`
yann@96
   109
    CT_DoLog INFO "Build completed at ${CT_STOP_DATE_HUMAN}"
yann@96
   110
    elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
yann@96
   111
    elapsed_min=$((elapsed/(60*1000*1000*1000)))
yann@96
   112
    elapsed_sec=`printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000)))`
yann@96
   113
    elapsed_csec=`printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000)))`
yann@97
   114
    CT_DoLog ${1:-INFO} "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
yann@96
   115
}
yann@96
   116
yann@96
   117
# Abort the execution with an error message
yann@1
   118
# Usage: CT_Abort <message>
yann@1
   119
CT_Abort() {
yann@128
   120
    CT_DoLog ERROR "$1"
yann@1
   121
    exit 1
yann@1
   122
}
yann@1
   123
yann@1
   124
# Test a condition, and print a message if satisfied
yann@1
   125
# Usage: CT_Test <message> <tests>
yann@1
   126
CT_Test() {
yann@1
   127
    local ret
yann@1
   128
    local m="$1"
yann@1
   129
    shift
yann@1
   130
    test "$@" && CT_DoLog WARN "$m"
yann@1
   131
    return 0
yann@1
   132
}
yann@1
   133
yann@1
   134
# Test a condition, and abort with an error message if satisfied
yann@1
   135
# Usage: CT_TestAndAbort <message> <tests>
yann@1
   136
CT_TestAndAbort() {
yann@1
   137
    local m="$1"
yann@1
   138
    shift
yann@1
   139
    test "$@" && CT_Abort "$m"
yann@1
   140
    return 0
yann@1
   141
}
yann@1
   142
yann@1
   143
# Test a condition, and abort with an error message if not satisfied
yann@1
   144
# Usage: CT_TestAndAbort <message> <tests>
yann@1
   145
CT_TestOrAbort() {
yann@1
   146
    local m="$1"
yann@1
   147
    shift
yann@1
   148
    test "$@" || CT_Abort "$m"
yann@1
   149
    return 0
yann@1
   150
}
yann@1
   151
yann@1
   152
# Test the presence of a tool, or abort if not found
yann@1
   153
# Usage: CT_HasOrAbort <tool>
yann@1
   154
CT_HasOrAbort() {
yann@1
   155
    CT_TestAndAbort "\"${1}\" not found and needed for successfull toolchain build." -z "`which \"${1}\"`"
yann@1
   156
    return 0
yann@1
   157
}
yann@1
   158
yann@1
   159
# Get current date with nanosecond precision
yann@1
   160
# On those system not supporting nanosecond precision, faked with rounding down
yann@1
   161
# to the highest entire second
yann@1
   162
# Usage: CT_DoDate <fmt>
yann@1
   163
CT_DoDate() {
yann@1
   164
    date "$1" |sed -r -e 's/%N$/000000000/;'
yann@1
   165
}
yann@1
   166
yann@1
   167
CT_STEP_COUNT=1
yann@1
   168
CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
yann@1
   169
# Memorise a step being done so that any error is caught
yann@1
   170
# Usage: CT_DoStep <loglevel> <message>
yann@1
   171
CT_DoStep() {
yann@1
   172
    local start=`CT_DoDate +%s%N`
yann@1
   173
    CT_DoLog "$1" "================================================================="
yann@1
   174
    CT_DoLog "$1" "$2"
yann@1
   175
    CT_STEP_COUNT=$((CT_STEP_COUNT+1))
yann@1
   176
    CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
yann@1
   177
    CT_STEP_START[${CT_STEP_COUNT}]="${start}"
yann@1
   178
    CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
yann@1
   179
    return 0
yann@1
   180
}
yann@1
   181
yann@1
   182
# End the step just being done
yann@1
   183
# Usage: CT_EndStep
yann@1
   184
CT_EndStep() {
yann@1
   185
    local stop=`CT_DoDate +%s%N`
yann@1
   186
    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@1
   187
    local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
yann@1
   188
    local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
yann@1
   189
    CT_STEP_COUNT=$((CT_STEP_COUNT-1))
yann@1
   190
    CT_DoLog "${level}" "${message}: done in ${duration}s"
yann@1
   191
    return 0
yann@1
   192
}
yann@1
   193
yann@1
   194
# Pushes into a directory, and pops back
yann@1
   195
CT_Pushd() {
yann@1
   196
    pushd "$1" >/dev/null 2>&1
yann@1
   197
}
yann@1
   198
CT_Popd() {
yann@1
   199
    popd >/dev/null 2>&1
yann@1
   200
}
yann@1
   201
yann@1
   202
# Makes a path absolute
yann@1
   203
# Usage: CT_MakeAbsolutePath path
yann@1
   204
CT_MakeAbsolutePath() {
yann@1
   205
    # Try to cd in that directory
yann@1
   206
    if [ -d "$1" ]; then
yann@1
   207
        CT_Pushd "$1"
yann@1
   208
        pwd
yann@1
   209
        CT_Popd
yann@1
   210
    else
yann@1
   211
        # No such directory, fail back to guessing
yann@1
   212
        case "$1" in
yann@1
   213
            /*)  echo "$1";;
yann@1
   214
            *)   echo "`pwd`/$1";;
yann@1
   215
        esac
yann@1
   216
    fi
yann@1
   217
    
yann@1
   218
    return 0
yann@1
   219
}
yann@1
   220
yann@1
   221
# Creates a temporary directory
yann@1
   222
# $1: variable to assign to
yann@1
   223
# Usage: CT_MktempDir foo
yann@1
   224
CT_MktempDir() {
yann@1
   225
    # Some mktemp do not allow more than 6 Xs
yann@1
   226
    eval "$1"="`mktemp -q -d \"${CT_BUILD_DIR}/.XXXXXX\"`"
yann@1
   227
    CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
yann@1
   228
}
yann@1
   229
yann@1
   230
# Echoes the specified string on stdout until the pipe breaks.
yann@1
   231
# Doesn't fail
yann@1
   232
# $1: string to echo
yann@1
   233
# Usage: CT_DoYes "" |make oldconfig
yann@1
   234
CT_DoYes() {
yann@1
   235
    yes "$1" || true
yann@1
   236
}
yann@63
   237
yann@85
   238
# Get the file name extension of a component
yann@85
   239
# Usage: CT_GetFileExtension <component_name-component_version>
yann@85
   240
# If found, echoes the extension to stdout
yann@85
   241
# If not found, echoes nothing on stdout.
yann@85
   242
CT_GetFileExtension() {
yann@85
   243
    local ext
yann@85
   244
    local file="$1"
yann@85
   245
    local got_it=1
yann@85
   246
yann@85
   247
    CT_Pushd "${CT_TARBALLS_DIR}"
yann@85
   248
    for ext in .tar.gz .tar.bz2 .tgz .tar; do
yann@85
   249
        if [ -f "${file}${ext}" ]; then
yann@85
   250
            echo "${ext}"
yann@85
   251
            got_it=0
yann@85
   252
            break
yann@85
   253
        fi
yann@85
   254
    done
yann@85
   255
    CT_Popd
yann@85
   256
yann@85
   257
    return 0
yann@85
   258
}
yann@85
   259
yann@63
   260
# Download an URL using wget
yann@63
   261
# Usage: CT_DoGetFileWget <URL>
yann@63
   262
CT_DoGetFileWget() {
yann@63
   263
    # Need to return true because it is legitimate to not find the tarball at
yann@63
   264
    # some of the provided URLs (think about snapshots, different layouts for
yann@63
   265
    # different gcc versions, etc...)
yann@63
   266
    # Some (very old!) FTP server might not support the passive mode, thus
yann@63
   267
    # retry without
yann@63
   268
    # With automated download as we are doing, it can be very dangerous to use
yann@63
   269
    # -c to continue the downloads. It's far better to simply overwrite the
yann@63
   270
    # destination file
yann@63
   271
    wget -nc --progress=dot:binary --tries=3 --passive-ftp "$1" || wget -nc --progress=dot:binary --tries=3 "$1" || true
yann@63
   272
}
yann@63
   273
yann@63
   274
# Download an URL using curl
yann@63
   275
# Usage: CT_DoGetFileCurl <URL>
yann@63
   276
CT_DoGetFileCurl() {
yann@63
   277
	# Note: comments about wget method are also valid here
yann@63
   278
	# Plus: no good progreess indicator is available with curl,
yann@63
   279
	#       so output is consigned to oblivion
yann@63
   280
	curl --ftp-pasv -O --retry 3 "$1" >/dev/null || curl -O --retry 3 "$1" >/dev/null || true
yann@63
   281
}
yann@63
   282
yann@63
   283
# Wrapper function to call one of curl or wget
yann@63
   284
# Usage: CT_DoGetFile <URL>
yann@63
   285
CT_DoGetFile() {
yann@63
   286
    local _wget=`which wget`
yann@63
   287
    local _curl=`which curl`
yann@63
   288
    case "${_wget},${_curl}" in
yann@63
   289
        ,)  CT_DoError "Could find neither wget nor curl";;
yann@79
   290
        ,*) CT_DoGetFileCurl "$1" 2>&1 |CT_DoLog DEBUG;;
yann@79
   291
        *)  CT_DoGetFileWget "$1" 2>&1 |CT_DoLog DEBUG;;
yann@63
   292
    esac
yann@63
   293
}
yann@63
   294
yann@63
   295
# Download the file from one of the URLs passed as argument
yann@63
   296
# Usage: CT_GetFile <filename> <url> [<url> ...]
yann@63
   297
CT_GetFile() {
yann@63
   298
    local got_it
yann@63
   299
    local ext
yann@63
   300
    local url
yann@63
   301
    local file="$1"
yann@63
   302
    shift
yann@63
   303
yann@63
   304
    # Do we already have it?
yann@63
   305
    ext=`CT_GetFileExtension "${file}"`
yann@63
   306
    if [ -n "${ext}" ]; then
yann@85
   307
        CT_DoLog DEBUG "Already have \"${file}\""
yann@85
   308
        return 0
yann@63
   309
    fi
yann@63
   310
yann@63
   311
    CT_Pushd "${CT_TARBALLS_DIR}"
yann@63
   312
    # File not yet downloaded, try to get it
yann@63
   313
    got_it=0
yann@85
   314
    # We'd rather have a bzip2'ed tarball, then gzipped, and finally plain tar.
yann@102
   315
    # Try local copy first, if it exists
yann@85
   316
    for ext in .tar.bz2 .tar.gz .tgz .tar; do
yann@102
   317
        if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
yann@102
   318
             "${CT_FORCE_DOWNLOAD}" != "y" ]; then
yann@107
   319
            CT_DoLog EXTRA "Copying \"${file}\" from local copy"
yann@121
   320
            cp -v "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
yann@102
   321
            return 0
yann@63
   322
        fi
yann@63
   323
    done
yann@102
   324
    # Try to download it
yann@107
   325
    CT_DoLog EXTRA "Retrieving \"${file}\""
yann@102
   326
    for ext in .tar.bz2 .tar.gz .tgz .tar; do
yann@102
   327
        # Try all urls in turn
yann@102
   328
        for url in "$@"; do
yann@102
   329
            case "${url}" in
yann@102
   330
                *)  CT_DoLog DEBUG "Trying \"${url}/${file}${ext}\""
yann@102
   331
                    CT_DoGetFile "${url}/${file}${ext}"
yann@102
   332
                    ;;
yann@102
   333
            esac
yann@102
   334
            [ -f "${file}${ext}" ] && return 0 || true
yann@102
   335
        done
yann@102
   336
    done
yann@63
   337
    CT_Popd
yann@63
   338
yann@102
   339
    CT_Abort "Could not download \"${file}\", and not present in \"${CT_LOCAL_TARBALLS_DIR}\""
yann@63
   340
}
yann@63
   341
yann@63
   342
# Extract a tarball and patch the resulting sources if necessary.
yann@63
   343
# Some tarballs need to be extracted in specific places. Eg.: glibc addons
yann@63
   344
# must be extracted in the glibc directory; uCLibc locales must be extracted
yann@63
   345
# in the extra/locale sub-directory of uClibc.
yann@63
   346
CT_ExtractAndPatch() {
yann@63
   347
    local file="$1"
yann@63
   348
    local base_file=`echo "${file}" |cut -d - -f 1`
yann@63
   349
    local ver_file=`echo "${file}" |cut -d - -f 2-`
yann@63
   350
    local official_patch_dir
yann@63
   351
    local custom_patch_dir
yann@63
   352
    local libc_addon
yann@63
   353
    local ext=`CT_GetFileExtension "${file}"`
yann@63
   354
    CT_TestAndAbort "\"${file}\" not found in \"${CT_TARBALLS_DIR}\"" -z "${ext}"
yann@63
   355
    local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
yann@63
   356
yann@63
   357
    CT_Pushd "${CT_SRC_DIR}"
yann@63
   358
yann@63
   359
    # Add-ons need a little love, really.
yann@63
   360
    case "${file}" in
yann@63
   361
        glibc-[a-z]*-*)
yann@63
   362
            CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
yann@63
   363
            cd "${CT_LIBC_FILE}"
yann@63
   364
            libc_addon=y
yann@63
   365
            [ -f ".${file}.extracted" ] && return 0
yann@63
   366
            touch ".${file}.extracted"
yann@63
   367
            ;;
yann@63
   368
        uClibc-locale-*)
yann@63
   369
            CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
yann@63
   370
            cd "${CT_LIBC_FILE}/extra/locale"
yann@63
   371
            libc_addon=y
yann@63
   372
            [ -f ".${file}.extracted" ] && return 0
yann@63
   373
            touch ".${file}.extracted"
yann@63
   374
            ;;
yann@63
   375
    esac
yann@63
   376
yann@63
   377
    # If the directory exists, then consider extraction and patching done
yann@77
   378
    if [ -d "${file}" ]; then
yann@78
   379
        CT_DoLog DEBUG "Already extracted \"${file}\""
yann@77
   380
        return 0
yann@77
   381
    fi
yann@63
   382
yann@63
   383
    CT_DoLog EXTRA "Extracting \"${file}\""
yann@63
   384
    case "${ext}" in
yann@78
   385
        .tar.bz2)     tar xvjf "${full_file}" |CT_DoLog ALL;;
yann@78
   386
        .tar.gz|.tgz) tar xvzf "${full_file}" |CT_DoLog ALL;;
yann@78
   387
        .tar)         tar xvf  "${full_file}" |CT_DoLog ALL;;
yann@63
   388
        *)            CT_Abort "Don't know how to handle \"${file}\": unknown extension" ;;
yann@63
   389
    esac
yann@63
   390
yann@63
   391
    # Snapshots might not have the version number in the extracted directory
yann@63
   392
    # name. This is also the case for some (old) packages, such as libfloat.
yann@63
   393
    # Overcome this issue by symlink'ing the directory.
yann@63
   394
    if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
yann@63
   395
        case "${ext}" in
yann@63
   396
            .tar.bz2)     base=`tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
yann@63
   397
            .tar.gz|.tgz) base=`tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
yann@63
   398
            .tar)         base=`tar tf  "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
yann@63
   399
        esac
yann@63
   400
        CT_TestOrAbort "There was a problem when extracting \"${file}\"" -d "${base}" -o "${base}" != "${file}"
yann@63
   401
        ln -s "${base}" "${file}"
yann@63
   402
    fi
yann@63
   403
yann@63
   404
    # Kludge: outside this function, we wouldn't know if we had just extracted
yann@63
   405
    # a libc addon, or a plain package. Apply patches now.
yann@63
   406
    CT_DoLog EXTRA "Patching \"${file}\""
yann@63
   407
yann@136
   408
    if [ "${libc_addon}" = "y" ]; then
yann@136
   409
        # Some addons tarball directly contian the correct addon directory,
yann@136
   410
        # while others have the addon directory named ofter the tarball.
yann@136
   411
        # Fix that bu always using the short name (eg: linuxthreads, ports, etc...)
yann@136
   412
        addon_short_name=`echo "${file}" |sed -r -e 's/^[^-]+-//; s/-[^-]+$//;'`
yann@136
   413
        [ -d "${addon_short_name}" ] || ln -s "${file}" "${addon_short_name}"
yann@136
   414
        # If libc addon, we're already in the correct place
yann@136
   415
    else
yann@136
   416
        cd "${file}"
yann@136
   417
    fi
yann@63
   418
yann@63
   419
    [ "${CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_TOP_DIR}/patches/${base_file}/${ver_file}"
yann@63
   420
    [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
yann@63
   421
    for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
yann@63
   422
        if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
yann@63
   423
            for p in "${patch_dir}"/*.patch; do
yann@63
   424
                if [ -f "${p}" ]; then
yann@63
   425
                    CT_DoLog DEBUG "Applying patch \"${p}\""
yann@78
   426
                    patch -g0 -F1 -p1 -f <"${p}" |CT_DoLog ALL
yann@63
   427
                    CT_TestAndAbort "Failed while applying patch file \"${p}\"" ${PIPESTATUS[0]} -ne 0
yann@63
   428
                fi
yann@63
   429
            done
yann@63
   430
        fi
yann@63
   431
    done
yann@63
   432
yann@63
   433
    CT_Popd
yann@63
   434
}
yann@63
   435
yann@63
   436
# Compute the target triplet from what is provided by the user
yann@63
   437
# Usage: CT_DoBuildTargetTriplet
yann@63
   438
# In fact this function takes the environment variables to build the target
yann@63
   439
# triplet. It is needed both by the normal build sequence, as well as the
yann@63
   440
# sample saving sequence.
yann@63
   441
CT_DoBuildTargetTriplet() {
yann@63
   442
    case "${CT_ARCH_BE},${CT_ARCH_LE}" in
yann@63
   443
        y,) target_endian_eb=eb; target_endian_el=;;
yann@63
   444
        ,y) target_endian_eb=; target_endian_el=el;;
yann@63
   445
    esac
yann@63
   446
    case "${CT_ARCH}" in
yann@63
   447
        arm)  CT_TARGET="${CT_ARCH}${target_endian_eb}";;
yann@63
   448
        mips) CT_TARGET="${CT_ARCH}${target_endian_el}";;
yann@63
   449
        x86*) # Much love for this one :-(
yann@63
   450
              arch="${CT_ARCH_ARCH}"
yann@63
   451
              [ -z "${arch}" ] && arch="${CT_ARCH_TUNE}"
yann@63
   452
              case "${CT_ARCH}" in
yann@127
   453
                  x86_64)  CT_TARGET=x86_64;;
yann@63
   454
              	  *)  case "${arch}" in
yann@63
   455
                          "")                                       CT_TARGET=i386;;
yann@63
   456
                          i386|i486|i586|i686)                      CT_TARGET="${arch}";;
yann@63
   457
                          winchip*)                                 CT_TARGET=i486;;
yann@63
   458
                          pentium|pentium-mmx|c3*)                  CT_TARGET=i586;;
yann@63
   459
                          nocona|athlon*64|k8|athlon-fx|opteron)    CT_TARGET=x86_64;;
yann@63
   460
                          pentiumpro|pentium*|athlon*)              CT_TARGET=i686;;
yann@63
   461
                          *)                                        CT_TARGET=i586;;
yann@63
   462
                      esac;;
yann@63
   463
              esac;;
yann@63
   464
    esac
yann@63
   465
    case "${CT_TARGET_VENDOR}" in
yann@63
   466
        "") CT_TARGET="${CT_TARGET}-unknown";;
yann@63
   467
        *)  CT_TARGET="${CT_TARGET}-${CT_TARGET_VENDOR}";;
yann@63
   468
    esac
yann@63
   469
    case "${CT_KERNEL}" in
yann@63
   470
        linux*)  CT_TARGET="${CT_TARGET}-linux";;
yann@63
   471
    esac
yann@63
   472
    case "${CT_LIBC}" in
yann@63
   473
        glibc)  CT_TARGET="${CT_TARGET}-gnu";;
yann@63
   474
        uClibc) CT_TARGET="${CT_TARGET}-uclibc";;
yann@63
   475
    esac
yann@63
   476
    CT_TARGET="`${CT_TOP_DIR}/tools/config.sub ${CT_TARGET}`"
yann@63
   477
}
yann@121
   478
yann@121
   479
# This function does pause the build until the user strikes "Return"
yann@121
   480
# Usage: CT_DoPause [optional_message]
yann@121
   481
CT_DoPause() {
yann@121
   482
    local foo
yann@121
   483
    local message="${1:-Pausing for your pleasure}"
yann@121
   484
    CT_DoLog INFO "${message}"
yann@121
   485
    read -p "Press \"Enter\" to continue, or Ctrl-C to stop..." foo >&6
yann@121
   486
    return 0
yann@121
   487
}
yann@121
   488
yann@121
   489
# This function saves the state of the toolchain to be able to restart
yann@121
   490
# at any one point
yann@121
   491
# Usage: CT_DoSaveState <next_step_name>
yann@121
   492
CT_DoSaveState() {
yann@121
   493
	[ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
yann@121
   494
    local state_name="$1"
yann@121
   495
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@121
   496
yann@121
   497
    CT_DoLog DEBUG "Saving state to restart at step \"${state_name}\"..."
yann@121
   498
    rm -rf "${state_dir}"
yann@121
   499
    mkdir -p "${state_dir}"
yann@121
   500
yann@121
   501
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@121
   502
        y)  tar_opt=czf; tar_ext=".tar.gz";;
yann@121
   503
        *)  tar_opt=cf;  tar_ext=".tar";;
yann@121
   504
    esac
yann@121
   505
yann@121
   506
    CT_DoLog DEBUG "  Saving environment and aliases"
yann@121
   507
    # We must omit shell functions
yann@121
   508
    # 'isgrep' is here because I don't seem to
yann@121
   509
    # be able to remove the functions names.
yann@121
   510
    set |awk '
yann@121
   511
         BEGIN { _p = 1; }
yann@121
   512
         $0~/^[^ ] ()/ { _p = 0; }
yann@121
   513
         _p == 1
yann@121
   514
         $0 == "}" { _p = 1; }
yann@121
   515
         ' |egrep -v '^[^ ]+ \(\)' >"${state_dir}/env.sh"
yann@121
   516
yann@136
   517
    CT_DoLog DEBUG "  Saving CT_CC_CORE_STATIC_PREFIX_DIR=\"${CT_CC_CORE_STATIC_PREFIX_DIR}\""
yann@136
   518
    CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@136
   519
    tar ${tar_opt} "${state_dir}/cc_core_static_prefix_dir${tar_ext}" .
yann@136
   520
    CT_Popd
yann@136
   521
yann@136
   522
    CT_DoLog DEBUG "  Saving CT_CC_CORE_SHARED_PREFIX_DIR=\"${CT_CC_CORE_SHARED_PREFIX_DIR}\""
yann@136
   523
    CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
yann@136
   524
    tar ${tar_opt} "${state_dir}/cc_core_shared_prefix_dir${tar_ext}" .
yann@121
   525
    CT_Popd
yann@121
   526
yann@121
   527
    CT_DoLog DEBUG "  Saving CT_PREFIX_DIR=\"${CT_PREFIX_DIR}\""
yann@121
   528
    CT_Pushd "${CT_PREFIX_DIR}"
yann@136
   529
    tar ${tar_opt} "${state_dir}/prefix_dir${tar_ext}" --exclude '*.log' .
yann@121
   530
    CT_Popd
yann@121
   531
yann@121
   532
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
   533
        CT_DoLog DEBUG "  Saving log file"
yann@121
   534
        exec >/dev/null
yann@121
   535
        case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@121
   536
            y)  gzip -3 -c "${CT_LOG_FILE}"  >"${state_dir}/log.gz";;
yann@121
   537
            *)  cat "${CT_LOG_FILE}" >"${state_dir}/log";;
yann@121
   538
        esac
yann@121
   539
        exec >>"${CT_LOG_FILE}"
yann@121
   540
    fi
yann@121
   541
}
yann@121
   542
yann@136
   543
# This function restores a previously saved state
yann@121
   544
# Usage: CT_DoLoadState <state_name>
yann@121
   545
CT_DoLoadState(){
yann@121
   546
    local state_name="$1"
yann@121
   547
    local state_dir="${CT_STATE_DIR}/${state_name}"
yann@135
   548
    local old_RESTART="${CT_RESTART}"
yann@135
   549
    local old_STOP="${CT_STOP}"
yann@121
   550
yann@121
   551
    # We need to do something special with the log file!
yann@121
   552
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
   553
        exec >"${state_dir}/tail.log"
yann@121
   554
    fi
yann@135
   555
    CT_DoLog INFO "Restoring state at step \"${state_name}\", as requested."
yann@121
   556
yann@121
   557
    case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@121
   558
        y)  tar_opt=xzf; tar_ext=".tar.gz";;
yann@121
   559
        *)  tar_opt=cf;  tar_ext=".tar";;
yann@121
   560
    esac
yann@121
   561
yann@121
   562
    CT_DoLog DEBUG "  Removing previous build directories"
yann@136
   563
    chmod -R u+rwX "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@136
   564
    rm -rf         "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@136
   565
    mkdir -p       "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@121
   566
yann@121
   567
    CT_DoLog DEBUG "  Restoring CT_PREFIX_DIR=\"${CT_PREFIX_DIR}\""
yann@121
   568
    CT_Pushd "${CT_PREFIX_DIR}"
yann@121
   569
    tar ${tar_opt} "${state_dir}/prefix_dir${tar_ext}"
yann@121
   570
    CT_Popd
yann@121
   571
yann@136
   572
    CT_DoLog DEBUG "  Restoring CT_CC_CORE_SHARED_PREFIX_DIR=\"${CT_CC_CORE_SHARED_PREFIX_DIR}\""
yann@136
   573
    CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
yann@136
   574
    tar ${tar_opt} "${state_dir}/cc_core_shared_prefix_dir${tar_ext}"
yann@136
   575
    CT_Popd
yann@136
   576
yann@136
   577
    CT_DoLog DEBUG "  Restoring CT_CC_CORE_STATIC_PREFIX_DIR=\"${CT_CC_CORE_STATIC_PREFIX_DIR}\""
yann@136
   578
    CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
yann@136
   579
    tar ${tar_opt} "${state_dir}/cc_core_static_prefix_dir${tar_ext}"
yann@121
   580
    CT_Popd
yann@121
   581
yann@121
   582
    # Restore the environment, discarding any error message
yann@121
   583
    # (for example, read-only bash internals)
yann@121
   584
    CT_DoLog DEBUG "  Restoring environment"
yann@121
   585
    . "${state_dir}/env.sh" >/dev/null 2>&1 || true
yann@121
   586
yann@135
   587
    # Restore the new RESTART and STOP steps
yann@135
   588
    CT_RESTART="${old_RESTART}"
yann@135
   589
    CT_STOP="${old_STOP}"
yann@135
   590
    unset old_stop old_restart
yann@135
   591
yann@121
   592
    if [ "${CT_LOG_TO_FILE}" = "y" ]; then
yann@121
   593
        CT_DoLog DEBUG "  Restoring log file"
yann@121
   594
        exec >/dev/null
yann@121
   595
        case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
yann@121
   596
            y)  zcat "${state_dir}/log.gz" >"${CT_LOG_FILE}";;
yann@121
   597
            *)  cat "${state_dir}/log" >"${CT_LOG_FILE}";;
yann@121
   598
        esac
yann@121
   599
        cat "${state_dir}/tail.log" >>"${CT_LOG_FILE}"
yann@121
   600
        exec >>"${CT_LOG_FILE}"
yann@121
   601
        rm -f "${state_dir}/tail.log"
yann@121
   602
    fi
yann@121
   603
}