build 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. [ "x${DEBUG+set}" = 'xset' ] && set -v
  8. set -u -e
  9. . "include/err.sh"
  10. . "include/option.sh"
  11. eval "$(setvars "" option aur_notice tmpdir)"
  12. tmpdir_was_set="y"
  13. set | grep TMPDIR 1>/dev/null 2>/dev/null || tmpdir_was_set="n"
  14. if [ "${tmpdir_was_set}" = "y" ]; then
  15. tmpdir="${TMPDIR##*/}"
  16. tmpdir="${TMPDIR%_*}"
  17. if [ "${tmpdir}" = "lbmk" ]; then
  18. tmpdir=""
  19. tmpdir_was_set="n"
  20. fi
  21. fi
  22. if [ "${tmpdir_was_set}" = "n" ]; then
  23. export TMPDIR="/tmp"
  24. tmpdir="$(mktemp -d -t lbmk_XXXXXXXX)"
  25. export TMPDIR="${tmpdir}"
  26. else
  27. export TMPDIR="${TMPDIR}"
  28. fi
  29. tmpdir="${TMPDIR}"
  30. linkpath="${0}"
  31. linkname="${linkpath##*/}"
  32. buildpath="./script/${linkname}"
  33. main()
  34. {
  35. xx_ id -u 1>/dev/null 2>/dev/null
  36. [ $# -lt 1 ] && fail "Too few arguments. Try: ${0} help"
  37. [ "${1}" = "dependencies" ] && xx_ install_packages $@ && lbmk_exit 0
  38. initialise_command $@ && shift 1
  39. check_git
  40. check_project "fail"
  41. git_init
  42. execute_command $@
  43. lbmk_exit 0
  44. }
  45. initialise_command()
  46. {
  47. [ "$(id -u)" != "0" ] || fail "this command as root is not permitted"
  48. case "${1}" in
  49. help) usage ${0} && lbmk_exit 0 ;;
  50. list) items "${buildpath}" && lbmk_exit 0 ;;
  51. esac
  52. option="${1}"
  53. }
  54. install_packages()
  55. {
  56. if [ $# -lt 2 ]; then
  57. printf "You must specify a distro, namely:\n" 1>&2
  58. printf "Look at files under config/dependencies/\n" 1>&2
  59. printf "Example: ./build dependencies debian\n" 1>&2
  60. fail "install_packages: target not specified"
  61. fi
  62. [ -f "config/dependencies/${2}" ] || fail "Unsupported target"
  63. . "config/dependencies/${2}"
  64. xx_ ${pkg_add} ${pkglist}
  65. [ -z "${aur_notice}" ] && return 0
  66. printf "You must install AUR packages: %s\n" "${aur_notice}" 1>&2
  67. }
  68. # release archives contain .gitignore, but not .git.
  69. # lbmk can be run from lbmk.git, or an archive.
  70. git_init()
  71. {
  72. [ -L ".git" ] && fail "Reference .git is a symlink"
  73. [ -e ".git" ] && return 0
  74. eval "$(setvars "$(date -Rd @${versiondate})" cdate _nogit)"
  75. git init || fail "${PWD}: cannot initialise Git repository"
  76. git add -A . || fail "${PWD}: cannot add files to Git repository"
  77. git commit -m "${projectname} ${version}" --date "${cdate}" || \
  78. fail "${PWD}: can't commit ${projectname}/${version}, date ${cdate}"
  79. git tag -a "${version}" -m "${projectname} ${version}" || \
  80. fail "${PWD}: cannot git-tag ${projectname}/${version}"
  81. }
  82. execute_command()
  83. {
  84. lbmkcmd="${buildpath}/${option}"
  85. [ -f "${lbmkcmd}" ] || fail "Invalid command. Run: ${linkpath} help"
  86. "${lbmkcmd}" $@ || fail "execute_command: ${lbmkcmd} ${@}"
  87. }
  88. usage()
  89. {
  90. progname=${0}
  91. cat <<- EOF
  92. USAGE: ${progname} <OPTION>
  93. possible values for 'OPTION':
  94. $(items "${buildpath}")
  95. Refer to ${projectname} documentation for more info.
  96. EOF
  97. }
  98. lbmk_exit()
  99. {
  100. tmp_cleanup || err "lbmk_exit: can't rm tmpdir upon exit $1: ${tmpdir}"
  101. exit $1
  102. }
  103. fail()
  104. {
  105. tmp_cleanup || printf "WARNING: can't rm tmpdir: %s\n" "${tmpdir}" 1>&2
  106. err "${1}"
  107. }
  108. tmp_cleanup()
  109. {
  110. [ "${tmpdir_was_set}" = "n" ] || return 0
  111. rm -Rf "${tmpdir}" || return 1
  112. }
  113. main $@