chore: Make the compilation and linking crossplatform (win, linux)

This commit is contained in:
Florian Briand 2024-07-05 23:30:11 +02:00
parent ec1e147e91
commit 078523ebad
14 changed files with 115 additions and 11 deletions

2
.env.linux.example Normal file
View File

@ -0,0 +1,2 @@
SESAM_FSV_VERSION=1.40.13
SESAM_INI_PATH=/etc/opt/santesocial/fsv/${SESAM_FSV_VERSION}/conf/sesam.ini

2
.env.win.example Normal file
View File

@ -0,0 +1,2 @@
SESAM_FSV_VERSION=1.40.13
SESAM_INI_PATH=${ALLUSERSPROFILE}\\santesocial\\fsv\\${SESAM_FSV_VERSION}\\conf\\sesam.ini

4
.gitignore vendored
View File

@ -1 +1,5 @@
# Ignore Rust target directory
/target /target
# Ignore .env file
.env

7
Cargo.lock generated
View File

@ -2,6 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.155" version = "0.2.155"
@ -12,5 +18,6 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
name = "utils-debug-c-lib" name = "utils-debug-c-lib"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dotenv",
"libc", "libc",
] ]

View File

@ -4,5 +4,9 @@ version = "0.1.0"
edition = "2021" edition = "2021"
build = "build.rs" build = "build.rs"
[build]
rustflags = "-L ./lib"
[dependencies] [dependencies]
dotenv = "0.15"
libc = "0.2" libc = "0.2"

View File

@ -5,8 +5,23 @@
## Setup ## Setup
- Installer le [package FSV](https://industriels.sesam-vitale.fr/group/fournitures-sesam-vitale) - Installer le [package FSV](https://industriels.sesam-vitale.fr/group/fournitures-sesam-vitale)
- Ajouter le path des librairies FSV à `LD_LIBRARY_PATH` : `export LD_LIBRARY_PATH=/path/to/your/library:$LD_LIBRARY_PATH` - Linux : ajouter le path des librairies FSV à la variable d'environnement `LD_LIBRARY_PATH` avant execution
- Windows : ajouter le path des librairies FSV à la variable d'environnement `PATH` avant execution
- Installer la [CryptolibCPS](https://industriels.sesam-vitale.fr/group/galss-cryptolib-cps) - Installer la [CryptolibCPS](https://industriels.sesam-vitale.fr/group/galss-cryptolib-cps)
- Fourni l'utilitaire "CPS Gestion" pour obtenir des informations sur le lecteur de carte, etc. - Fourni l'utilitaire "CPS Gestion" pour obtenir des informations sur le lecteur de carte, etc.
- Linux : `cpgeslux` - Linux : `cpgeslux`
- Créer un fichier `.env` en s'inspirant d'un des fichiers d'exemple (`.env.linux.example`, `.env.win.example`...)
## C Compilation
Pour compiler les fichiers de librairie :
- Windows : `.\build.bat`
- Linux : `make`
Pour nettoyer le dossier `./lib` :
- Windows : `.\build.bat /clean`
- Linux : `make clean`

36
build.bat Normal file
View File

@ -0,0 +1,36 @@
@echo off
rem Set variables
set SRC_DIR=src
set LIB_DIR=lib
rem Create the %LIB_DIR% directory if it does not exist
if not exist %LIB_DIR% mkdir %LIB_DIR%
if "%1"=="/clean" (
goto clean
)
rem Configure the environment for Visual Studio
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
rem Build %SRC_DIR%\*.c files
cl /c /Fd:%LIB_DIR%\ /Fo:%LIB_DIR%\ /Iinclude\ /I%SRC_DIR%\ %SRC_DIR%\*.c
rem Link the object files into a static library
lib %LIB_DIR%\*.obj
rem Create a ssvw64.lib file from a ssvw64.def file
lib /def:%SRC_DIR%\ssvw64.def /out:%LIB_DIR%\ssvw64.lib /machine:x64
rem Build complete
pause
exit /b 0
:clean
del %LIB_DIR%\*.obj
del %LIB_DIR%\*.lib
del %LIB_DIR%\*.exp
rem Clean complete
pause
exit /b 0

View File

@ -1,9 +1,32 @@
fn main() { // use std::env;
println!("cargo:rustc-link-search=native=./lib"); // use std::path::PathBuf;
println!("cargo:rustc-link-lib=static=p4pillondebuglib");
println!("cargo:rerun-if-changed=lib/libp4pillondebuglib.a");
println!("cargo:rustc-link-search=native=/opt/santesocial/fsv/1.40.13/lib"); fn main() {
println!("cargo:rustc-link-lib=dylib=ssvlux64"); // --- OLD VERSION ---
println!("cargo:rerun-if-changed=/opt/santesocial/fsv/1.40.13/lib/libssvlux64.so"); // In fact, the commented code is not necessary
// Indeed, the static library is automatically found
// when the directory is provided by rustflags in Cargo.toml
// The dynamic library is found in the PATH or LD_LIBRARY_PATH
// let static_lib_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("lib");
// println!("cargo:rustc-link-search=native={}", static_lib_path.display());
// println!("cargo:rustc-link-lib=static=p4pillondebuglib");
// println!("cargo:rerun-if-changed={}", static_lib_path.join("libp4pillondebuglib.lib").display());
// let dynamic_lib_path = PathBuf::from("C:/Program Files/santesocial/fsv/1.40.13/lib");
// println!("cargo:rustc-link-search=native={}", dynamic_lib_path.display());
// println!("cargo:rustc-link-lib=dylib=ssvw64");
// println!("cargo:rerun-if-changed={}", dynamic_lib_path.join("ssvw64.dll").display());
// --- NEW VERSION ---
// Cross-platform libraries
println!("cargo:rustc-link-lib=static=p4pillondebuglib");
// Target-specific libraries
if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=dylib=ssvw64");
} else if cfg!(target_os = "linux") {
println!("cargo:rustc-link-lib=dylib=ssvlux64");
}
} }

BIN
lib/p4pillondebuglib.lib Normal file

Binary file not shown.

BIN
lib/p4pillondebuglib.obj Normal file

Binary file not shown.

BIN
lib/ssvw64.exp Normal file

Binary file not shown.

BIN
lib/ssvw64.lib Normal file

Binary file not shown.

View File

@ -1,10 +1,13 @@
extern crate libc; extern crate libc;
extern crate dotenv;
use libc::{ c_char, c_void, c_ushort, size_t }; use libc::{ c_char, c_void, c_ushort, size_t };
use std::ffi::CString; use std::ffi::CString;
use std::ptr; use std::ptr;
#[link(name = "ssvlux64")] use dotenv::dotenv;
use std::env;
extern "C" { extern "C" {
fn SSV_InitLIB2(pcRepSesamIni: *const c_char) -> c_ushort; fn SSV_InitLIB2(pcRepSesamIni: *const c_char) -> c_ushort;
fn SSV_LireCartePS(NomRessourcePS: *const c_char, NomRessourceLecteur: *const c_char, CodePorteurPS: *const c_char, ZDonneesSortie: *mut *mut c_void, TTailleDonneesSortie: *mut size_t) -> c_ushort; fn SSV_LireCartePS(NomRessourcePS: *const c_char, NomRessourceLecteur: *const c_char, CodePorteurPS: *const c_char, ZDonneesSortie: *mut *mut c_void, TTailleDonneesSortie: *mut size_t) -> c_ushort;
@ -12,7 +15,8 @@ extern "C" {
} }
fn ssv_init_lib_2() { fn ssv_init_lib_2() {
let ini = CString::new("/etc/opt/santesocial/fsv/1.40.13/conf/sesam.ini").expect("CString::new failed"); let ini_str = env::var("SESAM_INI_PATH").expect("SESAM_INI_PATH must be set");
let ini = CString::new(ini_str).expect("CString::new failed");
unsafe { unsafe {
let result = SSV_InitLIB2(ini.as_ptr()); let result = SSV_InitLIB2(ini.as_ptr());
println!("SSV_InitLIB2 result: {}", result); println!("SSV_InitLIB2 result: {}", result);
@ -68,6 +72,8 @@ fn ssv_lire_config() {
} }
pub fn demo() { pub fn demo() {
dotenv().ok();
println!("------- Demo for the SSV library --------"); println!("------- Demo for the SSV library --------");
ssv_init_lib_2(); ssv_init_lib_2();

5
src/ssvw64.def Normal file
View File

@ -0,0 +1,5 @@
LIBRARY "ssvw64"
EXPORTS
SSV_InitLIB2
SSV_LireCartePS
SSV_LireConfig