run cargo from CMake, remove hardcoded target from .cargo/config (#5)
* cmake: run cargo build from idf.py build
This commit is contained in:
parent
135c6e5670
commit
d797b1e692
|
|
@ -1,6 +1,3 @@
|
|||
[build]
|
||||
target = "xtensa-esp32-none-elf"
|
||||
|
||||
[unstable]
|
||||
build-std = ["std", "panic_abort"]
|
||||
build-std-features = ["panic_immediate_abort"]
|
||||
[unstable]
|
||||
build-std = ["std", "panic_abort"]
|
||||
build-std-features = ["panic_immediate_abort"]
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ build/
|
|||
target/
|
||||
|
||||
sdkconfig.old
|
||||
sdkconfig
|
||||
debug/
|
||||
target/
|
||||
Cargo.lock
|
||||
|
|
|
|||
46
README.md
46
README.md
|
|
@ -20,50 +20,6 @@ Install the RISCV target for Rust:
|
|||
rustup target add riscv32i-unknown-none-elf
|
||||
```
|
||||
|
||||
## Build Rust library
|
||||
|
||||
This will eventually be folded into the main CMake build, but for now, build the library separately.
|
||||
|
||||
### ESP32 or ESP32-S2
|
||||
|
||||
To build the Rust library, run:
|
||||
|
||||
```sh
|
||||
cd rustlib
|
||||
xargo build --release
|
||||
cd ..
|
||||
```
|
||||
|
||||
Build with manual configuration of the target:
|
||||
|
||||
```sh
|
||||
export RUSTC=/usr/local/xtensa/rust/bin/rustc
|
||||
export XARGO_RUST_SRC=/usr/local/xtensa/rust/library
|
||||
cd rustlib
|
||||
xargo build --release --target=xtensa-esp32-none-elf
|
||||
cd ..
|
||||
```
|
||||
|
||||
### ESP32-C3
|
||||
|
||||
To build the Rust library, run:
|
||||
|
||||
```sh
|
||||
cd rustlib
|
||||
cargo build --release
|
||||
cd ..
|
||||
```
|
||||
|
||||
Build with manual configuration of the target:
|
||||
|
||||
```sh
|
||||
unset RUSTC
|
||||
unset XARGO_RUST_SRC
|
||||
cd rustlib
|
||||
cargo build --release --target=riscv32i-unknown-none-elf
|
||||
cd ..
|
||||
```
|
||||
|
||||
## Configure
|
||||
|
||||
First ensure that the environment variables for the ESP32 SDK are properly set up. If you have followed the instructions in the Getting Started guide, activate the environment with the `get_idf` alias:
|
||||
|
|
@ -103,6 +59,8 @@ Build the project by running:
|
|||
idf.py build
|
||||
```
|
||||
|
||||
This also runs Cargo internally, building a static library out of Rust code.
|
||||
|
||||
## Flash
|
||||
|
||||
Flash the compiled binary by running:
|
||||
|
|
|
|||
|
|
@ -33,9 +33,7 @@ export PATH="`pwd`/xtensa-esp32-elf-clang/bin/:$PATH"
|
|||
|
||||
wget --continue https://github.com/espressif/rust-esp32-example/archive/refs/heads/main.zip
|
||||
unzip main.zip
|
||||
cd rust-esp32-example-main/rustlib
|
||||
cargo build --release
|
||||
cd ..
|
||||
cd rust-esp32-example-main
|
||||
idf.py build
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -31,11 +31,9 @@ wget https://dl.espressif.com/dl/idf-rust/dist/x86_64-apple-darwin/xtensa-esp32-
|
|||
tar xf xtensa-esp32-elf-llvm11_0_0-x86_64-apple-darwin.tar.xz
|
||||
export PATH="`pwd`/xtensa-esp32-elf-clang/bin/:$PATH"
|
||||
|
||||
wget https://dl.espressif.com/dl/idf-rust/rust-esp32-example-0.2.tar.gz
|
||||
tar xvzf rust-esp32-example-0.2.tar.gz
|
||||
cd rust-esp32-example/rustlib
|
||||
cargo build --release
|
||||
cd ..
|
||||
wget --continue https://github.com/espressif/rust-esp32-example/archive/refs/heads/main.zip
|
||||
unzip main.zip
|
||||
cd rust-esp32-example-main
|
||||
idf.py build
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -48,8 +48,6 @@ $env:PATH="${ClangPatchPath};$env:PATH"
|
|||
|
||||
Invoke-WebRequest https://github.com/espressif/rust-esp32-example/archive/refs/heads/main.zip -OutFile rust-esp32-example.zip
|
||||
7z x rust-esp32-example.zip
|
||||
cd rust-esp32-example-main/rustlib
|
||||
cargo build --release
|
||||
cd ..
|
||||
cd rust-esp32-example-main
|
||||
idf.py build
|
||||
```
|
||||
|
|
|
|||
|
|
@ -39,11 +39,8 @@ wget https://dl.espressif.com/dl/idf-rust/dist/x86_64-unknown-linux-gnu/llvm-pat
|
|||
tar xzf llvm-patch-0.1.x86_64-unknown-linux-gnu.tar.gz
|
||||
export PATH="`pwd`/llvm-patch/bin/:$PATH"
|
||||
|
||||
wget https://dl.espressif.com/dl/idf-rust/rust-esp32-example-0.2.tar.gz
|
||||
tar xvzf rust-esp32-example-0.2.tar.gz
|
||||
cd rust-esp32-example/rustlib
|
||||
cargo build --release
|
||||
cd ..
|
||||
wget --continue https://github.com/espressif/rust-esp32-example/archive/refs/heads/main.zip
|
||||
unzip main.zip
|
||||
cd rust-esp32-example-main
|
||||
idf.py build
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,44 @@
|
|||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS "")
|
||||
|
||||
set(CargoBuildType "release")
|
||||
|
||||
# Import the Rust library.
|
||||
if((${IDF_TARGET} MATCHES "esp32") OR (${IDF_TARGET} MATCHES "esp32s2"))
|
||||
set(ToolchainTarget "xtensa-esp32-none-elf")
|
||||
elseif(${IDF_TARGET} MATCHES "esp32c3")
|
||||
set(ToolchainTarget "riscv32i-unknown-none-elf")
|
||||
set(CARGO_BUILD_TYPE "release")
|
||||
set(CARGO_BUILD_ARG "--release")
|
||||
if(CONFIG_IDF_TARGET_ARCH_RISCV)
|
||||
set(CARGO_TARGET "riscv32i-unknown-elf")
|
||||
elseif(CONFIG_IDF_TARGET_ARCH_XTENSA)
|
||||
set(CARGO_TARGET "xtensa-${CONFIG_IDF_TARGET}-none-elf")
|
||||
endif()
|
||||
|
||||
add_prebuilt_library(rustlib "../rustlib/target/${ToolchainTarget}/${CargoBuildType}/librustlib.a")
|
||||
set(RUST_PROJECT_DIR "${CMAKE_CURRENT_LIST_DIR}/../rustlib")
|
||||
set(RUST_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/rustlib")
|
||||
set(RUST_STATIC_LIBRARY "${RUST_BUILD_DIR}/${CARGO_TARGET}/${CARGO_BUILD_TYPE}/librustlib.a")
|
||||
|
||||
ExternalProject_Add(
|
||||
rustlib_project
|
||||
PREFIX "${RUST_PROJECT_DIR}"
|
||||
BUILD_COMMAND ${CMAKE_COMMAND} -E env
|
||||
CARGO_BUILD_TARGET=${CARGO_TARGET}
|
||||
CARGO_BUILD_TARGET_DIR=${RUST_BUILD_DIR}
|
||||
cargo build ${CARGO_BUILD_ARG}
|
||||
DOWNLOAD_COMMAND ""
|
||||
CONFIGURE_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
BUILD_ALWAYS TRUE
|
||||
BUILD_DIR "${RUST_BUILD_DIR}"
|
||||
INSTALL_DIR "${RUST_BUILD_DIR}"
|
||||
STAMP_DIR "${RUST_BUILD_DIR}/stamp"
|
||||
TMP_DIR "${RUST_BUILD_DIR}/tmp"
|
||||
DOWNLOAD_DIR "${RUST_BUILD_DIR}"
|
||||
SOURCE_DIR "${RUST_PROJECT_DIR}"
|
||||
BUILD_BYPRODUCTS "${RUST_STATIC_LIBRARY}"
|
||||
)
|
||||
|
||||
# this adds "rustlib" static library target.
|
||||
# PRIV_REQUIRES tells CMake that librustlib.a itself depends on libmain.a.
|
||||
add_prebuilt_library(rustlib "${RUST_STATIC_LIBRARY}" PRIV_REQUIRES main)
|
||||
|
||||
# rustlib will be produced by building rustlib_project target
|
||||
add_dependencies(rustlib rustlib_project)
|
||||
|
||||
# 'main' calls a function from the library, so link it to 'main'
|
||||
target_link_libraries(${COMPONENT_LIB} PRIVATE rustlib)
|
||||
|
|
|
|||
Loading…
Reference in New Issue