build 3.5 KB

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