Integrate patches inside git repository
[hvlinux.git] / functions / main
index c8d2356..2460314 100644 (file)
@@ -2,6 +2,10 @@
 set -o errtrace # Let shell functions inherit ERR trap.
 set -o errexit
 
+# Don' t locate and remember (hash) commands as they are looked up for
+# execution (don' t remember path):
+set +h
+
 hvtrap_setup()
 {
     # Setting ERR trap does implicit `set -o errexit'.
@@ -47,6 +51,17 @@ fi
 # Load stage number definition
 source ./stage.def
 
+if [ -z "${LFS_STAGE}" ]; then
+    echo "LFS_STAGE is undefined (see stage.def)"
+    return ${EXIT_FAILURE}
+fi
+
+if [ "x${LFS_STAGE}" != "xstage0" -a "x${LFS_STAGE}" != "xstage1" ]; then
+    LFS=""
+fi
+
+CLFS=${LFS}
+
 # Load functions
 source ../functions/fpkg
 source ../functions/lpkg
@@ -83,13 +98,8 @@ case "${HVL_TARGET}" in
     arm*)
         CLFS_BUILDFLAGS="-mabi=aapcs-linux -mfloat-abi=soft"
         CLFS_TARGET="arm-linux-gnueabi"
-        CLFS_ARCH=$(echo ${CLFS_TARGET} | sed -e 's/-.*//' -e 's/arm.*/arm/g')
-        CLFS_ENDIAN=$(echo ${CLFS_ARCH} | sed -e 's/armeb/BIG/' -e 's/arm/LITTLE/')
-        if [ "${CLFS_ENDIAN}" = "LITTLE" ]; then
-            CLFS_NOT_ENDIAN="BIG"
-        else
-            CLFS_NOT_ENDIAN="LITTLE"
-        fi
+        CLFS_ARCH=arm
+        CLFS_ENDIAN=LITTLE
        ;;
     "x86_64")
        CLFS_BUILDFLAGS="-m64"
@@ -102,6 +112,12 @@ case "${HVL_TARGET}" in
         CLFS_TARGET="i686-unknown-linux-gnu"
         CLFS_ARCH=${HVL_TARGET}
        ;;
+    m68k*)
+       #CLFS_BUILDFLAGS="-mcpu=51jm"
+       CLFS_BUILDFLAGS=""
+        CLFS_TARGET="${HVL_TARGET}-unknown-linux-gnu"
+        CLFS_ARCH=${HVL_TARGET}
+       ;;
     *)
        echo "Unsupported target architecture: ${HVL_TARGET}"
        return ${EXIT_FAILURE}
@@ -111,18 +127,7 @@ esac
 CLFS_HOST="$(echo $MACHTYPE | \
     sed "s/$(echo $MACHTYPE | cut -d- -f2)/cross/")"
 
-export CLFS_BUILDFLAGS CLFS_TARGET CLFS_ARCH CLFS_HOST CLFS_ENDIAN CLFS_NOT_ENDIAN
-
-if [ -z "${LFS_STAGE}" ]; then
-    echo "LFS_STAGE is undefined (see stage.def)"
-    return ${EXIT_FAILURE}
-fi
-
-if [ "x${LFS_STAGE}" != "xstage0" -a "x${LFS_STAGE}" != "xstage1" ]; then
-    LFS=""
-fi
-
-CLFS=${LFS}
+export CLFS_BUILDFLAGS CLFS_TARGET CLFS_ARCH CLFS_HOST CLFS_ENDIAN
 
 test_presence_of_packages_directory()
 {
@@ -142,6 +147,7 @@ init_log_file()
     test_presence_of_packages_directory
 
     export LFS_PKG_DIR="$(dirname $(pwd))/packages/${LFS_STAGE}"
+    export LFS_PATCHES_DIR="${SCRDIR}/patches"
     export LFS_LOG_DIR=${LFS}/var/log/hvlinux-install/${LFS_STAGE}
     export LFS_LOG_FILE=${LFS_LOG_DIR}/install.log
     export LFS_TMP="${LFS}/tmp"
@@ -174,6 +180,19 @@ get_pkg_ver()
     echo ${1} | sed "s!.*-\([0-9].*\)!\1!g"
 }
 
+# Extracting the version number from a complete package name using underscore as separator.
+# Arg. #1: Complete package name with version (ex: ecryptfs-utils_103 will output 103)
+get_pkg_ver_underscore()
+{
+    # Checking for correct number of arguments
+    if [ $# -ne 1 ]; then
+       echo "${FUNCNAME}(), wrong number of arguments: ${*}"
+       return 1
+    fi
+
+    echo ${1} | sed "s!.*_\([0-9].*\)!\1!g"
+}
+
 # Extracting the first digit version number from a complete package name.
 # Arg. #1: Complete package name with version (ex: gcc-3.4.4 will output 3)
 get_pkg_ver1()
@@ -566,11 +585,21 @@ hv_useradd()
 
 # Applying patch
 # First argument is the name of the patch
-# Second argument is the package name (target dir in $LFS_TMP)
+# Second argument is the package name
+# Third argument is the subdirectory inside the patches directory
+# Fourth argument: optional target directory (default is $LFS_TMP)
 apply_patch()
 {
     local PATCH_FILE=${1}
-    local TARGET_DIR=${2}
+    local PACKAGE=${2}
+    local PDIR=${3}
+    local TARGET_DIR=""
+
+    if [ $# -eq 4 ]; then
+       TARGET_DIR=${4}/${PACKAGE}
+    else
+       TARGET_DIR=${LFS_TMP}/${PACKAGE}
+    fi
 
     if [ -z "${PATCH_FILE}" ]; then
         echo
@@ -578,14 +607,14 @@ apply_patch()
        return ${EXIT_FAILURE}
     fi
 
-    if [ ! -f ${LFS_PKG_DIR}/${PATCH_FILE} ]; then
+    if [ ! -f ${PDIR}/${PATCH_FILE} ]; then
         echo
        echo "${FUNCNAME}(): patch file '${PATCH_FILE}' not found."
        return ${EXIT_FAILURE}
     fi
 
     echo "Applying patch: ${PATCH_FILE}"
-    patch -Np1 -d ${LFS_TMP}/${TARGET_DIR} -i ${LFS_PKG_DIR}/${PATCH_FILE}
+    patch -Np1 -d ${TARGET_DIR} -i ${PDIR}/${PATCH_FILE}
 }
 
 # Applying any patch(es) found for the current package.
@@ -598,7 +627,7 @@ apply_patch()
 apply_patches()
 {
     PACKAGE=${1}
-    local TARGET_DIR=${PACKAGE}
+    local TARGET_DIR=""
 
     # Checking for correct number of arguments
     if [ $# -gt 2 ]; then
@@ -609,17 +638,19 @@ apply_patches()
 
     if [ $# -eq 2 ]; then
        TARGET_DIR=${2}
-    else
-       TARGET_DIR=${PACKAGE}
     fi
 
-    # Checking if we can find at least one patch.
-    if ls ${LFS_PKG_DIR}/${1}-*.patch 1> /dev/null 2>&1; then
-       cd ${LFS_PKG_DIR}
-       for patch in ${1}-*.patch; do
-            apply_patch ${patch} ${TARGET_DIR}
-       done
-    fi
+    for s in common ${HVL_TARGET}; do
+        pdir=${LFS_PATCHES_DIR}/${s}
+        if [ -d ${pdir} ]; then
+            if ls ${pdir}/${1}-*.patch 1> /dev/null 2>&1; then
+               cd ${pdir}
+               for patch in ${1}-*.patch; do
+                    apply_patch ${patch} ${PACKAGE} ${pdir} ${TARGET_DIR}
+               done
+            fi
+        fi
+    done
 
     return $?
 }
@@ -644,9 +675,10 @@ static_decompressed_dirname()
                 # and extract base directory name with awk.
                 # tar 1.23 reports an error when using pipes, so
                 # remove error message with "2> /dev/null"
+                # (we extract the last line from tar output)
                 DIRNAME=$(tar ${TAR_OPTS} -tf \
                     ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} 2> /dev/null | \
-                    head -n1 | sed 's!^\./!!' | awk -F \/ '{print $1}')
+                    tail -n 1 | sed 's!^\./!!' | awk -F \/ '{print $1}')
                 ;;
             zip)
                 DIRNAME=$(unzip -l \
@@ -796,6 +828,7 @@ indicate_pkg_build_complete()
 # Arg. #1: Package name and version (ex: gcc-4.5.1)
 # Remaining arguments: Additional configure options
 # Options:
+#   -c     Do not decompress package
 #   -j     Disable parallel make for this package
 #   -l     Unique identification label in 'install.log'
 #          (default is package name and version)
@@ -806,21 +839,29 @@ indicate_pkg_build_complete()
 #            gnome
 #            xorg
 #            pm
+#   -t     Enable dependency tracking
 #   -s     Name of script to source (default is from name of package)
 ipkg()
 {
     START_TIME=$(echo `date +%s`)
 
     unset ALT_SCRIPT_NAME
+
+    export DECOMPRESS="1"
     export IPKG_MODE="ac" # Default mode if not specified
     export HVLABEL="" # Global variable
+    export ENABLE_DEPENDENCY_TRACKING="0"
 
     local HVMAKE_ORIG=${HVMAKE}
 
-    while getopts "jl:m:s:" flag ;do
+    while getopts "cjl:m:s:t" flag ;do
         case ${flag} in
+            c)
+                # Do not decompress package
+                DECOMPRESS="0"
+                ;;
             j)
-                # Disable parallel make onlyfor this package
+                # Disable parallel make only for this package
                 HVMAKE="make"
                 ;;
            l)
@@ -843,6 +884,10 @@ ipkg()
                 # Alternate script name
                 ALT_SCRIPT_NAME=${OPTARG}
                ;;
+            t)
+                # Do not disable dependency tracking
+                ENABLE_DEPENDENCY_TRACKING="1"
+               ;;
            ?)
                echo "${FUNCNAME}(): Invalid option: ${OPTARG}."
                return 1
@@ -908,6 +953,12 @@ ipkg()
     # Make sure we are at a known location
     cd ${SCRDIR}
 
+    if [ "x${LFS_STAGE}" != "xstage0" -a "x${LFS_STAGE}" != "xstage1" ]; then
+       # Make sure to update dynamic linker run-time bindings, except for
+       # stages 0 and 1 where we run as the LFS user.
+       ldconfig
+    fi
+
     exec 1>&6 # Restore stdout.
 
     # Displaying build time after the package name.
@@ -921,9 +972,6 @@ ipkg()
 
     ipkg_trap_end
 
-    # Make sure to update dynamic linker run-time bindings
-    ldconfig
-
     return $EXIT_SUCCESS
 }