build 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # SPDX-FileCopyrightText: 2014,2015,2020,2021,2023 Leah Rowe <leah@libreboot.org>
  4. # SPDX-FileCopyrightText: 2015 Patrick "P. J." McDermott <pj@pehjota.net>
  5. # SPDX-FileCopyrightText: 2015, 2016 Klemens Nanni <contact@autoboot.org>
  6. # SPDX-FileCopyrightText: 2022, Caleb La Grange <thonkpeasant@protonmail.com>
  7. set -u -e
  8. export LC_COLLATE=C
  9. export LC_ALL=C
  10. . "include/err.sh"
  11. . "include/option.sh"
  12. eval "$(setvars "" option aur_notice tmpdir)"
  13. tmpdir_was_set="y"
  14. set | grep TMPDIR 1>/dev/null 2>/dev/null || tmpdir_was_set="n"
  15. if [ "${tmpdir_was_set}" = "y" ]; then
  16. [ "${TMPDIR%_*}" = "/tmp/cbmk" ] || tmpdir_was_set="n"
  17. fi
  18. if [ "${tmpdir_was_set}" = "n" ]; then
  19. export TMPDIR="/tmp"
  20. tmpdir="$(mktemp -d -t cbmk_XXXXXXXX)"
  21. export TMPDIR="${tmpdir}"
  22. else
  23. export TMPDIR="${TMPDIR}"
  24. tmpdir="${TMPDIR}"
  25. fi
  26. linkpath="${0}"
  27. linkname="${linkpath##*/}"
  28. buildpath="./script/${linkname}"
  29. main()
  30. {
  31. xx_ id -u 1>/dev/null 2>/dev/null
  32. [ $# -lt 1 ] && fail "Too few arguments. Try: ${0} help"
  33. [ "$1" = "dependencies" ] && xx_ install_packages $@ && cbmk_exit 0
  34. for cmd in initcmd check_git check_project excmd; do
  35. eval "${cmd} \$@"
  36. done
  37. cbmk_exit 0
  38. }
  39. initcmd()
  40. {
  41. [ "$(id -u)" != "0" ] || fail "this command as root is not permitted"
  42. check_project
  43. case "${1}" in
  44. list) items "${buildpath}" ;;
  45. *)
  46. option="${1}"
  47. return 0 ;;
  48. esac
  49. cbmk_exit 0
  50. }
  51. install_packages()
  52. {
  53. if [ $# -lt 2 ]; then
  54. printf "You must specify a distro, namely:\n" 1>&2
  55. printf "Look at files under config/dependencies/\n" 1>&2
  56. printf "Example: ./build dependencies debian\n" 1>&2
  57. fail "install_packages: target not specified"
  58. fi
  59. [ -f "config/dependencies/${2}" ] || fail "Unsupported target"
  60. . "config/dependencies/${2}"
  61. xx_ ${pkg_add} ${pkglist}
  62. [ -z "${aur_notice}" ] && return 0
  63. printf "You must install AUR packages: %s\n" "$aur_notice" 1>&2
  64. }
  65. excmd()
  66. {
  67. cbmkcmd="${buildpath}/${option}"
  68. [ -f "${cbmkcmd}" ] || fail "Invalid command. Run: ${linkpath} help"
  69. shift 1; "$cbmkcmd" $@ || fail "excmd: ${cbmkcmd} ${@}"
  70. }
  71. cbmk_exit()
  72. {
  73. tmp_cleanup || err "cbmk_exit: can't rm tmpdir upon exit $1: $tmpdir"
  74. exit $1
  75. }
  76. fail()
  77. {
  78. tmp_cleanup || printf "WARNING: can't rm tmpdir: %s\n" "$tmpdir" 1>&2
  79. err "${1}"
  80. }
  81. tmp_cleanup()
  82. {
  83. [ "${tmpdir_was_set}" = "n" ] || return 0
  84. rm -Rf "${tmpdir}" || return 1
  85. }
  86. main $@