From 215364a6a5296208aba1ba03184cca0e13ed52c1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 1 Jun 2024 23:21:50 +0200 Subject: [PATCH] improvements for libcsp cargo builder --- Cargo.lock | 15 +++------------ Cargo.toml | 14 +++++++------- clib/cfg/csp/autoconfig.h | 2 +- examples/Cargo.toml | 2 +- examples/autoconfig.h | 2 +- examples/autoconfig.rs | 2 +- examples/build.rs | 3 ++- libcsp-cargo-build/CHANGELOG.md | 4 ++++ libcsp-cargo-build/Cargo.toml | 2 +- libcsp-cargo-build/src/lib.rs | 28 ++++++++++++++++++++++++---- 10 files changed, 45 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b90dc8a..5e29b4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,7 +44,7 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libcsp" -version = "0.1.2" +version = "0.1.3" dependencies = [ "bitflags", "libc", @@ -54,16 +54,7 @@ dependencies = [ [[package]] name = "libcsp-cargo-build" -version = "0.1.0" -dependencies = [ - "cc", -] - -[[package]] -name = "libcsp-cargo-build" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "554733e90f35324f10e32b4e848f5c85eb28cd099c5015d9fbe869f3b5a59944" +version = "0.2.0" dependencies = [ "cc", ] @@ -73,7 +64,7 @@ name = "libcsp-rust-examples" version = "0.1.0" dependencies = [ "libcsp", - "libcsp-cargo-build 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libcsp-cargo-build", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b3019d9..f51dcd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,10 @@ +[workspace] +members = [ + "libcsp-cargo-build", + "examples" +] +resolver = "2" + [package] name = "libcsp" version = "0.1.3" @@ -16,10 +23,3 @@ bitflags = "2" num_enum = "0.7" libc = "0.2" libcsp-sys = { version = "0.1", path = "libcsp-sys" } - -[workspace] -members = [ - "libcsp-cargo-build", - "examples" -] -resolver = "2" diff --git a/clib/cfg/csp/autoconfig.h b/clib/cfg/csp/autoconfig.h index 2eadec9..18db72d 100644 --- a/clib/cfg/csp/autoconfig.h +++ b/clib/cfg/csp/autoconfig.h @@ -1,4 +1,4 @@ -// This file was auto-generated by libcsp-cargo-build v0.1.0 +// This file was auto-generated by libcsp-cargo-build v0.2.0 #define CSP_POSIX 1 #define CSP_ZEPHYR 0 diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 9bef1f3..10693e2 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -9,4 +9,4 @@ edition = "2021" libcsp = { version = "0.1", path = ".." } [build-dependencies] -libcsp-cargo-build = "0.1" +libcsp-cargo-build = { version = "0.2", path = "../libcsp-cargo-build" } diff --git a/examples/autoconfig.h b/examples/autoconfig.h index 2eadec9..18db72d 100644 --- a/examples/autoconfig.h +++ b/examples/autoconfig.h @@ -1,4 +1,4 @@ -// This file was auto-generated by libcsp-cargo-build v0.1.0 +// This file was auto-generated by libcsp-cargo-build v0.2.0 #define CSP_POSIX 1 #define CSP_ZEPHYR 0 diff --git a/examples/autoconfig.rs b/examples/autoconfig.rs index 0477874..1df51ed 100644 --- a/examples/autoconfig.rs +++ b/examples/autoconfig.rs @@ -1,4 +1,4 @@ -// This file was auto-generated by libcsp-cargo-build v0.1.0 +// This file was auto-generated by libcsp-cargo-build v0.2.0 pub const CSP_CONN_RXQUEUE_LEN: usize = 16; pub const CSP_QFIFO_LEN: usize = 16; pub const CSP_PORT_MAX_BIND: usize = 16; diff --git a/examples/build.rs b/examples/build.rs index 6d53417..0a48089 100644 --- a/examples/build.rs +++ b/examples/build.rs @@ -11,7 +11,8 @@ fn main() { // This helper structure will take care of the majority of work to compile libcsp using the // cc crate. - let mut csp_builder = Builder::new(PathBuf::from(libcsp_path), PathBuf::from(&out_dir)); + let mut csp_builder = Builder::new(PathBuf::from(libcsp_path), PathBuf::from(&out_dir)) + .expect("creating libcsp builder failed"); // A lot of spam we are not interested in usually. csp_builder.compiler_warnings = false; diff --git a/libcsp-cargo-build/CHANGELOG.md b/libcsp-cargo-build/CHANGELOG.md index fd4480e..2ea29d1 100644 --- a/libcsp-cargo-build/CHANGELOG.md +++ b/libcsp-cargo-build/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). # [unreleased] +# [v0.2.0] 2024-06-01 + +Added basic sanity checks for the user-provided `libcsp` location. + # [v0.1.0] 2024-06-01 Initial release diff --git a/libcsp-cargo-build/Cargo.toml b/libcsp-cargo-build/Cargo.toml index c96deec..5a990ae 100644 --- a/libcsp-cargo-build/Cargo.toml +++ b/libcsp-cargo-build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libcsp-cargo-build" -version = "0.1.0" +version = "0.2.0" edition = "2021" description = "Tools to build libcsp using cargo" homepage = "https://egit.irs.uni-stuttgart.de/rust/libcsp-rust" diff --git a/libcsp-cargo-build/src/lib.rs b/libcsp-cargo-build/src/lib.rs index ecd2e2f..6b57e16 100644 --- a/libcsp-cargo-build/src/lib.rs +++ b/libcsp-cargo-build/src/lib.rs @@ -133,12 +133,32 @@ pub struct Builder { build: cc::Build, } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum BuildCreationError { + PathDoesNotExist, + InvalidLibcspFormat, +} + impl Builder { - /// Create a new builder instance. - pub fn new(libcsp_path: PathBuf, out_dir: PathBuf) -> Self { + /// Create a new builder instance. Returns [BuildCreationError] if the specified libcsp path + /// does not exist or does not have the expected format. + pub fn new(libcsp_path: PathBuf, out_dir: PathBuf) -> Result { + // Basic sanity checks for the passed libcsp path. + if !libcsp_path.exists() { + return Err(BuildCreationError::PathDoesNotExist); + } + if !libcsp_path + .join("include") + .join("csp") + .join("csp.h") + .exists() + { + return Err(BuildCreationError::InvalidLibcspFormat); + } + let mut libcsp_src_path_base = libcsp_path.clone(); libcsp_src_path_base.push("src"); - Self { + Ok(Self { generate_autoconf_file: true, libcsp_path, libcsp_src_path_base, @@ -146,7 +166,7 @@ impl Builder { cfg: Default::default(), compiler_warnings: true, build: Default::default(), - } + }) } /// Access to the underlying [cc::Build] builder object.