From 03162988254a8aacc1268e88d71aef90017f396f Mon Sep 17 00:00:00 2001 From: cscherr Date: Fri, 11 Jul 2025 12:03:03 +0200 Subject: [PATCH] fix: flexible build.rs for algorithms (support both host and stm32) --- .cargo/config.toml | 1 - crates/algorithms/algorithms-c/README.md | 18 ++++++++----- crates/algorithms/algorithms-c/project.yml | 4 +-- crates/algorithms/benches/crc32bench.rs | 6 ++--- crates/algorithms/benches/crc32bench_iai.rs | 6 ++--- crates/algorithms/build.rs | 29 +++++++++++++++++++-- 6 files changed, 46 insertions(+), 18 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index dc51815..90b5161 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,7 +1,6 @@ [target.thumbv6m-none-eabi] runner = 'probe-rs run --chip STM32L053R8' - [alias] arun = "run --target thumbv6m-none-eabi" atest = "test --target x86_64-unknown-linux-gnu" diff --git a/crates/algorithms/algorithms-c/README.md b/crates/algorithms/algorithms-c/README.md index b0539df..b543f49 100755 --- a/crates/algorithms/algorithms-c/README.md +++ b/crates/algorithms/algorithms-c/README.md @@ -1,7 +1,7 @@ # Algorithms-c -This subproject contains all C Code for the benchmarking. It is built with -[ceedling](https://www.throwtheswitch.org/ceedling), a more modern build tool +This subproject contains all C Code for the benchmarking. It is built with +[ceedling](https://www.throwtheswitch.org/ceedling), a more modern build tool for C projects that also integrates well with the Unity framework. ## Compiler @@ -12,9 +12,13 @@ To compile for STM32, you need to crosscompile # apt install binutils-arm-none-eabi gcc-arm-none-eabi ``` -Then, use your tool with an `arm-none-eabi-` prefix: `arm-none-eabi-gcc`. This -project has been configured to use `arm-none-eabi-gcc` to compile modules and to -"link" the object files into a static library with `arm-none-eabi-ar`. +Then, use your tool with an `arm-none-eabi-` prefix: `arm-none-eabi-gcc`. This +project has been configured to use the compiler specified in `$CC` to compile modules and `$AR` to +"link" the object files (this should be `ar` for a static library like this). -The unit tests will be compiled with `gcc` to be ran on the host computer, not -on the STM32. +```bash +# compile for STM32 +CC=arm-none-eabi-gcc AR=arm-none-eabi-ar ceedling release +# compile for this computer +CC=gcc AR=ar ceedling release +``` diff --git a/crates/algorithms/algorithms-c/project.yml b/crates/algorithms/algorithms-c/project.yml index dcf9307..cead82f 100755 --- a/crates/algorithms/algorithms-c/project.yml +++ b/crates/algorithms/algorithms-c/project.yml @@ -381,7 +381,7 @@ # :name: # :optional: FALSE :release_compiler: - :executable: arm-none-eabi-gcc + :executable: "#{ENV['CC']}" :arguments: - "-g" - "-Wall" @@ -391,7 +391,7 @@ - "${2}" :optional: FALSE :release_linker: - :executable: arm-none-eabi-ar + :executable: "#{ENV['AR']}" :arguments: - "-rcs" - "${2}" diff --git a/crates/algorithms/benches/crc32bench.rs b/crates/algorithms/benches/crc32bench.rs index f39a72d..de7338e 100755 --- a/crates/algorithms/benches/crc32bench.rs +++ b/crates/algorithms/benches/crc32bench.rs @@ -1,12 +1,12 @@ -use crc::{Crc, Crc32, ffi}; +use algorithms::crc::{CHECK_DATA, Crc, Crc32, ffi}; use criterion::{Criterion, black_box, criterion_group, criterion_main}; pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("crc32", |b| { - b.iter(|| Crc32::checksum(black_box(&crc::CHECK_DATA))) + b.iter(|| Crc32::checksum(black_box(&CHECK_DATA))) }); c.bench_function("ffi::crc32", |b| { - b.iter(|| ffi::Crc32::checksum(black_box(&crc::CHECK_DATA))) + b.iter(|| ffi::Crc32::checksum(black_box(&CHECK_DATA))) }); } diff --git a/crates/algorithms/benches/crc32bench_iai.rs b/crates/algorithms/benches/crc32bench_iai.rs index 9ab08b4..56c6bc3 100755 --- a/crates/algorithms/benches/crc32bench_iai.rs +++ b/crates/algorithms/benches/crc32bench_iai.rs @@ -1,11 +1,11 @@ -use crc::{Crc, Crc32, ffi}; +use algorithms::crc::{CHECK_DATA, Crc, Crc32, ffi}; use iai::black_box; fn iai_benchmark_native() -> ::Checksum { - Crc32::checksum(black_box(&crc::CHECK_DATA)) + Crc32::checksum(black_box(&CHECK_DATA)) } fn iai_benchmark_ffi() -> ::Checksum { - ffi::Crc32::checksum(black_box(&crc::CHECK_DATA)) + ffi::Crc32::checksum(black_box(&CHECK_DATA)) } iai::main!(iai_benchmark_native, iai_benchmark_ffi); diff --git a/crates/algorithms/build.rs b/crates/algorithms/build.rs index b75f118..68c43b6 100755 --- a/crates/algorithms/build.rs +++ b/crates/algorithms/build.rs @@ -1,6 +1,31 @@ +fn env(s: &str) -> String { + std::env::var(s).unwrap() +} + fn main() { - let status = std::process::Command::new("ceedling") - .arg("release") + std::process::Command::new("ceedling") + .arg("clobber") + .current_dir("./algorithms-c/") + .status() + .expect("could not cleanup old algorithms-c files"); + + let mut cmd = std::process::Command::new("ceedling"); + cmd.arg("release"); + + let arch = env("CARGO_CFG_TARGET_ARCH"); + let pw = env("CARGO_CFG_TARGET_POINTER_WIDTH"); + let os = env("CARGO_CFG_TARGET_OS"); + + if arch == "arm" && pw == "32" && os == "none" { + cmd.env("CC", "arm-none-eabi-gcc") + .env("AR", "arm-none-eabi-ar"); + } else if os == "linux" { + cmd.env("CC", "gcc").env("AR", "ar"); + } else { + panic!("Unsupported build target") + } + + let status = cmd .current_dir("./algorithms-c/") .status() .expect("could not make c stuff");