Previous edition Create a cargo workspace

Set build target as RISC-V

After creating a cargo workspace in previous edition, its time to configure cargo to build for a RISC-V target instead of host target. Host target might be your Mac, Linux or Windows Machine with x86, Mx, etc Processor; however the intention here is to set the environment/cargo setup to build for RISC-V target(processor).

Cargo Configuration

Cargo allows local configuration at directory, crate level and extends it to global for project, workspace & even at user level (at user home directory). So, its important to know the cargo configuration hierarchy and path from which cargo commands are invoked. For simplicity, lets keep configuration at workspace’s root directory and invoke cargo commands from root directory.

Cargo.toml vs .cargo/config.toml

The default Cargo.toml configuration created while executing the cargo new command is must for every cargo rust project to track project’s structure and project dependencies. However, it don’t cover build settings & any custom configuration. To set the cargo project for RISC-V target (instead of host target), custom settings are required. Custom settings are placed in .cargo directory either as config or as config.toml. In a line, .cargo/config.toml is for build settings passed to rust-compiler and Cargo.toml for project dependencies for package manager.

1. Create empty .cargo/config.toml file

Create .cargo directory at the root of workspace.

bootstrap-ws $mkdir .cargo
bootstrap-ws $ls -a
.		.cargo		Cargo.toml	libs
..		Cargo.lock	execs		target
bootstrap-ws $

Now, create a config file (config.toml) inside .cargo directory with touch command. Lets figure out what goes in later.

bootstrap-ws $cd .cargo/
.cargo $touch config.toml
.cargo $ls -a
.		..		config.toml
.cargo $cd ..
bootstrap-ws $tree -a
.
├── .cargo
│   └── config.toml
├── Cargo.lock
├── Cargo.toml
├── execs
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── libs
    ├── Cargo.toml
    └── src
        └── lib.rs

5 directories, 7 files
bootstrap-ws $
2. Set build target for RISC-V
$cat .cargo/config.toml 
[build]
target = "riscv32imac-unknown-none-elf"

For a moment, lets not try to understand what target is riscv32imac-unknown-none-elf and presume its a valid target for riscv.

Now, the cargo build will fail for many reasons. First, so far, the cargo and the project are set for host target & host toolchain. If the rust toolchain for risc-v target is NOT installed, it would complain. You can resolve this by adding the target toolchain with rustup command.

rustup target add riscv32imac-unknown-none-elf

Even after installing the rust toolchain for target, cargo build would still fail. Lets try to dig-in more why it fails and how to resolve it in the next edition !!

Keep reading