scripts/functions
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Thu May 10 21:33:35 2007 +0000 (2007-05-10)
changeset 85 ac2845835b13
parent 82 a1c93f975268
child 88 f67b52e42fd1
permissions -rw-r--r--
Update the way we handle directories supplied by the user:
- the tarball directory is considered as a local copy, and tarballs are copied to a working area,
- the sources and build directories (CT_SRC_DIR and CT_BUILD_DIR) are now computed, and no longer an option,
- the build dir has been renamed from 'build' to 'targets'.
That should ease preparing a tarball of the resulting target.
     1 # This file contains some usefull common functions
     2 # Copyright 2007 Yann E. MORIN
     3 # Licensed under the GPL v2. See COPYING in the root of this package
     4 
     5 CT_OnError() {
     6     ret=$?
     7     CT_DoLog ERROR "Build failed in step \"${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}\""
     8     for((step=(CT_STEP_COUNT-1); step>1; step--)); do
     9         CT_DoLog ERROR "      called in step \"${CT_STEP_MESSAGE[${step}]}\""
    10     done
    11     CT_DoLog ERROR "Error happened in \"${BASH_SOURCE[1]}\" in function \"${FUNCNAME[1]}\" (line unknown, sorry)"
    12     for((depth=2; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
    13         CT_DoLog ERROR "      called from \"${BASH_SOURCE[${depth}]}\" at line # ${BASH_LINENO[${depth}-1]} in function \"${FUNCNAME[${depth}]}\""
    14     done
    15     CT_DoLog ERROR "Look at \"${CT_ACTUAL_LOG_FILE}\" for more info on this error."
    16     exit $ret
    17 }
    18 trap CT_OnError ERR
    19 
    20 set -E
    21 set -o pipefail
    22 
    23 # This is crosstool-ng-0.0.1
    24 CT_VERSION=ng-0.0.1
    25 
    26 # The different log levels:
    27 CT_LOG_LEVEL_ERROR=0
    28 CT_LOG_LEVEL_WARN=1
    29 CT_LOG_LEVEL_INFO=2
    30 CT_LOG_LEVEL_EXTRA=3
    31 CT_LOG_LEVEL_DEBUG=4
    32 CT_LOG_LEVEL_ALL=5
    33 
    34 # Attributes
    35 _A_NOR="\\033[0m"
    36 _A_BRI="\\033[1m"
    37 _A_DIM="\\033[2m"
    38 _A_UND="\\033[4m"
    39 _A_BRB="\\033[5m"
    40 _A_REV="\\033[7m"
    41 _A_HID="\\033[8m"
    42 
    43 # Fore colors
    44 _F_BLK="\\033[30m"
    45 _F_RED="\\033[31m"
    46 _F_GRN="\\033[32m"
    47 _F_YEL="\\033[33m"
    48 _F_BLU="\\033[34m"
    49 _F_MAG="\\033[35m"
    50 _F_CYA="\\033[36m"
    51 _F_WHI="\\033[37m"
    52 
    53 # A function to log what is happening
    54 # Different log level are available:
    55 #   - ERROR:   A serious, fatal error occurred
    56 #   - WARN:    A non fatal, non serious error occurred, take your responsbility with the generated build
    57 #   - INFO:    Informational messages
    58 #   - EXTRA:   Extra informational messages
    59 #   - DEBUG:   Debug messages
    60 #   - ALL:     Component's build messages
    61 # Usage: CT_DoLog <level> [message]
    62 # If message is empty, then stdin will be logged.
    63 CT_DoLog() {
    64     local max_level LEVEL level cur_l cur_L
    65     local l
    66     eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
    67     # Set the maximum log level to DEBUG if we have none
    68     [ -z "${max_level}" ] && max_level=${CT_LOG_LEVEL_DEBUG}
    69 
    70     LEVEL="$1"; shift
    71     eval level="\${CT_LOG_LEVEL_${LEVEL}}"
    72 
    73     if [ $# -eq 0 ]; then
    74         cat -
    75     else
    76         echo "${1}"
    77     fi |( IFS="\n" # We want the full lines, even leading spaces
    78           CT_PROG_BAR_CPT=0
    79           indent=$((2*CT_STEP_COUNT))
    80           while read line; do
    81               case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
    82                 y,*"warning:"*)         cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
    83                 *"error:"*)             cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
    84                 *"make["?*"]:"*"Stop.") cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
    85                 *)                      cur_L="${LEVEL}"; cur_l="${level}";;
    86               esac
    87               l="`printf \"[%-5s]%*s%s%s\" \"${cur_L}\" \"${indent}\" \" \" \"${line}\"`"
    88               # There will always be a log file, be it /dev/null
    89               echo -e "${l}" >>"${CT_ACTUAL_LOG_FILE}"
    90               color="CT_${cur_L}_COLOR"
    91               normal="CT_NORMAL_COLOR"
    92               if [ ${cur_l} -le ${max_level} ]; then
    93                   echo -e "\r${!color}${l}${!normal}"
    94               fi
    95               if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
    96                   str=`CT_DoDate +%s`
    97                   elapsed=$((str-(CT_STAR_DATE/(1000*1000*1000))))
    98                   [ ${CT_PROG_BAR_CPT} -eq 0  ] && bar="/"
    99                   [ ${CT_PROG_BAR_CPT} -eq 10 ] && bar="-"
   100                   [ ${CT_PROG_BAR_CPT} -eq 20 ] && bar="\\"
   101                   [ ${CT_PROG_BAR_CPT} -eq 30 ] && bar="|"
   102                   printf "\r[%02d:%02d] %s " $((elapsed/60)) $((elapsed%60)) "${bar}"
   103                   CT_PROG_BAR_CPT=$(((CT_PROG_BAR_CPT+1)%40))
   104               fi
   105           done
   106         )
   107 
   108     return 0
   109 }
   110 
   111 # Abort the execution with a error message
   112 # Usage: CT_Abort <message>
   113 CT_Abort() {
   114     CT_DoLog ERROR "$1" >&2
   115     exit 1
   116 }
   117 
   118 # Test a condition, and print a message if satisfied
   119 # Usage: CT_Test <message> <tests>
   120 CT_Test() {
   121     local ret
   122     local m="$1"
   123     shift
   124     test "$@" && CT_DoLog WARN "$m"
   125     return 0
   126 }
   127 
   128 # Test a condition, and abort with an error message if satisfied
   129 # Usage: CT_TestAndAbort <message> <tests>
   130 CT_TestAndAbort() {
   131     local m="$1"
   132     shift
   133     test "$@" && CT_Abort "$m"
   134     return 0
   135 }
   136 
   137 # Test a condition, and abort with an error message if not satisfied
   138 # Usage: CT_TestAndAbort <message> <tests>
   139 CT_TestOrAbort() {
   140     local m="$1"
   141     shift
   142     test "$@" || CT_Abort "$m"
   143     return 0
   144 }
   145 
   146 # Test the presence of a tool, or abort if not found
   147 # Usage: CT_HasOrAbort <tool>
   148 CT_HasOrAbort() {
   149     CT_TestAndAbort "\"${1}\" not found and needed for successfull toolchain build." -z "`which \"${1}\"`"
   150     return 0
   151 }
   152 
   153 # Get current date with nanosecond precision
   154 # On those system not supporting nanosecond precision, faked with rounding down
   155 # to the highest entire second
   156 # Usage: CT_DoDate <fmt>
   157 CT_DoDate() {
   158     date "$1" |sed -r -e 's/%N$/000000000/;'
   159 }
   160 
   161 CT_STEP_COUNT=1
   162 CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
   163 # Memorise a step being done so that any error is caught
   164 # Usage: CT_DoStep <loglevel> <message>
   165 CT_DoStep() {
   166     local start=`CT_DoDate +%s%N`
   167     CT_DoLog "$1" "================================================================="
   168     CT_DoLog "$1" "$2"
   169     CT_STEP_COUNT=$((CT_STEP_COUNT+1))
   170     CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
   171     CT_STEP_START[${CT_STEP_COUNT}]="${start}"
   172     CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
   173     return 0
   174 }
   175 
   176 # End the step just being done
   177 # Usage: CT_EndStep
   178 CT_EndStep() {
   179     local stop=`CT_DoDate +%s%N`
   180     local duration=`printf "%032d" $((stop-${CT_STEP_START[${CT_STEP_COUNT}]})) |sed -r -e 's/([[:digit:]]{2})[[:digit:]]{7}$/\.\1/; s/^0+//; s/^\./0\./;'`
   181     local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
   182     local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
   183     CT_STEP_COUNT=$((CT_STEP_COUNT-1))
   184     CT_DoLog "${level}" "${message}: done in ${duration}s"
   185     return 0
   186 }
   187 
   188 # Pushes into a directory, and pops back
   189 CT_Pushd() {
   190     pushd "$1" >/dev/null 2>&1
   191 }
   192 CT_Popd() {
   193     popd >/dev/null 2>&1
   194 }
   195 
   196 # Makes a path absolute
   197 # Usage: CT_MakeAbsolutePath path
   198 CT_MakeAbsolutePath() {
   199     # Try to cd in that directory
   200     if [ -d "$1" ]; then
   201         CT_Pushd "$1"
   202         pwd
   203         CT_Popd
   204     else
   205         # No such directory, fail back to guessing
   206         case "$1" in
   207             /*)  echo "$1";;
   208             *)   echo "`pwd`/$1";;
   209         esac
   210     fi
   211     
   212     return 0
   213 }
   214 
   215 # Creates a temporary directory
   216 # $1: variable to assign to
   217 # Usage: CT_MktempDir foo
   218 CT_MktempDir() {
   219     # Some mktemp do not allow more than 6 Xs
   220     eval "$1"="`mktemp -q -d \"${CT_BUILD_DIR}/.XXXXXX\"`"
   221     CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
   222 }
   223 
   224 # Echoes the specified string on stdout until the pipe breaks.
   225 # Doesn't fail
   226 # $1: string to echo
   227 # Usage: CT_DoYes "" |make oldconfig
   228 CT_DoYes() {
   229     yes "$1" || true
   230 }
   231 
   232 # Get the file name extension of a component
   233 # Usage: CT_GetFileExtension <component_name-component_version>
   234 # If found, echoes the extension to stdout
   235 # If not found, echoes nothing on stdout.
   236 CT_GetFileExtension() {
   237     local ext
   238     local file="$1"
   239     local got_it=1
   240 
   241     CT_Pushd "${CT_TARBALLS_DIR}"
   242     for ext in .tar.gz .tar.bz2 .tgz .tar; do
   243         if [ -f "${file}${ext}" ]; then
   244             echo "${ext}"
   245             got_it=0
   246             break
   247         fi
   248     done
   249     CT_Popd
   250 
   251     return 0
   252 }
   253 
   254 # Download an URL using wget
   255 # Usage: CT_DoGetFileWget <URL>
   256 CT_DoGetFileWget() {
   257     # Need to return true because it is legitimate to not find the tarball at
   258     # some of the provided URLs (think about snapshots, different layouts for
   259     # different gcc versions, etc...)
   260     # Some (very old!) FTP server might not support the passive mode, thus
   261     # retry without
   262     # With automated download as we are doing, it can be very dangerous to use
   263     # -c to continue the downloads. It's far better to simply overwrite the
   264     # destination file
   265     wget -nc --progress=dot:binary --tries=3 --passive-ftp "$1" || wget -nc --progress=dot:binary --tries=3 "$1" || true
   266 }
   267 
   268 # Download an URL using curl
   269 # Usage: CT_DoGetFileCurl <URL>
   270 CT_DoGetFileCurl() {
   271 	# Note: comments about wget method are also valid here
   272 	# Plus: no good progreess indicator is available with curl,
   273 	#       so output is consigned to oblivion
   274 	curl --ftp-pasv -O --retry 3 "$1" >/dev/null || curl -O --retry 3 "$1" >/dev/null || true
   275 }
   276 
   277 # Wrapper function to call one of curl or wget
   278 # Usage: CT_DoGetFile <URL>
   279 CT_DoGetFile() {
   280     local _wget=`which wget`
   281     local _curl=`which curl`
   282     case "${_wget},${_curl}" in
   283         ,)  CT_DoError "Could find neither wget nor curl";;
   284         ,*) CT_DoGetFileCurl "$1" 2>&1 |CT_DoLog DEBUG;;
   285         *)  CT_DoGetFileWget "$1" 2>&1 |CT_DoLog DEBUG;;
   286     esac
   287 }
   288 
   289 # Download the file from one of the URLs passed as argument
   290 # Usage: CT_GetFile <filename> <url> [<url> ...]
   291 CT_GetFile() {
   292     local got_it
   293     local ext
   294     local url
   295     local file="$1"
   296     shift
   297 
   298     # Do we already have it?
   299     ext=`CT_GetFileExtension "${file}"`
   300     if [ -n "${ext}" ]; then
   301         CT_DoLog DEBUG "Already have \"${file}\""
   302         return 0
   303     fi
   304 
   305     CT_DoLog EXTRA "Retrieving \"${file}\""
   306     CT_Pushd "${CT_TARBALLS_DIR}"
   307     # File not yet downloaded, try to get it
   308     got_it=0
   309     # We'd rather have a bzip2'ed tarball, then gzipped, and finally plain tar.
   310     for ext in .tar.bz2 .tar.gz .tgz .tar; do
   311         if [ ${got_it} -ne 1 ]; then
   312             # Try local copy first, if it exists
   313             if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
   314                  "${CT_FORCE_DOWNLOAD}" != "y" ]; then
   315                 cp -v "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog DEBUG
   316                 got_it=1
   317                 break 1
   318             else
   319                 # Try all urls in turn
   320                 for url in "$@"; do
   321                     case "${url}" in
   322                         *)  CT_DoLog DEBUG "Trying \"${url}/${file}${ext}\""
   323                             CT_DoGetFile "${url}/${file}${ext}"
   324                             ;;
   325                     esac
   326                     [ -f "${file}${ext}" ] && got_it=1 && break 2 || true
   327                 done
   328             fi
   329         fi
   330     done
   331     CT_Popd
   332 
   333     CT_TestAndAbort "Could not download \"${file}\", and not present in \"${CT_LOCAL_TARBALLS_DIR}\"" ${got_it} -eq 0
   334 }
   335 
   336 # Extract a tarball and patch the resulting sources if necessary.
   337 # Some tarballs need to be extracted in specific places. Eg.: glibc addons
   338 # must be extracted in the glibc directory; uCLibc locales must be extracted
   339 # in the extra/locale sub-directory of uClibc.
   340 CT_ExtractAndPatch() {
   341     local file="$1"
   342     local base_file=`echo "${file}" |cut -d - -f 1`
   343     local ver_file=`echo "${file}" |cut -d - -f 2-`
   344     local official_patch_dir
   345     local custom_patch_dir
   346     local libc_addon
   347     local ext=`CT_GetFileExtension "${file}"`
   348     CT_TestAndAbort "\"${file}\" not found in \"${CT_TARBALLS_DIR}\"" -z "${ext}"
   349     local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
   350 
   351     CT_Pushd "${CT_SRC_DIR}"
   352 
   353     # Add-ons need a little love, really.
   354     case "${file}" in
   355         glibc-[a-z]*-*)
   356             CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
   357             cd "${CT_LIBC_FILE}"
   358             libc_addon=y
   359             [ -f ".${file}.extracted" ] && return 0
   360             touch ".${file}.extracted"
   361             ;;
   362         uClibc-locale-*)
   363             CT_TestAndAbort "Trying to extract the C-library addon/locales \"${file}\" when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
   364             cd "${CT_LIBC_FILE}/extra/locale"
   365             libc_addon=y
   366             [ -f ".${file}.extracted" ] && return 0
   367             touch ".${file}.extracted"
   368             ;;
   369     esac
   370 
   371     # If the directory exists, then consider extraction and patching done
   372     if [ -d "${file}" ]; then
   373         CT_DoLog DEBUG "Already extracted \"${file}\""
   374         return 0
   375     fi
   376 
   377     CT_DoLog EXTRA "Extracting \"${file}\""
   378     case "${ext}" in
   379         .tar.bz2)     tar xvjf "${full_file}" |CT_DoLog ALL;;
   380         .tar.gz|.tgz) tar xvzf "${full_file}" |CT_DoLog ALL;;
   381         .tar)         tar xvf  "${full_file}" |CT_DoLog ALL;;
   382         *)            CT_Abort "Don't know how to handle \"${file}\": unknown extension" ;;
   383     esac
   384 
   385     # Snapshots might not have the version number in the extracted directory
   386     # name. This is also the case for some (old) packages, such as libfloat.
   387     # Overcome this issue by symlink'ing the directory.
   388     if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
   389         case "${ext}" in
   390             .tar.bz2)     base=`tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   391             .tar.gz|.tgz) base=`tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   392             .tar)         base=`tar tf  "${full_file}" |head -n 1 |cut -d / -f 1 || true`;;
   393         esac
   394         CT_TestOrAbort "There was a problem when extracting \"${file}\"" -d "${base}" -o "${base}" != "${file}"
   395         ln -s "${base}" "${file}"
   396     fi
   397 
   398     # Kludge: outside this function, we wouldn't know if we had just extracted
   399     # a libc addon, or a plain package. Apply patches now.
   400     CT_DoLog EXTRA "Patching \"${file}\""
   401 
   402     # If libc addon, we're already in the correct place.
   403     [ -z "${libc_addon}" ] && cd "${file}"
   404 
   405     [ "${CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_TOP_DIR}/patches/${base_file}/${ver_file}"
   406     [ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
   407     for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
   408         if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
   409             for p in "${patch_dir}"/*.patch; do
   410                 if [ -f "${p}" ]; then
   411                     CT_DoLog DEBUG "Applying patch \"${p}\""
   412                     patch -g0 -F1 -p1 -f <"${p}" |CT_DoLog ALL
   413                     CT_TestAndAbort "Failed while applying patch file \"${p}\"" ${PIPESTATUS[0]} -ne 0
   414                 fi
   415             done
   416         fi
   417     done
   418 
   419     CT_Popd
   420 }
   421 
   422 # Compute the target triplet from what is provided by the user
   423 # Usage: CT_DoBuildTargetTriplet
   424 # In fact this function takes the environment variables to build the target
   425 # triplet. It is needed both by the normal build sequence, as well as the
   426 # sample saving sequence.
   427 CT_DoBuildTargetTriplet() {
   428     case "${CT_ARCH_BE},${CT_ARCH_LE}" in
   429         y,) target_endian_eb=eb; target_endian_el=;;
   430         ,y) target_endian_eb=; target_endian_el=el;;
   431     esac
   432     case "${CT_ARCH}" in
   433         arm)  CT_TARGET="${CT_ARCH}${target_endian_eb}";;
   434         mips) CT_TARGET="${CT_ARCH}${target_endian_el}";;
   435         x86*) # Much love for this one :-(
   436               # Ultimately, we should use config.sub to output the correct
   437               # procesor name. Work for later...
   438               arch="${CT_ARCH_ARCH}"
   439               [ -z "${arch}" ] && arch="${CT_ARCH_TUNE}"
   440               case "${CT_ARCH}" in
   441                   x86_64)      CT_TARGET=x86_64;;
   442               	  *)  case "${arch}" in
   443                           "")                                       CT_TARGET=i386;;
   444                           i386|i486|i586|i686)                      CT_TARGET="${arch}";;
   445                           winchip*)                                 CT_TARGET=i486;;
   446                           pentium|pentium-mmx|c3*)                  CT_TARGET=i586;;
   447                           nocona|athlon*64|k8|athlon-fx|opteron)    CT_TARGET=x86_64;;
   448                           pentiumpro|pentium*|athlon*)              CT_TARGET=i686;;
   449                           *)                                        CT_TARGET=i586;;
   450                       esac;;
   451               esac;;
   452     esac
   453     case "${CT_TARGET_VENDOR}" in
   454         "") CT_TARGET="${CT_TARGET}-unknown";;
   455         *)  CT_TARGET="${CT_TARGET}-${CT_TARGET_VENDOR}";;
   456     esac
   457     case "${CT_KERNEL}" in
   458         linux*)  CT_TARGET="${CT_TARGET}-linux";;
   459         cygwin*) CT_TARGET="${CT_TARGET}-cygwin";;
   460     esac
   461     case "${CT_LIBC}" in
   462         glibc)  CT_TARGET="${CT_TARGET}-gnu";;
   463         uClibc) CT_TARGET="${CT_TARGET}-uclibc";;
   464     esac
   465     case "${CT_ARCH_ABI}" in
   466         eabi)   CT_TARGET="${CT_TARGET}eabi";;
   467     esac
   468     CT_TARGET="`${CT_TOP_DIR}/tools/config.sub ${CT_TARGET}`"
   469 }