#!/bin/sh set -e NCPUS=$(sysctl hw.ncpuonline | sed 's/.*=//') # Try to find a valid way to become root if not already SUDO= if [ -e '/etc/doas.conf' ]; then SUDO='/usr/bin/doas' elif [ -x '/usr/local/bin/sudo' ]; then SUDO='/usr/local/bin/sudo' fi # Bail out if not root and neither sudo or doas was found. _uid=$(id -u) if [ -z ${SUDO} -a \( ${_uid} -ne 0 \) ]; then echo 'Need a way to become root, neither sudo or doas found...' 1>&2 exit 1 elif [ ${_uid} -eq 0 ]; then # Clear SUDO if already root SUDO= fi # Find the source tree for the base system if [ -e /etc/mk.conf ]; then BSDSRCDIR=$(make -f /etc/mk.conf -V BSDSRCDIR) BSDOBJDIR=$(make -f /etc/mk.conf -V BSDOBJDIR) fi BSDSRCDIR=${BSDSRCDIR:=/usr/src} BSDOBJDIR=${BSDOBJDIR:=/usr/obj} START_TIME=$(date) # Make a backup of kernel config files before cleaning /usr/obj _kernel_obj_tar=$(mktemp) || { echo "Unable to create temporary file for kernel obj backup" exit 1 } if stat -q -f '' /usr/obj/sys/arch/*/compile/[GR]* ; then tar -czf ${_kernel_obj_tar} /usr/obj/sys/arch/*/compile/[GR]* else rm ${_kernel_obj_tar} fi cd ${BSDOBJDIR} && mkdir -p .old && ${SUDO} mv * .old && ${SUDO} rm -rf .old & cd ${BSDSRCDIR} && ${SUDO} make obj cd ${BSDSRCDIR} || { echo "Unable to chdir to source directory" ; exit 1; } ${SUDO} make build -j ${NCPUS} RESULT=$? test -f ${_kernel_obj_tar} && \ tar -C / -xzphf ${_kernel_obj_tar} && rm ${_kernel_obj_tar} END_TIME=$(date) echo "Build started at ${START_TIME}, ended at ${END_TIME}" exit ${RESULT}