install_esp32_rust_toolchain.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/bin/sh
  2. #
  3. # Install prebuilt ESP32 Rust toolchain.
  4. #
  5. # Author: Michael Buesch <m@bues.ch>
  6. #
  7. # This code is Public Domain.
  8. # Permission to use, copy, modify, and/or distribute this software for any
  9. # purpose with or without fee is hereby granted.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  14. # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  15. # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  16. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
  17. # USE OR PERFORMANCE OF THIS SOFTWARE.
  18. #
  19. die()
  20. {
  21. echo "$*" >&2
  22. exit 1
  23. }
  24. show_help()
  25. {
  26. echo "Usage: install_esp32_rust_toolchain.sh <OPTIONS> [INSTALLDIR]"
  27. echo
  28. echo "Options:"
  29. echo " -h|--help Print help."
  30. echo
  31. echo
  32. echo "Install toolchain to $HOME/rust-esp32-prebuilt-toolchain"
  33. echo " ./install_esp32_rust_toolchain.sh"
  34. echo
  35. echo "Install toolchain to another destination"
  36. echo " ./install_esp32_rust_toolchain.sh /home/user/directory"
  37. }
  38. parse_args()
  39. {
  40. # Defaults:
  41. INSTALLDIR="$HOME/rust-esp32-prebuilt-toolchain"
  42. # Parse command line options
  43. while [ $# -ge 1 ]; do
  44. [ "$(printf '%s' "$1" | cut -c1)" != "-" ] && break
  45. case "$1" in
  46. -h|--help)
  47. show_help
  48. exit 0
  49. ;;
  50. *)
  51. echo "Unknown option: $1"
  52. exit 1
  53. ;;
  54. esac
  55. shift
  56. done
  57. if [ $# -ge 1 -a -n "$1" ]; then
  58. # User defined INSTALLDIR
  59. INSTALLDIR="$1"
  60. fi
  61. }
  62. checkprog()
  63. {
  64. local prog="$1"
  65. which "$prog" >/dev/null ||\
  66. die "$prog is not installed. Please install it by use of the distribution package manager (apt, apt-get, rpm, etc...)"
  67. }
  68. check_environment()
  69. {
  70. [ "$(id -u)" = "0" ] && die "Do not run this as root!"
  71. checkprog curl
  72. checkprog schedtool
  73. [ -e "$HOME/export-esp.sh" ] &&\
  74. die "There already is a '$HOME/export-esp.sh'. This program would overwrite it."
  75. }
  76. prepare()
  77. {
  78. # Resolve paths.
  79. INSTALLDIR="$(realpath -m -s "$INSTALLDIR")"
  80. echo "INSTALLDIR=$INSTALLDIR"
  81. [ -n "$INSTALLDIR" ] || die "Failed to resolve install directory"
  82. echo
  83. # Set priority
  84. schedtool -D -n19 $$ || die "Failed to reduce process priority"
  85. # Create the install directory.
  86. mkdir -p "$INSTALLDIR" || die "Failed to create INSTALLDIR"
  87. # Unset flags
  88. unset CFLAGS
  89. unset CXXFLAGS
  90. unset CPPFLAGS
  91. unset CARGO_REGISTRIES_CRATES_IO_PROTOCOL
  92. }
  93. install_rust()
  94. {
  95. echo "Installing Rust..."
  96. rm -rf "$INSTALLDIR/rust" || die "Failed to clean rust"
  97. mkdir -p "$INSTALLDIR/rust" || die "Failed to create rust directory"
  98. # Create activation script
  99. cat > "$INSTALLDIR/activate" <<EOF
  100. if [ -z "\$RUST_ESP32_TOOLCHAIN_ACTIVE" ]; then
  101. unset CFLAGS
  102. unset CXXFLAGS
  103. unset CPPFLAGS
  104. export RUSTUP_HOME="$INSTALLDIR/rust/rustup"
  105. export CARGO_HOME="$INSTALLDIR/rust/cargo"
  106. export PATH="\$CARGO_HOME/bin:\$PATH"
  107. [ -f "$INSTALLDIR/rust/export-esp.sh" ] && . "$INSTALLDIR/rust/export-esp.sh"
  108. PS1="rust-esp32/\$PS1"
  109. export RUST_ESP32_TOOLCHAIN_ACTIVE=1
  110. fi
  111. EOF
  112. [ $? -eq 0 ] || die "Failed to create activate script"
  113. # Install nightly
  114. (
  115. . "$INSTALLDIR/activate" || die "Failed to activate rust environment"
  116. cd "$INSTALLDIR/rust" || die "Failed to switch to rust directory"
  117. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh ||\
  118. die "Failed to fetch rustup-init"
  119. sh rustup-init.sh --default-toolchain nightly --no-modify-path -y || die "rustup-init.sh failed"
  120. ) || die
  121. }
  122. install_utils()
  123. {
  124. echo "Installing utilities..."
  125. (
  126. . "$INSTALLDIR/activate" || die "Failed to activate rust environment"
  127. cargo install ldproxy || die "Failed to install ldproxy"
  128. cargo install cargo-espmonitor || die "Failed to install cargo-espmonitor"
  129. cargo install cargo-espflash || die "Failed to install cargo-espflash"
  130. cargo install cargo-generate || die "Failed to install cargo-generate"
  131. cargo install cargo-cache || die "Failed to install cargo-cache"
  132. cargo install cargo-audit || die "Failed to install cargo-audit"
  133. cargo install cargo-edit || die "Failed to install cargo-edit"
  134. cargo install --locked bacon || die "Failed to install bacon"
  135. ) || die
  136. }
  137. install_espup()
  138. {
  139. echo "Installing espup..."
  140. (
  141. . "$INSTALLDIR/activate" || die "Failed to activate rust environment"
  142. cargo install espup || die "Failed to install espup"
  143. ) || die
  144. }
  145. install_esp_toolchain()
  146. {
  147. echo "Installing ESP toolchain..."
  148. (
  149. . "$INSTALLDIR/activate" || die "Failed to activate rust environment"
  150. rm -rf "$HOME/.espup"
  151. espup install || die "Failed to install esp toolchain"
  152. mv "$HOME/export-esp.sh" "$INSTALLDIR/rust/" || die "Failed to move export script"
  153. ) || die
  154. }
  155. cargo_clean()
  156. {
  157. echo "Cleaning up..."
  158. (
  159. . "$INSTALLDIR/activate" || die "Failed to activate rust environment"
  160. cargo cache -a || die "Failed to autoclean the cargo cache"
  161. ) || die
  162. }
  163. parse_args "$@"
  164. check_environment
  165. prepare
  166. install_rust
  167. install_utils
  168. install_espup
  169. install_esp_toolchain
  170. cargo_clean
  171. echo
  172. echo
  173. echo
  174. echo "Successfully installed all tools to: $INSTALLDIR"
  175. echo