Skip to content

Commit 8cbae41

Browse files
committed
move header files generation to the target directory
1 parent 19fb152 commit 8cbae41

7 files changed

Lines changed: 51 additions & 133 deletions

File tree

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,5 @@ cobertura.xml
4646
# Python
4747
__pycache__/
4848

49-
# Generated FFI headers (produced by build.rs via cbindgen)
50-
dash-spv-ffi/include/
51-
key-wallet-ffi/include/
52-
5349
# Build scripts artifacts
5450
*.log

dash-spv-ffi/build.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
use std::env;
2-
use std::path::PathBuf;
1+
use std::path::Path;
2+
use std::{env, fs};
33

44
fn main() {
5+
let crate_name = env::var("CARGO_PKG_NAME").unwrap();
56
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
6-
let output_path = PathBuf::from(&crate_dir).join("include");
7+
let out_dir = env::var("OUT_DIR").unwrap();
78

8-
std::fs::create_dir_all(&output_path).unwrap();
9-
10-
// Ensure the build script reruns when header-relevant files change
119
println!("cargo:rerun-if-changed=cbindgen.toml");
12-
println!("cargo:rerun-if-changed=src");
13-
14-
let config = cbindgen::Config::from_file("cbindgen.toml")
15-
.expect("cbindgen config missing or invalid: cbindgen.toml");
16-
17-
match cbindgen::Builder::new().with_crate(&crate_dir).with_config(config).generate() {
18-
Ok(bindings) => {
19-
bindings.write_to_file(output_path.join("dash_spv_ffi.h"));
20-
println!("cargo:warning=Generated C header at {:?}", output_path);
21-
}
22-
Err(e) => {
23-
// Fail the build to avoid shipping stale headers
24-
panic!("Failed to generate C header via cbindgen: {}", e);
25-
}
26-
}
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));
22+
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");
25+
26+
cbindgen::Builder::new()
27+
.with_crate(&crate_dir)
28+
.with_config(config)
29+
.generate()
30+
.expect("Unable to generate bindings")
31+
.write_to_file(&output_path);
2732
}

dash-spv-ffi/cbindgen.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
# cbindgen configuration for dash-spv-ffi
2-
31
language = "C"
42
header = "/* dash-spv-ffi C bindings - Auto-generated by cbindgen */"
53
include_guard = "DASH_SPV_FFI_H"
64
autogen_warning = "/* Warning: This file is auto-generated by cbindgen. Do not modify manually. */"
75
include_version = true
8-
namespace = "dash_spv_ffi"
96
cpp_compat = true
107

118
[export]

key-wallet-ffi/IMPORT_WALLET_FFI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The function may set the following error codes:
4141
## Usage Example
4242

4343
```c
44-
#include "key_wallet_ffi.h"
44+
#include "key-wallet-ffi.h"
4545

4646
// Load wallet bytes from file or network
4747
uint8_t *wallet_bytes = load_wallet_bytes();

key-wallet-ffi/build.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
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};
63

74
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();
226
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));
2422

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");
2725

28-
match cbindgen::Builder::new()
26+
cbindgen::Builder::new()
2927
.with_crate(&crate_dir)
30-
.with_config(cbindgen::Config::from_file("cbindgen.toml").unwrap_or_default())
28+
.with_config(config)
3129
.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);
4132
}

key-wallet-ffi/cbindgen.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# cbindgen configuration for key-wallet-ffi
2-
# This file configures how cbindgen generates the C header from Rust code
3-
41
language = "C"
52
header = """/**
63
* Key Wallet FFI - C Header File
@@ -15,7 +12,6 @@ header = """/**
1512
include_guard = "KEY_WALLET_FFI_H"
1613
autogen_warning = "/* Warning: This file is auto-generated by cbindgen. Do not modify manually. */"
1714
include_version = true
18-
cpp_compat = true
1915
usize_is_size_t = true
2016
no_includes = false
2117
sys_includes = ["stdint.h", "stddef.h", "stdbool.h"]

key-wallet-ffi/generate_header.sh

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)