Skip to content
Snippets Groups Projects
Commit 5e11b1f7 authored by Samuel Tardieu's avatar Samuel Tardieu
Browse files

Add common build support files in "build-support" crate

parent a7c080a5
No related branches found
No related tags found
No related merge requests found
......@@ -41,6 +41,14 @@ version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "build-support"
version = "0.1.0"
dependencies = [
"semver 1.0.23",
"thiserror",
]
[[package]]
name = "byteorder"
version = "1.5.0"
......@@ -131,6 +139,7 @@ dependencies = [
name = "dc-motor-driver-hat"
version = "0.3.0"
dependencies = [
"build-support",
"cortex-m",
"cortex-m-rt",
"critical-section",
......@@ -144,7 +153,6 @@ dependencies = [
"heapless",
"i2c2-target",
"panic-probe",
"semver 1.0.23",
"support",
]
......@@ -758,18 +766,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.62"
version = "1.0.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb"
checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.62"
version = "1.0.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c"
checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [
"proc-macro2",
"quote",
......
[workspace]
members = ["controller", "i2c2-target", "support"]
exclude = ["pid"]
exclude = ["build-support", "pid"]
resolver = "2"
[profile.release]
......
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "build-support"
version = "0.1.0"
dependencies = [
"semver",
"thiserror",
]
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "semver"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]]
name = "syn"
version = "2.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[package]
name = "build-support"
version = "0.1.0"
edition = "2021"
[dependencies]
semver = "1.0.23"
thiserror = "1.0.63"
use semver::Version;
use std::env;
use std::fs::{self, File};
use std::io::{self, Write};
use std::path::Path;
use std::str::FromStr;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("cannot find this string in Python library: {0}")]
InconsistentPythonLibraryFirmwareVersion(String),
#[error(transparent)]
Io(#[from] io::Error),
#[error("missing environment variable {0}")]
MissingEnv(#[from] env::VarError),
#[error(transparent)]
BadSemVer(#[from] semver::Error),
}
fn version() -> Result<Version, Error> {
let version_str = env::var("CARGO_PKG_VERSION")?;
Ok(Version::from_str(&version_str)?)
}
pub fn check_python_library_consistency(python_file: &str) -> Result<(), Error> {
let version = version()?;
let haystack = fs::read_to_string(python_file)?;
let needle = format!(
"REQUIRED_FIRMWARE_VERSION = ({}, {})",
version.major, version.minor
);
if haystack.contains(&needle) {
Ok(())
} else {
Err(Error::InconsistentPythonLibraryFirmwareVersion(needle))
}
}
pub fn make_version() -> Result<(), Error> {
let version = version()?;
let mut out = File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("version.rs"))?;
writeln!(
out,
"pub const PKG_VERSION: [u8; 3] = [{}, {}, {}];",
u8::try_from(version.major).unwrap(),
u8::try_from(version.minor).unwrap(),
u8::try_from(version.patch).unwrap()
)?;
Ok(())
}
......@@ -24,4 +24,4 @@ support = { version = "0.1.0", path = "../support" }
pedantic = "deny"
[build-dependencies]
semver = "1.0.23"
build-support = { path = "../build-support" }
use semver::Version;
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;
use std::str::FromStr;
#[derive(Debug)]
enum Error {
InconsistentPythonLibraryFirmwareVersion,
}
fn check_python_library_consistency(version: &Version) -> Result<(), Error> {
let haystack = fs::read_to_string("python/controller.py").unwrap();
let needle = format!(
"REQUIRED_FIRMWARE_VERSION = ({}, {})",
version.major, version.minor
);
if !haystack.contains(&needle) {
eprintln!("Cannot find the following string in Python library:\n {needle}");
return Err(Error::InconsistentPythonLibraryFirmwareVersion);
}
Ok(())
}
fn main() -> Result<(), Error> {
let version_str = env!("CARGO_PKG_VERSION");
let version = Version::from_str(version_str).unwrap();
check_python_library_consistency(&version)?;
let mut out =
File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("version.rs")).unwrap();
writeln!(
out,
"pub const PKG_VERSION: [u8; 3] = [{}, {}, {}];",
u8::try_from(version.major).unwrap(),
u8::try_from(version.minor).unwrap(),
u8::try_from(version.patch).unwrap()
)
.unwrap();
fn main() -> Result<(), build_support::Error> {
build_support::check_python_library_consistency("python/controller.py")?;
build_support::make_version()?;
Ok(())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment