build_esp32_rust_toolchain.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/bin/sh
  2. #
  3. # ESP32 Rust toolchain install script.
  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. XTENSA_CLANG_URL="https://github.com/espressif/llvm-project/releases/download/esp-15.0.0-20221201/llvm-esp-15.0.0-20221201-linux-amd64.tar.xz"
  20. XTENSA_CLANG_SHA256="839e5adfa7f44982e8a2d828680f6e4aa435dcd3d1df765e02f015b04286056f"
  21. die()
  22. {
  23. echo "$*" >&2
  24. exit 1
  25. }
  26. show_help()
  27. {
  28. echo "Usage: build_esp32_rust_toolchain.sh <OPTIONS> [INSTALLDIR]"
  29. echo
  30. echo "Options:"
  31. echo " -h|--help Print help."
  32. echo
  33. echo
  34. echo "Install toolchain to $HOME/rust-esp32-xtensa-toolchain"
  35. echo " ./build_esp32_rust_toolchain.sh"
  36. echo
  37. echo "Install toolchain to another destination"
  38. echo " ./build_esp32_rust_toolchain.sh /home/user/directory"
  39. }
  40. parse_args()
  41. {
  42. # Defaults:
  43. INSTALLDIR="$HOME/rust-esp32-xtensa-toolchain"
  44. # Parse command line options
  45. while [ $# -ge 1 ]; do
  46. [ "$(printf '%s' "$1" | cut -c1)" != "-" ] && break
  47. case "$1" in
  48. -h|--help)
  49. show_help
  50. exit 0
  51. ;;
  52. *)
  53. echo "Unknown option: $1"
  54. exit 1
  55. ;;
  56. esac
  57. shift
  58. done
  59. if [ $# -ge 1 -a -n "$1" ]; then
  60. # User defined INSTALLDIR
  61. INSTALLDIR="$1"
  62. fi
  63. }
  64. checkprog()
  65. {
  66. local prog="$1"
  67. which "$prog" >/dev/null ||\
  68. die "$prog is not installed. Please install it by use of the distribution package manager (apt, apt-get, rpm, etc...)"
  69. }
  70. check_build_environment()
  71. {
  72. [ "$(id -u)" = "0" ] && die "Do not run this as root!"
  73. checkprog curl
  74. checkprog wget
  75. checkprog gcc
  76. checkprog make
  77. checkprog nproc
  78. checkprog tar
  79. checkprog git
  80. checkprog schedtool
  81. checkprog sha256sum
  82. checkprog python3
  83. checkprog help2man # for crosstool-ng
  84. }
  85. prepare()
  86. {
  87. # Resolve paths.
  88. INSTALLDIR="$(realpath -m -s "$INSTALLDIR")"
  89. echo "INSTALLDIR=$INSTALLDIR"
  90. [ -n "$INSTALLDIR" ] || die "Failed to resolve install directory"
  91. echo
  92. # Set priority
  93. schedtool -D -n19 $$ || die "Failed to reduce process priority"
  94. # Create the build directories.
  95. mkdir -p "$INSTALLDIR" || die "Failed to create INSTALLDIR"
  96. # Unset flags
  97. unset CFLAGS
  98. unset CXXFLAGS
  99. unset CPPFLAGS
  100. unset CARGO_REGISTRIES_CRATES_IO_PROTOCOL
  101. }
  102. build_python2()
  103. {
  104. if [ -d "$INSTALLDIR/python2" ]; then
  105. echo "Python2 is already built."
  106. return
  107. fi
  108. echo "Building Python2..."
  109. rm -rf "$INSTALLDIR/python2" "$INSTALLDIR/python2-src" || die "Failed to clean Python2"
  110. mkdir -p "$INSTALLDIR/python2" "$INSTALLDIR/python2-src" || die "Failed to create Python2 directory"
  111. cd "$INSTALLDIR/python2-src" || die "Failed to switch to Python2 src directory"
  112. wget "https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz"
  113. [ "$(sha256sum -b "Python-2.7.18.tar.xz" | cut -f1 -d' ')" = "b62c0e7937551d0cc02b8fd5cb0f544f9405bafc9a54d3808ed4594812edef43" ] ||\
  114. die "Python2 checksum failed"
  115. tar xf "Python-2.7.18.tar.xz" || die "Failed to extract Python2"
  116. cd "Python-2.7.18" || die "Failed to switch to Python2 source"
  117. ./configure --prefix="$INSTALLDIR/python2" || die "Failed to configure Python2"
  118. make -j "$(nproc)" || die "Failed to make Python2"
  119. make -j "$(nproc)" install || die "Failed to install Python2"
  120. }
  121. build_xtensa_crosstoolng()
  122. {
  123. if [ -d "$INSTALLDIR/crosstool-ng" ]; then
  124. echo "Xtensa toolchain is already built."
  125. return
  126. fi
  127. echo "Building Xtensa toolchain..."
  128. local oldpath="$PATH"
  129. export PATH="$INSTALLDIR/python2/bin:$PATH"
  130. rm -rf "$INSTALLDIR/crosstool-ng" || die "Failed to clean crosstool-ng"
  131. mkdir -p "$INSTALLDIR/crosstool-ng" || die "Failed to create crosstool-ng directory"
  132. cd "$INSTALLDIR/crosstool-ng" || die "Failed to switch to crosstool-ng directory"
  133. git clone --depth 1 "https://github.com/espressif/crosstool-NG.git"
  134. cd "$INSTALLDIR/crosstool-ng/crosstool-NG" || die "Failed to switch to crosstool-ng src"
  135. git submodule update --init --depth 1 || die "Failed to clone crosstool-ng submodules"
  136. ./bootstrap || die "Crosstool-ng bootstrap failed"
  137. ./configure --enable-local || die "Crosstool-ng configure failed"
  138. make -j "$(nproc)" || die "Crosstool-ng make failed"
  139. cp ./samples/xtensa-esp32-elf/crosstool.config ./.config || die "Crosstool-ng config copy failed"
  140. ./ct-ng upgradeconfig || die "Crosstool-ng upgradeconfig failed"
  141. ./ct-ng oldconfig || die "Crosstool-ng oldconfig failed"
  142. make -r -j "$(nproc)" -f ./ct-ng build || die "Crosstool-ng build failed"
  143. export PATH="$oldpath"
  144. }
  145. download_xtensa_clang()
  146. {
  147. if [ -d "$INSTALLDIR/xtensa-clang" ]; then
  148. echo "Xtensa clang is already downloaded."
  149. return
  150. fi
  151. echo "Downloading and installing xtensa clang..."
  152. rm -rf "$INSTALLDIR/xtensa-clang" || die "Failed to clean xtensa-clang"
  153. mkdir -p "$INSTALLDIR/xtensa-clang" || die "Failed to create xtensa-clang directory"
  154. cd "$INSTALLDIR/xtensa-clang" || die "Failed to switch to xtensa-clang directory"
  155. wget "$XTENSA_CLANG_URL" || die "Failed to download xtensa-clang"
  156. local file="$(basename "$XTENSA_CLANG_URL")"
  157. [ "$(sha256sum -b "$file" | cut -f1 -d' ')" = "$XTENSA_CLANG_SHA256" ] ||\
  158. die "xtensa-clang checksum failed"
  159. tar xf "$file" || die "Failed to extract xtensa-clang"
  160. }
  161. build_rust()
  162. {
  163. echo "Building Rust toolchain..."
  164. rm -rf "$INSTALLDIR/rust" || die "Failed to clean rust"
  165. mkdir -p "$INSTALLDIR/rust" || die "Failed to create rust directory"
  166. # Create activation script
  167. cat > "$INSTALLDIR/activate" <<EOF
  168. if [ -z "\$RUST_ESP32_TOOLCHAIN_ACTIVE" ]; then
  169. unset CFLAGS
  170. unset CXXFLAGS
  171. unset CPPFLAGS
  172. export RUSTUP_HOME="$INSTALLDIR/rust/rust-install/rustup"
  173. export CARGO_HOME="$INSTALLDIR/rust/rust-install/cargo"
  174. export PATH="\$CARGO_HOME/bin:$INSTALLDIR/crosstool-ng/crosstool-NG/builds/xtensa-esp32-elf/bin:\$PATH"
  175. export LIBCLANG_PATH="$INSTALLDIR/xtensa-clang/esp-clang/lib"
  176. PS1="rust-esp32/\$PS1"
  177. export RUST_ESP32_TOOLCHAIN_ACTIVE=1
  178. fi
  179. EOF
  180. [ $? -eq 0 ] || die "Failed to create activate script"
  181. # Install nightly
  182. (
  183. . "$INSTALLDIR/activate" || die "Failed to activate rust environment"
  184. cd "$INSTALLDIR/rust" || die "Failed to switch to rust directory"
  185. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh ||\
  186. die "Failed to fetch rustup-init"
  187. sh rustup-init.sh --default-toolchain nightly --no-modify-path -y || die "rustup-init.sh failed"
  188. cargo install ldproxy || die "Failed to install ldproxy"
  189. cargo install cargo-espmonitor || die "Failed to install cargo-espmonitor"
  190. cargo install cargo-espflash || die "Failed to install cargo-espflash"
  191. cargo install cargo-generate || die "Failed to install cargo-generate"
  192. ) || die
  193. # Build rust esp32 compiler
  194. (
  195. . "$INSTALLDIR/activate" || die "Failed to activate rust environment"
  196. mkdir -p "$INSTALLDIR/rust/rust-esp32" || die "Failed to create rust esp32 directory"
  197. cd "$INSTALLDIR/rust/rust-esp32" || die "Failed to switch to rust esp32 directory"
  198. git clone --depth 1 "https://github.com/esp-rs/rust" || die "Failed to clone rust esp32"
  199. cd "$INSTALLDIR/rust/rust-esp32/rust" || die "Failed to switch to rust esp32 source directory"
  200. git submodule update --init --depth 1 || die "Failed to clone rust esp32 submodules"
  201. ./configure --experimental-targets=Xtensa --release-channel=nightly || die "Failed to configure rust esp32"
  202. ./x.py build --stage 2 || die "Failed to build rust esp32"
  203. rustup toolchain link esp "$INSTALLDIR/rust/rust-esp32/rust/build/x86_64-unknown-linux-gnu/stage2" ||\
  204. die "Failed to link rust esp32 toolchain"
  205. ) || die
  206. }
  207. parse_args "$@"
  208. check_build_environment
  209. prepare
  210. build_python2
  211. build_xtensa_crosstoolng
  212. download_xtensa_clang
  213. build_rust
  214. echo
  215. echo
  216. echo
  217. echo "Successfully built and installed all tools to: $INSTALLDIR"
  218. echo