Commit patches #91453, #91481 from bug #139866.
[genkernel.git] / gen_funcs.sh
index d4bba7b145b5e65a4f43dea20c3c59312be8aca3..d419e0d386d29cc9ba639a65a5e3990a52061c8c 100755 (executable)
@@ -21,29 +21,27 @@ isTrue() {
        return 1
 }
 
-
 if isTrue ${USECOLOR}
 then
-#      COLS="`stty size 2> /dev/null`"
-#        COLS="`getcols ${COLS}`"
-#        COLS=$((${COLS} - 7))
-#        ENDCOL=$'\e[A\e['${COLS}'G'    # Now, ${ENDCOL} will move us to the end of the
-                                       # column;  irregardless of character width
        GOOD=$'\e[32;01m'
        WARN=$'\e[33;01m'
        BAD=$'\e[31;01m'
        NORMAL=$'\e[0m'
-       HILITE=$'\e[36;01m'
-       BRACKET=$'\e[34;01m'
+       BOLD=$'\e[0;01m'
+       UNDER=$'\e[4m'
 else
-       GOOD=""
-       WARN=""
-       BAD=""
-       NORMAL=""
-       HILITE=""
-       BRACKET=""
+       GOOD=''
+       WARN=''
+       BAD=''
+       NORMAL=''
+       BOLD=''
+       UNDER=''
 fi
 
+dump_debugcache() {
+       TODEBUGCACHE=0
+       echo "${DEBUGCACHE}" >> ${DEBUGFILE}
+}
 
 # print_info(debuglevel, print [, newline [, prefixline [, forcefile ] ] ])
 print_info() {
@@ -51,42 +49,42 @@ print_info() {
        local FORCEFILE=0
        local PREFIXLINE=1
        local SCRPRINT=0
-       local STR=""
+       local STR=''
 
        # NOT ENOUGH ARGS
-       if [ "$#" -lt "2" ] ; then return 1; fi
+       if [ "$#" -lt '2' ] ; then return 1; fi
 
        # IF 3 OR MORE ARGS, CHECK IF WE WANT A NEWLINE AFTER PRINT
-       if [ "$#" -gt "2" ]
+       if [ "$#" -gt '2' ]
        then
                if isTrue "$3"
                then
-                       NEWLINE="1";
+                       NEWLINE='1';
                else
-                       NEWLINE="0";
+                       NEWLINE='0';
                fi
        fi
 
        # IF 4 OR MORE ARGS, CHECK IF WE WANT TO PREFIX WITH A *
-       if [ "$#" -gt "3" ]
+       if [ "$#" -gt '3' ]
        then
                if isTrue "$4"
                then
-                       PREFIXLINE="1"
+                       PREFIXLINE='1'
                else
-                       PREFIXLINE="0"
+                       PREFIXLINE='0'
                fi
        fi
 
        # IF 5 OR MORE ARGS, CHECK IF WE WANT TO FORCE OUTPUT TO DEBUG
        # FILE EVEN IF IT DOESN'T MEET THE MINIMUM DEBUG REQS
-       if [ "$#" -gt "4" ]
+       if [ "$#" -gt '4' ]
        then
                if isTrue "$5"
                then
-                       FORCEFILE="1"
+                       FORCEFILE='1'
                else
-                       FORCEFILE="0"
+                       FORCEFILE='0'
                fi
        fi
 
@@ -94,26 +92,26 @@ print_info() {
        # OR EQUAL TO SET DEBUG LEVEL
        if [ "$1" -lt "${DEBUGLEVEL}" -o "$1" -eq "${DEBUGLEVEL}" ]
        then
-               SCRPRINT="1"
+               SCRPRINT='1'
        fi
 
        # RETURN IF NOT OUTPUTTING ANYWHERE
-       if [ "${SCRPRINT}" != "1" -a "${FORCEFILE}" != "1" ]
+       if [ "${SCRPRINT}" != '1' -a "${FORCEFILE}" != '1' ]
        then
                return 0
        fi
 
        # STRUCTURE DATA TO BE OUTPUT TO SCREEN, AND OUTPUT IT
-       if [ "${SCRPRINT}" -eq "1" ]
+       if [ "${SCRPRINT}" -eq '1' ]
        then
-               if [ "${PREFIXLINE}" = "1" ]
+               if [ "${PREFIXLINE}" = '1' ]
                then
                        STR="${GOOD}*${NORMAL} ${2}"
                else
                        STR="${2}"
                fi
 
-               if [ "${NEWLINE}" = "0" ]
+               if [ "${NEWLINE}" = '0' ]
                then
                        echo -ne "${STR}"
                else
@@ -122,54 +120,127 @@ print_info() {
        fi
 
        # STRUCTURE DATA TO BE OUTPUT TO FILE, AND OUTPUT IT
-       if [ "${SCRPRINT}" -eq "1" -o "${FORCEFILE}" -eq "1" ]
+       if [ "${SCRPRINT}" -eq '1' -o "${FORCEFILE}" -eq '1' ]
        then
-               if [ "${PREFIXLINE}" = "1" ]
+               STRR=${2//${WARN}/}
+               STRR=${STRR//${BAD}/}
+               STRR=${STRR//${BOLD}/}
+               STRR=${STRR//${NORMAL}/}
+
+               if [ "${PREFIXLINE}" = '1' ]
                then
-                       STR="* ${2}"
+                       STR="* ${STRR}"
                else
-                       STR="${2}"
+                       STR="${STRR}"
                fi
 
-               if [ "${NEWLINE}" = "0" ]
+               if [ "${NEWLINE}" = '0' ]
                then
-                       echo -ne "${STR}" >> ${DEBUGFILE}
+                       if [ "${TODEBUGCACHE}" -eq 1 ]; then
+                               DEBUGCACHE="${DEBUGCACHE}${STR}"
+                       else
+                               echo -ne "${STR}" >> ${DEBUGFILE}
+                       fi      
                else
-                       echo "${STR}" >> ${DEBUGFILE}
+                       if [ "${TODEBUGCACHE}" -eq 1 ]; then
+                               DEBUGCACHE="${DEBUGCACHE}${STR}"$'\n'
+                       else
+                               echo "${STR}" >> ${DEBUGFILE}
+                       fi
                fi
        fi
 
        return 0
 }
 
+print_error()
+{
+       GOOD=${BAD} print_info "$@"
+}
+
+print_warning()
+{
+       GOOD=${WARN} print_info "$@"
+}
+
 # var_replace(var_name, var_value, string)
 # $1 = variable name
 # $2 = variable value
 # $3 = string
+
 var_replace()
 {
-  echo "${3}" | sed -e "s/%%${1}%%/${2}/g" -
+  # Escape '\' and '.' in $2 to make it safe to use
+  # in the later sed expression
+  local SAFE_VAR
+  SAFE_VAR=`echo "${2}" | sed -e 's/\([\/\.]\)/\\\\\\1/g'`
+  
+  echo "${3}" | sed -e "s/%%${1}%%/${SAFE_VAR}/g" -
 }
 
 arch_replace() {
   var_replace "ARCH" "${ARCH}" "${1}"
 }
 
+kv_replace() {
+  var_replace "KV" "${KV}" "${1}"
+}
+
+cache_replace() {
+  var_replace "CACHE" "${CACHE_DIR}" "${1}"
+}
+
 clear_log() {
-  rm -f ${DEBUGFILE}
-  touch ${DEBUGFILE}
+    if [ -f "${DEBUGFILE}" ]
+    then
+       (echo > "${DEBUGFILE}") 2>/dev/null || small_die "Genkernel: Could not write to ${DEBUGFILE}."
+    fi   
 }
 
 gen_die() {
-       if [ "$#" -gt "0" ]
+       dump_debugcache
+
+       if [ "$#" -gt '0' ]
        then
-               print_info 1 "gen_die(): ${1}"
+               print_error 1 "ERROR: ${1}"
+       fi
+       echo
+       print_info 1 "-- Grepping log... --"
+       echo
+
+       if isTrue ${USECOLOR}
+       then
+               GREP_COLOR='1' grep -B5 -E --colour=always "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${DEBUGFILE}
+       else
+               grep -B5 -E "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${DEBUGFILE}
        fi
+       echo
+       print_info 1 "-- End log... --"
+       echo
+       print_info 1 "Please consult ${DEBUGFILE} for more information and any"
+       print_info 1 "errors that were reported above."
+       echo
+       print_info 1 "Report any genkernel bugs to bugs.gentoo.org and"
+       print_info 1 "assign your bug to genkernel@gentoo.org. Please include"
+       print_info 1 "as much information as you can in your bug report; attaching"
+       print_info 1 "${DEBUGFILE} so that your issue can be dealt with effectively."
+       print_info 1 ''
+       print_info 1 'Please do *not* report compilation failures as genkernel bugs!'
+       print_info 1 ''
+
+       # Cleanup temp dirs and caches if requested
+       cleanup
        exit 1
 }
 
 has_loop() {
-       if [ -e "/dev/loop0" -o -e "/dev/loop/0" ]
+       if [ -e /var/log/dmesg ]
+       then
+               cat /var/log/dmesg | egrep -q '^loop:'
+       else
+               dmesg | egrep -q '^loop:'
+       fi
+       if [ -e '/dev/loop0' -o -e '/dev/loop/0' -a ${PIPESTATUS[1]} ]
        then
                # We found devfs or standard dev loop device, assume
                # loop is compiled into the kernel or the module is loaded
@@ -178,3 +249,41 @@ has_loop() {
                return 1
        fi
 }
+
+isBootRO()
+{
+       return $(awk '( $2 == "'${BOOTDIR}'" && $4 ~ /(^|,)ro(,|$)/){ I=1; exit }END{print !I }' /proc/mounts);
+}
+
+setup_cache_dir()
+{
+
+[ ! -d "${CACHE_DIR}" ] && mkdir -p "${CACHE_DIR}"
+
+if [ "${CLEAR_CACHE_DIR}" == 'yes' ]
+then
+       print_info 1 "Clearing cache dir contents from ${CACHE_DIR}"
+       CACHE_DIR_CONTENTS=`ls ${CACHE_DIR}|grep -v CVS|grep -v cpio|grep -v README`
+       
+       for i in ${CACHE_DIR_CONTENTS}
+       do
+               print_info 1 "   >> removing ${i}"
+               rm ${CACHE_DIR}/${i}
+       done
+fi
+
+}
+
+clear_tmpdir()
+{
+if ! isTrue ${CMD_NOINSTALL}
+then
+    TMPDIR_CONTENTS=`ls ${TMPDIR}`
+    print_info 1 "Removing tmp dir contents"
+    for i in ${TMPDIR_CONTENTS}
+    do
+       print_info 1 "   >> removing ${i}"
+       rm ${TMPDIR}/${i}
+    done
+fi
+}