#![doc(html_root_url = "https://terminusdb-labs.github.io/swipl-rs/swipl_info/")]
use regex::*;
use std::env;
use std::process::Command;
#[derive(Debug)]
pub struct SwiplInfo {
pub version: u32,
pub swi_home: String,
pub pack_so_dir: String,
pub cflags: String,
pub ldflags: String,
pub arch: String,
pub lib_name: String,
pub lib_dir: String,
pub header_dir: String,
}
pub fn get_swipl_info() -> SwiplInfo {
let swipl_path = env::var("SWIPL").unwrap_or_else(|_| "swipl".to_string());
let output = Command::new(&swipl_path)
.arg("-g")
.arg("current_prolog_flag(version, Version), writeq(Version)")
.arg("-g")
.arg("halt")
.output()
.unwrap();
if !output.status.success() {
panic!("Error retrieving version number using swipl: {:?}", output);
}
let version: u32 = String::from_utf8_lossy(&output.stdout).parse().unwrap();
let build_env_command = if version >= 80504 {
"use_module(library(build/tools)), build_tools:build_environment(Env, []), memberchk('SWIHOME'=Swihome, Env), memberchk('PACKSODIR'=Packsodir, Env), memberchk('CFLAGS'=Cflags, Env), memberchk('LDSOFLAGS'=Ldflags, Env), format('~s~n~s~n~s~n~s~n', [Swihome, Packsodir, Cflags, Ldflags])"
} else {
"use_module(library(prolog_pack)), prolog_pack:build_environment(Env), memberchk('SWIHOME'=Swihome, Env), memberchk('PACKSODIR'=Packsodir, Env), memberchk('CFLAGS'=Cflags, Env), memberchk('LDSOFLAGS'=Ldflags, Env), format('~s~n~s~n~s~n~s~n', [Swihome, Packsodir, Cflags, Ldflags])"
};
let output = Command::new(&swipl_path)
.arg("-g")
.arg(build_env_command)
.arg("-g")
.arg("halt")
.output()
.unwrap();
if !output.status.success() {
panic!(
"Error retrieving build environment using swipl: {:?}",
output
);
}
let runtime_output = Command::new(&swipl_path)
.arg("--dump-runtime-variables")
.output()
.unwrap();
let runtime_output = String::from_utf8_lossy(&runtime_output.stdout);
let plarch_regex = Regex::new(r#"PLARCH="([^"]*)""#).unwrap();
let pllibdir_regex = Regex::new(r#"PLLIBDIR="([^"]*)""#).unwrap();
let arch = plarch_regex.captures(&runtime_output).unwrap()[1].to_string();
let lib_dir = pllibdir_regex.captures(&runtime_output).unwrap()[1].to_string();
let stdout = String::from_utf8_lossy(&output.stdout);
let mut lines = stdout.lines();
let swi_home = lines.next().unwrap().to_owned();
let pack_so_dir = lines.next().unwrap().to_owned();
let cflags = lines.next().unwrap().to_owned();
let ldflags = lines.next().unwrap().to_owned();
let lib_name = if cfg!(target_os = "windows") {
"libswipl".to_string()
} else {
"swipl".to_string()
};
let header_dir = format!("{}/include", swi_home);
SwiplInfo {
version,
swi_home,
pack_so_dir,
cflags,
ldflags,
arch,
lib_name,
lib_dir,
header_dir,
}
}