build 3.3 KB

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