run cargo from CMake, remove hardcoded target from .cargo/config (#5)

* cmake: run cargo build from idf.py build
This commit is contained in:
Ivan Grokhotkov 2021-06-14 08:51:26 +02:00 committed by GitHub
parent 135c6e5670
commit d797b1e692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 1330 deletions

View File

@ -1,6 +1,3 @@
[build] [unstable]
target = "xtensa-esp32-none-elf" build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ build/
target/ target/
sdkconfig.old sdkconfig.old
sdkconfig
debug/ debug/
target/ target/
Cargo.lock Cargo.lock

View File

@ -20,50 +20,6 @@ Install the RISCV target for Rust:
rustup target add riscv32i-unknown-none-elf 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 ## 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: 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 idf.py build
``` ```
This also runs Cargo internally, building a static library out of Rust code.
## Flash ## Flash
Flash the compiled binary by running: Flash the compiled binary by running:

View File

@ -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 wget --continue https://github.com/espressif/rust-esp32-example/archive/refs/heads/main.zip
unzip main.zip unzip main.zip
cd rust-esp32-example-main/rustlib cd rust-esp32-example-main
cargo build --release
cd ..
idf.py build idf.py build
``` ```

View File

@ -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 tar xf xtensa-esp32-elf-llvm11_0_0-x86_64-apple-darwin.tar.xz
export PATH="`pwd`/xtensa-esp32-elf-clang/bin/:$PATH" export PATH="`pwd`/xtensa-esp32-elf-clang/bin/:$PATH"
wget https://dl.espressif.com/dl/idf-rust/rust-esp32-example-0.2.tar.gz wget --continue https://github.com/espressif/rust-esp32-example/archive/refs/heads/main.zip
tar xvzf rust-esp32-example-0.2.tar.gz unzip main.zip
cd rust-esp32-example/rustlib cd rust-esp32-example-main
cargo build --release
cd ..
idf.py build idf.py build
``` ```

View File

@ -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 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 7z x rust-esp32-example.zip
cd rust-esp32-example-main/rustlib cd rust-esp32-example-main
cargo build --release
cd ..
idf.py build idf.py build
``` ```

View File

@ -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 tar xzf llvm-patch-0.1.x86_64-unknown-linux-gnu.tar.gz
export PATH="`pwd`/llvm-patch/bin/:$PATH" export PATH="`pwd`/llvm-patch/bin/:$PATH"
wget https://dl.espressif.com/dl/idf-rust/rust-esp32-example-0.2.tar.gz wget --continue https://github.com/espressif/rust-esp32-example/archive/refs/heads/main.zip
tar xvzf rust-esp32-example-0.2.tar.gz unzip main.zip
cd rust-esp32-example/rustlib cd rust-esp32-example-main
cargo build --release
cd ..
idf.py build idf.py build
``` ```

View File

@ -1,16 +1,44 @@
idf_component_register(SRCS "main.c" idf_component_register(SRCS "main.c"
INCLUDE_DIRS "") INCLUDE_DIRS "")
set(CargoBuildType "release") set(CARGO_BUILD_TYPE "release")
set(CARGO_BUILD_ARG "--release")
# Import the Rust library. if(CONFIG_IDF_TARGET_ARCH_RISCV)
if((${IDF_TARGET} MATCHES "esp32") OR (${IDF_TARGET} MATCHES "esp32s2")) set(CARGO_TARGET "riscv32i-unknown-elf")
set(ToolchainTarget "xtensa-esp32-none-elf") elseif(CONFIG_IDF_TARGET_ARCH_XTENSA)
elseif(${IDF_TARGET} MATCHES "esp32c3") set(CARGO_TARGET "xtensa-${CONFIG_IDF_TARGET}-none-elf")
set(ToolchainTarget "riscv32i-unknown-none-elf")
endif() 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' # 'main' calls a function from the library, so link it to 'main'
target_link_libraries(${COMPONENT_LIB} PRIVATE rustlib) target_link_libraries(${COMPONENT_LIB} PRIVATE rustlib)

1255
sdkconfig

File diff suppressed because it is too large Load Diff

0
sdkconfig.defaults Normal file
View File