build 3.5 KB

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