Script "srcpkg_to_binpkg.sh"
#!/bin/sh

# ------------------------
# srcpkg_to_binpkg.sh
# version 2.5.1.4
# ------------------------

# check for parameters

if [ $# -eq 0 ] || [ ${1} = "--help" ] || [ ${1} = "--usage" ]; then
  echo "Usage: ${0} [ --debug-pkg ] [ --kernel-mode ] package-title"
  exit 0
fi

PKGNAME=""
DEBUG_PKG_OPT=""
KERNEL_MODE_OPT=""
for arg in $@
do
  is_option=`echo "${arg}" | grep -- '\-\-'`
  if [ -z "${is_option}" ]; then
    [ -z ${PKGNAME} ] && PKGNAME="${arg}"
  else
    [ $arg = "--debug-pkg" ] && DEBUG_PKG_OPT="yes" && continue
    [ $arg = "--kernel-mode" ] && KERNEL_MODE_OPT="yes" && continue
    echo "${0}: unknown option '${arg}'"
  fi
done

if [ -z ${PKGNAME} ]; then
  echo "Usage: ${0} [ --debug-pkg ] package-title (for example, ${0} gcc)."
  exit 0
fi

[ -z "${BUILD_FARM}" ] && BUILD_FARM="${HOME}/build-farm"
if [ ! -d "${BUILD_FARM}" ]; then
  echo "ERROR: build-farm directory '${BUILD_FARM}' doesn't exist. Abort."
  exit 1
fi

PKG_TITLE=`basename "${PKGNAME}"`
PKG_MODE=`dirname "${PKGNAME}"`
[ -z "${PKG_MODE}" ] && PKG_MODE="."

[ -z "${PKGBS_DIR}" ] && PKGBS_DIR="${BUILD_FARM}"/BUILD-SCRIPTS
[ -d "${PKGBS_DIR}" ] || PKGBS_DIR=`pwd`
CONFIG_PATH="${PKGBS_DIR}/${PKG_MODE}"/CONFIG
PPFILES_PATH="${PKGBS_DIR}/${PKG_MODE}"/PPFILES

# check for required binaries
REQD_BINS=""
which sed >/dev/null 2>&1 || type sed >/dev/null 2>&1 || REQD_BINS="sed,${REQD_BINS}"
which awk >/dev/null 2>&1 || type awk >/dev/null 2>&1 || REQD_BINS="awk,${REQD_BINS}"
which tar >/dev/null 2>&1 || type tar >/dev/null 2>&1 || REQD_BINS="tar,${REQD_BINS}"
if [ -n "${REQD_BINS}" ]; then
  echo -e `basename ${0}`: "Required binaries (${REQD_BINS}\010) are not present."
  exit 1
fi

[ -z "${BUILD_DIR}" ] && BUILD_DIR=`ls -t "${BUILD_FARM}/${PKG_MODE}" | grep "${PKG_TITLE}" \
  | grep ".srcpkg" | grep -v ".tar." | head -1`
[ -z "${BUILD_DIR}" ] && BUILD_DIR=`ls -t "${BUILD_FARM}/${PKG_MODE}" | grep "${PKG_TITLE}" \
  | grep ".builddir" | grep -v ".tar." | head -1`
if [ -z "${BUILD_DIR}" ]; then
  echo "ERROR: build directory for '${PKG_MODE}:${PKG_TITLE}' not found. Abort."
  exit 1
fi
if [ ! -z `echo "${BUILD_DIR}" | grep ".srcpkg"` ];then
  builddir=`echo "${BUILD_DIR}" | sed 's/.srcpkg/.builddir/'`
  if [ -d "${BUILD_FARM}/${PKG_MODE}/${builddir}" ]; then
    if [ ! -z "${builddir}" ]; then
      printf "removing previous build directory ('%s')..." "${BUILD_FARM}/${PKG_MODE}/${builddir}"
      rm -rf "${BUILD_FARM}/${PKG_MODE}/${builddir}" && echo " ok"
    fi
  fi
  mv "${BUILD_FARM}/${PKG_MODE}/${BUILD_DIR}" "${BUILD_FARM}/${PKG_MODE}/${builddir}" && BUILD_DIR="${builddir}"
fi

if [ ! -z ${DEBUG_PKG_OPT} ]; then
  builddir=`echo "${BUILD_DIR}" | sed 's/.builddir/-DEBUG.builddir/'`
  if [ -d "${BUILD_FARM}/${PKG_MODE}/${builddir}" ]; then
    if [ ! -z "${builddir}" ]; then
      printf "debug package requested; removing previous debug build directory ('%s')..." "${BUILD_FARM}/${PKG_MODE}/${builddir}"
      rm -rf "${BUILD_FARM}/${PKG_MODE}/${builddir}" && echo " ok"
    fi
  fi
  mv "${BUILD_FARM}/${PKG_MODE}/${BUILD_DIR}" "${BUILD_FARM}/${PKG_MODE}/${builddir}" 2>/dev/null \
    && echo "debug package requested;" "'${BUILD_FARM}/${PKG_MODE}/${BUILD_DIR} --> '${BUILD_FARM}/${PKG_MODE}/${builddir}'" \
    && BUILD_DIR="${builddir}"
fi

echo "build directory:" "${PKG_MODE}/${BUILD_DIR}"

target_triplet=`gcc -v 2>&1 | grep "Target:" | awk '{print $2}' | tr '-' ' '`

if [ -z ${ARCH_NAME} ]
then   
  case `uname -m` in
    ppc)
      ARCH_NAME="powerpc"
      ;;
    *)
      ARCH_NAME=`echo "${target_triplet}" | awk '{print $1}'`
      ;;
  esac
fi

[ -z ${VENDOR_NAME} ] && VENDOR_NAME=`echo "${target_triplet}" | awk '{print $2}'`
[ -z ${VENDOR_NAME} ] && VENDOR_NAME="unknown"

[ -z ${LIBC_NAME} ] && LIBC_NAME=`echo "${target_triplet}" | awk '{print $4}'`
[ `ls /lib/ld-* | grep uClibc | wc -l` -ne 0 ] && LIBC_NAME="uclibc"
[ -z ${LIBC_NAME} ] && LIBC_NAME="unknown"

NO_MAKEINFO=""
which makeinfo >/dev/null 2>&1 || type makeinfo >/dev/null 2>&1 || NO_MAKEINFO="yes"
if [ "${NO_MAKEINFO}" = "yes" ]; then
  if [ `whoami` = "root" ]; then
    echo '#!/bin/sh' > /usr/bin/makeinfo
    echo 'if [ "$1" = "--version" ]; then echo "makeinfo (GNU texinfo) 4.10"; fi' >> /usr/bin/makeinfo
    chmod +x /usr/bin/makeinfo
  else
    echo "ERROR: your system doesn't have 'makeinfo' and I cannot fix this; try again as root. Abort."
    exit 1
  fi
fi

[ -z "${INSTALL_DIR}" ] && INSTALL_DIR="INSTALL-DEST"
[ -z "${INSTALL_PATH}" ] && INSTALL_PATH="${BUILD_FARM}/${PKG_MODE}/${BUILD_DIR}/${INSTALL_DIR}"

echo "-------------------- building --------------------"
if [ ! -f "${PKGBS_DIR}/${PKG_MODE}"/BUILDCMDS/buildcmds-"${PKG_TITLE}" ]; then
  echo "ERROR: missing build commands for '${PKG_TITLE}'. Abort."
  exit 1
fi

DEBUG_ON=""
[ ! -z ${DEBUG_PKG_OPT} ] && DEBUG_ON="yes"

cd "${BUILD_FARM}/${PKG_MODE}/${BUILD_DIR}"
source "${PKGBS_DIR}"/${PKG_MODE}/BUILDCMDS/buildcmds-"${PKG_TITLE}"
cd "${PKGBS_DIR}"
echo "-------------------- build done --------------------"

pkgname_with_version="${PKG_TITLE}"
[ ! -z "${BUILD_DIR}" ] && pkgname_with_version=`echo "${BUILD_DIR}" | sed -e 's/.builddir//' -e 's/.srcpkg//' -e 's/.sourcedir//'`
[ ! -z "${SOURCE_DIR}" ] && pkgname_with_version=`echo "${SOURCE_DIR}" | sed -e 's/.builddir//' -e 's/.srcpkg//' -e 's/.sourcedir//'`
[ -z "${BINPKG_NAME}" ] && BINPKG_NAME="${pkgname_with_version}"-"${ARCH_NAME}"-"${LIBC_NAME}".binpkg.tar.bz2
if [ "${PKG_TITLE}" = "${LIBC_NAME}" ]; then
  BINPKG_NAME=`echo "${BINPKG_NAME}" | sed "s/-${LIBC_NAME}//"`
fi

if [ -f "${PKGBS_DIR}"/${PKG_MODE}/POSTCMDS/postcmds-"${PKG_TITLE}" ]; then
  echo -n "executing post-build commands..."
  source "${PKGBS_DIR}"/${PKG_MODE}/POSTCMDS/postcmds-"${PKG_TITLE}"
  echo " done"
fi

do_sstrip="yes"
[ -z ${KERNEL_MODE_OPT} ] || do_sstrip="no"
if [ -z ${DEBUG_PKG_OPT} ]; then
  if [ ! -f "${PKGBS_DIR}"/sstrip-ET_EXECs.sh ]; then
    echo "NOTE: can't find script 'sstrip-ET_EXECs.sh'; executable binaries will not be sstrip-d."
    do_sstrip="no"
  fi
else
  echo "debug package requested; executable binaries will not be sstrip-d!"
  do_sstrip="no"
fi

rmdir "${INSTALL_PATH}" 1>/dev/null 2>&1
if [ $? -ne 0 ] && [ -d "${INSTALL_PATH}" ]
then
  rm -rf "${INSTALL_PATH}"/usr/share/doc
  rm -rf "${INSTALL_PATH}"/usr/share/info
  rm -rf "${INSTALL_PATH}"/usr/doc
  rm -rf "${INSTALL_PATH}"/usr/info
  rmdir "${INSTALL_PATH}"/usr/share 2>/dev/null

  prev_wd=`pwd`
  cd "${INSTALL_PATH}"
  [ ${do_sstrip} = "yes" ] && "${PKGBS_DIR}"/sstrip-ET_EXECs.sh

  if [ -d ./.archives ]; then
    mv ./.archives/* "${BUILD_FARM}"/
    rm -rf ./.archives
  fi

  if [ -z ${KERNEL_MODE_OPT} ]; then
    echo -n "compressing '${BINPKG_NAME}'..."
    tar cjf "${BINPKG_NAME}" * 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
      echo " ok"
      printf "%s" "created binary package:"
      mkdir -p "${BUILD_FARM}"/PACKAGES/binpkg-"${ARCH_NAME}"/"${PKG_MODE}"
      mv -f "${INSTALL_PATH}/${BINPKG_NAME}" "${BUILD_FARM}"/PACKAGES/binpkg-"${ARCH_NAME}"/"${PKG_MODE}"/
      echo " ${BUILD_FARM}/PACKAGES/binpkg-"${ARCH_NAME}"/"${PKG_MODE}"/${BINPKG_NAME}"

      NEW_BUILD_DIR=`echo "${BUILD_DIR}" | sed "s/.builddir/-"${ARCH_NAME}"-"${LIBC_NAME}".builddir/"`
      if [ "${NEW_BUILD_DIR}" != "${BUILD_DIR}" ]; then
        mv "${BUILD_FARM}/${PKG_MODE}/${BUILD_DIR}" "${BUILD_FARM}/${PKG_MODE}/${NEW_BUILD_DIR}" && \
          echo "build directory renamed:" \'"${BUILD_DIR}"\' "-->" \'"${NEW_BUILD_DIR}"\' && \
          BUILD_DIR="${NEW_BUILD_DIR}" && NEW_BUILD_DIR=""
      fi
    else
      echo "binpkg cannot be created: tar error"
      exit 1
    fi
  fi

  cd "${prev_wd}"  
else
  if [ -z ${KERNEL_MODE_OPT} ]; then
    echo "binpkg cannot be created:" \'"${INSTALL_DIR}"\' "was empty (now removed)"
  else
    echo "oops! kernel installation directory" \'"${INSTALL_DIR}"\' "was empty (now removed)"
  fi
  exit 1
fi

if [ "${NO_MAKEINFO}" = "yes" ]; then
  rm -f /usr/bin/makeinfo
fi
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.