Merge first shot from the MIPS branch.
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
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}]}\""
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}]}\""
15 CT_DoLog ERROR "Look at \"${CT_ACTUAL_LOG_FILE}\" for more info on this error."
23 # This is crosstool-ng-0.0.1
26 # The different log levels:
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.
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}
70 eval level="\${CT_LOG_LEVEL_${LEVEL}}"
76 fi |( IFS="\n" # We want the full lines, even leading spaces
78 indent=$((2*CT_STEP_COUNT))
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}"
96 # Abort the execution with a error message
97 # Usage: CT_Abort <message>
99 CT_DoLog ERROR "$1" >&2
103 # Test a condition, and print a message if satisfied
104 # Usage: CT_Test <message> <tests>
109 test "$@" && CT_DoLog WARN "$m"
113 # Test a condition, and abort with an error message if satisfied
114 # Usage: CT_TestAndAbort <message> <tests>
118 test "$@" && CT_Abort "$m"
122 # Test a condition, and abort with an error message if not satisfied
123 # Usage: CT_TestAndAbort <message> <tests>
127 test "$@" || CT_Abort "$m"
131 # Test the presence of a tool, or abort if not found
132 # Usage: CT_HasOrAbort <tool>
134 CT_TestAndAbort "\"${1}\" not found and needed for successfull toolchain build." -z "`which \"${1}\"`"
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>
143 date "$1" |sed -r -e 's/%N$/000000000/;'
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>
151 local start=`CT_DoDate +%s%N`
152 CT_DoLog "$1" "================================================================="
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"
161 # End the step just being done
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"
173 # Pushes into a directory, and pops back
175 pushd "$1" >/dev/null 2>&1
181 # Makes a path absolute
182 # Usage: CT_MakeAbsolutePath path
183 CT_MakeAbsolutePath() {
184 # Try to cd in that directory
190 # No such directory, fail back to guessing
200 # Creates a temporary directory
201 # $1: variable to assign to
202 # Usage: CT_MktempDir foo
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}"
209 # Echoes the specified string on stdout until the pipe breaks.
212 # Usage: CT_DoYes "" |make oldconfig