build 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/lbmk" ] || tmpdir_was_set="n"
  17. fi
  18. if [ "${tmpdir_was_set}" = "n" ]; then
  19. export TMPDIR="/tmp"
  20. tmpdir="$(mktemp -d -t lbmk_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 $@ && lbmk_exit 0
  34. for cmd in initcmd check_git check_project git_init excmd; do
  35. eval "${cmd} \$@"
  36. done
  37. lbmk_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. help) usage ${0} ;;
  45. list) items "${buildpath}" ;;
  46. version) mkversion ;;
  47. *)
  48. option="${1}"
  49. return 0 ;;
  50. esac
  51. lbmk_exit 0
  52. }
  53. install_packages()
  54. {
  55. if [ $# -lt 2 ]; then
  56. printf "You must specify a distro, namely:\n" 1>&2
  57. printf "Look at files under config/dependencies/\n" 1>&2
  58. printf "Example: ./build dependencies debian\n" 1>&2
  59. fail "install_packages: target not specified"
  60. fi
  61. [ -f "config/dependencies/${2}" ] || fail "Unsupported target"
  62. . "config/dependencies/${2}"
  63. xx_ ${pkg_add} ${pkglist}
  64. [ -z "${aur_notice}" ] && return 0
  65. printf "You must install AUR packages: %s\n" "$aur_notice" 1>&2
  66. }
  67. # release archives contain .gitignore, but not .git.
  68. # lbmk can be run from lbmk.git, or an archive.
  69. git_init()
  70. {
  71. [ -L ".git" ] && fail "Reference .git is a symlink"
  72. [ -e ".git" ] && return 0
  73. eval "$(setvars "$(date -Rd @${versiondate})" cdate _nogit)"
  74. git init || fail "${PWD}: cannot initialise Git repository"
  75. git add -A . || fail "${PWD}: cannot add files to Git repository"
  76. git commit -m "${projectname} ${version}" --date "${cdate}" \
  77. --author="lbmk <lbmk@libreboot.org>" || \
  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. excmd()
  83. {
  84. lbmkcmd="${buildpath}/${option}"
  85. [ -f "${lbmkcmd}" ] || fail "Invalid command. Run: ${linkpath} help"
  86. shift 1; "$lbmkcmd" $@ || fail "excmd: ${lbmkcmd} ${@}"
  87. }
  88. usage()
  89. {
  90. progname=${0}
  91. cat <<- EOF
  92. $(mkversion)
  93. USAGE: ${progname} <OPTION>
  94. possible values for 'OPTION':
  95. $(items "${buildpath}")
  96. To know what ${projectname} version you're on, type:
  97. ${progname} version
  98. Refer to ${projectname} documentation for more info.
  99. EOF
  100. }
  101. mkversion()
  102. {
  103. printf "revision: %s %s\n" "$projectname" "$version"
  104. printf "revision date: %s\n" "$(date -Rud @${versiondate})"
  105. }
  106. lbmk_exit()
  107. {
  108. tmp_cleanup || err "lbmk_exit: can't rm tmpdir upon exit $1: $tmpdir"
  109. exit $1
  110. }
  111. fail()
  112. {
  113. tmp_cleanup || printf "WARNING: can't rm tmpdir: %s\n" "$tmpdir" 1>&2
  114. err "${1}"
  115. }
  116. tmp_cleanup()
  117. {
  118. [ "${tmpdir_was_set}" = "n" ] || return 0
  119. rm -Rf "${tmpdir}" || return 1
  120. }
  121. main $@