import subprocess
import os
import shutil
path_cwd = os.path.abspath(os.getcwd())
path_repo = f"{os.getenv('HOME')}/Git-Others/simple-dftd3"
path_header = f"{path_cwd}/../header"
path_temp = f"{path_cwd}/tmp"
path_out = f"{path_cwd}/.."
os.makedirs(path_header, exist_ok=True)
os.makedirs(f"{path_out}/src", exist_ok=True)
for name in ["s-dftd3.h"]:
shutil.copy(f"{path_repo}/include/{name}", f"{path_header}")
shutil.rmtree(path_temp, ignore_errors=True)
shutil.copytree(path_header, path_temp)
os.chdir(path_temp)
subprocess.run([
"bindgen",
"s-dftd3.h", "-o", "ffi.rs",
"--allowlist-file", "s-dftd3.h",
"--no-layout-tests",
"--use-core",
"--merge-extern-blocks",
])
with open("ffi.rs", "r") as f:
token = f.read()
token = token.replace("::core::ffi::", "")
token = token.replace("minor + 100", "minor * 100")
token = """
//! FFI bindings for simple-dftd3.
#![allow(non_camel_case_types)]
use core::ffi::{c_char, c_int};
""" + "\n\n" + token
with open("ffi.rs", "w") as f:
f.write(token)
for name in ["ffi.rs"]:
shutil.copy(f"{path_temp}/{name}", f"{path_out}/src/{name}")
os.chdir(path_out)
subprocess.run(["cargo", "fmt"])