scripts/functions
changeset 63 89b41dbffe8d
parent 47 7e2539937b6e
child 76 5f84983926e9
     1.1 --- a/scripts/functions	Mon Apr 23 20:30:34 2007 +0000
     1.2 +++ b/scripts/functions	Mon May 07 09:04:02 2007 +0000
     1.3 @@ -218,3 +218,234 @@
     1.4  CT_DoYes() {
     1.5      yes "$1" || true
     1.6  }
     1.7 +
     1.8 +# Download an URL using wget
     1.9 +# Usage: CT_DoGetFileWget <URL>
    1.10 +CT_DoGetFileWget() {
    1.11 +    # Need to return true because it is legitimate to not find the tarball at
    1.12 +    # some of the provided URLs (think about snapshots, different layouts for
    1.13 +    # different gcc versions, etc...)
    1.14 +    # Some (very old!) FTP server might not support the passive mode, thus
    1.15 +    # retry without
    1.16 +    # With automated download as we are doing, it can be very dangerous to use
    1.17 +    # -c to continue the downloads. It's far better to simply overwrite the
    1.18 +    # destination file
    1.19 +    wget -nc --progress=dot:binary --tries=3 --passive-ftp "$1" || wget -nc --progress=dot:binary --tries=3 "$1" || true
    1.20 +}
    1.21 +
    1.22 +# Download an URL using curl
    1.23 +# Usage: CT_DoGetFileCurl <URL>
    1.24 +CT_DoGetFileCurl() {
    1.25 +	# Note: comments about wget method are also valid here
    1.26 +	# Plus: no good progreess indicator is available with curl,
    1.27 +	#       so output is consigned to oblivion
    1.28 +	curl --ftp-pasv -O --retry 3 "$1" >/dev/null || curl -O --retry 3 "$1" >/dev/null || true
    1.29 +}
    1.30 +
    1.31 +# Wrapper function to call one of curl or wget
    1.32 +# Usage: CT_DoGetFile <URL>
    1.33 +CT_DoGetFile() {
    1.34 +    local _wget=`which wget`
    1.35 +    local _curl=`which curl`
    1.36 +    case "${_wget},${_curl}" in
    1.37 +        ,)  CT_DoError "Could find neither wget nor curl";;
    1.38 +        ,*) CT_DoGetFileCurl "$1";;
    1.39 +        *)  CT_DoGetFileWget "$1";;
    1.40 +    esac
    1.41 +}
    1.42 +
    1.43 +# Download the file from one of the URLs passed as argument
    1.44 +# Usage: CT_GetFile <filename> <url> [<url> ...]
    1.45 +CT_GetFile() {
    1.46 +    local got_it
    1.47 +    local ext
    1.48 +    local url
    1.49 +    local file="$1"
    1.50 +    shift
    1.51 +
    1.52 +    # Do we already have it?
    1.53 +    ext=`CT_GetFileExtension "${file}"`
    1.54 +    if [ -n "${ext}" ]; then
    1.55 +        if [ "${CT_FORCE_DOWNLOAD}" = "y" ]; then
    1.56 +            rm -f "${CT_TARBALLS_DIR}/${file}${ext}"
    1.57 +        else
    1.58 +            return 0
    1.59 +        fi
    1.60 +    fi
    1.61 +
    1.62 +    CT_DoLog EXTRA "Retrieving \"${file}\""
    1.63 +    CT_Pushd "${CT_TARBALLS_DIR}"
    1.64 +    # File not yet downloaded, try to get it
    1.65 +    got_it=0
    1.66 +    if [ "${got_it}" != "y" ]; then
    1.67 +        # We'd rather have a bzip2'ed tarball, then gzipped, and finally plain tar.
    1.68 +        for ext in .tar.bz2 .tar.gz .tgz .tar; do
    1.69 +            # Try all urls in turn
    1.70 +            for url in "$@"; do
    1.71 +                case "${url}" in
    1.72 +                    *)  CT_DoLog EXTRA "Trying \"${url}/${file}${ext}\""
    1.73 +                        CT_DoGetFile "${url}/${file}${ext}" 2>&1 |CT_DoLog DEBUG
    1.74 +                        ;;
    1.75 +                esac
    1.76 +                [ -f "${file}${ext}" ] && got_it=1 && break 2 || true
    1.77 +            done
    1.78 +        done
    1.79 +    fi
    1.80 +    CT_Popd
    1.81 +
    1.82 +    CT_TestAndAbort "Could not download \"${file}\", and not present in \"${CT_TARBALLS_DIR}\"" ${got_it} -eq 0
    1.83 +}
    1.84 +
    1.85 +# Get the file name extension of a component
    1.86 +# Usage: CT_GetFileExtension <component_name-component_version>
    1.87 +# If found, echoes the extension to stdout
    1.88 +# If not found, echoes nothing on stdout.
    1.89 +CT_GetFileExtension() {
    1.90 +    local ext
    1.91 +    local file="$1"
    1.92 +    local got_it=1
    1.93 +
    1.94 +    CT_Pushd "${CT_TARBALLS_DIR}"
    1.95 +    for ext in .tar.gz .tar.bz2 .tgz .tar; do
    1.96 +        if [ -f "${file}${ext}" ]; then
    1.97 +            echo "${ext}"
    1.98 +            got_it=0
    1.99 +            break
   1.100 +        fi
   1.101 +    done
   1.102 +    CT_Popd
   1.103 +
   1.104 +    return 0
   1.105 +}
   1.106 +
   1.107 +# Extract a tarball and patch the resulting sources if necessary.
   1.108 +# Some tarballs need to be extracted in specific places. Eg.: glibc addons
   1.109 +# must be extracted in the glibc directory; uCLibc locales must be extracted
   1.110 +# in the extra/locale sub-directory of uClibc.
   1.111 +CT_ExtractAndPatch() {
   1.112 +    local file="$1"
   1.113 +    local base_file=`echo "${file}" |cut -d - -f 1`
   1.114 +    local ver_file=`echo "${file}" |cut -d - -f 2-`
   1.115 +    local official_patch_dir
   1.116 +    local custom_patch_dir
   1.117 +    local libc_addon
   1.118 +    local ext=`CT_GetFileExtension "${file}"`
   1.119 +    CT_TestAndAbort "\"${file}\" not found in \"${CT_TARBALLS_DIR}\"" -z "${ext}"
   1.120 +    local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
   1.121 +
   1.122 +    CT_Pushd "${CT_SRC_DIR}"
   1.123 +
   1.124 +    # Add-ons need a little love, really.
   1.125 +    case "${file}" in
   1.126 +        glibc-[a-z]*-*)
   1.127 +            CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
   1.128 +            cd "${CT_LIBC_FILE}"
   1.129 +            libc_addon=y
   1.130 +            [ -f ".${file}.extracted" ] && return 0
   1.131 +            touch ".${file}.extracted"
   1.132 +            ;;
   1.133 +        uClibc-locale-*)
   1.134 +            CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
   1.135 +            cd "${CT_LIBC_FILE}/extra/locale"
   1.136 +            libc_addon=y
   1.137 +            [ -f ".${file}.extracted" ] && return 0
   1.138 +            touch ".${file}.extracted"
   1.139 +            ;;
   1.140 +    esac
   1.141 +
   1.142 +    # If the directory exists, then consider extraction and patching done
   1.143 +    [ -d "${file}" ] && return 0
   1.144 +
   1.145 +    CT_DoLog EXTRA "Extracting \"${file}\""
   1.146 +    case "${ext}" in
   1.147 +        .tar.bz2)     tar xvjf "${full_file}" |CT_DoLog DEBUG;;
   1.148 +        .tar.gz|.tgz) tar xvzf "${full_file}" |CT_DoLog DEBUG;;
   1.149 +        .tar)         tar xvf  "${full_file}" |CT_DoLog DEBUG;;
   1.150 +        *)            CT_Abort "Don't know how to handle \"${file}\": unknown extension" ;;
   1.151 +    esac
   1.152 +
   1.153 +    # Snapshots might not have the version number in the extracted directory
   1.154 +    # name. This is also the case for some (old) packages, such as libfloat.
   1.155 +    # Overcome this issue by symlink'ing the directory.
   1.156 +    if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
   1.157 +        case "${ext}" in
   1.158 +            .tar.bz2)     base=`tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   1.159 +            .tar.gz|.tgz) base=`tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   1.160 +            .tar)         base=`tar tf  "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   1.161 +        esac
   1.162 +        CT_TestOrAbort "There was a problem when extracting \"${file}\"" -d "${base}" -o "${base}" != "${file}"
   1.163 +        ln -s "${base}" "${file}"
   1.164 +    fi
   1.165 +
   1.166 +    # Kludge: outside this function, we wouldn't know if we had just extracted
   1.167 +    # a libc addon, or a plain package. Apply patches now.
   1.168 +    CT_DoLog EXTRA "Patching \"${file}\""
   1.169 +
   1.170 +    # If libc addon, we're already in the correct place.
   1.171 +    [ -z "${libc_addon}" ] && cd "${file}"
   1.172 +
   1.173 +    [ "${CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_TOP_DIR}/patches/${base_file}/${ver_file}"
   1.174 +    [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
   1.175 +    for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
   1.176 +        if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
   1.177 +            for p in "${patch_dir}"/*.patch; do
   1.178 +                if [ -f "${p}" ]; then
   1.179 +                    CT_DoLog DEBUG "Applying patch \"${p}\""
   1.180 +                    patch -g0 -F1 -p1 -f <"${p}" |CT_DoLog DEBUG
   1.181 +                    CT_TestAndAbort "Failed while applying patch file \"${p}\"" ${PIPESTATUS[0]} -ne 0
   1.182 +                fi
   1.183 +            done
   1.184 +        fi
   1.185 +    done
   1.186 +
   1.187 +    CT_Popd
   1.188 +}
   1.189 +
   1.190 +# Compute the target triplet from what is provided by the user
   1.191 +# Usage: CT_DoBuildTargetTriplet
   1.192 +# In fact this function takes the environment variables to build the target
   1.193 +# triplet. It is needed both by the normal build sequence, as well as the
   1.194 +# sample saving sequence.
   1.195 +CT_DoBuildTargetTriplet() {
   1.196 +    case "${CT_ARCH_BE},${CT_ARCH_LE}" in
   1.197 +        y,) target_endian_eb=eb; target_endian_el=;;
   1.198 +        ,y) target_endian_eb=; target_endian_el=el;;
   1.199 +    esac
   1.200 +    case "${CT_ARCH}" in
   1.201 +        arm)  CT_TARGET="${CT_ARCH}${target_endian_eb}";;
   1.202 +        mips) CT_TARGET="${CT_ARCH}${target_endian_el}";;
   1.203 +        x86*) # Much love for this one :-(
   1.204 +              # Ultimately, we should use config.sub to output the correct
   1.205 +              # procesor name. Work for later...
   1.206 +              arch="${CT_ARCH_ARCH}"
   1.207 +              [ -z "${arch}" ] && arch="${CT_ARCH_TUNE}"
   1.208 +              case "${CT_ARCH}" in
   1.209 +                  x86_64)      CT_TARGET=x86_64;;
   1.210 +              	  *)  case "${arch}" in
   1.211 +                          "")                                       CT_TARGET=i386;;
   1.212 +                          i386|i486|i586|i686)                      CT_TARGET="${arch}";;
   1.213 +                          winchip*)                                 CT_TARGET=i486;;
   1.214 +                          pentium|pentium-mmx|c3*)                  CT_TARGET=i586;;
   1.215 +                          nocona|athlon*64|k8|athlon-fx|opteron)    CT_TARGET=x86_64;;
   1.216 +                          pentiumpro|pentium*|athlon*)              CT_TARGET=i686;;
   1.217 +                          *)                                        CT_TARGET=i586;;
   1.218 +                      esac;;
   1.219 +              esac;;
   1.220 +    esac
   1.221 +    case "${CT_TARGET_VENDOR}" in
   1.222 +        "") CT_TARGET="${CT_TARGET}-unknown";;
   1.223 +        *)  CT_TARGET="${CT_TARGET}-${CT_TARGET_VENDOR}";;
   1.224 +    esac
   1.225 +    case "${CT_KERNEL}" in
   1.226 +        linux*)  CT_TARGET="${CT_TARGET}-linux";;
   1.227 +        cygwin*) CT_TARGET="${CT_TARGET}-cygwin";;
   1.228 +    esac
   1.229 +    case "${CT_LIBC}" in
   1.230 +        glibc)  CT_TARGET="${CT_TARGET}-gnu";;
   1.231 +        uClibc) CT_TARGET="${CT_TARGET}-uclibc";;
   1.232 +    esac
   1.233 +    case "${CT_ARCH_ABI}" in
   1.234 +        eabi)   CT_TARGET="${CT_TARGET}eabi";;
   1.235 +    esac
   1.236 +    CT_TARGET="`${CT_TOP_DIR}/tools/config.sub ${CT_TARGET}`"
   1.237 +}