build 3.6 KB

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