diff --git a/wutil-rs/Cargo.toml b/wutil-rs/Cargo.toml index 29c8b68b..9eff16fb 100644 --- a/wutil-rs/Cargo.toml +++ b/wutil-rs/Cargo.toml @@ -5,3 +5,6 @@ edition = "2024" [lib] crate-type = ["staticlib"] + +[build-dependencies] +cc = "1.0" diff --git a/wutil-rs/build.rs b/wutil-rs/build.rs new file mode 100644 index 00000000..58c269c3 --- /dev/null +++ b/wutil-rs/build.rs @@ -0,0 +1,8 @@ +use cc; + +fn main() { + cc::Build::new() + .file("src/defines.c") + .compile("defines"); + println!("cargo::rerun-if-changed=src/defines.c"); +} diff --git a/wutil-rs/src/defines.c b/wutil-rs/src/defines.c new file mode 100644 index 00000000..e75292f9 --- /dev/null +++ b/wutil-rs/src/defines.c @@ -0,0 +1,5 @@ +#include "../../config-paths.h" + +const char *get_GSUSER_SUBDIR() { + return GSUSER_SUBDIR; +} diff --git a/wutil-rs/src/defines.rs b/wutil-rs/src/defines.rs new file mode 100644 index 00000000..678c5883 --- /dev/null +++ b/wutil-rs/src/defines.rs @@ -0,0 +1,45 @@ +//! Lookup functions for preprocessor symbols. +//! +//! Functions in this module may be called to get the value of various +//! preprocessor symbols that are available on the C side of things. +//! +//! ## Rust rewrite notes +//! +//! Until we move away from autootols entirely, we might be stuck with this as +//! along as it makes sense to keep the configure script as the main entrypoint +//! for compile-time configuration. + +use std::ffi::{c_char, CStr}; + +// Functions defined in src/defines.c. +unsafe extern "C" { + fn get_GSUSER_SUBDIR() -> *const c_char; +} + +/// Returns the value of the GSUSER_SUBDIR preprocessor symbol defined at +/// Autotools configuration time prior to compilation. This is the final +/// component of the root path where user data files are stored (usually +/// `GNUstep`, as in `$HOME/GNUstep`). Returns `None` if this value cannot be +/// determined or is empty. +pub fn gsuser_subdir() -> Option { + let s = unsafe { get_GSUSER_SUBDIR() }; + if s.is_null() { + return None; + } + let s = unsafe { CStr::from_ptr(s) }; + if s.is_empty() { + return None; + } + String::from_utf8(s.to_bytes().iter().copied().collect()).ok() +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn gsuser_subdir_is_set() { + let s = gsuser_subdir().unwrap(); + assert!(!s.is_empty()); + } +} diff --git a/wutil-rs/src/lib.rs b/wutil-rs/src/lib.rs index 5caabff8..081e4412 100644 --- a/wutil-rs/src/lib.rs +++ b/wutil-rs/src/lib.rs @@ -1,4 +1,5 @@ pub mod array; pub mod data; +pub mod defines; pub mod find_file; pub mod memory;