|
| 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 | +main() { |
| 21 | + need_cmd curl |
| 22 | + need_cmd mktemp |
| 23 | + need_cmd chmod |
| 24 | + need_cmd mkdir |
| 25 | + need_cmd rm |
| 26 | + need_cmd rmdir |
| 27 | + need_cmd printf |
| 28 | + |
| 29 | + get_architecture || return 1 |
| 30 | + local _arch="$RETVAL" |
| 31 | + assert_nz "$_arch" "arch" |
| 32 | + |
| 33 | + local _ext="" |
| 34 | + case "$_arch" in |
| 35 | + *windows*) |
| 36 | + _ext=".exe" |
| 37 | + ;; |
| 38 | + esac |
| 39 | + |
| 40 | + local _url="$RUSTUP_UPDATE_ROOT/$_arch/rustup-init$_ext" |
| 41 | + |
| 42 | + local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t rustup)" |
| 43 | + local _file="$_dir/rustup-init$_ext" |
| 44 | + |
| 45 | + printf "\33[1minfo:\33[0m downloading installer\n" |
| 46 | + |
| 47 | + ensure mkdir -p "$_dir" |
| 48 | + ensure curl -sSfL "$_url" -o "$_file" |
| 49 | + ensure chmod u+x "$_file" |
| 50 | + |
| 51 | + # The installer is going to want to ask for confirmation by |
| 52 | + # reading stdin. This script was piped into `sh` though and |
| 53 | + # doesn't have stdin to pass to its children. Instead we're going |
| 54 | + # to explicitly connect /dev/tty to the installer's stdin. |
| 55 | + if [ ! -e "/dev/tty" ]; then |
| 56 | + err "/dev/tty does not exist" |
| 57 | + fi |
| 58 | + |
| 59 | + run "$_file" "$@" < /dev/tty |
| 60 | + |
| 61 | + local _retval=$? |
| 62 | + |
| 63 | + ignore rm "$_file" |
| 64 | + ignore rmdir "$_dir" |
| 65 | + |
| 66 | + return "$_retval" |
| 67 | +} |
| 68 | + |
| 69 | +get_architecture() { |
| 70 | + |
| 71 | + local _ostype="$(uname -s)" |
| 72 | + local _cputype="$(uname -m)" |
| 73 | + |
| 74 | + if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then |
| 75 | + # Darwin `uname -s` lies |
| 76 | + if sysctl hw.optional.x86_64 | grep -q ': 1'; then |
| 77 | + local _cputype=x86_64 |
| 78 | + fi |
| 79 | + fi |
| 80 | + |
| 81 | + case "$_ostype" in |
| 82 | + |
| 83 | + Linux) |
| 84 | + local _ostype=unknown-linux-gnu |
| 85 | + ;; |
| 86 | + |
| 87 | + FreeBSD) |
| 88 | + local _ostype=unknown-freebsd |
| 89 | + ;; |
| 90 | + |
| 91 | + DragonFly) |
| 92 | + local _ostype=unknown-dragonfly |
| 93 | + ;; |
| 94 | + |
| 95 | + Darwin) |
| 96 | + local _ostype=apple-darwin |
| 97 | + ;; |
| 98 | + |
| 99 | + MINGW* | MSYS* | CYGWIN*) |
| 100 | + local _ostype=pc-windows-gnu |
| 101 | + ;; |
| 102 | + |
| 103 | + *) |
| 104 | + err "unrecognized OS type: $_ostype" |
| 105 | + ;; |
| 106 | + |
| 107 | + esac |
| 108 | + |
| 109 | + case "$_cputype" in |
| 110 | + |
| 111 | + i386 | i486 | i686 | i786 | x86) |
| 112 | + local _cputype=i686 |
| 113 | + ;; |
| 114 | + |
| 115 | + xscale | arm) |
| 116 | + local _cputype=arm |
| 117 | + ;; |
| 118 | + |
| 119 | + armv6l) |
| 120 | + local _cputype=arm |
| 121 | + local _ostype="${_ostype}eabihf" |
| 122 | + ;; |
| 123 | + |
| 124 | + armv7l) |
| 125 | + local _cputype=armv7 |
| 126 | + local _ostype="${_ostype}eabihf" |
| 127 | + ;; |
| 128 | + |
| 129 | + aarch64) |
| 130 | + local _cputype=aarch64 |
| 131 | + ;; |
| 132 | + |
| 133 | + x86_64 | x86-64 | x64 | amd64) |
| 134 | + local _cputype=x86_64 |
| 135 | + ;; |
| 136 | + |
| 137 | + *) |
| 138 | + err "unknown CPU type: $_cputype" |
| 139 | + |
| 140 | + esac |
| 141 | + |
| 142 | + # Detect 64-bit linux with 32-bit userland |
| 143 | + if [ $_ostype = unknown-linux-gnu -a $_cputype = x86_64 ]; then |
| 144 | + # $SHELL does not exist in standard 'sh', so probably only exists |
| 145 | + # if configure is running in an interactive bash shell. /usr/bin/env |
| 146 | + # exists *everywhere*. |
| 147 | + local _bin_to_probe="${SHELL-bogus_shell}" |
| 148 | + if [ ! -e "$_bin_to_probe" -a -e "/usr/bin/env" ]; then |
| 149 | + _bin_to_probe="/usr/bin/env" |
| 150 | + fi |
| 151 | + # $SHELL may be not a binary |
| 152 | + if [ -e "$_bin_to_probe" ]; then |
| 153 | + file -L "$_bin_to_probe" | grep -q "text" |
| 154 | + if [ $? = 0 ]; then |
| 155 | + _bin_to_probe="/usr/bin/env" |
| 156 | + fi |
| 157 | + fi |
| 158 | + if [ -e "$_bin_to_probe" ]; then |
| 159 | + file -L "$_bin_to_probe" | grep -q "x86[_-]64" |
| 160 | + if [ $? != 0 ]; then |
| 161 | + local _cputype=i686 |
| 162 | + fi |
| 163 | + fi |
| 164 | + fi |
| 165 | + |
| 166 | + local _arch="$_cputype-$_ostype" |
| 167 | + |
| 168 | + RETVAL="$_arch" |
| 169 | +} |
| 170 | + |
| 171 | +say() { |
| 172 | + echo "rustup: $1" |
| 173 | +} |
| 174 | + |
| 175 | +say_err() { |
| 176 | + say "$1" >&2 |
| 177 | +} |
| 178 | + |
| 179 | +err() { |
| 180 | + say "$1" >&2 |
| 181 | + exit 1 |
| 182 | +} |
| 183 | + |
| 184 | +need_cmd() { |
| 185 | + if ! command -v "$1" > /dev/null 2>&1 |
| 186 | + then err "need '$1' (command not found)" |
| 187 | + fi |
| 188 | +} |
| 189 | + |
| 190 | +need_ok() { |
| 191 | + if [ $? != 0 ]; then err "$1"; fi |
| 192 | +} |
| 193 | + |
| 194 | +assert_nz() { |
| 195 | + if [ -z "$1" ]; then err "assert_nz $2"; fi |
| 196 | +} |
| 197 | + |
| 198 | +# Run a command that should never fail. If the command fails execution |
| 199 | +# will immediately terminate with an error showing the failing |
| 200 | +# command. |
| 201 | +ensure() { |
| 202 | + "$@" |
| 203 | + need_ok "command failed: $*" |
| 204 | +} |
| 205 | + |
| 206 | +# This is just for indicating that commands' results are being |
| 207 | +# intentionally ignored. Usually, because it's being executed |
| 208 | +# as part of error handling. |
| 209 | +ignore() { |
| 210 | + run "$@" |
| 211 | +} |
| 212 | + |
| 213 | +# Runs a command and prints it to stderr if it fails. |
| 214 | +run() { |
| 215 | + "$@" |
| 216 | + local _retval=$? |
| 217 | + if [ $_retval != 0 ]; then |
| 218 | + say_err "command failed: $*" |
| 219 | + fi |
| 220 | + return $_retval |
| 221 | +} |
| 222 | + |
| 223 | +main "$@" || exit 1 |
0 commit comments