From 94fb57b9ea2a6bbbf3164fa739c006e79f435a81 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 11 Oct 2023 11:18:13 +0200 Subject: [PATCH] Automate has functions --- rust/trexio/.gitignore | 5 +++++ rust/trexio/build.py | 47 +++++++++++++++++++++++++++++++++++++++-- rust/trexio/build.rs | 37 ++++++++++++++++++++++++++++++++ rust/trexio/src/lib.rs | 18 +--------------- rust/trexio/src/test.rs | 22 ++++++------------- 5 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 rust/trexio/.gitignore create mode 100644 rust/trexio/build.rs diff --git a/rust/trexio/.gitignore b/rust/trexio/.gitignore new file mode 100644 index 0000000..1027d3c --- /dev/null +++ b/rust/trexio/.gitignore @@ -0,0 +1,5 @@ +Cargo.lock +src/generated.rs +target/ +wrapper.h + diff --git a/rust/trexio/build.py b/rust/trexio/build.py index 2b17df2..fe6a94f 100755 --- a/rust/trexio/build.py +++ b/rust/trexio/build.py @@ -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 \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 { + 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 { + 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() diff --git a/rust/trexio/build.rs b/rust/trexio/build.rs new file mode 100644 index 0000000..adf0e0b --- /dev/null +++ b/rust/trexio/build.rs @@ -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!"); + +} + diff --git a/rust/trexio/src/lib.rs b/rust/trexio/src/lib.rs index e89916b..673dd91 100644 --- a/rust/trexio/src/lib.rs +++ b/rust/trexio/src/lib.rs @@ -60,23 +60,7 @@ pub fn inquire(file_name: &str) -> Result { } -pub fn has_nucleus_num(trex_file: File) -> Result { - 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 { - 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> { diff --git a/rust/trexio/src/test.rs b/rust/trexio/src/test.rs index 66d08fc..10962e4 100644 --- a/rust/trexio/src/test.rs +++ b/rust/trexio/src/test.rs @@ -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();