From 5e11b1f72bd1591d9c62c44c14a9eaeaf09e37fc Mon Sep 17 00:00:00 2001
From: Samuel Tardieu <sam@rfc1149.net>
Date: Fri, 19 Jul 2024 11:21:04 +0200
Subject: [PATCH] Add common build support files in "build-support" crate

---
 Cargo.lock               | 18 +++++++---
 Cargo.toml               |  2 +-
 build-support/Cargo.lock | 72 ++++++++++++++++++++++++++++++++++++++++
 build-support/Cargo.toml |  8 +++++
 build-support/src/lib.rs | 50 ++++++++++++++++++++++++++++
 controller/Cargo.toml    |  2 +-
 controller/build.rs      | 42 ++---------------------
 7 files changed, 148 insertions(+), 46 deletions(-)
 create mode 100644 build-support/Cargo.lock
 create mode 100644 build-support/Cargo.toml
 create mode 100644 build-support/src/lib.rs

diff --git a/Cargo.lock b/Cargo.lock
index 0f7e101..8ba7603 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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",
diff --git a/Cargo.toml b/Cargo.toml
index ddf2b49..c36c716 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [workspace]
 members = ["controller", "i2c2-target", "support"]
-exclude = ["pid"]
+exclude = ["build-support", "pid"]
 resolver = "2"
 
 [profile.release]
diff --git a/build-support/Cargo.lock b/build-support/Cargo.lock
new file mode 100644
index 0000000..6d5e303
--- /dev/null
+++ b/build-support/Cargo.lock
@@ -0,0 +1,72 @@
+# 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"
diff --git a/build-support/Cargo.toml b/build-support/Cargo.toml
new file mode 100644
index 0000000..e278b81
--- /dev/null
+++ b/build-support/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "build-support"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+semver = "1.0.23"
+thiserror = "1.0.63"
diff --git a/build-support/src/lib.rs b/build-support/src/lib.rs
new file mode 100644
index 0000000..dab75ba
--- /dev/null
+++ b/build-support/src/lib.rs
@@ -0,0 +1,50 @@
+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(())
+}
diff --git a/controller/Cargo.toml b/controller/Cargo.toml
index 5f21437..08c6152 100644
--- a/controller/Cargo.toml
+++ b/controller/Cargo.toml
@@ -24,4 +24,4 @@ support = { version = "0.1.0", path = "../support" }
 pedantic = "deny"
 
 [build-dependencies]
-semver = "1.0.23"
+build-support = { path = "../build-support" }
diff --git a/controller/build.rs b/controller/build.rs
index 566a915..e671c0e 100644
--- a/controller/build.rs
+++ b/controller/build.rs
@@ -1,41 +1,5 @@
-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(())
 }
-- 
GitLab