|
1 | | -// Build script for key-wallet-ffi |
2 | | -// Generates C header file using cbindgen |
3 | | - |
4 | | -use std::env; |
5 | | -use std::path::PathBuf; |
| 1 | +use std::path::Path; |
| 2 | +use std::{env, fs}; |
6 | 3 |
|
7 | 4 | fn main() { |
8 | | - // Add platform-specific linking flags |
9 | | - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); |
10 | | - |
11 | | - match target_os.as_str() { |
12 | | - "ios" => { |
13 | | - println!("cargo:rustc-link-lib=framework=Security"); |
14 | | - } |
15 | | - "macos" => { |
16 | | - println!("cargo:rustc-link-lib=framework=Security"); |
17 | | - } |
18 | | - _ => {} |
19 | | - } |
20 | | - |
21 | | - // Generate C header file using cbindgen |
| 5 | + let crate_name = env::var("CARGO_PKG_NAME").unwrap(); |
22 | 6 | let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); |
23 | | - let output_path = PathBuf::from(&crate_dir).join("include/key_wallet_ffi.h"); |
| 7 | + let out_dir = env::var("OUT_DIR").unwrap(); |
| 8 | + |
| 9 | + println!("cargo:rerun-if-changed=cbindgen.toml"); |
| 10 | + println!("cargo:rerun-if-changed=src/"); |
| 11 | + |
| 12 | + let target_dir = Path::new(&out_dir) |
| 13 | + .ancestors() |
| 14 | + .nth(3) // This line moves up to the target/<PROFILE> directory |
| 15 | + .expect("Failed to find target dir"); |
| 16 | + |
| 17 | + let include_dir = target_dir.join("include").join(&crate_name); |
| 18 | + |
| 19 | + fs::create_dir_all(&include_dir).unwrap(); |
| 20 | + |
| 21 | + let output_path = include_dir.join(format!("{}.h", &crate_name)); |
24 | 22 |
|
25 | | - // Create include directory if it doesn't exist |
26 | | - std::fs::create_dir_all(output_path.parent().unwrap()).ok(); |
| 23 | + let config_path = Path::new(&crate_dir).join("cbindgen.toml"); |
| 24 | + let config = cbindgen::Config::from_file(&config_path).expect("Failed to read cbindgen.toml"); |
27 | 25 |
|
28 | | - match cbindgen::Builder::new() |
| 26 | + cbindgen::Builder::new() |
29 | 27 | .with_crate(&crate_dir) |
30 | | - .with_config(cbindgen::Config::from_file("cbindgen.toml").unwrap_or_default()) |
| 28 | + .with_config(config) |
31 | 29 | .generate() |
32 | | - { |
33 | | - Ok(bindings) => { |
34 | | - bindings.write_to_file(&output_path); |
35 | | - println!("cargo:warning=Generated C header at {:?}", output_path); |
36 | | - } |
37 | | - Err(e) => { |
38 | | - panic!("Failed to generate C header via cbindgen: {}", e); |
39 | | - } |
40 | | - } |
| 30 | + .expect("Unable to generate bindings") |
| 31 | + .write_to_file(&output_path); |
41 | 32 | } |
0 commit comments