1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2025-01-03 18:16:22 +01:00

Automate has functions

This commit is contained in:
Anthony Scemama 2023-10-11 11:18:13 +02:00
parent 142757572f
commit 94fb57b9ea
5 changed files with 94 additions and 35 deletions

5
rust/trexio/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
Cargo.lock
src/generated.rs
target/
wrapper.h

View File

@ -1,7 +1,10 @@
#!/usr/bin/env python3
import json
json_file = "../../trex.json"
trexio_h = "../../include/trexio.h"
wrapper_h = "wrapper.h"
generated_rs = "src/generated.rs"
def check_version():
with open('Cargo.toml','r') as f:
@ -37,7 +40,7 @@ def make_interface():
buf2 = buf.replace(")","").replace("(","").split()
be[buf2[1]] = int(buf2[3].strip())
with open("wrapper.h",'w') as f:
with open(wrapper_h,'w') as f:
f.write("#include <trexio.h>\n")
# Write exit codes
@ -56,7 +59,47 @@ def make_interface():
def make_functions():
pass
with open(json_file,'r') as f:
data = json.load(f)
r = []
for group in data:
group_l = group.lower()
r += [ """
pub fn has_{group_l}(trex_file: File) -> Result<bool, ExitCode> {
let rc = unsafe { c::trexio_has_{group}(trex_file) };
match rc {
c::TREXIO_SUCCESS => Ok(true),
c::TREXIO_HAS_NOT => Ok(false),
x => Err(ExitCode::from(x)),
}
}
"""
.replace("{group}",group)
.replace("{group_l}",group_l) ]
for element in data[group]:
element_l = element.lower()
r += [ """
pub fn has_{group_l}_{element_l}(trex_file: File) -> Result<bool, ExitCode> {
let rc = unsafe { c::trexio_has_{group}_{element}(trex_file) };
match rc {
c::TREXIO_SUCCESS => Ok(true),
c::TREXIO_HAS_NOT => Ok(false),
x => Err(ExitCode::from(x)),
}
}
"""
.replace("{group}",group)
.replace("{group_l}",group_l)
.replace("{element}",element)
.replace("{element_l}",element_l) ]
with open(generated_rs,'w') as f:
f.write('\n'.join(r))
if __name__ == '__main__':
check_version()

37
rust/trexio/build.rs Normal file
View File

@ -0,0 +1,37 @@
use std::env;
use std::path::PathBuf;
fn main() {
// Tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search=/usr/local/lib");
// Tell cargo to tell rustc to link the system bzip2
// shared library.
println!("cargo:rustc-link-lib=trexio");
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=wrapper.h");
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.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());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

View File

@ -60,23 +60,7 @@ pub fn inquire(file_name: &str) -> Result<bool, ExitCode> {
}
pub fn has_nucleus_num(trex_file: File) -> Result<bool, ExitCode> {
let rc = unsafe { c::trexio_has_nucleus_num(trex_file) };
match rc {
c::TREXIO_SUCCESS => Ok(true),
c::TREXIO_HAS_NOT => Ok(false),
x => Err(ExitCode::from(x)),
}
}
pub fn has_nucleus_charge(trex_file: File) -> Result<bool, ExitCode> {
let rc = unsafe { c::trexio_has_nucleus_charge(trex_file) };
match rc {
c::TREXIO_SUCCESS => Ok(true),
c::TREXIO_HAS_NOT => Ok(false),
x => Err(ExitCode::from(x)),
}
}
include!("generated.rs");
pub fn write_nucleus_num(trex_file: File, data: usize) -> Result<(), ExitCode> {

View File

@ -61,22 +61,12 @@ pub fn test_write(file_name: &str, back_end: BackEnd) {
let trex_file = trexio::open(file_name, 'w', back_end).unwrap();
assert( ! trexio::has_nucleus_num(trex_file).unwrap() );
assert( ! trexio::has_nucleus_charge(trex_file).unwrap() );
/*
let rc = unsafe { trexio_has_ao_2e_int_eri(trex_file) };
assert!(rc == TREXIO_HAS_NOT);
let rc = unsafe { trexio_has_determinant_list(trex_file) };
assert!(rc == TREXIO_HAS_NOT);
let rc = unsafe { trexio_has_nucleus(trex_file) };
assert!(rc == TREXIO_HAS_NOT);
let rc = unsafe { trexio_has_ao_2e_int(trex_file) };
assert!(rc == TREXIO_HAS_NOT);
*/
assert!( ! trexio::has_nucleus(trex_file).unwrap() );
assert!( ! trexio::has_nucleus_num(trex_file).unwrap() );
assert!( ! trexio::has_nucleus_charge(trex_file).unwrap() );
assert!( ! trexio::has_ao_2e_int(trex_file).unwrap() );
assert!( ! trexio::has_ao_2e_int_eri(trex_file).unwrap() );
assert!( ! trexio::has_determinant_list(trex_file).unwrap() );
trexio::write_nucleus_num(trex_file, nucleus_num).unwrap();
trexio::write_nucleus_charge(trex_file, charge).unwrap();