summaryrefslogtreecommitdiffstats
path: root/esp32/install_esp32_rust_toolchain.sh
blob: c95a210650ecd4d5a102c98c25efe3c226f938b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/sh
#
# Install prebuilt ESP32 Rust toolchain.
#
# Author: Michael Buesch <m@bues.ch>
#
# This code is Public Domain.
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
# USE OR PERFORMANCE OF THIS SOFTWARE.
#

die()
{
	echo "$*" >&2
	exit 1
}

show_help()
{
	echo "Usage: install_esp32_rust_toolchain.sh <OPTIONS> [INSTALLDIR]"
	echo
	echo "Options:"
	echo " -h|--help                     Print help."
	echo
	echo
	echo "Install toolchain to $HOME/rust-esp32-prebuilt-toolchain"
	echo "  ./install_esp32_rust_toolchain.sh"
	echo
	echo "Install toolchain to another destination"
	echo "  ./install_esp32_rust_toolchain.sh /home/user/directory"
}

parse_args()
{
	# Defaults:
	INSTALLDIR="$HOME/rust-esp32-prebuilt-toolchain"

	# Parse command line options
	while [ $# -ge 1 ]; do
		[ "$(printf '%s' "$1" | cut -c1)" != "-" ] && break

		case "$1" in
		-h|--help)
			show_help
			exit 0
			;;
		*)
			echo "Unknown option: $1"
			exit 1
			;;
		esac
		shift
	done

	if [ $# -ge 1 -a -n "$1" ]; then
		# User defined INSTALLDIR
		INSTALLDIR="$1"
	fi
}

checkprog()
{
	local prog="$1"
	which "$prog" >/dev/null ||\
		die "$prog is not installed. Please install it by use of the distribution package manager (apt, apt-get, rpm, etc...)"
}

check_environment()
{
	[ "$(id -u)" = "0" ] && die "Do not run this as root!"
	checkprog curl
	checkprog schedtool
	[ -e "$HOME/export-esp.sh" ] &&\
		die "There already is a '$HOME/export-esp.sh'. This program would overwrite it."
}

prepare()
{
	# Resolve paths.
	INSTALLDIR="$(realpath -m -s "$INSTALLDIR")"
	echo "INSTALLDIR=$INSTALLDIR"
	[ -n "$INSTALLDIR" ] || die "Failed to resolve install directory"
	echo

	# Set priority
	schedtool -D -n19 $$ || die "Failed to reduce process priority"

	# Create the install directory.
	mkdir -p "$INSTALLDIR" || die "Failed to create INSTALLDIR"

	# Unset flags
	unset CFLAGS
	unset CXXFLAGS
	unset CPPFLAGS
	unset CARGO_REGISTRIES_CRATES_IO_PROTOCOL
}

install_rust()
{
	echo "Installing Rust..."

	rm -rf "$INSTALLDIR/rust" || die "Failed to clean rust"
	mkdir -p "$INSTALLDIR/rust" || die "Failed to create rust directory"

	# Create activation script
	cat > "$INSTALLDIR/activate" <<EOF
if [ -z "\$RUST_ESP32_TOOLCHAIN_ACTIVE" ]; then
	unset CFLAGS
	unset CXXFLAGS
	unset CPPFLAGS
	export RUSTUP_HOME="$INSTALLDIR/rust/rustup"
	export CARGO_HOME="$INSTALLDIR/rust/cargo"
	export PATH="\$CARGO_HOME/bin:\$PATH"
	[ -f "$INSTALLDIR/rust/export-esp.sh" ] && . "$INSTALLDIR/rust/export-esp.sh"
	PS1="rust-esp32/\$PS1"
	export RUST_ESP32_TOOLCHAIN_ACTIVE=1
fi
EOF
	[ $? -eq 0 ] || die "Failed to create activate script"

	# Install nightly
	(
		. "$INSTALLDIR/activate" || die "Failed to activate rust environment"
		cd "$INSTALLDIR/rust" || die "Failed to switch to rust directory"
		curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh ||\
			die "Failed to fetch rustup-init"
		sh rustup-init.sh --default-toolchain nightly --no-modify-path -y || die "rustup-init.sh failed"
	) || die
}

install_utils()
{
	echo "Installing utilities..."
	(
		. "$INSTALLDIR/activate" || die "Failed to activate rust environment"
		cargo install ldproxy || die "Failed to install ldproxy"
		cargo install cargo-espmonitor || die "Failed to install cargo-espmonitor"
		cargo install cargo-espflash || die "Failed to install cargo-espflash"
		cargo install cargo-generate || die "Failed to install cargo-generate"
		cargo install cargo-cache || die "Failed to install cargo-cache"
		cargo install cargo-audit || die "Failed to install cargo-audit"
		cargo install cargo-edit || die "Failed to install cargo-edit"
		cargo install --locked bacon || die "Failed to install bacon"
	) || die
}

install_espup()
{
	echo "Installing espup..."
	(
		. "$INSTALLDIR/activate" || die "Failed to activate rust environment"
		cargo install espup || die "Failed to install espup"
	) || die
}

install_esp_toolchain()
{
	echo "Installing ESP toolchain..."
	(
		. "$INSTALLDIR/activate" || die "Failed to activate rust environment"
		rm -rf "$HOME/.espup"
		espup install || die "Failed to install esp toolchain"
		mv "$HOME/export-esp.sh" "$INSTALLDIR/rust/" || die "Failed to move export script"
	) || die
}

cargo_clean()
{
	echo "Cleaning up..."
	(
		. "$INSTALLDIR/activate" || die "Failed to activate rust environment"
		cargo cache -a || die "Failed to autoclean the cargo cache"
	) || die
}

parse_args "$@"
check_environment
prepare
install_rust
install_utils
install_espup
install_esp_toolchain
cargo_clean

echo
echo
echo
echo "Successfully installed all tools to: $INSTALLDIR"
echo
bues.ch cgit interface