-
Notifications
You must be signed in to change notification settings - Fork 11
chore(ffi): move header files generation to the target directory #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZocoLini
wants to merge
1
commit into
v0.42-dev
Choose a base branch
from
chore/header-generation
base: v0.42-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−133
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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/<PROFILE> 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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/<PROFILE> 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)); | ||
ZocoLini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.