From 4ab8a1de8119b75c76df73fae451c41ee9b57d6a Mon Sep 17 00:00:00 2001 From: Florian Briand Date: Sun, 29 Sep 2024 21:46:10 +0200 Subject: [PATCH] feat: handle multi-version bindings generation --- .../bindgen-wrappers/1.40.13/wrapper.linux.h | 7 ++ .../bindgen-wrappers/1.40.13/wrapper.macosx.h | 7 ++ .../bindgen-wrappers/1.40.13/wrapper.win.h | 7 ++ .../bindgen-wrappers/1.40.14/wrapper.linux.h | 7 ++ .../bindgen-wrappers/1.40.14/wrapper.macosx.h | 7 ++ .../bindgen-wrappers/1.40.14/wrapper.win.h | 7 ++ crates/fsv-sys/build.rs | 74 +++++++++-------- crates/fsv-sys/src/lib.rs | 79 ++++++++++++++----- .../fsv/1.40.14.13/includes/wrapper.linux.h | 7 -- .../fsv/1.40.14.13/includes/wrapper.macosx.h | 7 -- .../fsv/1.40.14.13/includes/wrapper.win.h | 7 -- 11 files changed, 137 insertions(+), 79 deletions(-) create mode 100644 crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.linux.h create mode 100644 crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.macosx.h create mode 100644 crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.win.h create mode 100644 crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.linux.h create mode 100644 crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.macosx.h create mode 100644 crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.win.h delete mode 100644 crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.linux.h delete mode 100644 crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.macosx.h delete mode 100644 crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.win.h diff --git a/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.linux.h b/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.linux.h new file mode 100644 index 0000000..f702b3f --- /dev/null +++ b/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.linux.h @@ -0,0 +1,7 @@ +#ifndef WRAPPER_LINUX_H +#define WRAPPER_LINUX_H + +#include "../../vendor/fsv/1.40.14.13/includes/SYS_DEF/linux/mc_sys_def.h" +#include "../../vendor/fsv/1.40.14.13/includes/SSV/pourFSV1.40.13/ssv.h" + +#endif // WRAPPER_LINUX_H \ No newline at end of file diff --git a/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.macosx.h b/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.macosx.h new file mode 100644 index 0000000..e502e5b --- /dev/null +++ b/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.macosx.h @@ -0,0 +1,7 @@ +#ifndef WRAPPER_MACOSX_H +#define WRAPPER_MACOSX_H + +#include "../../vendor/fsv/1.40.14.13/includes/SYS_DEF/macosx/mc_sys_def.h" +#include "../../vendor/fsv/1.40.14.13/includes/SSV/pourFSV1.40.13/ssv.h" + +#endif // WRAPPER_MACOSX_H \ No newline at end of file diff --git a/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.win.h b/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.win.h new file mode 100644 index 0000000..3eb9be8 --- /dev/null +++ b/crates/fsv-sys/bindgen-wrappers/1.40.13/wrapper.win.h @@ -0,0 +1,7 @@ +#ifndef WRAPPER_WIN_H +#define WRAPPER_WIN_H + +#include "../../vendor/fsv/1.40.14.13/includes/SYS_DEF/win/mc_sys_def.h" +#include "../../vendor/fsv/1.40.14.13/includes/SSV/pourFSV1.40.13/ssv.h" + +#endif // WRAPPER_WIN_H \ No newline at end of file diff --git a/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.linux.h b/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.linux.h new file mode 100644 index 0000000..943be7a --- /dev/null +++ b/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.linux.h @@ -0,0 +1,7 @@ +#ifndef WRAPPER_LINUX_H +#define WRAPPER_LINUX_H + +#include "../../vendor/fsv/1.40.14.13/includes/SYS_DEF/linux/mc_sys_def.h" +#include "../../vendor/fsv/1.40.14.13/includes/SSV/pourFSV1.40.14/ssv.h" + +#endif // WRAPPER_LINUX_H \ No newline at end of file diff --git a/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.macosx.h b/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.macosx.h new file mode 100644 index 0000000..ade3c54 --- /dev/null +++ b/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.macosx.h @@ -0,0 +1,7 @@ +#ifndef WRAPPER_MACOSX_H +#define WRAPPER_MACOSX_H + +#include "../../vendor/fsv/1.40.14.13/includes/SYS_DEF/macosx/mc_sys_def.h" +#include "../../vendor/fsv/1.40.14.13/includes/SSV/pourFSV1.40.14/ssv.h" + +#endif // WRAPPER_MACOSX_H \ No newline at end of file diff --git a/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.win.h b/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.win.h new file mode 100644 index 0000000..df2533e --- /dev/null +++ b/crates/fsv-sys/bindgen-wrappers/1.40.14/wrapper.win.h @@ -0,0 +1,7 @@ +#ifndef WRAPPER_WIN_H +#define WRAPPER_WIN_H + +#include "../../vendor/fsv/1.40.14.13/includes/SYS_DEF/win/mc_sys_def.h" +#include "../../vendor/fsv/1.40.14.13/includes/SSV/pourFSV1.40.14/ssv.h" + +#endif // WRAPPER_WIN_H \ No newline at end of file diff --git a/crates/fsv-sys/build.rs b/crates/fsv-sys/build.rs index 40a2d5f..00dbe07 100644 --- a/crates/fsv-sys/build.rs +++ b/crates/fsv-sys/build.rs @@ -1,40 +1,11 @@ use std::{env, path::PathBuf}; -fn main() { - // Configure for various targets - let target_code; +// Supported versions of FSV +static SUPPORTED_FSV_VERSIONS: [&str; 2] = ["1.40.14", "1.40.13"]; - // Use CARGO configuration env Variable, because !cfg(target_os) is not available in build.rs - // Source: https://kazlauskas.me/entries/writing-proper-buildrs-scripts +fn build_bindings(version: &str, target_code: &str) -> PathBuf { let target = env::var("TARGET").expect("TARGET not set"); - let target_os = env::var("CARGO_CFG_TARGET_OS"); - - println!("Target: {:?}", target); - - match target_os.as_ref().map(|x| &**x) { - Ok("linux") => { - println!("Building for Linux"); - // lib_name = "ssvlux64"; - target_code = "linux"; - }, - Ok("windows") => { - println!("Building for Windows"); - // lib_name = "Ssvw64"; - target_code = "win"; - }, - Ok("macos") => { - println!("Building for MacOS"); - // lib_name = "ssvosx"; - target_code = "macosx"; - }, - tos => panic!("Unsupported target_os {:?}", tos), - } - - // Link the library - // println!("cargo:rustc-link-lib={}", lib_name); - - // Build the bindings - let wrapper_path = format!("vendor/fsv/1.40.14.13/includes/wrapper.{}.h", target_code); + let wrapper_path = format!("bindgen-wrappers/{}/wrapper.{}.h", version, target_code); let bindings = bindgen::Builder::default() // The input header we would like to generate // bindings for. @@ -50,10 +21,37 @@ fn main() { .generate() // Unwrap the Result and panic on failure. .expect("Unable to generate bindings"); - + // Write the bindings to the $OUT_DIR/bindings.rs file. - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let out_file = format!("bindings_{}.rs", version); + let out_path = out_dir.join(out_file); bindings - .write_to_file(out_path.join("bindings.rs")) - .expect("Couldn't write bindings!"); -} \ No newline at end of file + .write_to_file(&out_path) + .expect("Couldn't write bindings! "); + out_path +} + +fn get_target_code() -> String { + // Use CARGO configuration env Variable, because !cfg(target_os) is not available in build.rs + // Source: https://kazlauskas.me/entries/writing-proper-buildrs-scripts + let target_os = env::var("CARGO_CFG_TARGET_OS"); + + match target_os.as_ref().map(|x| &**x) { + Ok("linux") => "linux", // lib_name = "ssvlux64"; + Ok("windows") => "win", // lib_name = "Ssvw64"; + Ok("macos") => "macosx", // lib_name = "ssvosx"; + tos => panic!("Unsupported target_os {:?}", tos), + } + .to_string() +} + +fn main() { + let target_code = get_target_code(); + // Build the bindings for each supported version of FSV + let bindings_paths: Vec = SUPPORTED_FSV_VERSIONS + .iter() + .map(|version| build_bindings(version, &target_code)) + .collect(); + println!("FSV bindings generated: {:#?}", bindings_paths); +} diff --git a/crates/fsv-sys/src/lib.rs b/crates/fsv-sys/src/lib.rs index 6e2d341..6efdc5f 100644 --- a/crates/fsv-sys/src/lib.rs +++ b/crates/fsv-sys/src/lib.rs @@ -2,8 +2,23 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] +#[derive(Debug)] +pub enum SUPPORTED_FSV_VERSIONS { + V1_40_14, // 1.40.14 + V1_40_13, // 1.40.13 +} + +impl SUPPORTED_FSV_VERSIONS { + fn as_str(&self) -> &'static str { + match self { + Self::V1_40_14 => "1.40.14", + Self::V1_40_13 => "1.40.13", + } + } +} + pub mod BINDINGS { - include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + include!(concat!(env!("OUT_DIR"), "/bindings_1.40.13.rs")); } // We need to keep the this use statement to get `ssv_function` macro working well @@ -41,28 +56,51 @@ pub enum Error { /// The library is loaded at creation and kept in memory until the struct is dropped. #[derive(Debug)] pub struct SSVLibrary { + version: SUPPORTED_FSV_VERSIONS, library: libloading::Library, } -pub fn get_library_path() -> String { +fn get_library_name() -> &'static str { // TODO : Use libloading::library_filename to get platform-specific filename ? - "/opt/santesocial/fsv/1.40.13/lib/libssvlux64.so".to_string() + "libssvlux64.so" +} + +fn get_library_root_path() -> &'static str { + "/opt/santesocial/fsv" +} + +fn get_sesam_ini_root_path() -> &'static str { + "/etc/opt/santesocial/fsv" } impl SSVLibrary { - pub fn new(library_path: &str) -> Result { + pub fn new(version: SUPPORTED_FSV_VERSIONS) -> Result { + let library_path = Self::get_library_path(&version); let library = unsafe { libloading::Library::new(library_path)? }; - Ok(SSVLibrary { library }) + Ok(SSVLibrary { version, library }) } pub fn library(&self) -> &libloading::Library { &self.library } + pub fn sesam_ini_path(&self) -> String { + let root_path = get_sesam_ini_root_path(); + let version = self.version.as_str(); + format!("{root_path}/{version}/conf/sesam.ini") + } + + pub fn get_library_path(version: &SUPPORTED_FSV_VERSIONS) -> String { + let root_path = get_library_root_path(); + let library_name = get_library_name(); + let version = version.as_str(); + format!("{root_path}/{version}/lib/{library_name}") + } + ssv_function!(SSV_InitLIB2, ssv_init_lib2, { pcFichierSesam: *const i8 }); - + ssv_function!(SSV_LireConfig, ssv_lire_config, { pZDataOut: *mut *mut libc::c_void, psTailleDataOut: *mut usize @@ -81,26 +119,26 @@ impl SSVLibrary { mod test { use std::{ffi::CString, ptr}; - use super::*; + use super::*; #[test] fn test_initlib2() { - let library_path = get_library_path(); - let ssv_library = SSVLibrary::new(&library_path).expect("SSVLibrary::new failed"); + let ssv_library = + SSVLibrary::new(SUPPORTED_FSV_VERSIONS::V1_40_13).expect("SSVLibrary::new failed"); - let sesam_ini_str = CString::new("/etc/opt/santesocial/fsv/1.40.13/conf/sesam.ini") - .expect("CString::new failed"); + let sesam_ini_str = + CString::new(ssv_library.sesam_ini_path()).expect("CString::new failed"); let result = unsafe { ssv_library.ssv_init_lib2(sesam_ini_str.as_ptr()) }.unwrap(); assert_eq!(result, 0); } #[test] fn test_lire_config_and_carte_ps() { - let library_path = get_library_path(); - let ssv_library = SSVLibrary::new(&library_path).expect("SSVLibrary::new failed"); + let ssv_library = + SSVLibrary::new(SUPPORTED_FSV_VERSIONS::V1_40_13).expect("SSVLibrary::new failed"); - let sesam_ini_str = CString::new("/etc/opt/santesocial/fsv/1.40.13/conf/sesam.ini") - .expect("CString::new failed"); + let sesam_ini_str = + CString::new(ssv_library.sesam_ini_path()).expect("CString::new failed"); let result = unsafe { ssv_library.ssv_init_lib2(sesam_ini_str.as_ptr()) }.unwrap(); assert_eq!(result, 0); @@ -109,10 +147,10 @@ mod test { let result = unsafe { ssv_library.ssv_lire_config(&mut buffer_ptr, &mut size) }.unwrap(); assert_eq!(result, 0); - let nom_ressource_ps = CString::new("Gemalto PC Twin Reader (645D94C3) 00 00") - .expect("CString::new failed"); - let nom_ressource_lecteur = CString::new("Gemalto PC Twin Reader (645D94C3) 00 00") - .expect("CString::new failed"); + let nom_ressource_ps = + CString::new("Gemalto PC Twin Reader (645D94C3) 00 00").expect("CString::new failed"); + let nom_ressource_lecteur = + CString::new("Gemalto PC Twin Reader (645D94C3) 00 00").expect("CString::new failed"); let code_porteur_ps = CString::new("1234").expect("CString::new failed"); let mut buffer_ptr: *mut libc::c_void = ptr::null_mut(); let mut size: libc::size_t = 0; @@ -124,7 +162,8 @@ mod test { &mut buffer_ptr, &mut size, ) - }.unwrap(); + } + .unwrap(); assert_eq!(result, 0); } } diff --git a/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.linux.h b/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.linux.h deleted file mode 100644 index 50bdd53..0000000 --- a/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.linux.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef WRAPPER_LINUX_H -#define WRAPPER_LINUX_H - -#include "SYS_DEF/linux/mc_sys_def.h" -#include "SSV/pourFSV1.40.14/ssv.h" - -#endif // WRAPPER_LINUX_H \ No newline at end of file diff --git a/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.macosx.h b/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.macosx.h deleted file mode 100644 index d981100..0000000 --- a/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.macosx.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef WRAPPER_MACOSX_H -#define WRAPPER_MACOSX_H - -#include "SYS_DEF/macosx/mc_sys_def.h" -#include "SSV/pourFSV1.40.14/ssv.h" - -#endif // WRAPPER_MACOSX_H \ No newline at end of file diff --git a/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.win.h b/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.win.h deleted file mode 100644 index 23aebe0..0000000 --- a/crates/fsv-sys/vendor/fsv/1.40.14.13/includes/wrapper.win.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef WRAPPER_WIN_H -#define WRAPPER_WIN_H - -#include "SYS_DEF/win/mc_sys_def.h" -#include "SSV/pourFSV1.40.14/ssv.h" - -#endif // WRAPPER_WIN_H \ No newline at end of file