X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=functions;h=89230b9afcffb510a4338dd60240f80789ad403c;hb=56e6ade2828debbb8210ffba1faa2193cb78d05c;hp=7497f3a7746d238b15cfe1362bb4b6a101644d55;hpb=0028eaa73ae0c3bd1dbf90c4bd508ae705bc386f;p=hvlinux.git diff --git a/functions b/functions index 7497f3a..89230b9 100644 --- a/functions +++ b/functions @@ -1,6 +1,6 @@ #!/bin/bash -# This file is 'sourced' by other scripts, therefore the above line is of no -# use, except when modifying the file in emacs to have syntax highlighting. +set -o errtrace # Let shell functions inherit ERR trap. +set -o errexit # Constants for return codes EXIT_SUCCESS=0 @@ -216,7 +216,7 @@ static_bootscript_add() echo "${FUNCNAME}(), invalid level argument : ${*}" return 1 fi - + # Making sure bootscript has correct permissions chmod -v 740 ${LFS}/etc/rc.d/init.d/${SCRIPTNAME} @@ -310,7 +310,7 @@ var_export() echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi - + # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" @@ -357,7 +357,7 @@ var_add() echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi - + # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" @@ -400,7 +400,7 @@ var_add() # Variable value is NOT enclosed by double-quotes sed -i "s!\(^export ${VARIABLE}=.*\)!\1${SEP}${VALUE}!" ${FILE} fi - + return $? } @@ -438,7 +438,7 @@ string_add() echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi - + # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" @@ -450,7 +450,7 @@ string_add() echo "${FUNCNAME}(), string already defined: ${STRING}" return 0 fi - + echo "${STRING}" >> ${FILE} } @@ -470,7 +470,7 @@ var_add_shadow() echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi - + # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" @@ -496,23 +496,14 @@ hv_groupadd() { arguments=${*} - set +e - + # The last argument is the group name while [ $# -ne 0 ]; do - last_argument=${*} + groupname=${*} shift done - - groupadd ${arguments} - error=$? - - set -e - if [ ${error} -eq 0 -o ${error} -eq 9 ]; then - # 9 means the group already exists - return ${EXIT_SUCCESS} - else - exit ${EXIT_FAILURE} + if ! cat /etc/group | egrep "^${groupname}:" 1> /dev/null 2>&1; then + groupadd ${arguments} fi } @@ -522,24 +513,14 @@ hv_useradd() { arguments="${*}" - set +e - - # The last argument is the username + # The last argument is the user name while [ $# -ne 0 ]; do - last_argument=${*} + username=${*} shift done - useradd ${arguments} - error=$? - - set -e - - if [ ${error} -eq 0 -o ${error} -eq 9 ]; then - # 9 means the user already exists - return ${EXIT_SUCCESS} - else - exit ${EXIT_FAILURE} + if ! cat /etc/passwd | egrep "^${username}:" 1> /dev/null 2>&1; then + useradd ${arguments} fi } @@ -585,13 +566,13 @@ apply_patches() echo "Usage: ${FUNCNAME} PACKAGE-NAME" exit ${EXIT_FAILURE} fi - + 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} @@ -640,7 +621,7 @@ static_decompressed_dirname() # Decompression of a package # First argument: package name -# Second argument: directory where decompressing (optional) +# Second argument: directory where decompressing (optional, defaults to LFS_TMP) decompress_package() { # Checking for correct number of arguments @@ -733,6 +714,28 @@ ipkg_trap_handler() exit 1 } +ipkg_display_build_infos() +{ + echo "hvlinux: configure options:" + for opt in ${CONFIGURE_OPTS}; do + echo " ${opt}" + done + echo "hvlinux: environment variables:" + echo " HOST: ${CLFS_HOST}" + echo " TARGET: ${CLFS_TARGET}" + echo " BUILD64: ${BUILD64}" + echo " CFLAGS: ${CFLAGS}" + echo " CPPFLAGS: ${CPPFLAGS}" + echo " CXXFLAGS: ${CFLAGS}" + echo " LDFLAGS: ${LDFLAGS}" + echo " RANLIB: ${RANLIB}" + echo " CC: ${CC}" + echo " CXX: ${CXX}" + echo " AR: ${AR}" + echo " AS: ${AS}" + echo " PATH: ${PATH}" +} + # Installation of a package # Arg. #1: Package name and version (ex: gcc-4.5.1) # Remaining arguments: Additional configure options @@ -741,23 +744,22 @@ ipkg_trap_handler() # -l Unique identification label in 'install.log' # (default is package name and version) # -m Installation mode: -# ac Standard autoconf package, build in separate dir -# acnb Standard autoconf package, building in source dir -# nb No autoconf (configure) +# ac Standard autoconf package, build in separate dir (default) +# acnb Standard autoconf package, build in source dir +# noac No autoconf (configure) # gnome # xorg # pm -# -s Name of script to execute or name of definition file (default is -# to source ipkg.def) +# -s Name of script to source (default is from name of package) ipkg() { START_TIME=$(echo `date +%s`) + unset ALT_SCRIPT_NAME export IPKG_MODE="ac" export HVLABEL="" # Global variable - local SCRIPT=${DEFAULT_IPKG_SCRIPT} - while getopts "hl:m:" flag ;do + while getopts "hl:m:s:" flag ;do case ${flag} in l) # Alternate label @@ -777,13 +779,7 @@ ipkg() ;; s) # Alternate script name - SCRIPT=${OPTARG} - - # Checking if script is valid and executable - if [ ! -x ${SCRIPT} ]; then - echo "${FUNCNAME}(): cannot execute script: ${SCRIPT}" - exit ${EXIT_FAILURE} - fi + ALT_SCRIPT_NAME=${OPTARG} ;; ?) echo "${FUNCNAME}(): Invalid option: ${OPTARG}." @@ -819,10 +815,10 @@ ipkg() # Checking if package was previously successfully installed if grep "^${HVLABEL} successfully installed" ${LFS_LOG_FILE} \ - 1> /dev/null 2>&1; then + 1> /dev/null 2>&1; then return $EXIT_SUCCESS fi - + # Displaying label MSGSTRING="Installing ${HVLABEL}" display_checkbox_msg ${MSGSTRING} @@ -832,18 +828,19 @@ ipkg() echo "------------------------" 1>> ${LFS_LOG_FILE} echo "${MSGSTRING}" 1>> ${LFS_LOG_FILE} + exec 7>&1 # Save current "value" of stdout. + # All output from commands in this block sent to file $LFS_LOG_FILE. + exec >> ${LFS_LOG_FILE} 2>&1 + ipkg_display_build_infos + exec 1>&7 7>&- # Restore stdout and close file descriptor #7. + # All output from commands in this block sent to file $PACKAGE_LOG. exec > ${PACKAGE_LOG} 2>&1 - if [ "x${SCRIPT}" = "x${DEFAULT_IPKG_SCRIPT}" ]; then - # Executing standard ac script. - source ${SCRDIR}/../functions-ac - source ${SCRDIR}/${DEFAULT_IPKG_SCRIPT} - ipkg_script - else - # Executing custom script. - ${SCRIPT} ${PACKAGE} ${CONFIGURE_OPTS} - fi + # Sourcing standard ac script. + source ${SCRDIR}/../functions-ipkg + source ${SCRDIR}/${DEFAULT_IPKG_SCRIPT} + ipkg_script ${ALT_SCRIPT_NAME} # Make sure we are at a known location cd ${SCRDIR} @@ -934,6 +931,33 @@ rscr() return $EXIT_SUCCESS } +rcmd_trap_setup() +{ + exec 6>&1 # Save current "value" of stdout. + trap rcmd_trap_handler INT TERM EXIT ERR +} + +rcmd_trap_end() +{ + trap - INT TERM EXIT ERR + + # We do not restore global trap +} + +rcmd_trap_handler() +{ + exec 1>&6 6>&- # Restore stdout to fd #6, where it had been saved, + # and close file descriptor #6. + print_status failure + + rcmd_trap_end + if [ -n "${RCMD_NO_EXIT}" ]; then + return ${EXIT_FAILURE} + else + exit ${EXIT_FAILURE} + fi +} + # Run command, no log # First argument: Message to display during script # Second argument: command + arguments @@ -954,12 +978,14 @@ rcmd() display_checkbox_msg ${MSGSTRING} + rcmd_trap_setup + echo ${CMD_WITH_ARGS} 1>> ${LFS_LOG_FILE} # Executing command ${CMD_WITH_ARGS} 1>> ${LFS_LOG_FILE} 2>&1 + if [ $? -ne 0 ]; then - print_status failure if [ -n "${RCMD_NO_EXIT}" ]; then return ${EXIT_FAILURE} else @@ -970,6 +996,8 @@ rcmd() # Displaying build time after the package name print_status success + rcmd_trap_end + return $EXIT_SUCCESS }