/ mesh-build-scripts / install_prereq.sh
install_prereq.sh
  1  #!/usr/bin/env bash
  2  set -euo pipefail
  3  
  4  NAME="Check required tools and install missing ones"
  5  
  6  platform() {
  7      case "$(uname_s)" in
  8          Darwin) echo "Darwin" ;;
  9          Linux)  echo "Linux" ;;
 10          CYGWIN*|MSYS*) echo "Windows" ;;
 11          *)     echo "Unknown" ;;
 12      esac
 13  }
 14  
 15  check_cmake() {
 16      if command -v cmake >/dev/null; then
 17          return 0
 18      fi
 19      return 1
 20  }
 21  
 22  check_rust() {
 23      if command -v cargo >/dev/null; then
 24          return 0
 25      fi
 26      return 1
 27  }
 28  
 29  check_xet() {
 30      if command -v xet >/dev/null; then
 31          return 0
 32      fi
 33      return 1
 34  }
 35  
 36  check_npm() {
 37      if command -v npm >/dev/null; then
 38          return 0
 39      fi
 40      if [[ -n "${NVM_HOME:-}" ]]; then
 41          return 0
 42      fi
 43      if [[ -f "$HOME/.nvm/nvm.sh" ]]; then
 44          return 0
 45      fi
 46      return 1
 47  }
 48  
 49  run_version_cmake() {
 50      local out
 51      out=$(cmake --version 2>&1 | head -1) || true
 52      if [[ -n "$out" ]]; then
 53          echo "[OK] CMake: $out"
 54      else
 55          echo "[OK] CMake: installed (no version available)"
 56      fi
 57  }
 58  
 59  run_version_rust() {
 60      local out
 61      out=$(cargo --version 2>&1) || true
 62      if [[ -n "$out" ]]; then
 63          echo "[OK] Rust/Cargo: $out"
 64      else
 65          echo "[OK] Rust/Cargo: installed (no version available)"
 66      fi
 67  }
 68  
 69  run_version_xet() {
 70      local out
 71      out=$(xet --version 2>&1) || true
 72      if [[ -n "$out" ]]; then
 73          echo "[OK] Git Xet: $out"
 74      else
 75          echo "[OK] Git Xet: installed (no version available)"
 76      fi
 77  }
 78  
 79  run_version_npm() {
 80      local out
 81      out=$(npm --version 2>&1) || true
 82      if [[ -n "$out" ]]; then
 83          echo "[OK] npm: $out"
 84      else
 85          echo "[OK] npm: installed (no version available)"
 86      fi
 87  }
 88  
 89  install_cmake() {
 90      local sys
 91      sys=$(platform)
 92      case "$sys" in
 93          Darwin)
 94              if command -v brew >/dev/null; then
 95                  echo "Run: brew install cmake"
 96                  return 1
 97              fi
 98              ;;
 99          Linux)
100              if command -v apt >/dev/null; then
101                  echo "Run: sudo apt install cmake"
102                  return 1
103              elif command -v dnf >/dev/null; then
104                  echo "Run: sudo dnf install cmake"
105                  return 1
106              elif command -v pacman >/dev/null; then
107                  echo "Run: sudo pacman install cmake"
108                  return 1
109              fi
110              ;;
111      esac
112      echo "No automatic install method for CMake on this platform."
113      return 1
114  }
115  
116  install_rust() {
117      curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
118  }
119  
120  install_xet() {
121      curl --proto '=https' --tlsv1.2 -sSf \
122          "https://raw.githubusercontent.com/huggingface/xet-core/refs/heads/main/git_xet/install.sh" | sh
123  }
124  
125  install_npm() {
126      curl -o- "https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh" | bash
127  }
128  
129  prompt_install() {
130      local name="$1"
131      read -p "$name not found. Install now? [y/N]: " resp
132      case "${resp,,}" in
133          y|yes) return 0 ;;
134          *)     return 1 ;;
135      esac
136  }
137  
138  do_install() {
139      local tool="$1"
140      local result
141  
142      case "$tool" in
143          cmake)
144              if ! install_cmake; then
145                  return 1
146              fi
147              ;;
148          rust)
149              echo "Installing Rust via rustup..."
150              if [[ "$(platform)" == "Darwin" ]]; then
151                  sudo sh -c "$(install_rust)"
152              else
153                  eval "$(install_rust)"
154              fi
155              ;;
156          xet)
157              echo "Installing Git Xet..."
158              if [[ "$(platform)" == "Darwin" ]]; then
159                  sudo sh -c "$(install_xet)"
160              else
161                  eval "$(install_xet)"
162              fi
163              ;;
164          npm)
165              echo "Installing nvm..."
166              eval "$(install_npm)"
167              ;;
168      esac
169  }
170  
171  main() {
172      local sys
173      local installed=0
174      local total=4
175      local missing=""
176  
177      sys=$(platform)
178  
179      echo "Platform: $sys"
180      echo "Bash: ${BASH_VERSION:-unknown}\n"
181  
182      for tool in cmake rust xet npm; do
183          case "$tool" in
184              cmake)  check_fn="check_$tool"; run_fn="run_version_$tool" ;;
185              rust)    check_fn="check_$tool"; run_fn="run_version_$tool" ;;
186              xet)     check_fn="check_$tool"; run_fn="run_version_$tool" ;;
187              npm)     check_fn="check_$tool"; run_fn="run_version_$tool" ;;
188          esac
189  
190          if "$check_fn"; then
191              echo "[OK] ${tool^} found"
192              "$run_fn"
193              ((installed++))
194          else
195              echo "${tool^} not found"
196              if prompt_install "${tool^}"; then
197                  if do_install "$tool"; then
198                      "$run_fn"
199                      ((installed++))
200                  fi
201              else
202                  missing="$missing $tool,"
203              fi
204              else
205                  missing="$missing $tool,"
206              fi
207          fi
208      done
209  
210      echo ""
211      echo "Tools available: $installed/$total"
212  
213      if [[ -n "$missing" ]]; then
214          echo "Missing:${missing%,}"
215      fi
216  }
217  
218  main "$@"