]> git.ipfire.org Git - people/ms/ipfire-2.x.git/commitdiff
make.sh: Create a large unified function to run commands
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Jul 2024 14:09:56 +0000 (14:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Jul 2024 14:09:56 +0000 (14:09 +0000)
This now includes the toolchain for which we need to set up the same
environment, except slightly differently.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
make.sh

diff --git a/make.sh b/make.sh
index 0c3f4884fe7f8be1cb8a7d6d6a346daefa5551b7..fe8c1c1f8d41b24c46b77bb5779ab912977626a5 100755 (executable)
--- a/make.sh
+++ b/make.sh
@@ -614,81 +614,7 @@ prepareenv() {
        esac
 
        # Setup ccache cache size
-       enterchroot ccache --max-size="${CCACHE_CACHE_SIZE}"
-}
-
-enterchroot() {
-       local PATH="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin"
-
-       # Check if we are running in our namespace
-       if [ -z "${IN_NAMESPACE}" ]; then
-               exiterror "Not running in namespace"
-       fi
-
-       # Prepend any custom changes to PATH
-       if [ -n "${CUSTOM_PATH}" ]; then
-               PATH="${CUSTOM_PATH}:${PATH}"
-       fi
-
-       # Create a new environment
-       local env=(
-               # Clear the previous environment
-               "--ignore-environment"
-
-               HOME="/root"
-               TERM="${TERM}"
-               PS1="${PS1}"
-               PATH="${PATH}"
-
-               # Compiler flags
-               CFLAGS="${CFLAGS} ${HARDENING_CFLAGS}"
-               CXXFLAGS="${CXXFLAGS} ${HARDENING_CFLAGS}"
-               RUSTFLAGS="${RUSTFLAGS}"
-
-               # Compiler cache
-               CCACHE_DIR="/usr/src/ccache"
-               CCACHE_TEMPDIR="${CCACHE_TEMPDIR}"
-               CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK}"
-
-               # Go cache
-               GOCACHE="/usr/src/ccache/go"
-
-               # Append the fake environment
-               $(fake_environ)
-
-               # Setup the QEMU environment
-               $(qemu_environ)
-       )
-
-       # 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"
-       )
-
-       # Put everything together
-       unshare "${unshare[@]}" chroot "${BUILD_DIR}" env "${env[@]}" "$@"
+       execute --chroot ccache --max-size="${CCACHE_CACHE_SIZE}"
 }
 
 entershell() {
@@ -697,7 +623,7 @@ entershell() {
        local PS1="ipfire build chroot (${BUILD_ARCH}) \u:\w\$ "
 
        # Run an interactive shell
-       enterchroot bash -i
+       execute --chroot --interactive bash -i
 }
 
 lfsmakecommoncheck() {
@@ -725,68 +651,113 @@ lfsmakecommoncheck() {
        return 0        # pass all!
 }
 
-lfsmake1() {
-       lfsmakecommoncheck $*
-       [ $? == 1 ] && return 0
+execute() {
+       local command=()
 
-       # Set PATH to use the toolchain tools first and then whatever the host has set
-       local PATH="${TOOLS_DIR}/ccache/bin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin:${PATH}"
+       local basedir="${BASEDIR}"
+       local chroot="false"
+       local interactive="false"
+       local timer
 
-       if [ -n "${CUSTOM_PATH}" ]; then
-               PATH="${CUSTOM_PATH}:${PATH}"
+       # Check if we are running in our namespace
+       if [ -z "${IN_NAMESPACE}" ]; then
+               exiterror "Not running in namespace"
        fi
 
-       cd $BASEDIR/lfs && env -i \
-               PATH="${PATH}" \
-               CCACHE_DIR="${CCACHE_DIR}" \
-               CCACHE_TEMPDIR="${CCACHE_TEMPDIR}" \
-               CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK}" \
-               CFLAGS="${CFLAGS}" \
-               CXXFLAGS="${CXXFLAGS}" \
-               DEFAULT_PARALLELISM="${DEFAULT_PARALLELISM}" \
-               SYSTEM_PROCESSORS="${SYSTEM_PROCESSORS}" \
-               SYSTEM_MEMORY="${SYSTEM_MEMORY}" \
-               make -f $* \
-                       TOOLCHAIN=1 \
-                       TOOLS_DIR="${TOOLS_DIR}" \
-                       CROSSTARGET="${CROSSTARGET}" \
-                       BUILDTARGET="${BUILDTARGET}" \
-                       BUILD_ARCH="${BUILD_ARCH}" \
-                       BUILD_PLATFORM="${BUILD_PLATFORM}" \
-                       LFS_BASEDIR="${BASEDIR}" \
-                       ROOT="${BUILD_DIR}" \
-                       KVER="${KVER}" \
-                       install >> $LOGFILE 2>&1 &
-
-       if ! wait_until_finished $!; then
-               print_status FAIL
-               exiterror "Building $*"
-       fi
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --chroot)
+                               chroot="true"
 
-       print_status DONE
-}
+                               # Move the basedir
+                               basedir="/usr/src"
+                               ;;
 
-run_command() {
-       local pkg
-       local actions=()
+                       --interactive)
+                               interactive="true"
+                               ;;
 
-       local basedir="${BASEDIR}"
-       local command=()
-       local quiet="false"
+                       --timer=*)
+                               timer="${1#--timer=}"
+                               ;;
 
-       # Pass some variables
-       local vars=(
-               BUILD_ARCH="${BUILD_ARCH}"
-               BUILD_PLATFORM="${BUILD_PLATFORM}"
+                       -*)
+                               echo "Unknown argument: ${1}" >&2
+                               return 2
+                               ;;
 
-               # Targets
-               CROSSTARGET="${CROSSTARGET}"
-               BUILDTARGET="${BUILDTARGET}"
+                       # Parse any custom environment variables
+                       *=*)
+                               env+=( "${1}" )
+                               ;;
 
-               # Paths
-               CONFIG_ROOT="${CONFIG_ROOT}"
-               IMAGES_DIR="/usr/src/images"
-               TOOLS_DIR="${TOOLS_DIR}"
+                       # The rest is the command
+                       *)
+                               command+=( "$@" )
+                               break
+                               ;;
+               esac
+               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}"
+       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}"
@@ -796,6 +767,7 @@ run_command() {
                SLOGAN="${SLOGAN}"
                SYSTEM_RELEASE="${SYSTEM_RELEASE}"
                PAKFIRE_TREE="${PAKFIRE_TREE}"
+               CONFIG_ROOT="${CONFIG_ROOT}"
 
                # Kernel Version
                KVER="${KVER}"
@@ -809,58 +781,81 @@ run_command() {
 
                # Compression Options
                XZ_OPT="${XZ_OPT}"
+
+               # 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)
+
+               # Setup the QEMU environment
+               $(qemu_environ)
+
+               BUILD_ARCH="${BUILD_ARCH}"
+               BUILD_PLATFORM="${BUILD_PLATFORM}"
+
+               # Targets
+               CROSSTARGET="${CROSSTARGET}"
+               BUILDTARGET="${BUILDTARGET}"
+
+               # Paths
+               LFS_BASEDIR="${basedir}"
+               TOOLS_DIR="${TOOLS_DIR}"
        )
 
-       while [ $# -gt 0 ]; do
-               case "${1}" in
-                       --chroot)
-                               command+=( "enterchroot" )
+       # Add extra environment variables for the chroot environment
+       case "${chroot}" in
+               true)
+                       env+=(
+                               IMAGES_DIR="/usr/src/images"
+                       )
+                       ;;
+       esac
 
-                               # Move the basedir
-                               basedir="/usr/src"
-                               ;;
+       # Configure a new namespace
+       local unshare=(
+               # Create a new cgroup namespace
+               "--cgroup"
 
-                       --quiet)
-                               quiet="true"
-                               ;;
+               # Create a new mount namespace
+               "--mount"
+               "--propagation=slave"
 
-                       -*)
-                               echo "Unknown argument: ${1}" >&2
-                               return 2
-                               ;;
+               # Create a new PID namespace and fork
+               "--pid"
+               "--fork"
 
-                       *=*)
-                               vars+=( "${1}" )
-                               ;;
+               # Create a new time namespace
+               "--time"
 
-                       *)
-                               # Set pkg
-                               if [ -z "${pkg}" ]; then
-                                       pkg="${1}"
+               # Create a new UTS namespace
+               "--uts"
 
-                               # Set actions
-                               else
-                                       actions+=( "${1}" )
-                               fi
-                               ;;
-               esac
-               shift
-       done
+               # Mount /proc so that the build environment does not see
+               # any foreign processes.
+               "--mount-proc=${BUILD_DIR}/proc"
 
-       # Pass LFS_BASEDIR
-       vars+=(
-               "LFS_BASEDIR=${basedir}"
+               # If unshare is asked to terminate, terminate all child processes
+               "--kill-child"
        )
 
-       # Build the command
-       command+=(
-               # Run make
-               "make"
-                       "--directory=${basedir}/lfs"
-                       "--file=${pkg}"
-                       "${vars[@]}"
-                       "${actions[@]}"
-       )
+       # Prepend the fresh environment to the command
+       command=( "env" "${env[@]}" "${command[@]}" )
+
+       # Change root?
+       case "${chroot}" in
+               true)
+                       command=( "chroot" "${BUILD_DIR}" "${command[@]}" )
+                       ;;
+       esac
+
+       # Create new namespaces
+       command=( "unshare" "${unshare[@]}" "${command[@]}" )
 
        # Return code
        local r=0
@@ -868,58 +863,95 @@ run_command() {
        # Store the start time
        local t="${SECONDS}"
 
-       # If we are not running in quiet mode, we set the timer
-       case "${quiet}" in
-               false)
-                       launch_timer
+       # Run the command in the background and pipe all output to the logfile
+       case "${interactive}" in
+               true)
+                       "${command[@]}" || return $?
                        ;;
-       esac
 
-       # Run the command in the background and pipe all output to the logfile
-       {
-               "${command[@]}" >> "${LOGFILE}" 2>&1 </dev/null
-       } &
-
-       # Wait for the process to complete
-       while :; do
-               wait "$!"
-
-               # Store the return code
-               r="$?"
-
-               # If the return code is >= 128, wait has been interrupted by the timer
-               if [ "${r}" -ge 128 ]; then
-                       # Update the runtime
-                       case "${quiet}" in
-                               false)
-                                       print_runtime "$(( SECONDS - t ))"
-                                       ;;
-                       esac
-
-                       continue
-               fi
+               false)
+                       # Launch the timer if needed
+                       if [ -n "${timer}" ]; then
+                               launch_timer
+                       fi
 
-               break
-       done
+                       # Dispatch the command to the background
+                       {
+                               "${command[@]}" >> "${LOGFILE}" 2>&1 </dev/null
+                       } &
 
-       # Show runtime and status unless quiet
-       case "${quiet}" in
-               false)
-                       # Print the runtime
-                       print_runtime "$(( SECONDS - t ))"
-
-                       # All done
-                       if [ "${r}" -eq 0 ]; then
-                               print_status DONE
-                       else
-                               print_status FAIL
+                       # Wait for the process to complete
+                       while :; do
+                               wait "$!"
+
+                               # Store the return code
+                               r="$?"
+
+                               # If the return code is >= 128, wait has been interrupted by the timer
+                               if [ "${r}" -ge 128 ]; then
+                                       # Call the timer callback
+                                       if [ -n "${timer}" ]; then
+                                               "${timer}"
+                                       fi
+
+                                       # Go back and wait
+                                       continue
+                               fi
+
+                               break
+                       done
+
+                       # Call the timer callback at least once
+                       if [ -n "${timer}" ]; then
+                               "${timer}"
                        fi
-                       ;;
        esac
 
        return "${r}"
 }
 
+# Calls the makefile of a package
+make_pkg() {
+       local args=()
+       local pkg
+
+       while [ $# -gt 0 ]; do
+               local arg="${1}"
+               shift
+
+               case "${arg}" in
+                       --*)
+                               args+=( "${arg}" )
+                               ;;
+
+                       *)
+                               pkg="${arg}"
+                               break
+                               ;;
+               esac
+       done
+
+       # Execute the make command in the environment
+       execute "${args[@]}" make --file="${pkg}" "$@"
+}
+
+lfsmake1() {
+       local pkg="${1}"
+       shift
+
+       # Run the common check
+       lfsmakecommoncheck "${pkg}" "$@"
+       [ $? == 1 ] && return 0
+
+       if ! make_pkg "${pkg}" TOOLCHAIN=1 ROOT="${BUILD_DIR}"; then
+               print_status FAIL
+
+               exiterror "Building ${pkg}"
+       fi
+
+       print_status DONE
+}
+
 lfsmake2() {
        local pkg="${1}"
        shift
@@ -929,14 +961,18 @@ lfsmake2() {
        [ $? == 1 ] && return 0
 
        # Download source outside of the toolchain
-       if ! run_command --quiet "${pkg}" download b2 "$@"; then
+       if ! make_pkg "${pkg}" download b2 "$@"; then
                exiterror "Downloading ${pkg}"
        fi
 
        # Run install on the package
-       if ! run_command --chroot "${pkg}" install "$@"; then
+       if ! make_pkg --chroot --timer="update_runtime" "${pkg}" install "$@"; then
+               print_status FAIL
+
                exiterror "Building ${pkg}"
        fi
+
+       print_status DONE
 }
 
 ipfiredist() {
@@ -948,40 +984,17 @@ ipfiredist() {
        [ $? == 1 ] && return 0
 
        # Run dist on the package
-       if ! run_command --chroot "${pkg}" dist "$@"; then
-               exiterror "Packging ${pkg}"
-       fi
-}
-
-wait_until_finished() {
-       local pid=${1}
-
-       local start_time="${SECONDS}"
-
-       # 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 $(( SECONDS - start_time ))
+       if ! make_pkg --chroot --timer="update_runtime" "${pkg}" dist "$@"; then
+               print_status FAIL
 
-                       # Wait a little
-                       sleep 1
-               done
+               exiterror "Packaging ${pkg}"
        fi
 
-       # Returns the exit code of the child process
-       wait ${pid}
-       local ret=$?
-
-       if ! ${INTERACTIVE}; then
-               print_runtime $(( SECONDS - start_time ))
-       fi
+       print_status DONE
+}
 
-       return ${ret}
+update_runtime() {
+       print_runtime "$(( SECONDS - t ))"
 }
 
 fake_environ() {