Expose the GSUSER_SUBDIR preprocessor symbol to Rust.
This symbol's value must be known to port `wmkdirhier` and `wrmdirhier` from `proplist.c` to Rust. This change introduces a basic C library under wutil-rs that is linked into the Rust code to expose preprocessor symbols and other Autotools configuration decisions to Rust. See the rust rewrite notes at the top of `wutil-rs/src/defines.rs` for further thoughts.
This commit is contained in:
@@ -5,3 +5,6 @@ edition = "2024"
|
||||
|
||||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
||||
|
||||
8
wutil-rs/build.rs
Normal file
8
wutil-rs/build.rs
Normal file
@@ -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");
|
||||
}
|
||||
5
wutil-rs/src/defines.c
Normal file
5
wutil-rs/src/defines.c
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "../../config-paths.h"
|
||||
|
||||
const char *get_GSUSER_SUBDIR() {
|
||||
return GSUSER_SUBDIR;
|
||||
}
|
||||
45
wutil-rs/src/defines.rs
Normal file
45
wutil-rs/src/defines.rs
Normal file
@@ -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<String> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod array;
|
||||
pub mod data;
|
||||
pub mod defines;
|
||||
pub mod find_file;
|
||||
pub mod memory;
|
||||
|
||||
Reference in New Issue
Block a user