+entershell() {
+ if [ ! -e $BASEDIR/build/usr/src/lfs/ ]; then
+ exiterror "No such file or directory: $BASEDIR/build/usr/src/lfs/"
+ fi
+
+ echo "Entering to a shell inside LFS chroot, go out with exit"
+ local PS1="ipfire build chroot ($(uname -m)) \u:\w\$ "
+
+ if enterchroot bash -i; then
+ stdumount
+ else
+ beautify message FAIL
+ exiterror "chroot error"
+ fi
+}
+
+lfsmakecommoncheck() {
+ # Script present?
+ if [ ! -f $BASEDIR/lfs/$1 ]; then
+ exiterror "No such file or directory: $BASEDIR/$1"
+ fi
+
+ local PKG_VER=`get_pkg_ver $BASEDIR/lfs/$1`
+ beautify make_pkg "$PKG_VER $*"
+
+ # Check if this package is supported by our architecture.
+ # If no SUP_ARCH is found, we assume the package can be built for all.
+ if grep "^SUP_ARCH" ${BASEDIR}/lfs/${1} >/dev/null; then
+ # Check if package supports ${BUILD_ARCH} or all architectures.
+ if ! grep -E "^SUP_ARCH.*${BUILD_ARCH}|^SUP_ARCH.*all" ${BASEDIR}/lfs/${1} >/dev/null; then
+ beautify result SKIP
+ return 1
+ fi
+ fi
+
+ # Script slipped?
+ local i
+ for i in $SKIP_PACKAGE_LIST
+ do
+ if [ "$i" == "$1" ]; then
+ beautify result SKIP
+ return 1;
+ fi
+ done
+
+ echo -ne "`date -u '+%b %e %T'`: Building $* " >> $LOGFILE
+
+ cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR BUILD_ARCH="${BUILD_ARCH}" \
+ MESSAGE="$1\t " download >> $LOGFILE 2>&1
+ if [ $? -ne 0 ]; then
+ exiterror "Download error in $1"
+ fi
+
+ cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR BUILD_ARCH="${BUILD_ARCH}" \
+ MESSAGE="$1\t md5sum" md5 >> $LOGFILE 2>&1
+ if [ $? -ne 0 ]; then
+ exiterror "md5sum error in $1, check file in cache or signature"
+ fi
+
+ return 0 # pass all!
+}
+
+lfsmake1() {
+ lfsmakecommoncheck $*
+ [ $? == 1 ] && return 0
+
+ local PKG_TIME_START=`date +%s`
+
+ cd $BASEDIR/lfs && env -i \
+ PATH="${TOOLS_DIR}/ccache/bin:${TOOLS_DIR}/bin:$PATH" \
+ CCACHE_DIR="${CCACHE_DIR}" \
+ CCACHE_COMPRESS="${CCACHE_COMPRESS}" \
+ CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK}" \
+ CFLAGS="${CFLAGS}" \
+ CXXFLAGS="${CXXFLAGS}" \
+ MAKETUNING="${MAKETUNING}" \
+ make -f $* \
+ TOOLCHAIN=1 \
+ TOOLS_DIR="${TOOLS_DIR}" \
+ CROSSTARGET="${CROSSTARGET}" \
+ BUILDTARGET="${BUILDTARGET}" \
+ BUILD_ARCH="${BUILD_ARCH}" \
+ BUILD_PLATFORM="${BUILD_PLATFORM}" \
+ LFS_BASEDIR="${BASEDIR}" \
+ ROOT="${LFS}" \
+ KVER="${KVER}" \
+ install >> $LOGFILE 2>&1
+
+ local COMPILE_SUCCESS=$?
+ local PKG_TIME_END=`date +%s`
+
+ if [ $COMPILE_SUCCESS -ne 0 ]; then
+ beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
+ exiterror "Building $*";
+ else
+ beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
+ fi
+
+ return 0
+}
+
+lfsmake2() {
+ lfsmakecommoncheck $*
+ [ $? == 1 ] && return 0
+
+ local PKG_TIME_START=`date +%s`
+ local PS1='\u:\w$ '
+
+ enterchroot \
+ bash -x -c "cd /usr/src/lfs && \
+ MAKETUNING=${MAKETUNING} \
+ make -f $* \
+ LFS_BASEDIR=/usr/src install" \
+ >> ${LOGFILE} 2>&1
+
+ local COMPILE_SUCCESS=$?
+ local PKG_TIME_END=`date +%s`
+
+ if [ $COMPILE_SUCCESS -ne 0 ]; then
+ beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
+ exiterror "Building $*";
+ else
+ beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
+ fi
+
+ return 0
+}
+
+ipfiredist() {
+ lfsmakecommoncheck $*
+ [ $? == 1 ] && return 0
+
+ local PKG_TIME_START=`date +%s`
+ local PS1='\u:\w$ '
+
+ enterchroot \
+ bash -x -c "cd /usr/src/lfs && make -f $* LFS_BASEDIR=/usr/src dist" \
+ >> ${LOGFILE} 2>&1
+
+ local COMPILE_SUCCESS=$?
+ local PKG_TIME_END=`date +%s`
+
+ if [ $COMPILE_SUCCESS -ne 0 ]; then
+ beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
+ exiterror "Packaging $*";
+ else
+ beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
+ fi
+ return 0
+}
+
+fake_environ() {
+ [ -e "${BASEDIR}/build${TOOLS_DIR}/lib/libpakfire_preload.so" ] || return
+
+ local env="LD_PRELOAD=${TOOLS_DIR}/lib/libpakfire_preload.so"
+
+ # Fake kernel version, because some of the packages do not compile
+ # with kernel 3.0 and later.
+ env="${env} UTS_RELEASE=${KVER}"
+
+ # Fake machine version.
+ env="${env} UTS_MACHINE=${BUILD_ARCH}"
+
+ echo "${env}"
+}
+
+qemu_environ() {
+ local env
+
+ # Don't add anything if qemu is not used.
+ if ! qemu_is_required; then
+ return
+ fi
+
+ # Set default qemu options
+ case "${BUILD_ARCH}" in
+ arm*)
+ QEMU_CPU="${QEMU_CPU:-cortex-a9}"
+
+ env="${env} QEMU_CPU=${QEMU_CPU}"
+ ;;
+ esac
+
+ # Enable QEMU strace
+ #env="${env} QEMU_STRACE=1"
+
+ echo "${env}"
+}
+
+qemu_is_required() {
+ local build_arch="${1}"
+
+ if [ -z "${build_arch}" ]; then
+ build_arch="${BUILD_ARCH}"
+ fi
+
+ case "${HOST_ARCH},${build_arch}" in
+ x86_64,arm*|i?86,arm*|i?86,x86_64)
+ return 0
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+
+qemu_install_helper() {
+ # Do nothing, if qemu is not required
+ if ! qemu_is_required; then
+ return 0
+ fi
+
+ if [ ! -e /proc/sys/fs/binfmt_misc/status ]; then
+ exiterror "binfmt_misc not mounted. QEMU_TARGET_HELPER not useable."
+ fi
+
+ if [ ! $(cat /proc/sys/fs/binfmt_misc/status) = 'enabled' ]; then
+ exiterror "binfmt_misc not enabled. QEMU_TARGET_HELPER not useable."
+ fi
+
+
+ if [ -z "${QEMU_TARGET_HELPER}" ]; then
+ exiterror "QEMU_TARGET_HELPER not set"
+ fi
+
+ # Check if the helper is already installed.
+ if [ -x "${LFS}${QEMU_TARGET_HELPER}" ]; then
+ return 0
+ fi
+
+ # Try to find a suitable binary that we can install
+ # to the build environment.
+ local file
+ for file in "${QEMU_TARGET_HELPER}" "${QEMU_TARGET_HELPER}-static"; do
+ # file must exist and be executable.
+ [ -x "${file}" ] || continue
+
+ # Must be static.
+ file_is_static "${file}" || continue
+
+ local dirname="${LFS}$(dirname "${file}")"
+ mkdir -p "${dirname}"
+
+ install -m 755 "${file}" "${LFS}${QEMU_TARGET_HELPER}"
+ return 0
+ done
+
+ exiterror "Could not find a statically-linked QEMU emulator: ${QEMU_TARGET_HELPER}"
+}
+
+qemu_find_build_helper_name() {
+ local build_arch="${1}"
+
+ local magic
+ case "${build_arch}" in
+ arm*)
+ magic="7f454c4601010100000000000000000002002800"
+ ;;
+ x86_64)
+ magic="7f454c4602010100000000000000000002003e00"
+ ;;
+ esac
+
+ [ -z "${magic}" ] && return 1
+
+ local file
+ for file in /proc/sys/fs/binfmt_misc/*; do
+ # skip write only register entry
+ [ $(basename "${file}") = "register" ] && continue
+ # Search for the file with the correct magic value.
+ grep -qE "^magic ${magic}$" "${file}" || continue
+
+ local interpreter="$(grep "^interpreter" "${file}" | awk '{ print $2 }')"
+
+ [ -n "${interpreter}" ] || continue
+ [ "${interpreter:0:1}" = "/" ] || continue
+ [ -x "${interpreter}" ] || continue
+
+ echo "${interpreter}"
+ return 0
+ done
+
+ return 1
+}
+
+file_is_static() {
+ local file="${1}"
+
+ file ${file} 2>/dev/null | grep -q "statically linked"
+}
+
+update_language_list() {
+ local path="${1}"
+
+ local lang
+ for lang in ${path}/*.po; do
+ lang="$(basename "${lang}")"
+ echo "${lang%*.po}"
+ done | sort -u > "${path}/LINGUAS"
+}
+
+# Load configuration file
+if [ -f .config ]; then
+ . .config
+fi
+
+# TARGET_ARCH is BUILD_ARCH now
+if [ -n "${TARGET_ARCH}" ]; then
+ BUILD_ARCH="${TARGET_ARCH}"
+ unset TARGET_ARCH
+fi
+
+# Get the amount of memory in this build system
+HOST_MEM=$(system_memory)
+
+if [ -n "${BUILD_ARCH}" ]; then
+ configure_build "${BUILD_ARCH}"
+else
+ configure_build "default"
+fi
+