From 304319bbd5a4d7714aed8d9ca509df66db3993ad Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 9 Jul 2024 14:59:45 +0000 Subject: [PATCH] make.sh: Refactor the execute function The first version was a little bit messy with all the checks in all sorts of places. We now create one large array and update it whenever the configuration changes. Signed-off-by: Michael Tremer --- make.sh | 358 ++++++++++++++++++++++++++------------------------------ 1 file changed, 167 insertions(+), 191 deletions(-) diff --git a/make.sh b/make.sh index fe8c1c1f8..b454591ce 100755 --- a/make.sh +++ b/make.sh @@ -652,29 +652,141 @@ lfsmakecommoncheck() { } execute() { + # Check if we are running in our namespace + if [ -z "${IN_NAMESPACE}" ]; then + exiterror "Not running in namespace" + fi + local command=() - local basedir="${BASEDIR}" local chroot="false" local interactive="false" local timer - # Check if we are running in our namespace - if [ -z "${IN_NAMESPACE}" ]; then - exiterror "Not running in namespace" - fi + # Collect environment variables + local -A environ=( + [PATH]="${TOOLS_DIR}/ccache/bin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin:${PATH}" + [HOME]="${HOME}" + [PS1]="${PS1}" + [TERM]="vt100" + + # Distro Information + [NAME]="${NAME}" + [SNAME]="${SNAME}" + [VERSION]="${VERSION}" + [CORE]="${CORE}" + [SLOGAN]="${SLOGAN}" + [SYSTEM_RELEASE]="${SYSTEM_RELEASE}" + [PAKFIRE_TREE]="${PAKFIRE_TREE}" + [CONFIG_ROOT]="${CONFIG_ROOT}" + + # Kernel Version + [KVER]="${KVER}" + + # Compiler flags + [CFLAGS]="${CFLAGS} ${HARDENING_CFLAGS}" + [CXXFLAGS]="${CXXFLAGS} ${HARDENING_CFLAGS}" + [RUSTFLAGS]="${RUSTFLAGS}" + + # ccache + [CCACHE_DIR]="${CCACHE_DIR}" + [CCACHE_TEMPDIR]="${CCACHE_TEMPDIR}" + [CCACHE_COMPILERCHECK]="${CCACHE_COMPILERCHECK}" + + # System Properties + [SYSTEM_PROCESSORS]="${SYSTEM_PROCESSORS}" + [SYSTEM_MEMORY]="${SYSTEM_MEMORY}" + + # Parallelism + [DEFAULT_PARALLELISM]="${DEFAULT_PARALLELISM}" + + # Compression Options + [XZ_OPT]="${XZ_OPT}" + + # Build Architecture + [BUILD_ARCH]="${BUILD_ARCH}" + [BUILD_PLATFORM]="${BUILD_PLATFORM}" + + # Targets + [CROSSTARGET]="${CROSSTARGET}" + [BUILDTARGET]="${BUILDTARGET}" + + # Paths + [LFS_BASEDIR]="${BASEDIR}" + [IMAGES_DIR]="${IMAGES_DIR}" + [TOOLS_DIR]="${TOOLS_DIR}" + ) + + # Configure a new namespace + local unshare=( + # Create a new cgroup namespace + "--cgroup" + + # Create a new mount namespace + "--mount" + "--propagation=slave" + + # Create a new PID namespace and fork + "--pid" + "--fork" + + # Create a new time namespace + "--time" + + # Create a new UTS namespace + "--uts" + + # Mount /proc so that the build environment does not see + # any foreign processes. + "--mount-proc=${BUILD_DIR}/proc" + + # If unshare is asked to terminate, terminate all child processes + "--kill-child" + ) while [ $# -gt 0 ]; do case "${1}" in --chroot) chroot="true" - # Move the basedir - basedir="/usr/src" + # Update some variables + environ+=( + [PATH]="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin" + [HOME]="/root" + + # Paths + [LFS_BASEDIR]="/usr/src" + [IMAGES_DIR]="/usr/src/images" + + # Compiler Cache + [CCACHE_DIR]="/usr/src/ccache" + + # Go Cache + [GOCACHE]="/usr/src/ccache/go" + ) + + # Fake environment + if [ -e "${BUILD_DIR}${TOOLS_DIR}/lib/libpakfire_preload.so" ]; then + environ+=( + [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 + [UTS_RELEASE]="${KVER}-${SNAME}" + + # Fake machine + [UTS_MACHINE]="${BUILD_ARCH}" + ) + fi ;; --interactive) interactive="true" + + # Use the actual value of $TERM + environ+=( + [TERM]="${TERM}" + ) ;; --timer=*) @@ -688,7 +800,7 @@ execute() { # Parse any custom environment variables *=*) - env+=( "${1}" ) + environ["${1%=*}"]="${1#*=}" ;; # The rest is the command @@ -700,162 +812,69 @@ execute() { shift done - local cwd="${basedir}/lfs" - local home="${HOME}" - local path - local ps1="${PS1}" - local term="vt100" - - local ccache_dir="${CCACHE_DIR}" - - # Initialize lots of variables depending on whether we are running in chroot or not - case "${chroot}" in - true) - # Set HOME - home="/root" - - # Set PATH - path="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin" - - # Update the ccache directory - ccache_dir="/usr/src/ccache" - ;; - - false) - # Set PATH - path="${TOOLS_DIR}/ccache/bin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin:${PATH}" - ;; - esac - - # Initilize some variables depending on whether we are running interactively or not - case "${interactive}" in - true) - cwd="/usr/src" - term="${TERM}" - ;; - esac - # Prepend any custom changes to PATH if [ -n "${CUSTOM_PATH}" ]; then - path="${CUSTOM_PATH}:${path}" + environ[PATH]="${CUSTOM_PATH}:${environ[PATH]}" fi - # Create a new environment - local env=( - # Clear the previous environment - "--ignore-environment" - - # Change the working directory - --chdir="${cwd}" - - # Set basic variables - HOME="${home}" - PATH="${path}" - PS1="${ps1}" - TERM="${term}" - - # Compiler flags - CFLAGS="${CFLAGS} ${HARDENING_CFLAGS}" - CXXFLAGS="${CXXFLAGS} ${HARDENING_CFLAGS}" - RUSTFLAGS="${RUSTFLAGS}" - - # Distro Information - NAME="${NAME}" - SNAME="${SNAME}" - VERSION="${VERSION}" - CORE="${CORE}" - SLOGAN="${SLOGAN}" - SYSTEM_RELEASE="${SYSTEM_RELEASE}" - PAKFIRE_TREE="${PAKFIRE_TREE}" - CONFIG_ROOT="${CONFIG_ROOT}" - - # Kernel Version - KVER="${KVER}" + # Setup QEMU + if qemu_is_required; then + environ+=( + [QEMU_TARGET_HELPER]="${QEMU_TARGET_HELPER}" - # System Properties - SYSTEM_PROCESSORS="${SYSTEM_PROCESSORS}" - SYSTEM_MEMORY="${SYSTEM_MEMORY}" - - # Parallelism - DEFAULT_PARALLELISM="${DEFAULT_PARALLELISM}" - - # Compression Options - XZ_OPT="${XZ_OPT}" + # Enable QEMU strace + #[QEMU_STRACE]="1" + ) - # Compiler Cache - CCACHE_DIR="${ccache_dir}" - CCACHE_TEMPDIR="${CCACHE_TEMPDIR}" - CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK}" - - # Go Cache - GOCACHE="${ccache_dir}/go" - - # Append the fake environment - $(fake_environ) + case "${BUILD_ARCH}" in + arm*) + environ+=( + [QEMU_CPU]="${QEMU_CPU:-cortex-a9}" + ) + ;; - # Setup the QEMU environment - $(qemu_environ) + riscv64) + environ+=( + [QEMU_CPU]="${QEMU_CPU:-sifive-u54}" - BUILD_ARCH="${BUILD_ARCH}" - BUILD_PLATFORM="${BUILD_PLATFORM}" + # Bug fix for QEMU locking up + [G_SLICE]="always-malloc" + ) + ;; + esac + fi - # Targets - CROSSTARGET="${CROSSTARGET}" - BUILDTARGET="${BUILDTARGET}" + local execute=() + local env - # Paths - LFS_BASEDIR="${basedir}" - TOOLS_DIR="${TOOLS_DIR}" + # Create new namespaces + execute+=( + "unshare" "${unshare[@]}" ) - # Add extra environment variables for the chroot environment - case "${chroot}" in - true) - env+=( - IMAGES_DIR="/usr/src/images" - ) - ;; - esac - - # Configure a new namespace - local unshare=( - # Create a new cgroup namespace - "--cgroup" - - # Create a new mount namespace - "--mount" - "--propagation=slave" - - # Create a new PID namespace and fork - "--pid" - "--fork" - - # Create a new time namespace - "--time" + # Run in chroot? + if [ "${chroot}" = "true" ]; then + execute+=( "chroot" "${BUILD_DIR}" ) + fi - # Create a new UTS namespace - "--uts" + # Reset the environment + execute+=( + "env" - # Mount /proc so that the build environment does not see - # any foreign processes. - "--mount-proc=${BUILD_DIR}/proc" + # Clear the previous environment + "--ignore-environment" - # If unshare is asked to terminate, terminate all child processes - "--kill-child" + # Change the working directory + --chdir="${environ[LFS_BASEDIR]}/lfs" ) - # Prepend the fresh environment to the command - command=( "env" "${env[@]}" "${command[@]}" ) - - # Change root? - case "${chroot}" in - true) - command=( "chroot" "${BUILD_DIR}" "${command[@]}" ) - ;; - esac + # Export the environment + for env in ${!environ[@]}; do + execute+=( "${env}=${environ[${env}]}" ) + done - # Create new namespaces - command=( "unshare" "${unshare[@]}" "${command[@]}" ) + # Append the command + execute+=( "${command[@]}" ) # Return code local r=0 @@ -866,7 +885,7 @@ execute() { # Run the command in the background and pipe all output to the logfile case "${interactive}" in true) - "${command[@]}" || return $? + "${execute[@]}" || return $? ;; false) @@ -877,7 +896,7 @@ execute() { # Dispatch the command to the background { - "${command[@]}" >> "${LOGFILE}" 2>&1 > "${LOGFILE}" 2>&1