libreboot 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/usr/bin/env 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 '%s\n' "${executable} [action] [target] [arguments]" >&2
  22. printf '\n%s\n' 'Generic project actions:' >&2
  23. for action in ${PROJECT_ACTIONS_GENERIC}; do
  24. printf '%s\n' " ${action}" >&2
  25. done
  26. printf '\n%s\n' 'Virtual project actions:' >&2
  27. printf '%s\n' ' sources'
  28. printf '%s\n' ' produce'
  29. printf '%s\n' ' test'
  30. printf '\n%s\n' 'Project targets:' >&2
  31. for target in "${root}/${PROJECTS}"/*; do
  32. if project_check "${target}"; then
  33. printf '%s\n' " ${target}" >&2
  34. fi
  35. done
  36. printf '\n%s\n' 'Generic tool actions:' >&2
  37. for action in ${TOOL_ACTIONS_GENERIC}; do
  38. printf '%s\n' " ${action}" >&2
  39. done
  40. printf '\n%s\n' 'Tool targets:' >&2
  41. for target in "${root}/${TOOLS}"/*; do
  42. if tool_check "${target}"; then
  43. printf '%s\n' " ${target}" >&2
  44. fi
  45. done
  46. printf '\n%s\n' 'Environment variables:' >&2
  47. printf '%s\n' ' PROJECTS_FORCE - Projects to always perform actions for' >&2
  48. printf '%s\n' ' TOOLS_FORCE - Tools to always perform actions for' >&2
  49. printf '%s\n' ' RELEASE_KEY - GPG key to use for release' >&2
  50. printf '%s\n' ' VBOOT_KEYS_PATH - Path to the vboot keys' >&2
  51. printf '%s\n' ' LIBFAKETIME_PATH - Path to the libfaketime shared library' >&2
  52. printf '%s\n' ' TASKS - Number of simultaneous tasks to run' >&2
  53. printf '%s\n' ' VERSION - Version string to use' >&2
  54. printf '\n%s\n' 'Configuration files:' >&2
  55. printf '%s\n' " ${BUILD_SYSTEM}.conf - Environment variables configuration" >&2
  56. }
  57. libreboot_project() {
  58. action="$1"
  59. shift
  60. project="$1"
  61. shift
  62. case ${action} in
  63. "sources")
  64. (
  65. set +e
  66. project_action_arguments "extract" "${project}" "$@" && return 0
  67. project_action_arguments "download" "${project}" "$@" && return 0
  68. )
  69. ;;
  70. "produce")
  71. for action in "build" "install" "release"; do
  72. project_action_arguments "${action}" "${project}" "$@"
  73. done
  74. ;;
  75. "test")
  76. for action in ${PROJECT_ACTIONS}; do
  77. project_action_arguments "${action}" "${project}" "$@"
  78. done
  79. ;;
  80. *)
  81. if ! project_function_check "${project}" "${action}"; then
  82. libreboot_usage
  83. exit 1
  84. fi
  85. if [[ "${action}" == "usage" ]]; then
  86. project_action "${action}" "${project}" "$@"
  87. else
  88. project_action_arguments "${action}" "${project}" "$@"
  89. fi
  90. ;;
  91. esac
  92. }
  93. libreboot_tool() {
  94. action="$1"
  95. shift
  96. tool="$1"
  97. shift
  98. case ${action} in
  99. *)
  100. if ! tool_function_check "${tool}" "${action}"; then
  101. libreboot_usage
  102. exit 1
  103. fi
  104. if [[ "${action}" == "usage" ]]; then
  105. tool_action "${action}" "${tool}" "$@"
  106. else
  107. tool_action_arguments_recursive "${action}" "${tool}" "$@"
  108. fi
  109. ;;
  110. esac
  111. }
  112. libreboot_setup() {
  113. root="$(readlink -f "$(dirname "$0")")"
  114. executable="$(basename "$0")"
  115. local requirements="git"
  116. local requirement_path
  117. libreboot_setup_include
  118. libreboot_setup_variables
  119. for requirement in ${requirements}; do
  120. requirement_path="$(which "${requirement}" || true)"
  121. if [[ -z "${requirement_path}" ]]; then
  122. printf '%s\n' "Missing requirement: ${requirement}" >&2
  123. exit 1
  124. fi
  125. done
  126. }
  127. libreboot_setup_include() {
  128. local libs_path="${root}/libs"
  129. local conf_path
  130. source "${libs_path}/project"
  131. source "${libs_path}/tool"
  132. source "${libs_path}/common"
  133. source "${libs_path}/git"
  134. conf_path="${root}/${BUILD_SYSTEM}.conf"
  135. if [[ -f "${conf_path}" ]]; then
  136. source "${conf_path}"
  137. fi
  138. }
  139. libreboot_setup_variables() {
  140. local vboot_tools_path="$(project_install_path 'vboot' 'tools')"
  141. local version_path="${root}/${DOTVERSION}"
  142. local epoch_path="${root}/${DOTEPOCH}"
  143. local rnd_seed_path="${root}/${DOTRNDSEED}"
  144. # Used by GCC, e.g., -frandom-seed="${RANDOM_SEED}"
  145. if [[ -z "${RANDOM_SEED}" ]]; then
  146. if [[ -f "${rnd_seed_path}" ]]; then
  147. RANDOM_SEED="$(cat "${rnd_seed_path}")"
  148. else
  149. RANDOM_SEED="${RANDOM}" # True randomness is unnecessary
  150. fi
  151. fi
  152. # Also used by GCC, but as an environment variable
  153. if [[ -z "${SOURCE_DATE_EPOCH}" ]]; then
  154. if git_check "${root}"; then
  155. SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
  156. elif [[ -f "${epoch_path}" ]]; then
  157. SOURCE_DATE_EPOCH="$(cat "${epoch_path}")"
  158. else
  159. SOURCE_DATE_EPOCH="$(date +%s)"
  160. fi
  161. fi
  162. if [[ -z "${VERSION}" ]]; then
  163. if git_check "${root}"; then
  164. VERSION="${BUILD_SYSTEM}-$(git_describe "${root}" 2> /dev/null || echo 'git')"
  165. elif [[ -f "${version_path}" ]]; then
  166. VERSION="$(cat "${version_path}")"
  167. else
  168. VERSION="${BUILD_SYSTEM}"
  169. fi
  170. fi
  171. if [[ -d "${vboot_tools_path}/devkeys/" ]]; then
  172. VBOOT_KEYS_PATH="${VBOOT_KEYS_PATH:-${vboot_tools_path}/devkeys/}"
  173. fi
  174. CONFIG_SHELL="${CONFIG_SHELL:-$(which bash)}"
  175. EDITOR="${EDITOR:-$(which vi || true)}"
  176. TASKS="${TASKS:-1}"
  177. # Environment variables useful for creating reproducible builds
  178. if [[ -n "${LIBFAKETIME_PATH}" ]]; then
  179. BUILD_DATE_FMT="%Y-%m-%d %H:%M:%S"
  180. BUILD_DATE="$(date -u -d "@${SOURCE_DATE_EPOCH}" "+${BUILD_DATE_FMT}" 2>/dev/null || date -u -r "${SOURCE_DATE_EPOCH}" "+${BUILD_DATE_FMT}" 2>/dev/null || date -u "+${BUILD_DATE_FMT}")"
  181. FAKETIME="@${BUILD_DATE}"
  182. LC_ALL='C.UTF-8'
  183. LD_PRELOAD="${LIBFAKETIME_PATH}"
  184. TZ='UTC'
  185. fi
  186. }
  187. libreboot() {
  188. action="$1"
  189. shift
  190. target="$1"
  191. shift
  192. set -e
  193. libreboot_setup "$@"
  194. if [[ -z "${action}" ]] || [[ -z "${target}" ]]; then
  195. libreboot_usage
  196. exit 1
  197. fi
  198. requirements "tar" "sed" "gpg" "sha256sum"
  199. if project_check "${target}"; then
  200. libreboot_project "${action}" "${target}" "$@"
  201. elif tool_check "${target}"; then
  202. libreboot_tool "${action}" "${target}" "$@"
  203. else
  204. libreboot_usage
  205. exit 1
  206. fi
  207. }
  208. libreboot "$@"