+enterchroot() {
+ # Install QEMU helper, if needed
+ qemu_install_helper
+
+ local PATH="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/bin"
+
+ PATH="${PATH}" chroot ${LFS} env -i \
+ HOME="/root" \
+ TERM="${TERM}" \
+ PS1="${PS1}" \
+ PATH="${PATH}" \
+ SYSTEM_RELEASE="${SYSTEM_RELEASE}" \
+ PAKFIRE_CORE="${PAKFIRE_CORE}" \
+ NAME="${NAME}" \
+ SNAME="${SNAME}" \
+ VERSION="${VERSION}" \
+ CORE="${CORE}" \
+ SLOGAN="${SLOGAN}" \
+ TOOLS_DIR="${TOOLS_DIR}" \
+ CONFIG_ROOT="${CONFIG_ROOT}" \
+ CFLAGS="${CFLAGS} ${HARDENING_CFLAGS}" \
+ CXXFLAGS="${CXXFLAGS} ${HARDENING_CFLAGS}" \
+ BUILDTARGET="${BUILDTARGET}" \
+ CROSSTARGET="${CROSSTARGET}" \
+ BUILD_ARCH="${BUILD_ARCH}" \
+ BUILD_PLATFORM="${BUILD_PLATFORM}" \
+ CCACHE_DIR=/usr/src/ccache \
+ CCACHE_COMPRESS="${CCACHE_COMPRESS}" \
+ CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK}" \
+ KVER="${KVER}" \
+ $(fake_environ) \
+ $(qemu_environ) \
+ "$@"
+}
+
+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 (${BUILD_ARCH}) \u:\w\$ "
+
+ if enterchroot bash -i; then
+ stdumount
+ else
+ print_status FAIL
+ exiterror "chroot error"
+ fi
+}
+
+lfsmakecommoncheck() {
+ # Script present?
+ if [ ! -f $BASEDIR/lfs/$1 ]; then
+ exiterror "No such file or directory: $BASEDIR/$1"
+ fi
+
+ # Print package name and version
+ print_package $@
+
+ # 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
+ print_runtime 0
+ print_status SKIP
+ return 1
+ fi
+ fi
+
+ # Script slipped?
+ local i
+ for i in $SKIP_PACKAGE_LIST
+ do
+ if [ "$i" == "$1" ]; then
+ print_status 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
+
+ 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 &
+
+ if ! wait_until_finished $!; then
+ print_status FAIL
+ exiterror "Building $*"
+ fi
+
+ print_status DONE
+}
+
+lfsmake2() {
+ lfsmakecommoncheck $*
+ [ $? == 1 ] && return 0
+
+ local PS1='\u:\w$ '
+
+ enterchroot \
+ ${EXTRA_PATH}bash -x -c "cd /usr/src/lfs && \
+ MAKETUNING=${MAKETUNING} \
+ make -f $* \
+ LFS_BASEDIR=/usr/src install" \
+ >> ${LOGFILE} 2>&1 &
+
+ if ! wait_until_finished $!; then
+ print_status FAIL
+ exiterror "Building $*"
+ fi
+
+ print_status DONE
+}
+
+ipfiredist() {
+ lfsmakecommoncheck $*
+ [ $? == 1 ] && return 0
+
+ local PS1='\u:\w$ '
+
+ enterchroot \
+ bash -x -c "cd /usr/src/lfs && make -f $* LFS_BASEDIR=/usr/src dist" \
+ >> ${LOGFILE} 2>&1 &
+
+ if ! wait_until_finished $!; then
+ print_status FAIL
+ exiterror "Packaging $*"
+ fi
+
+ print_status DONE
+}
+
+wait_until_finished() {
+ local pid=${1}
+
+ local start_time=$(now)
+
+ # Show progress
+ if ${INTERACTIVE}; then
+ # Wait a little just in case the process
+ # has finished very quickly.
+ sleep 0.1
+
+ local runtime
+ while kill -0 ${pid} 2>/dev/null; do
+ print_runtime $(( $(now) - ${start_time} ))
+
+ # Wait a little
+ sleep 1
+ done
+ fi
+
+ # Returns the exit code of the child process
+ wait ${pid}
+ local ret=$?
+
+ if ! ${INTERACTIVE}; then
+ print_runtime $(( $(now) - ${start_time} ))
+ fi
+
+ return ${ret}
+}
+
+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
+