scripts/functions
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Mar 04 20:09:22 2007 +0000 (2007-03-04)
changeset 9 22fec39016f4
child 47 7e2539937b6e
permissions -rw-r--r--
Update i586-geode-linux-uclibc sample:
- use uClibc-0.9.28.3
- use linux-2.6.20 for kernel headers
     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 
    33 # Attributes
    34 _A_NOR="\\033[0m"
    35 _A_BRI="\\033[1m"
    36 _A_DIM="\\033[2m"
    37 _A_UND="\\033[4m"
    38 _A_BRB="\\033[5m"
    39 _A_REV="\\033[7m"
    40 _A_HID="\\033[8m"
    41 
    42 # Fore colors
    43 _F_BLK="\\033[30m"
    44 _F_RED="\\033[31m"
    45 _F_GRN="\\033[32m"
    46 _F_YEL="\\033[33m"
    47 _F_BLU="\\033[34m"
    48 _F_MAG="\\033[35m"
    49 _F_CYA="\\033[36m"
    50 _F_WHI="\\033[37m"
    51 
    52 # A function to log what is happening
    53 # Different log level are available:
    54 #   - ERROR:   A serious, fatal error occurred
    55 #   - WARN:    A non fatal, non serious error occurred, take your responsbility with the generated build
    56 #   - INFO:    Informational messages
    57 #   - EXTRA:   Extra informational messages
    58 #   - DEBUG:   Debug messages
    59 # Usage: CT_DoLog <level> [message]
    60 # If message is empty, then stdin will be logged.
    61 CT_DoLog() {
    62     local max_level
    63     local level
    64     eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
    65     # Set the maximum log level to DEBUG if we have none
    66     [ -z ${max_level} ] && max_level=${CT_LOG_LEVEL_DEBUG}
    67 
    68     local LEVEL="$1"
    69     shift
    70     eval level="\${CT_LOG_LEVEL_${LEVEL}}"
    71 
    72     if [ $# -eq 0 ]; then
    73         cat -
    74     else
    75         echo "${1}"
    76     fi |( IFS="\n" # We want the full lines, even leading spaces
    77           cpt=0
    78           indent=$((2*CT_STEP_COUNT))
    79           while read line; do
    80               l="`printf \"[%-5s]%*s%s%s\" \"${LEVEL}\" \"${indent}\" \" \" \"${line}\"`"
    81               # There will always be a log file, be it /dev/null
    82               echo -e "${l}" >>"${CT_ACTUAL_LOG_FILE}"
    83               color="CT_${LEVEL}_COLOR"
    84               normal="CT_NORMAL_COLOR"
    85               if [ ${level} -le ${max_level} ]; then
    86                   echo -e "${!color}${l}${!normal}"
    87               else
    88                   ${CT_PROG_BAR}
    89               fi
    90           done
    91         )
    92 
    93     return 0
    94 }
    95 
    96 # Abort the execution with a error message
    97 # Usage: CT_Abort <message>
    98 CT_Abort() {
    99     CT_DoLog ERROR "$1" >&2
   100     exit 1
   101 }
   102 
   103 # Test a condition, and print a message if satisfied
   104 # Usage: CT_Test <message> <tests>
   105 CT_Test() {
   106     local ret
   107     local m="$1"
   108     shift
   109     test "$@" && CT_DoLog WARN "$m"
   110     return 0
   111 }
   112 
   113 # Test a condition, and abort with an error message if satisfied
   114 # Usage: CT_TestAndAbort <message> <tests>
   115 CT_TestAndAbort() {
   116     local m="$1"
   117     shift
   118     test "$@" && CT_Abort "$m"
   119     return 0
   120 }
   121 
   122 # Test a condition, and abort with an error message if not satisfied
   123 # Usage: CT_TestAndAbort <message> <tests>
   124 CT_TestOrAbort() {
   125     local m="$1"
   126     shift
   127     test "$@" || CT_Abort "$m"
   128     return 0
   129 }
   130 
   131 # Test the presence of a tool, or abort if not found
   132 # Usage: CT_HasOrAbort <tool>
   133 CT_HasOrAbort() {
   134     CT_TestAndAbort "\"${1}\" not found and needed for successfull toolchain build." -z "`which \"${1}\"`"
   135     return 0
   136 }
   137 
   138 # Get current date with nanosecond precision
   139 # On those system not supporting nanosecond precision, faked with rounding down
   140 # to the highest entire second
   141 # Usage: CT_DoDate <fmt>
   142 CT_DoDate() {
   143     date "$1" |sed -r -e 's/%N$/000000000/;'
   144 }
   145 
   146 CT_STEP_COUNT=1
   147 CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
   148 # Memorise a step being done so that any error is caught
   149 # Usage: CT_DoStep <loglevel> <message>
   150 CT_DoStep() {
   151     local start=`CT_DoDate +%s%N`
   152     CT_DoLog "$1" "================================================================="
   153     CT_DoLog "$1" "$2"
   154     CT_STEP_COUNT=$((CT_STEP_COUNT+1))
   155     CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
   156     CT_STEP_START[${CT_STEP_COUNT}]="${start}"
   157     CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
   158     return 0
   159 }
   160 
   161 # End the step just being done
   162 # Usage: CT_EndStep
   163 CT_EndStep() {
   164     local stop=`CT_DoDate +%s%N`
   165     local duration=`printf "%032d" $((stop-${CT_STEP_START[${CT_STEP_COUNT}]})) |sed -r -e 's/([[:digit:]]{2})[[:digit:]]{7}$/\.\1/; s/^0+//; s/^\./0\./;'`
   166     local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
   167     local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
   168     CT_STEP_COUNT=$((CT_STEP_COUNT-1))
   169     CT_DoLog "${level}" "${message}: done in ${duration}s"
   170     return 0
   171 }
   172 
   173 # Pushes into a directory, and pops back
   174 CT_Pushd() {
   175     pushd "$1" >/dev/null 2>&1
   176 }
   177 CT_Popd() {
   178     popd >/dev/null 2>&1
   179 }
   180 
   181 # Makes a path absolute
   182 # Usage: CT_MakeAbsolutePath path
   183 CT_MakeAbsolutePath() {
   184     # Try to cd in that directory
   185     if [ -d "$1" ]; then
   186         CT_Pushd "$1"
   187         pwd
   188         CT_Popd
   189     else
   190         # No such directory, fail back to guessing
   191         case "$1" in
   192             /*)  echo "$1";;
   193             *)   echo "`pwd`/$1";;
   194         esac
   195     fi
   196     
   197     return 0
   198 }
   199 
   200 # Creates a temporary directory
   201 # $1: variable to assign to
   202 # Usage: CT_MktempDir foo
   203 CT_MktempDir() {
   204     # Some mktemp do not allow more than 6 Xs
   205     eval "$1"="`mktemp -q -d \"${CT_BUILD_DIR}/.XXXXXX\"`"
   206     CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
   207 }
   208 
   209 # Echoes the specified string on stdout until the pipe breaks.
   210 # Doesn't fail
   211 # $1: string to echo
   212 # Usage: CT_DoYes "" |make oldconfig
   213 CT_DoYes() {
   214     yes "$1" || true
   215 }