download 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env bash
  2. # Generic script for downloading programs used by the build system
  3. #
  4. # Copyright (C) 2014, 2015, 2020, 2021 Leah Rowe <info@minifree.org>
  5. # Copyright (C) 2015 Patrick "P. J." McDermott <pj@pehjota.net>
  6. # Copyright (C) 2015, 2016 Klemens Nanni <contact@autoboot.org>
  7. # Copyright (C) 2022, Caleb La Grange <thonkpeasant@protonmail.com>
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ./$(dirname $0)/.gitcheck
  23. [ "x${DEBUG+set}" = 'xset' ] && set -v
  24. set -u -e
  25. $(dirname $0)/resources/scripts/misc/versioncheck
  26. # set this when you want to modify each coreboot tree
  27. # for example, you want to test custom patches
  28. # NODELETE= ./download coreboot
  29. deleteblobs="true"
  30. [ "x${NODELETE+set}" = 'xset' ] && deleteblobs="false"
  31. rm -f "build_error"
  32. download="$(dirname $0)/resources/scripts/download"
  33. extra_dir=""
  34. listprograms() {
  35. for program in "${download}"/*; do
  36. printf '%s\n' "${program##*/}"
  37. done
  38. if [ "${extra_dir}" != "" ] ; then
  39. for program in ${extra_dir}/resources/scripts/download/*; do
  40. if [ -f "${download}/${program}" ] ; then
  41. continue
  42. fi
  43. printf '%s\n' "${program##*/}"
  44. done
  45. fi
  46. }
  47. help() {
  48. # As we handle only one --include-extra-dir for now, if
  49. # another script wrap this script with --include-extra-dir,
  50. # the wrapper would end up displaying --include-extra-dir in
  51. # help too. And if the user passes --include-extra-dir to the
  52. # wrapper script, then this script won't be able to handle two
  53. # --include-extra-dir and it would not work.
  54. if [ "${extra_dir}" == "" ] ; then
  55. echo "USAGE: ./download [--include-extra-dir <PATH>] <PROGRAM> <OPTIONS>"
  56. else
  57. echo "USAGE: ./download <PROGRAM> <OPTIONS>"
  58. fi
  59. cat <<- EOF
  60. possible values for 'program':
  61. $(listprograms)
  62. Example: ./download flashrom
  63. Example: ./download coreboot
  64. Some program options allow for additional parameters:
  65. Example: ./download coreboot default
  66. Example: ./download coreboot x60
  67. Each program download script should work without extra paramaters, but
  68. they can use them. For instance, './download coreboot' will download all
  69. coreboot trees by default, but './download coreboot x60' will only download
  70. the coreboot tree required for the target: x60
  71. Each program download script should also accept the --help paramater to
  72. display the usage of the script.
  73. Refer to the documentation for more information.
  74. EOF
  75. if [ "${extra_dir}" == "" ] ; then
  76. cat <<- EOF
  77. --include-extra-dir PATH makes download also look for scripts
  78. in PATH/resources/scripts/download/. If a script of the same
  79. name exist in both PATH/resources/scripts/download/ and
  80. resources/scripts/download/, the first one will be preffered.
  81. Example: ./download --include-extra-dir ../mydir coreboot
  82. EOF
  83. fi
  84. }
  85. die() {
  86. printf 'Error: %s\n' "${@}" 1>&2
  87. exit 1
  88. }
  89. run_download_program() {
  90. path="$1"
  91. shift 1
  92. if [ "${deleteblobs}" = "false" ]; then
  93. NODELETE= "${path}" $@
  94. else
  95. "${path}" $@
  96. fi
  97. }
  98. if [ $# -gt 1 ] && [ "$1" = "--include-extra-dir" ] ; then
  99. if [ $# -lt 2 ]; then
  100. die "Missing PATH for --include-extra-dir"
  101. else
  102. extra_dir="$2"
  103. shift 2
  104. fi
  105. fi
  106. if [ $# -lt 1 ]; then
  107. help
  108. die "Please specify arguments."
  109. fi
  110. program="${1}"
  111. shift 1
  112. [ "${program}" = help ] && help && exit 0
  113. if [ "${program}" = "all" ]; then
  114. for downloadProgram in ${download}/*; do
  115. if [ -f "${downloadProgram}" ]; then
  116. # If we have two scripts of the same name, the one in
  117. # --include-extra-dir will take precendece to enable
  118. # overriding scripts. So we execute later the one in
  119. # include-extra-dir instead.
  120. if [ "${extra_dir}" != "" ] && \
  121. [ -f "${extra_dir}/${downloadProgram}" ] ; then
  122. continue
  123. fi
  124. run_download_program "${downloadProgram}"
  125. fi
  126. done
  127. for downloadProgram in ${extra_dir}/resources/scripts/download/*; do
  128. if [ -f "${downloadProgram}" ]; then
  129. run_download_program "${downloadProgram}"
  130. fi
  131. done
  132. exit 0
  133. elif [ ! -f "${download}/${program}" ] && \
  134. [ ! -f "${extra_dir}//resources/scripts/download/${program}" ]; then
  135. help
  136. die "Invalid argument '${program}'. See: './download help'."
  137. fi
  138. if [ "${extra_dir}" != "" ] && \
  139. [ -f "${extra_dir}//resources/scripts/download/${program}" ] ; then
  140. run_download_program "${extra_dir}/resources/scripts/download/${program}" $@
  141. else
  142. run_download_program "${download}/${program}" $@
  143. fi
  144. exit 0
  145. ./$(dirname $0)/.gitcheck clean