install_qemu_deps.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Install ARM bare-metal toolchain + QEMU needed by RyanJsonQemu.
  4. # Supported package managers: apt, dnf, yum, pacman, brew.
  5. SCRIPT_NAME="$(basename "$0")"
  6. NO_UPDATE="0"
  7. usage() {
  8. cat <<USAGE
  9. Usage: ${SCRIPT_NAME} [--no-update]
  10. Options:
  11. --no-update Skip package index refresh step (apt/dnf/yum/pacman).
  12. USAGE
  13. }
  14. while [[ $# -gt 0 ]]; do
  15. case "$1" in
  16. --no-update)
  17. NO_UPDATE="1"
  18. shift
  19. ;;
  20. -h|--help)
  21. usage
  22. exit 0
  23. ;;
  24. *)
  25. echo "[ERROR] Unknown argument: $1"
  26. usage
  27. exit 1
  28. ;;
  29. esac
  30. done
  31. if [[ $(id -u) -eq 0 ]]; then
  32. SUDO=""
  33. else
  34. if command -v sudo >/dev/null 2>&1; then
  35. SUDO="sudo"
  36. else
  37. echo "[ERROR] sudo is required when not running as root."
  38. exit 1
  39. fi
  40. fi
  41. log() {
  42. echo "[install_qemu_deps] $*"
  43. }
  44. have_cmd() {
  45. command -v "$1" >/dev/null 2>&1
  46. }
  47. install_with_apt() {
  48. local -a pkgs=(
  49. gcc-arm-none-eabi
  50. binutils-arm-none-eabi
  51. qemu-system-arm
  52. qemu-utils
  53. )
  54. if [[ "${NO_UPDATE}" != "1" ]]; then
  55. log "apt-get update"
  56. ${SUDO} apt-get update
  57. fi
  58. log "apt-get install: ${pkgs[*]}"
  59. DEBIAN_FRONTEND=noninteractive ${SUDO} apt-get install -y "${pkgs[@]}"
  60. }
  61. install_with_dnf() {
  62. local -a pkgs=(
  63. arm-none-eabi-gcc-cs
  64. arm-none-eabi-binutils-cs
  65. qemu-system-arm
  66. )
  67. if [[ "${NO_UPDATE}" != "1" ]]; then
  68. log "dnf makecache"
  69. ${SUDO} dnf -y makecache
  70. fi
  71. log "dnf install: ${pkgs[*]}"
  72. ${SUDO} dnf install -y "${pkgs[@]}"
  73. }
  74. install_with_yum() {
  75. local -a pkgs=(
  76. arm-none-eabi-gcc-cs
  77. arm-none-eabi-binutils-cs
  78. qemu-system-arm
  79. )
  80. if [[ "${NO_UPDATE}" != "1" ]]; then
  81. log "yum makecache"
  82. ${SUDO} yum -y makecache
  83. fi
  84. log "yum install: ${pkgs[*]}"
  85. ${SUDO} yum install -y "${pkgs[@]}"
  86. }
  87. install_with_pacman() {
  88. local -a pkgs=(
  89. arm-none-eabi-gcc
  90. arm-none-eabi-binutils
  91. qemu-system-arm
  92. )
  93. if [[ "${NO_UPDATE}" != "1" ]]; then
  94. log "pacman -Sy"
  95. ${SUDO} pacman -Sy --noconfirm
  96. fi
  97. log "pacman install: ${pkgs[*]}"
  98. ${SUDO} pacman -S --noconfirm --needed "${pkgs[@]}"
  99. }
  100. install_with_brew() {
  101. local -a pkgs=(
  102. arm-none-eabi-gcc
  103. qemu
  104. )
  105. log "brew install: ${pkgs[*]}"
  106. brew install "${pkgs[@]}"
  107. }
  108. install_deps() {
  109. if have_cmd apt-get; then
  110. install_with_apt
  111. return
  112. fi
  113. if have_cmd dnf; then
  114. install_with_dnf
  115. return
  116. fi
  117. if have_cmd yum; then
  118. install_with_yum
  119. return
  120. fi
  121. if have_cmd pacman; then
  122. install_with_pacman
  123. return
  124. fi
  125. if have_cmd brew; then
  126. install_with_brew
  127. return
  128. fi
  129. echo "[ERROR] Unsupported package manager. Please install manually:"
  130. echo " - arm-none-eabi-gcc"
  131. echo " - arm-none-eabi-objcopy (binutils)"
  132. echo " - qemu-system-arm"
  133. exit 1
  134. }
  135. verify() {
  136. local missing=0
  137. for cmd in arm-none-eabi-gcc arm-none-eabi-objcopy qemu-system-arm; do
  138. if have_cmd "${cmd}"; then
  139. log "found ${cmd}: $(command -v "${cmd}")"
  140. else
  141. echo "[ERROR] missing command after install: ${cmd}"
  142. missing=1
  143. fi
  144. done
  145. if [[ "${missing}" -ne 0 ]]; then
  146. exit 1
  147. fi
  148. arm-none-eabi-gcc --version | head -n 1
  149. arm-none-eabi-objcopy --version | head -n 1
  150. qemu-system-arm --version | head -n 1
  151. log "Dependencies are ready."
  152. }
  153. install_deps
  154. verify