libreboot 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. elif [[ "${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="$(readlink -f "$(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 '%s\n' "Missing requirement: ${requirement}" >&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. local rnd_seed_path="${root}/${DOTRNDSEED}"
  143. # Used by GCC, e.g., -frandom-seed="${RANDOM_SEED}"
  144. if [[ -z "${RANDOM_SEED}" ]]; then
  145. if [[ -f "${rnd_seed_path}" ]]; then
  146. RANDOM_SEED="$(cat "${rnd_seed_path}")"
  147. else
  148. RANDOM_SEED="${RANDOM}" # True randomness is unnecessary
  149. fi
  150. fi
  151. # Also used by GCC, but as an environment variable
  152. if [[ -z "${SOURCE_DATE_EPOCH}" ]]; then
  153. if git_check "${root}"; then
  154. SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
  155. elif [[ -f "${epoch_path}" ]]; then
  156. SOURCE_DATE_EPOCH="$(cat "${epoch_path}")"
  157. else
  158. SOURCE_DATE_EPOCH="$(date +%s)"
  159. fi
  160. fi
  161. if [[ -z "${VERSION}" ]]; then
  162. if git_check "${root}"; then
  163. VERSION="${BUILD_SYSTEM}-$(git_describe "${root}" 2> /dev/null || echo 'git')"
  164. elif [[ -f "${version_path}" ]]; then
  165. VERSION="$(cat "${version_path}")"
  166. else
  167. VERSION="${BUILD_SYSTEM}"
  168. fi
  169. fi
  170. if [[ -d "${vboot_tools_path}/devkeys/" ]]; then
  171. VBOOT_KEYS_PATH="${VBOOT_KEYS_PATH:-${vboot_tools_path}/devkeys/}"
  172. fi
  173. CONFIG_SHELL="${CONFIG_SHELL:-$(which bash)}"
  174. EDITOR="${EDITOR:-$(which vi || true)}"
  175. TASKS="${TASKS:-1}"
  176. # Environment variables useful for creating reproducible builds
  177. if [[ -n "${LIBFAKETIME_PATH}" ]]; then
  178. BUILD_DATE_FMT="%Y-%m-%d %H:%M:%S"
  179. 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}")"
  180. FAKETIME="@${BUILD_DATE}"
  181. LC_ALL='C.UTF-8'
  182. LD_PRELOAD="${LIBFAKETIME_PATH}"
  183. TZ='UTC'
  184. fi
  185. }
  186. libreboot() {
  187. action="$1"
  188. shift
  189. target="$1"
  190. shift
  191. set -e
  192. libreboot_setup "$@"
  193. if [[ -z "${action}" ]] || [[ -z "${target}" ]]; then
  194. libreboot_usage
  195. exit 1
  196. fi
  197. requirements 'tar' 'sed' 'gpg' 'sha256sum' 'wget'
  198. if project_check "${target}"; then
  199. libreboot_project "${action}" "${target}" "$@"
  200. elif tool_check "${target}"; then
  201. libreboot_tool "${action}" "${target}" "$@"
  202. else
  203. libreboot_usage
  204. exit 1
  205. fi
  206. }
  207. libreboot "$@"