Skip to content

Minimal buildsystem #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/rust_all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ jobs:
run: |
rustup component add llvm-tools-preview
rustup target add thumbv7em-none-eabihf
cargo install cbindgen
cargo install cargo-binutils
cargo install cargo-make
cargo install cbindgen

- name: Environment info
run: |
Expand All @@ -47,3 +48,10 @@ jobs:
cargo size
cargo build --release
cargo size --release

- name: Minimal buildsystem
working-directory: ${{github.workspace}}/minimal_buildsystem
run: |
cargo make ci_debug
cargo make ci_release
cargo doc
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# lowlevel_rust

Rust on microcontrollers

# Projects

- Minimal blinky
- Barebones blinky example i.e linker script to main
- Minimal buildsystem
- Initial [cargo-make](https://github.com/sagiegurari/cargo-make) framework to have configurable build options i.e extending `cargo`

# Roadmap

## RTOS

## Debugging

## Tooling
43 changes: 43 additions & 0 deletions minimal_buildsystem/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[target.thumbv7em-none-eabihf]
# uncomment this to make `cargo run` execute programs on QEMU
# runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# uncomment ONE of these three option to make `cargo run` start a GDB session
# which option to pick depends on your system
# runner = "arm-none-eabi-gdb -q -x openocd.gdb"
# runner = "gdb-multiarch -q -x openocd.gdb"
# runner = "gdb -q -x openocd.gdb"
rustflags = [
# This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x
# See https://github.com/rust-embedded/cortex-m-quickstart/pull/95
# "-C", "link-arg=--nmagic",

# LLD (shipped with the Rust toolchain) is used as the default linker
# "-C", "link-arg=-Tgcc_arm.ld",

# Generate a .map file
# "-C", "link-args=-Map=minimal_buildsystem.map",

# if you run into problems with LLD switch to the GNU linker by commenting out
# this line
"-C", "linker=arm-none-eabi-ld",

# if you need to link to pre-compiled C libraries provided by a C toolchain
# use GCC as the linker by commenting out both lines above and then
# uncommenting the three lines below
"-C", "linker=arm-none-eabi-gcc",
"-C", "link-arg=-Wl,-Tgcc_arm.ld",
"-C", "link-arg=-Wl,-Map,minimal_buildsystem.map",
"-C", "link-arg=-nostartfiles",
]

[build]
# Pick ONE of these compilation targets
# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
# target = "thumbv7m-none-eabi" # Cortex-M3
# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
# target = "thumbv8m.base-none-eabi" # Cortex-M23
# target = "thumbv8m.main-none-eabi" # Cortex-M33 (no FPU)
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)
6 changes: 6 additions & 0 deletions minimal_buildsystem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Files
*.map
.vscode/.cortex-debug.*

# Folders
target
34 changes: 34 additions & 0 deletions minimal_buildsystem/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"configurations": [
{
"cwd": "${workspaceFolder}",
"executable": "target/thumbv7em-none-eabihf/debug/minimal_buildsystem",
"configFiles": [
"stm32l4discovery.cfg"
],
"postLaunchCommands": [
"load",
"monitor arm semihosting enable",
],
"name": "Rust Debug",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd"
},
{
"cwd": "${workspaceFolder}",
"executable": "target/thumbv7em-none-eabihf/release/minimal_buildsystem",
"configFiles": [
"stm32l4discovery.cfg"
],
"postLaunchCommands": [
"load",
"monitor arm semihosting enable",
],
"name": "Rust Release",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd"
}
]
}
10 changes: 10 additions & 0 deletions minimal_buildsystem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "minimal_buildsystem"
version = "0.1.0"
authors = ["Niket Naidu <coder137@gmail.com>"]
edition = "2021"
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
83 changes: 83 additions & 0 deletions minimal_buildsystem/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Duckscript is used here to convert \ to / for binary output path
[tasks.build_debug]
script_runner = "@duckscript"
script = '''
output = set ${CARGO_MAKE_CRATE_CUSTOM_TRIPLE_TARGET_DIRECTORY}/debug/${CARGO_MAKE_CRATE_NAME}
output = replace ${output} \\ /
set_env OUTPUT ${output}
exec cargo build
'''

# Duckscript is used here to convert \ to / for binary output path
[tasks.build_release]
script_runner = "@duckscript"
script = '''
output = set ${CARGO_MAKE_CRATE_CUSTOM_TRIPLE_TARGET_DIRECTORY}/release/${CARGO_MAKE_CRATE_NAME}
output = replace ${output} \\ /
set_env OUTPUT ${output}
exec cargo build --release
'''

[tasks.test]
command = "cargo"
args = ["test", "--target", "${CARGO_MAKE_RUST_TARGET_TRIPLE}"]

[tasks.flash_debug]
script_runner = "@shell"
script = '''
openocd -f board/stm32l4discovery.cfg -c "program ${OUTPUT} verify reset exit"
'''
dependencies = ["build_debug"]

[tasks.ci_debug]
dependencies = [
"build_debug",
"test",
"objcopy_to_binary",
"objcopy_to_hex",
"objdump",
"size",
]

[tasks.ci_release]
dependencies = [
"build_release",
"test",
"objcopy_to_binary",
"objcopy_to_hex",
"objdump",
"size",
]

# Private Tasks

# Requires
# arm-none-eabi-size executable (ARM GCC toolchain)
# OUTPUT env variable (Set by build_*)
[tasks.size]
private = true
command = "arm-none-eabi-size"
args = ["${OUTPUT}"]

# arm-none-eabi-objcopy executable (ARM GCC toolchain)
# OUTPUT env variable (Set by build_*)
[tasks.objcopy_to_binary]
private = true
command = "arm-none-eabi-objcopy"
args = ["-O", "binary", "${OUTPUT}", "${OUTPUT}.bin"]

# arm-none-eabi-objcopy executable (ARM GCC toolchain)
# OUTPUT env variable (Set by build_*)
[tasks.objcopy_to_hex]
private = true
command = "arm-none-eabi-objcopy"
args = ["-O", "ihex", "${OUTPUT}", "${OUTPUT}.hex"]

# arm-none-eabi-objdump executable (ARM GCC toolchain)
# OUTPUT env variable (Set by build_*)
[tasks.objdump]
private = true
script_runner = "@shell"
script = '''
arm-none-eabi-objdump --source --all-headers --demangle --line-numbers --wide ${OUTPUT} > ${OUTPUT}.lst
'''
78 changes: 78 additions & 0 deletions minimal_buildsystem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
- [Minimal Buildsystem](#minimal-buildsystem)
- [Links](#links)
- [Pre-requisites](#pre-requisites)
- [Build system for Rust](#build-system-for-rust)
- [\[build\_debug | build\_release\]](#build_debug--build_release)
- [test](#test)
- [flash\_debug](#flash_debug)
- [\[ci\_debug | ci\_release\]](#ci_debug--ci_release)
- [doc](#doc)

# Minimal Buildsystem

This code has been tested on

- B-L475-IOT01A board (STM32L475VGT6 ARM Cortex M4 CPU with FPU)

## Links

- [Cargo binutils](https://github.com/rust-embedded/cargo-binutils)
- [Embedded Rust book](https://doc.rust-lang.org/stable/embedded-book/)
- [Lowlevel Embedded Rust book](https://docs.rust-embedded.org/embedonomicon/)

## Pre-requisites

- Pre-requisites from `minimal_blinky`
- cargo install cargo-make

## Build system for Rust

Cargo make is used to build, run and deploy various aspects of this project.
This is because we need configurations for

- Building microcontroller (on-target) code for different supported architectures and toolchains.
- Pre-processing (.c to .rs conversion, code generation)
- Building (convert to .elf)
- Post-processing (.elf size, .bin and .hex generation, flashing after build, CI run)
- Unit-testing functionality (off-target) using host toolchain
- Documentation generation

Commands can be run using

```bash
cargo make [command]
```

### [build_debug | build_release]

Makes a debug or release build of the project using the microcontroller target

See `.cargo/config.toml`, **build.target** field

### test

Make a build of the project using the default system host toolchain and target

Run `rustup default` to see your system host toolchain
Run `rustup target list` to see the system host target installed for your toolchain

### flash_debug

Uses openocd to flash your generated `/debug/*.elf` file to the STM32 microcontroller

### [ci_debug | ci_release]

Single command that does the following in order

- Build on-target code [debug | release]
- Size of on-target code
- Executes unit-tests and mocks using an off-target build
- Convert `*.elf` to `*.bin`
- Convert `*.elf` to `*.hex`
- Dump `*.elf` symbols to `*.lst`

### doc

This is not added to `cargo make`

Invoke cargo doc from the root
26 changes: 26 additions & 0 deletions minimal_buildsystem/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.

use std::env;
use std::path::PathBuf;

fn reference() {
let out: &PathBuf = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
println!("{}", out.display());
}

fn linker_script() {
println!("cargo:rerun-if-changed=gcc_arm.ld");
}

fn main() {
reference();
linker_script();
}
Loading