buildsystem.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/sh
  2. set -e
  3. NCPUS=$(sysctl hw.ncpuonline | sed 's/.*=//')
  4. # Try to find a valid way to become root if not already
  5. SUDO=
  6. if [ -e '/etc/doas.conf' ]; then
  7. SUDO='/usr/bin/doas'
  8. elif [ -x '/usr/local/bin/sudo' ]; then
  9. SUDO='/usr/local/bin/sudo'
  10. fi
  11. # Bail out if not root and neither sudo or doas was found.
  12. _uid=$(id -u)
  13. if [ -z ${SUDO} -a \( ${_uid} -ne 0 \) ]; then
  14. echo 'Need a way to become root, neither sudo or doas found...' 1>&2
  15. exit 1
  16. elif [ ${_uid} -eq 0 ]; then
  17. # Clear SUDO if already root
  18. SUDO=
  19. fi
  20. # Find the source tree for the base system
  21. if [ -e /etc/mk.conf ]; then
  22. BSDSRCDIR=$(make -f /etc/mk.conf -V BSDSRCDIR)
  23. BSDOBJDIR=$(make -f /etc/mk.conf -V BSDOBJDIR)
  24. fi
  25. BSDSRCDIR=${BSDSRCDIR:=/usr/src}
  26. BSDOBJDIR=${BSDOBJDIR:=/usr/obj}
  27. START_TIME=$(date)
  28. # Make a backup of kernel config files before cleaning /usr/obj
  29. _kernel_obj_tar=$(mktemp) || {
  30. echo "Unable to create temporary file for kernel obj backup"
  31. exit 1
  32. }
  33. if stat -q -f '' /usr/obj/sys/arch/*/compile/[GR]* ; then
  34. tar -czf ${_kernel_obj_tar} /usr/obj/sys/arch/*/compile/[GR]*
  35. else
  36. rm ${_kernel_obj_tar}
  37. fi
  38. cd ${BSDOBJDIR} && mkdir -p .old && ${SUDO} mv * .old && ${SUDO} rm -rf .old &
  39. cd ${BSDSRCDIR} && ${SUDO} make obj
  40. cd ${BSDSRCDIR} || { echo "Unable to chdir to source directory" ; exit 1; }
  41. ${SUDO} make build -j ${NCPUS}
  42. RESULT=$?
  43. test -f ${_kernel_obj_tar} && \
  44. tar -C / -xzphf ${_kernel_obj_tar} && rm ${_kernel_obj_tar}
  45. END_TIME=$(date)
  46. echo "Build started at ${START_TIME}, ended at ${END_TIME}"
  47. exit ${RESULT}