├── README.md └── Dockerfile /README.md: -------------------------------------------------------------------------------- 1 | # Rust build environment for ESP32 2 | 3 | This image builds the rust compiler toolchain for the Xtensa ISA 4 | using [llvm-project](https://github.com/espressif/llvm-project) and 5 | [rust-xtensa](https://github.com/MabezDev). 6 | 7 | Most of the steps in this image are based on a 8 | [blog post](http://quickhack.net/nom/blog/2019-05-14-build-rust-environment-for-esp32.html) 9 | by Yoshinari Nomura. 10 | 11 | # Usage 12 | 13 | To build your project, run this in the same directory as your `Cargo.toml`. 14 | Your project needs to be mounted in `/code` to build out of the box. 15 | 16 | ```bash 17 | docker run -v $PWD:/code mtnmts/rust-esp32 18 | ``` 19 | 20 | If you want an interactive session to use anything inside the machine 21 | (esptool.py is installed and in the path for example, if you want to 22 | use elf2image). 23 | 24 | ```bash 25 | docker run -v $PWD:/code -ti mtnmts/rust-esp32 bash 26 | ``` 27 | 28 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:18.04 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | RUN apt update -y && apt upgrade -y 5 | 6 | ## Various dependencies 7 | RUN apt-get install -y wget sudo cmake clang python zlib1g make git \ 8 | ninja-build llvm libssl-dev pkg-config curl 9 | 10 | ## ESP-IDF dependencies 11 | ## https://docs.espressif.com/projects/esp-idf/en/latest/get-started/linux-setup.html 12 | RUN sudo apt-get install -y git wget flex bison gperf python python-pip python-setuptools \ 13 | python-serial python-click python-cryptography python-future python-pyparsing \ 14 | python-pyelftools cmake ninja-build ccache libffi-dev libssl-dev 15 | 16 | ## Build LLVM 17 | ## based on these build instructions 18 | ## http://quickhack.net/nom/blog/2019-05-14-build-rust-environment-for-esp32.html 19 | ENV BUILD_ROOT $HOME/.xtensa 20 | RUN mkdir -p "${BUILD_ROOT}" 21 | WORKDIR ${BUILD_ROOT} 22 | RUN git clone https://github.com/espressif/llvm-project.git --depth 1 23 | ENV LLVM_BUILD ${BUILD_ROOT}/llvm_build 24 | RUN mkdir -p "${LLVM_BUILD}" 25 | WORKDIR ${LLVM_BUILD} 26 | ENV CC clang 27 | ENV CXX clang++ 28 | RUN cmake ../llvm-project/llvm -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="Xtensa" -DLLVM_TARGETS_TO_BUILD="X86" -DCMAKE_BUILD_TYPE=Release -G "Ninja" 29 | RUN cmake --build . 30 | 31 | ## Build Rust 32 | WORKDIR / 33 | RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh && \ 34 | chmod +x ./rustup.sh && \ 35 | ./rustup.sh --default-toolchain nightly --profile default -y && \ 36 | rm rustup.sh 37 | 38 | ## Build LLVM 39 | WORKDIR ${BUILD_ROOT} 40 | RUN git clone https://github.com/MabezDev/rust-xtensa.git --depth 1 41 | WORKDIR ${BUILD_ROOT}/rust-xtensa 42 | ENV RUST_BUILD ${BUILD_ROOT}/rust_build 43 | RUN mkdir -p ${RUST_BUILD} 44 | RUN ./configure --llvm-root="${LLVM_BUILD}" --prefix="${RUST_BUILD}" 45 | 46 | ## Build the compiler 47 | RUN python ./x.py build 48 | RUN python ./x.py install 49 | RUN $HOME/.cargo/bin/rustup toolchain link xtensa ${RUST_BUILD} 50 | RUN $HOME/.cargo/bin/rustup run xtensa rustc --print target-list | grep xtensa 51 | 52 | # Setup ESP-IDF & esptool 53 | ENV ESP32_IDF /xtensa-esp32-elf 54 | ENV ESP8266_IDF /xtensa-lx106-elf 55 | WORKDIR / 56 | RUN wget https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz && \ 57 | tar xzf xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz && \ 58 | rm xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz && \ 59 | wget https://dl.espressif.com/dl/xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz && \ 60 | tar xzf xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz && \ 61 | rm xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz 62 | RUN pip install esptool 63 | 64 | ## Setup Xargo 65 | RUN $HOME/.cargo/bin/cargo install xargo 66 | ENV XARGO_RUST_SRC ${BUILD_ROOT}/rust-xtensa/src 67 | ENV RUSTC ${RUST_BUILD}/bin/rustc 68 | 69 | ## Setup path 70 | ENV HOME /root 71 | ENV PATH ${ESP8266_IDF}/bin:${ESP32_IDF}/bin:/usr/local/bin:${HOME}/.cargo/bin:$PATH 72 | 73 | ## test build sample project 74 | WORKDIR / 75 | RUN git clone https://github.com/mtnmts/xtensa-rust-quickstart 76 | WORKDIR /xtensa-rust-quickstart 77 | RUN xargo build --release 78 | WORKDIR / 79 | RUN rm -rf /xtensa-rust-quickstart 80 | 81 | ## Make it a bit more convinient for people dropping into the container 82 | ## The image is already quite large, this doesn't make a significant difference 83 | RUN apt-get install -y neovim vim xxd tmux fish 84 | 85 | ## Build project from /code 86 | WORKDIR /code 87 | CMD [ "xargo", "build", "--release" ] 88 | 89 | --------------------------------------------------------------------------------