|
| 1 | +#!/bin/sh |
| 2 | +# Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 3 | +# file at the top-level directory of this distribution and at |
| 4 | +# http://rust-lang.org/COPYRIGHT. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 8 | +# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 9 | +# option. This file may not be copied, modified, or distributed |
| 10 | +# except according to those terms. |
| 11 | + |
| 12 | +# This is just a little script that can be curled from the internet to |
| 13 | +# install rustup. It just does platform detection, curls the installer |
| 14 | +# and runs it. |
| 15 | + |
| 16 | +set -u |
| 17 | + |
| 18 | +RUSTUP_UPDATE_ROOT="https://static.rust-lang.org/rustup/dist" |
| 19 | + |
| 20 | +#XXX: If you change anything here, please make the same changes in setup_mode.rs |
| 21 | +usage() { |
| 22 | + cat 1>&2 <<EOF |
| 23 | +rustup-init 1.0.0 (408ed84 2017-02-11) |
| 24 | +The installer for rustup |
| 25 | +
|
| 26 | +USAGE: |
| 27 | + rustup-init [FLAGS] [OPTIONS] |
| 28 | +
|
| 29 | +FLAGS: |
| 30 | + -v, --verbose Enable verbose output |
| 31 | + -y Disable confirmation prompt. |
| 32 | + --no-modify-path Don't configure the PATH environment variable |
| 33 | + -h, --help Prints help information |
| 34 | + -V, --version Prints version information |
| 35 | +
|
| 36 | +OPTIONS: |
| 37 | + --default-host <default-host> Choose a default host triple |
| 38 | + --default-toolchain <default-toolchain> Choose a default toolchain to install |
| 39 | + --default-toolchain none Do not install any toolchains |
| 40 | +EOF |
| 41 | +} |
| 42 | + |
| 43 | +main() { |
| 44 | + need_cmd uname |
| 45 | + need_cmd curl |
| 46 | + need_cmd mktemp |
| 47 | + need_cmd chmod |
| 48 | + need_cmd mkdir |
| 49 | + need_cmd rm |
| 50 | + need_cmd rmdir |
| 51 | + |
| 52 | + get_architecture || return 1 |
| 53 | + local _arch="$RETVAL" |
| 54 | + assert_nz "$_arch" "arch" |
| 55 | + |
| 56 | + local _ext="" |
| 57 | + case "$_arch" in |
| 58 | + *windows*) |
| 59 | + _ext=".exe" |
| 60 | + ;; |
| 61 | + esac |
| 62 | + |
| 63 | + local _url="$RUSTUP_UPDATE_ROOT/$_arch/rustup-init$_ext" |
| 64 | + |
| 65 | + local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t rustup)" |
| 66 | + local _file="$_dir/rustup-init$_ext" |
| 67 | + |
| 68 | + local _ansi_escapes_are_valid=false |
| 69 | + if [ -t 2 ]; then |
| 70 | + if [ "${TERM+set}" = 'set' ]; then |
| 71 | + case "$TERM" in |
| 72 | + xterm*|rxvt*|urxvt*|linux*|vt*) |
| 73 | + _ansi_escapes_are_valid=true |
| 74 | + ;; |
| 75 | + esac |
| 76 | + fi |
| 77 | + fi |
| 78 | + |
| 79 | + # check if we have to use /dev/tty to prompt the user |
| 80 | + local need_tty=yes |
| 81 | + for arg in "$@"; do |
| 82 | + case "$arg" in |
| 83 | + -h|--help) |
| 84 | + usage |
| 85 | + exit 0 |
| 86 | + ;; |
| 87 | + -y) |
| 88 | + # user wants to skip the prompt -- we don't need /dev/tty |
| 89 | + need_tty=no |
| 90 | + ;; |
| 91 | + *) |
| 92 | + ;; |
| 93 | + esac |
| 94 | + done |
| 95 | + |
| 96 | + if $_ansi_escapes_are_valid; then |
| 97 | + printf "\33[1minfo:\33[0m downloading installer\n" 1>&2 |
| 98 | + else |
| 99 | + printf '%s\n' 'info: downloading installer' 1>&2 |
| 100 | + fi |
| 101 | + |
| 102 | + ensure mkdir -p "$_dir" |
| 103 | + ensure curl -sSfL "$_url" -o "$_file" |
| 104 | + ensure chmod u+x "$_file" |
| 105 | + if [ ! -x "$_file" ]; then |
| 106 | + printf '%s\n' "Cannot execute $_file (likely because of mounting /tmp as noexec)." 1>&2 |
| 107 | + printf '%s\n' "Please copy the file to a location where you can execute binaries and run ./rustup-init$_ext." 1>&2 |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + if [ "$need_tty" = "yes" ]; then |
| 114 | + # The installer is going to want to ask for confirmation by |
| 115 | + # reading stdin. This script was piped into `sh` though and |
| 116 | + # doesn't have stdin to pass to its children. Instead we're going |
| 117 | + # to explicitly connect /dev/tty to the installer's stdin. |
| 118 | + if [ ! -t 1 ]; then |
| 119 | + err "Unable to run interactively. Run with -y to accept defaults, --help for additional options" |
| 120 | + fi |
| 121 | + |
| 122 | + ignore "$_file" "$@" < /dev/tty |
| 123 | + else |
| 124 | + ignore "$_file" "$@" |
| 125 | + fi |
| 126 | + |
| 127 | + local _retval=$? |
| 128 | + |
| 129 | + ignore rm "$_file" |
| 130 | + ignore rmdir "$_dir" |
| 131 | + |
| 132 | + return "$_retval" |
| 133 | +} |
| 134 | + |
| 135 | +get_bitness() { |
| 136 | + need_cmd head |
| 137 | + # Architecture detection without dependencies beyond coreutils. |
| 138 | + # ELF files start out "\x7fELF", and the following byte is |
| 139 | + # 0x01 for 32-bit and |
| 140 | + # 0x02 for 64-bit. |
| 141 | + # The printf builtin on some shells like dash only supports octal |
| 142 | + # escape sequences, so we use those. |
| 143 | + local _current_exe_head=$(head -c 5 /proc/self/exe ) |
| 144 | + if [ "$_current_exe_head" = "$(printf '\177ELF\001')" ]; then |
| 145 | + echo 32 |
| 146 | + elif [ "$_current_exe_head" = "$(printf '\177ELF\002')" ]; then |
| 147 | + echo 64 |
| 148 | + else |
| 149 | + err "unknown platform bitness" |
| 150 | + fi |
| 151 | +} |
| 152 | + |
| 153 | +get_endianness() { |
| 154 | + local cputype=$1 |
| 155 | + local suffix_eb=$2 |
| 156 | + local suffix_el=$3 |
| 157 | + |
| 158 | + # detect endianness without od/hexdump, like get_bitness() does. |
| 159 | + need_cmd head |
| 160 | + need_cmd tail |
| 161 | + |
| 162 | + local _current_exe_endianness="$(head -c 6 /proc/self/exe | tail -c 1)" |
| 163 | + if [ "$_current_exe_endianness" = "$(printf '\001')" ]; then |
| 164 | + echo "${cputype}${suffix_el}" |
| 165 | + elif [ "$_current_exe_endianness" = "$(printf '\002')" ]; then |
| 166 | + echo "${cputype}${suffix_eb}" |
| 167 | + else |
| 168 | + err "unknown platform endianness" |
| 169 | + fi |
| 170 | +} |
| 171 | + |
| 172 | +get_architecture() { |
| 173 | + |
| 174 | + local _ostype="$(uname -s)" |
| 175 | + local _cputype="$(uname -m)" |
| 176 | + |
| 177 | + if [ "$_ostype" = Linux ]; then |
| 178 | + if [ "$(uname -o)" = Android ]; then |
| 179 | + local _ostype=Android |
| 180 | + fi |
| 181 | + fi |
| 182 | + |
| 183 | + if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then |
| 184 | + # Darwin `uname -s` lies |
| 185 | + if sysctl hw.optional.x86_64 | grep -q ': 1'; then |
| 186 | + local _cputype=x86_64 |
| 187 | + fi |
| 188 | + fi |
| 189 | + |
| 190 | + case "$_ostype" in |
| 191 | + |
| 192 | + Android) |
| 193 | + local _ostype=linux-android |
| 194 | + ;; |
| 195 | + |
| 196 | + Linux) |
| 197 | + local _ostype=unknown-linux-gnu |
| 198 | + ;; |
| 199 | + |
| 200 | + FreeBSD) |
| 201 | + local _ostype=unknown-freebsd |
| 202 | + ;; |
| 203 | + |
| 204 | + NetBSD) |
| 205 | + local _ostype=unknown-netbsd |
| 206 | + ;; |
| 207 | + |
| 208 | + DragonFly) |
| 209 | + local _ostype=unknown-dragonfly |
| 210 | + ;; |
| 211 | + |
| 212 | + Darwin) |
| 213 | + local _ostype=apple-darwin |
| 214 | + ;; |
| 215 | + |
| 216 | + MINGW* | MSYS* | CYGWIN*) |
| 217 | + local _ostype=pc-windows-gnu |
| 218 | + ;; |
| 219 | + |
| 220 | + *) |
| 221 | + err "unrecognized OS type: $_ostype" |
| 222 | + ;; |
| 223 | + |
| 224 | + esac |
| 225 | + |
| 226 | + case "$_cputype" in |
| 227 | + |
| 228 | + i386 | i486 | i686 | i786 | x86) |
| 229 | + local _cputype=i686 |
| 230 | + ;; |
| 231 | + |
| 232 | + xscale | arm) |
| 233 | + local _cputype=arm |
| 234 | + if [ "$_ostype" = "linux-android" ]; then |
| 235 | + local _ostype=linux-androideabi |
| 236 | + fi |
| 237 | + ;; |
| 238 | + |
| 239 | + armv6l) |
| 240 | + local _cputype=arm |
| 241 | + if [ "$_ostype" = "linux-android" ]; then |
| 242 | + local _ostype=linux-androideabi |
| 243 | + else |
| 244 | + local _ostype="${_ostype}eabihf" |
| 245 | + fi |
| 246 | + ;; |
| 247 | + |
| 248 | + armv7l | armv8l) |
| 249 | + local _cputype=armv7 |
| 250 | + if [ "$_ostype" = "linux-android" ]; then |
| 251 | + local _ostype=linux-androideabi |
| 252 | + else |
| 253 | + local _ostype="${_ostype}eabihf" |
| 254 | + fi |
| 255 | + ;; |
| 256 | + |
| 257 | + aarch64) |
| 258 | + local _cputype=aarch64 |
| 259 | + ;; |
| 260 | + |
| 261 | + x86_64 | x86-64 | x64 | amd64) |
| 262 | + local _cputype=x86_64 |
| 263 | + ;; |
| 264 | + |
| 265 | + mips) |
| 266 | + local _cputype="$(get_endianness $_cputype "" 'el')" |
| 267 | + ;; |
| 268 | + |
| 269 | + mips64) |
| 270 | + local _bitness="$(get_bitness)" |
| 271 | + if [ $_bitness = "32" ]; then |
| 272 | + if [ $_ostype = "unknown-linux-gnu" ]; then |
| 273 | + # 64-bit kernel with 32-bit userland |
| 274 | + # endianness suffix is appended later |
| 275 | + local _cputype=mips |
| 276 | + fi |
| 277 | + else |
| 278 | + # only n64 ABI is supported for now |
| 279 | + local _ostype="${_ostype}abi64" |
| 280 | + fi |
| 281 | + |
| 282 | + local _cputype="$(get_endianness $_cputype "" 'el')" |
| 283 | + ;; |
| 284 | + |
| 285 | + ppc) |
| 286 | + local _cputype=powerpc |
| 287 | + ;; |
| 288 | + |
| 289 | + ppc64) |
| 290 | + local _cputype=powerpc64 |
| 291 | + ;; |
| 292 | + |
| 293 | + ppc64le) |
| 294 | + local _cputype=powerpc64le |
| 295 | + ;; |
| 296 | + |
| 297 | + *) |
| 298 | + err "unknown CPU type: $_cputype" |
| 299 | + |
| 300 | + esac |
| 301 | + |
| 302 | + # Detect 64-bit linux with 32-bit userland |
| 303 | + if [ $_ostype = unknown-linux-gnu -a $_cputype = x86_64 ]; then |
| 304 | + if [ "$(get_bitness)" = "32" ]; then |
| 305 | + local _cputype=i686 |
| 306 | + fi |
| 307 | + fi |
| 308 | + |
| 309 | + # Detect armv7 but without the CPU features Rust needs in that build, |
| 310 | + # and fall back to arm. |
| 311 | + # See https://github.com/rust-lang-nursery/rustup.rs/issues/587. |
| 312 | + if [ $_ostype = "unknown-linux-gnueabihf" -a $_cputype = armv7 ]; then |
| 313 | + if ensure grep '^Features' /proc/cpuinfo | grep -q -v neon; then |
| 314 | + # At least one processor does not have NEON. |
| 315 | + local _cputype=arm |
| 316 | + fi |
| 317 | + fi |
| 318 | + |
| 319 | + local _arch="$_cputype-$_ostype" |
| 320 | + |
| 321 | + RETVAL="$_arch" |
| 322 | +} |
| 323 | + |
| 324 | +say() { |
| 325 | + echo "rustup: $1" |
| 326 | +} |
| 327 | + |
| 328 | +err() { |
| 329 | + say "$1" >&2 |
| 330 | + exit 1 |
| 331 | +} |
| 332 | + |
| 333 | +need_cmd() { |
| 334 | + if ! command -v "$1" > /dev/null 2>&1 |
| 335 | + then err "need '$1' (command not found)" |
| 336 | + fi |
| 337 | +} |
| 338 | + |
| 339 | +need_ok() { |
| 340 | + if [ $? != 0 ]; then err "$1"; fi |
| 341 | +} |
| 342 | + |
| 343 | +assert_nz() { |
| 344 | + if [ -z "$1" ]; then err "assert_nz $2"; fi |
| 345 | +} |
| 346 | + |
| 347 | +# Run a command that should never fail. If the command fails execution |
| 348 | +# will immediately terminate with an error showing the failing |
| 349 | +# command. |
| 350 | +ensure() { |
| 351 | + "$@" |
| 352 | + need_ok "command failed: $*" |
| 353 | +} |
| 354 | + |
| 355 | +# This is just for indicating that commands' results are being |
| 356 | +# intentionally ignored. Usually, because it's being executed |
| 357 | +# as part of error handling. |
| 358 | +ignore() { |
| 359 | + "$@" |
| 360 | +} |
| 361 | + |
| 362 | +main "$@" || exit 1 |
0 commit comments