Add workspace and move bin to bdk-ffi-bindgen package
This commit is contained in:
parent
f4e097c4ac
commit
fca5d1602b
11
Cargo.toml
11
Cargo.toml
@ -4,6 +4,10 @@ version = "0.4.1"
|
|||||||
authors = ["Steve Myers <steve@notmandatory.org>", "Sudarsan Balaji <sudarsan.balaji@artfuldev.com>"]
|
authors = ["Steve Myers <steve@notmandatory.org>", "Sudarsan Balaji <sudarsan.balaji@artfuldev.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
[workspace]
|
||||||
|
members = [".","bdk-ffi-bindgen"]
|
||||||
|
default-members = [".", "bdk-ffi-bindgen"]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["staticlib", "cdylib"]
|
crate-type = ["staticlib", "cdylib"]
|
||||||
@ -17,13 +21,6 @@ rusqlite = { version = "0.25.3", features = ["bundled"] }
|
|||||||
|
|
||||||
uniffi_macros = { version = "0.16.0", features = ["builtin-bindgen"] }
|
uniffi_macros = { version = "0.16.0", features = ["builtin-bindgen"] }
|
||||||
uniffi = { version = "0.16.0", features = ["builtin-bindgen"] }
|
uniffi = { version = "0.16.0", features = ["builtin-bindgen"] }
|
||||||
thiserror = "1.0"
|
|
||||||
anyhow = "=1.0.45" # remove after upgrading to next version of uniffi
|
|
||||||
uniffi_bindgen = "0.16.0"
|
|
||||||
structopt = "0.3"
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] }
|
uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] }
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "generate-bindings"
|
|
||||||
|
11
bdk-ffi-bindgen/Cargo.toml
Normal file
11
bdk-ffi-bindgen/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "bdk-ffi-bindgen"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "=1.0.45" # remove after upgrading to next version of uniffi
|
||||||
|
structopt = "0.3"
|
||||||
|
uniffi_bindgen = "0.16.0"
|
@ -1,10 +1,9 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use uniffi_bindgen;
|
use uniffi_bindgen;
|
||||||
|
|
||||||
pub const BDK_UDL: &str = "src/bdk.udl";
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Language {
|
pub enum Language {
|
||||||
KOTLIN,
|
KOTLIN,
|
||||||
@ -47,7 +46,7 @@ impl FromStr for Language {
|
|||||||
|
|
||||||
fn generate_bindings(opt: &Opt) -> anyhow::Result<(), anyhow::Error> {
|
fn generate_bindings(opt: &Opt) -> anyhow::Result<(), anyhow::Error> {
|
||||||
uniffi_bindgen::generate_bindings(
|
uniffi_bindgen::generate_bindings(
|
||||||
&format!("{}/{}", env!("CARGO_MANIFEST_DIR"), BDK_UDL),
|
&opt.udl_file,
|
||||||
None,
|
None,
|
||||||
vec![opt.language.to_string().as_str()],
|
vec![opt.language.to_string().as_str()],
|
||||||
Some(&opt.out_dir),
|
Some(&opt.out_dir),
|
||||||
@ -57,16 +56,16 @@ fn generate_bindings(opt: &Opt) -> anyhow::Result<(), anyhow::Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fixup_python_lib_path<O: AsRef<std::path::Path>>(
|
fn fixup_python_lib_path(
|
||||||
out_dir: O,
|
out_dir: &PathBuf,
|
||||||
lib_name: &str,
|
lib_name: &PathBuf,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
const LOAD_INDIRECT_DEF: &str = "def loadIndirect():";
|
const LOAD_INDIRECT_DEF: &str = "def loadIndirect():";
|
||||||
|
|
||||||
let bindings_file = out_dir.as_ref().join("bdk.py");
|
let bindings_file = out_dir.join("bdk.py");
|
||||||
let mut data = fs::read_to_string(&bindings_file)?;
|
let mut data = fs::read_to_string(&bindings_file)?;
|
||||||
|
|
||||||
let pos = data.find(LOAD_INDIRECT_DEF).expect(&format!(
|
let pos = data.find(LOAD_INDIRECT_DEF).expect(&format!(
|
||||||
@ -82,7 +81,7 @@ def loadIndirect():
|
|||||||
return getattr(ctypes.cdll, glob.glob(os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}.*'))[0])
|
return getattr(ctypes.cdll, glob.glob(os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}.*'))[0])
|
||||||
|
|
||||||
def _loadIndirectOld():"#,
|
def _loadIndirectOld():"#,
|
||||||
lib_name
|
&lib_name.to_str().expect("lib name")
|
||||||
);
|
);
|
||||||
data.replace_range(range, &replacement);
|
data.replace_range(range, &replacement);
|
||||||
|
|
||||||
@ -97,34 +96,39 @@ def _loadIndirectOld():"#,
|
|||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
#[structopt(
|
#[structopt(
|
||||||
name = "generate-bindings",
|
name = "bdk-ffi-bindgen",
|
||||||
about = "A tool to generate bdk-ffi language bindings"
|
about = "A tool to generate bdk-ffi language bindings"
|
||||||
)]
|
)]
|
||||||
struct Opt {
|
struct Opt {
|
||||||
|
/// UDL file
|
||||||
|
#[structopt(env = "BDKFFI_BINDGEN_UDL", short, long, default_value("src/bdk.udl"), parse(try_from_str = PathBuf::from_str))]
|
||||||
|
udl_file: PathBuf,
|
||||||
|
|
||||||
/// Language to generate bindings for
|
/// Language to generate bindings for
|
||||||
#[structopt(env = "UNIFFI_BINDGEN_LANGUAGE", short, long, possible_values(&["kotlin","swift","python"]), parse(try_from_str = Language::from_str))]
|
#[structopt(env = "BDKFFI_BINDGEN_LANGUAGE", short, long, possible_values(&["kotlin","swift","python"]), parse(try_from_str = Language::from_str))]
|
||||||
language: Language,
|
language: Language,
|
||||||
|
|
||||||
/// Output directory to put generated language bindings
|
/// Output directory to put generated language bindings
|
||||||
#[structopt(env = "UNIFFI_BINDGEN_OUTPUT_DIR", short, long)]
|
#[structopt(env = "BDKFFI_BINDGEN_OUTPUT_DIR", short, long, parse(try_from_str = PathBuf::from_str))]
|
||||||
out_dir: String,
|
out_dir: PathBuf,
|
||||||
|
|
||||||
/// Python fix up lib path
|
/// Python fix up lib path
|
||||||
#[structopt(env = "UNIFFI_BINDGEN_PYTHON_FIXUP_PATH", short, long)]
|
#[structopt(env = "BDKFFI_BINDGEN_PYTHON_FIXUP_PATH", short, long, parse(try_from_str = PathBuf::from_str))]
|
||||||
python_fixup_path: Option<String>,
|
python_fixup_path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
|
|
||||||
|
println!("Input UDL file is {:?}", opt.udl_file);
|
||||||
println!("Chosen language is {}", opt.language);
|
println!("Chosen language is {}", opt.language);
|
||||||
println!("Output directory is {}", opt.out_dir);
|
println!("Output directory is {:?}", opt.out_dir);
|
||||||
|
|
||||||
generate_bindings(&opt)?;
|
generate_bindings(&opt)?;
|
||||||
|
|
||||||
if opt.language == Language::PYTHON {
|
if opt.language == Language::PYTHON {
|
||||||
if let Some(path) = &opt.python_fixup_path {
|
if let Some(path) = opt.python_fixup_path {
|
||||||
println!("Fixing up python lib path, {}", &path);
|
println!("Fixing up python lib path, {:?}", &path);
|
||||||
fixup_python_lib_path(&opt.out_dir, &path)?;
|
fixup_python_lib_path(&opt.out_dir, &path)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,67 +0,0 @@
|
|||||||
pub const BDK_UDL: &str = "src/bdk.udl";
|
|
||||||
|
|
||||||
#[cfg(feature = "generate-python")]
|
|
||||||
fn fixup_python_lib_path<O: AsRef<std::path::Path>>(
|
|
||||||
out_dir: O,
|
|
||||||
lib_name: &str,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
use std::fs;
|
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
const LOAD_INDIRECT_DEF: &str = "def loadIndirect():";
|
|
||||||
|
|
||||||
let bindings_file = out_dir.as_ref().join("bdk.py");
|
|
||||||
let mut data = fs::read_to_string(&bindings_file)?;
|
|
||||||
|
|
||||||
let pos = data.find(LOAD_INDIRECT_DEF).expect(&format!(
|
|
||||||
"loadIndirect not found in `{}`",
|
|
||||||
bindings_file.display()
|
|
||||||
));
|
|
||||||
let range = pos..pos + LOAD_INDIRECT_DEF.len();
|
|
||||||
|
|
||||||
let replacement = format!(
|
|
||||||
r#"
|
|
||||||
def loadIndirect():
|
|
||||||
import glob
|
|
||||||
return getattr(ctypes.cdll, glob.glob(os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}.*'))[0])
|
|
||||||
|
|
||||||
def _loadIndirectOld():"#,
|
|
||||||
lib_name
|
|
||||||
);
|
|
||||||
data.replace_range(range, &replacement);
|
|
||||||
|
|
||||||
let mut file = fs::OpenOptions::new()
|
|
||||||
.write(true)
|
|
||||||
.truncate(true)
|
|
||||||
.open(&bindings_file)?;
|
|
||||||
file.write(data.as_bytes())?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "generate-python")]
|
|
||||||
fn generate_python() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
let out_path = env::var("GENERATE_PYTHON_BINDINGS_OUT")
|
|
||||||
.map_err(|_| String::from("`GENERATE_PYTHON_BINDINGS_OUT` env variable missing"))?;
|
|
||||||
uniffi_bindgen::generate_bindings(
|
|
||||||
&format!("{}/{}", env!("CARGO_MANIFEST_DIR"), BDK_UDL),
|
|
||||||
None,
|
|
||||||
vec!["python"],
|
|
||||||
Some(&out_path),
|
|
||||||
false,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
if let Some(name) = env::var("GENERATE_PYTHON_BINDINGS_FIXUP_LIB_PATH").ok() {
|
|
||||||
fixup_python_lib_path(&out_path, &name)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
#[cfg(feature = "generate-python")]
|
|
||||||
generate_python()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user