feat: handle multi-version bindings generation

This commit is contained in:
Florian Briand 2024-09-29 21:46:10 +02:00
parent f71b99a4c0
commit 473212c2e6
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B
11 changed files with 137 additions and 79 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.
@ -52,8 +23,35 @@ fn main() {
.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"))
.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<PathBuf> = SUPPORTED_FSV_VERSIONS
.iter()
.map(|version| build_bindings(version, &target_code))
.collect();
println!("FSV bindings generated: {:#?}", bindings_paths);
}

View File

@ -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,24 +56,47 @@ 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<Self, Error> {
pub fn new(version: SUPPORTED_FSV_VERSIONS) -> Result<Self, Error> {
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
});
@ -85,22 +123,22 @@ mod test {
#[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);
}
}

View File

@ -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

View File

@ -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

View File

@ -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