cargo: clean up dependencies and improve build times

This commit is contained in:
2025-10-17 18:06:24 +02:00
parent baa85612b7
commit 088dde4450
13 changed files with 184 additions and 237 deletions

108
build.rs
View File

@@ -3,13 +3,22 @@ use std::io::Write;
use std::path::Path;
use std::fs::File;
use image::GenericImageView;
use rmp;
use csv::Reader;
fn main() {
linker_be_nice();
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
println!("cargo:rustc-link-arg=-Tlinkall.x");
#[cfg(feature="simulation")]
write_sim_data();
#[cfg(feature="oled")]
compile_assets();
}
fn compile_assets() {
let asset_path = Path::new("assets");
let mut image_output = File::create(Path::new("target/images.rs")).unwrap();
@@ -55,13 +64,100 @@ fn main() {
println!("cargo::rerun-if-changed={fname_str}");
}
}
}
/*let test_data_path = Path::new("test-data");
let gps_data = File::open(test_data_path.join("LocationGps.csv")).unwrap();
let accel_data = File::open(test_data_path.join("AccelerometerUncalibrated.csv")).unwrap();
let gyro_data = File::open(test_data_path.join("GyroscopeUncalibrated.csv")).unwrap();
let mut test_data_output = File::create(Path::new("target/test_data.rs")).unwrap();*/
fn write_sim_data() {
let test_data_path = Path::new("test-data");
let output_path = Path::new("target");
let gps_input = test_data_path.join("LocationGps.csv");
let gyro_input = test_data_path.join("GyroscopeUncalibrated.csv");
let accel_input = test_data_path.join("AccelerometerUncalibrated.csv");
let gps_output = output_path.join("gps_test_data.msgpack");
let motion_output = output_path.join("motion_test_data.msgpack");
println!("cargo::rerun-if-changed={}", gps_input.to_str().unwrap());
if !gps_output.exists() || gps_output.metadata().unwrap().modified().unwrap() < gps_input.metadata().unwrap().modified().unwrap() {
let mut gps_data = Reader::from_reader(File::open(gps_input).unwrap());
let headers = gps_data.headers().unwrap();
let (timestamp_idx, lat_idx, lon_idx) = (
headers.iter().position(|x| { x == "seconds_elapsed" }).unwrap(),
headers.iter().position(|x| { x == "longitude" }).unwrap(),
headers.iter().position(|x| { x == "latitude" }).unwrap(),
);
let mut gps_output = File::create(gps_output).unwrap();
let mut last_stamp = 0.0;
for record in gps_data.records().flatten() {
let (timestamp, lat, lon) = (
record.get(timestamp_idx).unwrap().parse().unwrap(),
record.get(lat_idx).unwrap().parse().unwrap(),
record.get(lon_idx).unwrap().parse().unwrap()
);
let next_delay = timestamp - last_stamp;
last_stamp = timestamp;
rmp::encode::write_array_len(&mut gps_output, 3).unwrap();
rmp::encode::write_f64(&mut gps_output, next_delay).unwrap();
rmp::encode::write_f64(&mut gps_output, lat).unwrap();
rmp::encode::write_f64(&mut gps_output, lon).unwrap();
}
}
println!("cargo::rerun-if-changed={}", accel_input.to_str().unwrap());
println!("cargo::rerun-if-changed={}", gyro_input.to_str().unwrap());
let rebuild_motion = {
if motion_output.exists() {
let motion_stamp = motion_output.metadata().unwrap().modified().unwrap();
motion_stamp < accel_input.metadata().unwrap().modified().unwrap() || motion_stamp < gyro_input.metadata().unwrap().modified().unwrap()
} else {
true
}
};
if rebuild_motion {
let mut accel_data = Reader::from_reader(File::open(accel_input).unwrap());
let mut gyro_data = Reader::from_reader(File::open(gyro_input).unwrap());
let headers = accel_data.headers().unwrap();
let (timestamp_idx, accel_x_idx, accel_y_idx, accel_z_idx) = (
headers.iter().position(|x| { x == "seconds_elapsed" }).unwrap(),
headers.iter().position(|x| { x == "x" }).unwrap(),
headers.iter().position(|x| { x == "y" }).unwrap(),
headers.iter().position(|x| { x == "z" }).unwrap(),
);
let headers = gyro_data.headers().unwrap();
let (gyro_x_idx, gyro_y_idx, gyro_z_idx) = (
headers.iter().position(|x| { x == "x" }).unwrap(),
headers.iter().position(|x| { x == "y" }).unwrap(),
headers.iter().position(|x| { x == "z" }).unwrap(),
);
let mut motion_output = File::create(motion_output).unwrap();
let mut last_stamp = 0.0;
for (accel_record, gyro_record) in accel_data.records().flatten().zip(gyro_data.records().flatten()) {
let (timestamp, accel_x, accel_y, accel_z) = (
accel_record.get(timestamp_idx).unwrap().parse().unwrap(),
accel_record.get(accel_x_idx).unwrap().parse().unwrap(),
accel_record.get(accel_y_idx).unwrap().parse().unwrap(),
accel_record.get(accel_z_idx).unwrap().parse().unwrap()
);
let (gyro_x, gyro_y, gyro_z) = (
gyro_record.get(gyro_x_idx).unwrap().parse().unwrap(),
gyro_record.get(gyro_y_idx).unwrap().parse().unwrap(),
gyro_record.get(gyro_z_idx).unwrap().parse().unwrap()
);
let next_delay = timestamp - last_stamp;
if next_delay >= 0.02 {
last_stamp = timestamp;
rmp::encode::write_array_len(&mut motion_output, 7).unwrap();
rmp::encode::write_f64(&mut motion_output, next_delay).unwrap();
rmp::encode::write_f64(&mut motion_output, accel_x).unwrap();
rmp::encode::write_f64(&mut motion_output, accel_y).unwrap();
rmp::encode::write_f64(&mut motion_output, accel_z).unwrap();
rmp::encode::write_f64(&mut motion_output, gyro_x).unwrap();
rmp::encode::write_f64(&mut motion_output, gyro_y).unwrap();
rmp::encode::write_f64(&mut motion_output, gyro_z).unwrap();
}
}
}
}
fn linker_be_nice() {