libreboot 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/bash
  2. # Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. GLOBIGNORE=".:.." # This enables the shell option 'dotglob' as well.
  17. shopt -s nullglob
  18. libreboot_usage() {
  19. local action
  20. local target
  21. printf "${executable} [action] [target] [arguments]\n" >&2
  22. printf "\nGeneric project actions:\n" >&2
  23. for action in ${PROJECT_ACTIONS_GENERIC}; do
  24. printf " ${action}\n" >&2
  25. done
  26. printf "\nVirtual project actions:\n" >&2
  27. printf " sources\n"
  28. printf " produce\n"
  29. printf " test\n"
  30. printf "\nProject targets:\n" >&2
  31. for target in "${root}/${PROJECTS}"/*; do
  32. if project_check "${target}"; then
  33. printf " ${target}\n" >&2
  34. fi
  35. done
  36. printf "\nGeneric tool actions:\n" >&2
  37. for action in ${TOOL_ACTIONS_GENERIC}; do
  38. printf " ${action}\n" >&2
  39. done
  40. printf "\nTool targets:\n" >&2
  41. for target in "${root}/${TOOLS}"/*; do
  42. if tool_check "${target}"; then
  43. printf " ${target}\n" >&2
  44. fi
  45. done
  46. printf "\nEnvironment variables:\n" >&2
  47. printf " PROJECTS_FORCE - Projects to always perform actions for\n" >&2
  48. printf " TOOLS_FORCE - Tools to always perform actions for\n" >&2
  49. printf " RELEASE_KEY - GPG key to use for release\n" >&2
  50. printf " VBOOT_KEYS_PATH - Path to the vboot keys\n" >&2
  51. printf " TASKS - Number of simultaneous tasks to run\n" >&2
  52. printf " VERSION - Version string to use\n" >&2
  53. printf "\nConfiguration files:\n" >&2
  54. printf " ${BUILD_SYSTEM}.conf - Environment variables configuration\n" >&2
  55. }
  56. libreboot_project() {
  57. action="$1"
  58. shift
  59. project="$1"
  60. shift
  61. case ${action} in
  62. "sources")
  63. (
  64. set +e
  65. project_action_arguments "extract" "${project}" "$@" && return 0
  66. project_action_arguments "download" "${project}" "$@" && return 0
  67. )
  68. ;;
  69. "produce")
  70. for action in "build" "install" "release"; do
  71. project_action_arguments "${action}" "${project}" "$@"
  72. done
  73. ;;
  74. "test")
  75. for action in ${PROJECT_ACTIONS}; do
  76. project_action_arguments "${action}" "${project}" "$@"
  77. done
  78. ;;
  79. *)
  80. if ! project_function_check "${project}" "${action}"; then
  81. libreboot_usage
  82. exit 1
  83. fi
  84. if [[ "${action}" == "usage" ]]; then
  85. project_action "${action}" "${project}" "$@"
  86. else
  87. project_action_arguments "${action}" "${project}" "$@"
  88. fi
  89. ;;
  90. esac
  91. }
  92. libreboot_tool() {
  93. action="$1"
  94. shift
  95. tool="$1"
  96. shift
  97. case ${action} in
  98. *)
  99. if ! tool_function_check "${tool}" "${action}"; then
  100. libreboot_usage
  101. exit 1
  102. fi
  103. if [[ "${action}" == "usage" ]]; then
  104. tool_action "${action}" "${tool}" "$@"
  105. else
  106. tool_action_arguments_recursive "${action}" "${tool}" "$@"
  107. fi
  108. ;;
  109. esac
  110. }
  111. libreboot_setup() {
  112. root="$(realpath "$(dirname "$0")")"
  113. executable="$(basename "$0")"
  114. local requirements="git"
  115. local requirement_path
  116. libreboot_setup_include
  117. libreboot_setup_variables
  118. for requirement in ${requirements}; do
  119. requirement_path="$(which "${requirement}" || true)"
  120. if [[ -z "${requirement_path}" ]]; then
  121. printf "Missing requirement: ${requirement}\n" >&2
  122. exit 1
  123. fi
  124. done
  125. }
  126. libreboot_setup_include() {
  127. local libs_path="${root}/libs"
  128. local conf_path
  129. source "${libs_path}/project"
  130. source "${libs_path}/tool"
  131. source "${libs_path}/common"
  132. source "${libs_path}/git"
  133. conf_path="${root}/${BUILD_SYSTEM}.conf"
  134. if [[ -f "${conf_path}" ]]; then
  135. source "${conf_path}"
  136. fi
  137. }
  138. libreboot_setup_variables() {
  139. local vboot_tools_path="$(project_install_path 'vboot' 'tools')"
  140. local version_path="${root}/${DOTVERSION}"
  141. local epoch_path="${root}/${DOTEPOCH}"
  142. if [[ -z "${TASKS}" ]]; then
  143. TASKS=1
  144. fi
  145. if [[ -z "${VERSION}" ]]; then
  146. if git_check "${root}"; then
  147. VERSION="${BUILD_SYSTEM}-$(git_describe "${root}" 2> /dev/null || echo 'git')"
  148. elif [[ -f "${version_path}" ]]; then
  149. VERSION="$(cat "${version_path}")"
  150. else
  151. VERSION="${BUILD_SYSTEM}"
  152. fi
  153. fi
  154. if [[ -z "${SOURCE_DATE_EPOCH}" ]]; then
  155. if git_check "${root}"; then
  156. SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
  157. elif [[ -f "${epoch_path}" ]]; then
  158. SOURCE_DATE_EPOCH="$(cat "${epoch_path}")"
  159. else
  160. SOURCE_DATE_EPOCH="$(date +%s)"
  161. fi
  162. fi
  163. if [[ -z "${VBOOT_KEYS_PATH}" ]] && [[ -d "${vboot_tools_path}/devkeys/" ]]; then
  164. VBOOT_KEYS_PATH="${vboot_tools_path}/devkeys/"
  165. fi
  166. if [[ -z "${EDITOR}" ]]; then
  167. EDITOR="vi"
  168. fi
  169. }
  170. libreboot() {
  171. action="$1"
  172. shift
  173. target="$1"
  174. shift
  175. set -e
  176. libreboot_setup "$@"
  177. if [[ -z "${action}" ]] || [[ -z "${target}" ]]; then
  178. libreboot_usage
  179. exit 1
  180. fi
  181. requirements "tar" "sed" "gpg" "sha256sum"
  182. if project_check "${target}"; then
  183. libreboot_project "${action}" "${target}" "$@"
  184. elif tool_check "${target}"; then
  185. libreboot_tool "${action}" "${target}" "$@"
  186. else
  187. libreboot_usage
  188. exit 1
  189. fi
  190. }
  191. libreboot "$@"