diff --git a/.gitignore b/.gitignore index 4aa7ddd2e..78c251eba 100644 --- a/.gitignore +++ b/.gitignore @@ -46,9 +46,5 @@ cobertura.xml # Python __pycache__/ -# Generated FFI headers (produced by build.rs via cbindgen) -dash-spv-ffi/include/ -key-wallet-ffi/include/ - # Build scripts artifacts *.log diff --git a/dash-spv-ffi/build.rs b/dash-spv-ffi/build.rs index bd1de9edc..29549edbb 100644 --- a/dash-spv-ffi/build.rs +++ b/dash-spv-ffi/build.rs @@ -1,27 +1,32 @@ -use std::env; -use std::path::PathBuf; +use std::path::Path; +use std::{env, fs}; fn main() { + let crate_name = env::var("CARGO_PKG_NAME").unwrap(); let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - let output_path = PathBuf::from(&crate_dir).join("include"); + let out_dir = env::var("OUT_DIR").unwrap(); - std::fs::create_dir_all(&output_path).unwrap(); - - // Ensure the build script reruns when header-relevant files change println!("cargo:rerun-if-changed=cbindgen.toml"); - println!("cargo:rerun-if-changed=src"); - - let config = cbindgen::Config::from_file("cbindgen.toml") - .expect("cbindgen config missing or invalid: cbindgen.toml"); - - match cbindgen::Builder::new().with_crate(&crate_dir).with_config(config).generate() { - Ok(bindings) => { - bindings.write_to_file(output_path.join("dash_spv_ffi.h")); - println!("cargo:warning=Generated C header at {:?}", output_path); - } - Err(e) => { - // Fail the build to avoid shipping stale headers - panic!("Failed to generate C header via cbindgen: {}", e); - } - } + println!("cargo:rerun-if-changed=src/"); + + let target_dir = Path::new(&out_dir) + .ancestors() + .nth(3) // This line moves up to the target/ directory + .expect("Failed to find target dir"); + + let include_dir = target_dir.join("include").join(&crate_name); + + fs::create_dir_all(&include_dir).unwrap(); + + let output_path = include_dir.join(format!("{}.h", &crate_name)); + + let config_path = Path::new(&crate_dir).join("cbindgen.toml"); + let config = cbindgen::Config::from_file(&config_path).expect("Failed to read cbindgen.toml"); + + cbindgen::Builder::new() + .with_crate(&crate_dir) + .with_config(config) + .generate() + .expect("Unable to generate bindings") + .write_to_file(&output_path); } diff --git a/dash-spv-ffi/cbindgen.toml b/dash-spv-ffi/cbindgen.toml index 97954dc4a..24a71f877 100644 --- a/dash-spv-ffi/cbindgen.toml +++ b/dash-spv-ffi/cbindgen.toml @@ -1,11 +1,8 @@ -# cbindgen configuration for dash-spv-ffi - language = "C" header = "/* dash-spv-ffi C bindings - Auto-generated by cbindgen */" include_guard = "DASH_SPV_FFI_H" autogen_warning = "/* Warning: This file is auto-generated by cbindgen. Do not modify manually. */" include_version = true -namespace = "dash_spv_ffi" cpp_compat = true [export] diff --git a/key-wallet-ffi/IMPORT_WALLET_FFI.md b/key-wallet-ffi/IMPORT_WALLET_FFI.md index 20c252ca6..47bdd61e9 100644 --- a/key-wallet-ffi/IMPORT_WALLET_FFI.md +++ b/key-wallet-ffi/IMPORT_WALLET_FFI.md @@ -41,7 +41,7 @@ The function may set the following error codes: ## Usage Example ```c -#include "key_wallet_ffi.h" +#include "key-wallet-ffi.h" // Load wallet bytes from file or network uint8_t *wallet_bytes = load_wallet_bytes(); diff --git a/key-wallet-ffi/build.rs b/key-wallet-ffi/build.rs index 14970c647..29549edbb 100644 --- a/key-wallet-ffi/build.rs +++ b/key-wallet-ffi/build.rs @@ -1,41 +1,32 @@ -// Build script for key-wallet-ffi -// Generates C header file using cbindgen - -use std::env; -use std::path::PathBuf; +use std::path::Path; +use std::{env, fs}; fn main() { - // Add platform-specific linking flags - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); - - match target_os.as_str() { - "ios" => { - println!("cargo:rustc-link-lib=framework=Security"); - } - "macos" => { - println!("cargo:rustc-link-lib=framework=Security"); - } - _ => {} - } - - // Generate C header file using cbindgen + let crate_name = env::var("CARGO_PKG_NAME").unwrap(); let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - let output_path = PathBuf::from(&crate_dir).join("include/key_wallet_ffi.h"); + let out_dir = env::var("OUT_DIR").unwrap(); + + println!("cargo:rerun-if-changed=cbindgen.toml"); + println!("cargo:rerun-if-changed=src/"); + + let target_dir = Path::new(&out_dir) + .ancestors() + .nth(3) // This line moves up to the target/ directory + .expect("Failed to find target dir"); + + let include_dir = target_dir.join("include").join(&crate_name); + + fs::create_dir_all(&include_dir).unwrap(); + + let output_path = include_dir.join(format!("{}.h", &crate_name)); - // Create include directory if it doesn't exist - std::fs::create_dir_all(output_path.parent().unwrap()).ok(); + let config_path = Path::new(&crate_dir).join("cbindgen.toml"); + let config = cbindgen::Config::from_file(&config_path).expect("Failed to read cbindgen.toml"); - match cbindgen::Builder::new() + cbindgen::Builder::new() .with_crate(&crate_dir) - .with_config(cbindgen::Config::from_file("cbindgen.toml").unwrap_or_default()) + .with_config(config) .generate() - { - Ok(bindings) => { - bindings.write_to_file(&output_path); - println!("cargo:warning=Generated C header at {:?}", output_path); - } - Err(e) => { - panic!("Failed to generate C header via cbindgen: {}", e); - } - } + .expect("Unable to generate bindings") + .write_to_file(&output_path); } diff --git a/key-wallet-ffi/cbindgen.toml b/key-wallet-ffi/cbindgen.toml index 3ac2f0fad..e674abf64 100644 --- a/key-wallet-ffi/cbindgen.toml +++ b/key-wallet-ffi/cbindgen.toml @@ -1,6 +1,3 @@ -# cbindgen configuration for key-wallet-ffi -# This file configures how cbindgen generates the C header from Rust code - language = "C" header = """/** * Key Wallet FFI - C Header File @@ -15,7 +12,6 @@ header = """/** include_guard = "KEY_WALLET_FFI_H" autogen_warning = "/* Warning: This file is auto-generated by cbindgen. Do not modify manually. */" include_version = true -cpp_compat = true usize_is_size_t = true no_includes = false sys_includes = ["stdint.h", "stddef.h", "stdbool.h"] diff --git a/key-wallet-ffi/generate_header.sh b/key-wallet-ffi/generate_header.sh deleted file mode 100755 index a3ea6a365..000000000 --- a/key-wallet-ffi/generate_header.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -# Script to generate C header file from Rust FFI code using cbindgen -# Usage: ./generate_header.sh - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "$SCRIPT_DIR" - -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -NC='\033[0m' # No Color - -echo -e "${GREEN}Generating C header file for key-wallet-ffi...${NC}" - -# Check if cbindgen is installed -if ! command -v cbindgen &> /dev/null; then - echo -e "${YELLOW}cbindgen is not installed. Installing...${NC}" - cargo install cbindgen -fi - -# Create include directory if it doesn't exist -mkdir -p include - -# Generate the header file -echo -e "${GREEN}Running cbindgen...${NC}" -cbindgen \ - --config cbindgen.toml \ - --crate key-wallet-ffi \ - --output include/key_wallet_ffi.h \ - --lang c \ - . - -if [ $? -eq 0 ]; then - echo -e "${GREEN}✓ Header file generated successfully at include/key_wallet_ffi.h${NC}" - - # Show statistics - echo -e "${GREEN}Header file statistics:${NC}" - echo " Functions: $(grep -c "^[^/]*(" include/key_wallet_ffi.h 2>/dev/null || echo 0)" - echo " Structs: $(grep -c "^typedef struct" include/key_wallet_ffi.h 2>/dev/null || echo 0)" - echo " Enums: $(grep -c "^typedef enum" include/key_wallet_ffi.h 2>/dev/null || echo 0)" - -else - echo -e "${RED}✗ Failed to generate header file${NC}" - exit 1 -fi - -# Optional: Verify the header compiles -echo -e "${GREEN}Verifying header compilation...${NC}" -cat > /tmp/test_header.c << EOF -#include "$(pwd)/include/key_wallet_ffi.h" -int main() { return 0; } -EOF - -if cc -c /tmp/test_header.c -o /tmp/test_header.o 2>/dev/null; then - echo -e "${GREEN}✓ Header file compiles successfully${NC}" - rm -f /tmp/test_header.c /tmp/test_header.o -else - echo -e "${YELLOW}⚠ Warning: Header file may have compilation issues${NC}" - echo -e "${YELLOW} This might be normal if some types are platform-specific${NC}" - rm -f /tmp/test_header.c -fi - -echo -e "${GREEN}Done!${NC}"