scripts/getExtractPatch.sh
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue May 01 16:49:15 2007 +0000 (2007-05-01)
changeset 56 07a6a48962b7
parent 23 78d0b570baf7
permissions -rw-r--r--
Merge patches sent by Robert P. J. Day <rpjday@mindspring.com>.
Warning: the buildroot folks purposedly removed the skip-comment patch but didn't really said why. Keeping it for the sake of having it in svn just in case (removing it will be easier thant not having it at all).
yann@1
     1
# This script will download tarballs, extract them and patch the source.
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@1
     5
# Download tarballs in sequence. Once we have everything, start extracting
yann@1
     6
# and patching the tarballs.
yann@1
     7
yann@1
     8
#-----------------------------------------------------------------------------
yann@1
     9
yann@1
    10
_wget=`which wget || true`
yann@1
    11
_curl=`which curl || true`
yann@1
    12
#_svn=`which svn ||true`
yann@1
    13
#_cvs=`which cvs || true`
yann@1
    14
yann@1
    15
case "${_wget},${_curl}" in
yann@1
    16
    ,)  CT_Abort "Found neither curl nor wget. Please install one.";;
yann@1
    17
    ,*) CT_DoLog DEBUG "Using curl to retrieve tarballs"; CT_DoGetFile=CT_DoGetFileCurl;;
yann@1
    18
    *)  CT_DoLog DEBUG "Using wget to retrieve tarballs"; CT_DoGetFile=CT_DoGetFileWget;;
yann@1
    19
esac
yann@1
    20
yann@1
    21
CT_DoGetFileWget() {
yann@23
    22
    # Need to return true because it is legitimate to not find the tarball at
yann@1
    23
    # some of the provided URLs (think about snapshots, different layouts for
yann@1
    24
    # different gcc versions, etc...)
yann@1
    25
    # Some (very old!) FTP server might not support the passive mode, thus
yann@1
    26
    # retry without
yann@1
    27
    # With automated download as we are doing, it can be very dangerous to use
yann@1
    28
    # -c to continue the downloads. It's far better to simply overwrite the
yann@1
    29
    # destination file
yann@1
    30
    wget -nc --progress=dot:binary --tries=3 --passive-ftp "$1" || wget -nc --progress=dot:binary --tries=3 "$1" || true
yann@1
    31
}
yann@1
    32
yann@1
    33
CT_DoGetFileCurl() {
yann@1
    34
	# Note: comments about wget method are also valid here
yann@1
    35
	# Plus: no good progreess indicator is available with curl,
yann@1
    36
	#       so output is consigned to oblivion
yann@1
    37
	curl --ftp-pasv -O --retry 3 "$1" >/dev/null || curl -O --retry 3 "$1" >/dev/null || true
yann@1
    38
}
yann@1
    39
yann@1
    40
# For those wanting bleading edge, or to retrieve old uClibc snapshots
yann@1
    41
# Usage: CT_GetFileSVN basename url
yann@1
    42
#CT_DoGetFileSVN() {
yann@1
    43
#    local basename="$1"
yann@1
    44
#    local url="`echo \"$2\" |cut -d : -f 2-`"
yann@1
    45
#    local tmp_dir
yann@1
    46
#
yann@1
    47
#    CT_TestOrAbort "You don't have subversion" -n "${_svn}"
yann@1
    48
#    CT_MktempDir tmp_dir
yann@1
    49
#    CT_Pushd "${tmp_dir}"
yann@1
    50
#    svn export --force "${url}" "${basename}"
yann@1
    51
#    tar cfj "${CT_TARBALLS_DIR}/${basename}.tar.bz2" "${basename}"
yann@1
    52
#    CT_Popd
yann@1
    53
#    rm -rf "${tmp_dir}"
yann@1
    54
#}
yann@1
    55
#
yann@1
    56
#CT_DoGetFileCVS() {
yann@1
    57
#    :
yann@1
    58
#}
yann@1
    59
yann@1
    60
# Download the file from one of the URLs passed as argument
yann@1
    61
# Usage: CT_GetFile <filename> <url> [<url> ...]
yann@1
    62
CT_GetFile() {
yann@1
    63
    local got_it
yann@1
    64
    local ext
yann@1
    65
    local url
yann@1
    66
    local file="$1"
yann@1
    67
    shift
yann@1
    68
yann@1
    69
    # Do we already have it?
yann@1
    70
    ext=`CT_GetFileExtension "${file}"`
yann@1
    71
    if [ -n "${ext}" ]; then
yann@1
    72
        if [ "${CT_FORCE_DOWNLOAD}" = "y" ]; then
yann@1
    73
            rm -f "${CT_TARBALLS_DIR}/${file}${ext}"
yann@1
    74
        else
yann@1
    75
            return 0
yann@1
    76
        fi
yann@1
    77
    fi
yann@1
    78
yann@1
    79
    CT_DoLog EXTRA "Retrieving \"${file}\""
yann@1
    80
    CT_Pushd "${CT_TARBALLS_DIR}"
yann@1
    81
    # File not yet downloaded, try to get it
yann@1
    82
    got_it=0
yann@1
    83
    if [ "${got_it}" != "y" ]; then
yann@1
    84
        # We'd rather have a bzip2'ed tarball, then gzipped, and finally plain tar.
yann@1
    85
        for ext in .tar.bz2 .tar.gz .tgz .tar; do
yann@1
    86
            # Try all urls in turn
yann@1
    87
            for url in "$@"; do
yann@1
    88
                case "${url}" in
yann@1
    89
#                    svn://*)    CT_DoGetFileSVN "${file}" ${url}";;
yann@1
    90
#                    cvs://*)    CT_DoGetFileCVS "${file}" ${url}";;
yann@1
    91
                    *)  CT_DoLog EXTRA "Trying \"${url}/${file}${ext}\""
yann@1
    92
                        ${CT_DoGetFile} "${url}/${file}${ext}" 2>&1 |CT_DoLog DEBUG
yann@1
    93
                        ;;
yann@1
    94
                esac
yann@1
    95
                [ -f "${file}${ext}" ] && got_it=1 && break 2 || true
yann@1
    96
            done
yann@1
    97
        done
yann@1
    98
    fi
yann@1
    99
    CT_Popd
yann@1
   100
yann@1
   101
    CT_TestAndAbort "Could not download \"${file}\", and not present in \"${CT_TARBALLS_DIR}\"" ${got_it} -eq 0
yann@1
   102
}
yann@1
   103
yann@1
   104
#-----------------------------------------------------------------------------
yann@1
   105
yann@1
   106
# Extract a tarball and patch.
yann@1
   107
# Some tarballs need to be extracted in specific places. Eg.: glibc addons
yann@1
   108
# must be extracted in the glibc directory; uCLibc locales must be extracted
yann@1
   109
# in the extra/locale sub-directory of uClibc.
yann@1
   110
CT_ExtractAndPatch() {
yann@1
   111
    local file="$1"
yann@1
   112
    local base_file=`echo "${file}" |cut -d - -f 1`
yann@1
   113
    local ver_file=`echo "${file}" |cut -d - -f 2-`
yann@1
   114
    local official_patch_dir
yann@1
   115
    local custom_patch_dir
yann@1
   116
    local libc_addon
yann@1
   117
    local ext=`CT_GetFileExtension "${file}"`
yann@1
   118
    CT_TestAndAbort "\"${file}\" not found in \"${CT_TARBALLS_DIR}\"" -z "${ext}"
yann@1
   119
    local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
yann@1
   120
yann@1
   121
    CT_Pushd "${CT_SRC_DIR}"
yann@1
   122
yann@1
   123
    # Add-ons need a little love, really.
yann@1
   124
    case "${file}" in
yann@1
   125
        glibc-[a-z]*-*)
yann@1
   126
            CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
yann@1
   127
            cd "${CT_LIBC_FILE}"
yann@1
   128
            libc_addon=y
yann@1
   129
            [ -f ".${file}.extracted" ] && return 0
yann@1
   130
            touch ".${file}.extracted"
yann@1
   131
            ;;
yann@1
   132
        uClibc-locale-*)
yann@1
   133
            CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
yann@1
   134
            cd "${CT_LIBC_FILE}/extra/locale"
yann@1
   135
            libc_addon=y
yann@1
   136
            [ -f ".${file}.extracted" ] && return 0
yann@1
   137
            touch ".${file}.extracted"
yann@1
   138
            ;;
yann@1
   139
    esac
yann@1
   140
yann@1
   141
    # If the directory exists, then consider extraction and patching done
yann@1
   142
    [ -d "${file}" ] && return 0
yann@1
   143
yann@1
   144
    CT_DoLog EXTRA "Extracting \"${file}\""
yann@1
   145
    case "${ext}" in
yann@1
   146
        .tar.bz2)     tar xvjf "${full_file}" |CT_DoLog DEBUG;;
yann@1
   147
        .tar.gz|.tgz) tar xvzf "${full_file}" |CT_DoLog DEBUG;;
yann@1
   148
        .tar)         tar xvf  "${full_file}" |CT_DoLog DEBUG;;
yann@1
   149
        *)            CT_Abort "Don't know how to handle \"${file}\": unknown extension" ;;
yann@1
   150
    esac
yann@1
   151
yann@1
   152
    # Snapshots might not have the version number in the extracted directory
yann@1
   153
    # name. This is also the case for some (old) packages, such as libfloat.
yann@1
   154
    # Overcome this issue by symlink'ing the directory.
yann@1
   155
    if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
yann@1
   156
        case "${ext}" in
yann@1
   157
            .tar.bz2)     base=`tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
yann@1
   158
            .tar.gz|.tgz) base=`tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
yann@1
   159
            .tar)         base=`tar tf  "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
yann@1
   160
        esac
yann@1
   161
        CT_TestOrAbort "There was a problem when extracting \"${file}\"" -d "${base}" -o "${base}" != "${file}"
yann@1
   162
        ln -s "${base}" "${file}"
yann@1
   163
    fi
yann@1
   164
yann@1
   165
    # Kludge: outside this function, we wouldn't know if we had just extracted
yann@1
   166
    # a libc addon, or a plain package. Apply patches now.
yann@1
   167
    CT_DoLog EXTRA "Patching \"${file}\""
yann@1
   168
yann@1
   169
    # If libc addon, we're already in the correct place.
yann@1
   170
    [ -z "${libc_addon}" ] && cd "${file}"
yann@1
   171
yann@1
   172
    [ "${CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_TOP_DIR}/patches/${base_file}/${ver_file}"
yann@1
   173
    [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
yann@1
   174
    for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
yann@1
   175
        if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
yann@1
   176
            for p in "${patch_dir}"/*.patch; do
yann@1
   177
                if [ -f "${p}" ]; then
yann@1
   178
                    CT_DoLog DEBUG "Applying patch \"${p}\""
yann@1
   179
                    patch -g0 -F1 -p1 -f <"${p}" |CT_DoLog DEBUG
yann@1
   180
                    CT_TestAndAbort "Failed while applying patch file \"${p}\"" ${PIPESTATUS[0]} -ne 0
yann@1
   181
                fi
yann@1
   182
            done
yann@1
   183
        fi
yann@1
   184
    done
yann@1
   185
yann@1
   186
    CT_Popd
yann@1
   187
}
yann@1
   188
yann@1
   189
#-----------------------------------------------------------------------------
yann@1
   190
yann@1
   191
# Get the file name extension of a component
yann@1
   192
# Usage: CT_GetFileExtension <component-version>
yann@1
   193
# If found, echoes the extension to stdout
yann@1
   194
# If not found, echoes nothing on stdout.
yann@1
   195
CT_GetFileExtension() {
yann@1
   196
    local ext
yann@1
   197
    local file="$1"
yann@1
   198
    local got_it=1
yann@1
   199
yann@1
   200
    CT_Pushd "${CT_TARBALLS_DIR}"
yann@1
   201
    for ext in .tar.gz .tar.bz2 .tgz .tar; do
yann@1
   202
        if [ -f "${file}${ext}" ]; then
yann@1
   203
            echo "${ext}"
yann@1
   204
            got_it=0
yann@1
   205
            break
yann@1
   206
        fi
yann@1
   207
    done
yann@1
   208
    CT_Popd
yann@1
   209
yann@1
   210
    return 0
yann@1
   211
}
yann@1
   212
yann@1
   213
#-----------------------------------------------------------------------------
yann@1
   214
yann@1
   215
# Create needed directories, remove old ones
yann@1
   216
mkdir -p "${CT_TARBALLS_DIR}"
yann@1
   217
if [ "${CT_FORCE_EXTRACT}" = "y" -a -d "${CT_SRC_DIR}" ]; then
yann@1
   218
    mv "${CT_SRC_DIR}" "${CT_SRC_DIR}.$$"
yann@1
   219
    nohup rm -rf "${CT_SRC_DIR}.$$" >/dev/null 2>&1 &
yann@1
   220
fi
yann@1
   221
mkdir -p "${CT_SRC_DIR}"
yann@1
   222
yann@1
   223
# Make all path absolute, it so much easier!
yann@1
   224
# Now we have had the directories created, we even will get rid of embedded .. in paths:
yann@1
   225
CT_SRC_DIR="`CT_MakeAbsolutePath \"${CT_SRC_DIR}\"`"
yann@1
   226
CT_TARBALLS_DIR="`CT_MakeAbsolutePath \"${CT_TARBALLS_DIR}\"`"
yann@1
   227
yann@1
   228
# Prepare the addons list to be parsable:
yann@1
   229
addons_list="`echo \"${CT_LIBC_ADDONS_LIST}\" |sed -r -e 's/,/ /g; s/ $//g;'`"
yann@1
   230
yann@1
   231
if [ "${CT_NO_DOWNLOAD}" != "y" ]; then
yann@1
   232
    CT_DoStep INFO "Retrieving needed toolchain components' tarballs"
yann@1
   233
yann@1
   234
    # Kernel: for now, I don't care about cygwin.
yann@37
   235
    if [ "${CT_KERNEL_LINUX_HEADERS_USE_CUSTOM_DIR}" != "y" ]; then
yann@37
   236
        CT_GetFile "${CT_KERNEL_FILE}"                                  \
yann@37
   237
                   ftp://ftp.kernel.org/pub/linux/kernel/v2.6           \
yann@37
   238
                   ftp://ftp.kernel.org/pub/linux/kernel/v2.4           \
yann@37
   239
                   ftp://ftp.kernel.org/pub/linux/kernel/v2.2           \
yann@37
   240
                   ftp://ftp.kernel.org/pub/linux/kernel/v2.6/testing   \
yann@37
   241
                   http://ep09.pld-linux.org/~mmazur/linux-libc-headers
yann@37
   242
    fi
yann@1
   243
yann@1
   244
    # binutils
yann@1
   245
    CT_GetFile "${CT_BINUTILS_FILE}"                            \
yann@1
   246
               ftp://ftp.gnu.org/gnu/binutils                   \
yann@1
   247
               ftp://ftp.kernel.org/pub/linux/devel/binutils
yann@1
   248
yann@1
   249
    # Core and final gcc
yann@1
   250
    # Ah! gcc folks are kind of 'different': they store the tarballs in
yann@1
   251
    # subdirectories of the same name! That's because gcc is such /crap/ that
yann@1
   252
    # it is such /big/ that it needs being splitted for distribution! Sad. :-(
yann@1
   253
    # Arrgghh! Some of those versions does not follow this convention:
yann@1
   254
    # gcc-3.3.3 lives in releases/gcc-3.3.3, while gcc-2.95.* isn't in a
yann@1
   255
    # subdirectory! You bastard!
yann@1
   256
    CT_GetFile "${CT_CC_CORE_FILE}"                                    \
yann@1
   257
               ftp://ftp.gnu.org/gnu/gcc/${CT_CC_CORE_FILE}            \
yann@1
   258
               ftp://ftp.gnu.org/gnu/gcc/releases/${CT_CC_CORE_FILE}   \
yann@1
   259
               ftp://ftp.gnu.org/gnu/gcc
yann@1
   260
    CT_GetFile "${CT_CC_FILE}"                                  \
yann@1
   261
               ftp://ftp.gnu.org/gnu/gcc/${CT_CC_FILE}          \
yann@1
   262
               ftp://ftp.gnu.org/gnu/gcc/releases/${CT_CC_FILE} \
yann@1
   263
               ftp://ftp.gnu.org/gnu/gcc
yann@1
   264
yann@1
   265
    # C library
yann@1
   266
    case "${CT_LIBC}" in
yann@1
   267
        glibc)
yann@1
   268
            # Ah! Not all GNU folks seem stupid. All glibc releases are in the same
yann@1
   269
            # directory. Good. Alas, there is no snapshot there. I'll deal with them
yann@1
   270
            # later on... :-/
yann@1
   271
            libc_src="ftp://ftp.gnu.org/gnu/glibc"
yann@1
   272
            ;;
yann@1
   273
        uClibc)
yann@1
   274
            # For uClibc, we have almost every thing: releases, and snapshots
yann@1
   275
            # for the last month or so. We'll have to deal with svn revisions
yann@1
   276
            # later...
yann@1
   277
            libc_src="http://www.uclibc.org/downloads
yann@1
   278
                      http://www.uclibc.org/downloads/snapshots
yann@1
   279
                      http://www.uclibc.org/downloads/old-releases"
yann@1
   280
            ;;
yann@1
   281
    esac
yann@1
   282
    CT_GetFile "${CT_LIBC_FILE}" ${libc_src}
yann@1
   283
yann@1
   284
    # C library addons
yann@1
   285
    addons_list=`echo "${CT_LIBC_ADDONS}" |sed -r -e 's/,/ /g; s/ $//g;'`
yann@1
   286
    for addon in ${addons_list}; do
yann@16
   287
        CT_GetFile "${CT_LIBC}-${addon}-${CT_LIBC_VERSION}" ${libc_src}
yann@1
   288
    done
yann@16
   289
    [ "${CT_LIBC_GLIBC_USE_PORTS}" = "y" ] && CT_GetFile "${CT_LIBC}-ports-${CT_LIBC_VERSION}" ${libc_src}
yann@16
   290
    [ "${CT_LIBC_UCLIBC_LOCALES}" = "y" ] && CT_GetFile "uClibc-locale-030818" ${libc_src}
yann@1
   291
yann@1
   292
    # libfloat if asked for
yann@1
   293
    if [ "${CT_ARCH_FLOAT_SW_LIBFLOAT}" = "y" ]; then
yann@1
   294
        lib_float_url="ftp://ftp.de.debian.org/debian/pool/main/libf/libfloat/"
yann@1
   295
yann@1
   296
        # Please note: because the file we download, and the file we store on the
yann@1
   297
        # file system don't have the same name, CT_GetFile will always try to
yann@1
   298
        # download the file over and over.
yann@1
   299
        # To avoid this, we check that the file we want already exists in the
yann@1
   300
        # tarball directory first. This is an ugly hack that overrides the standard
yann@1
   301
        # CT_GetFile behavior... Sight...
yann@1
   302
        ext=`CT_GetFileExtension "${CT_LIBFLOAT_FILE}"`
yann@1
   303
        if [ -z "${ext}" ]; then
yann@1
   304
            CT_GetFile libfloat_990616.orig "${lib_float_url}"
yann@1
   305
            ext=`CT_GetFileExtension "libfloat_990616.orig"`
yann@1
   306
            # Hack: remove the .orig extension, and change _ to -
yann@1
   307
            mv -v "${CT_TARBALLS_DIR}/libfloat_990616.orig${ext}" \
yann@1
   308
                  "${CT_TARBALLS_DIR}/libfloat-990616${ext}"      2>&1 |CT_DoLog DEBUG
yann@1
   309
        fi
yann@1
   310
    fi
yann@1
   311
    
yann@1
   312
    CT_EndStep
yann@1
   313
fi # CT_NO_DOWNLOAD
yann@1
   314
yann@1
   315
if [ "${CT_ONLY_DOWNLOAD}" != "y" ]; then
yann@1
   316
    CT_DoStep INFO "Extracting and patching toolchain components"
yann@1
   317
yann@37
   318
    if [ "${CT_KERNEL_LINUX_HEADERS_USE_CUSTOM_DIR}" != "y" ]; then
yann@37
   319
        CT_ExtractAndPatch "${CT_KERNEL_FILE}"
yann@37
   320
    fi
yann@1
   321
    CT_ExtractAndPatch "${CT_BINUTILS_FILE}"
yann@1
   322
    CT_ExtractAndPatch "${CT_CC_CORE_FILE}"
yann@1
   323
    CT_ExtractAndPatch "${CT_CC_FILE}"
yann@1
   324
    CT_ExtractAndPatch "${CT_LIBC_FILE}"
yann@1
   325
    for addon in ${addons_list}; do
yann@1
   326
        CT_ExtractAndPatch "${CT_LIBC}-${addon}-${CT_LIBC_VERSION}"
yann@1
   327
    done
yann@16
   328
    [ "${CT_LIBC_GLIBC_USE_PORTS}" = "y" ] && CT_ExtractAndPatch "${CT_LIBC}-ports-${CT_LIBC_VERSION}"
yann@16
   329
    [ "${CT_LIBC_UCLIBC_LOCALES}" = "y" ] && CT_ExtractAndPatch "uClibc-locale-030818"
yann@1
   330
yann@1
   331
    [ "${CT_ARCH_FLOAT_SW_LIBFLOAT}" = "y" ] && CT_ExtractAndPatch "${CT_LIBFLOAT_FILE}"
yann@1
   332
yann@1
   333
    CT_EndStep
yann@1
   334
fi