From 4fe9d04c7a6bee883ff8181930f30a7d9148a650 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Sat, 14 Jun 2025 02:26:22 +0800 Subject: Added the functionality to automatically generate header files. --- Cargo.lock | 56 +++++++++++++++++++++++++- Project_Export.toml | 4 +- core/bindings/lang/clang/Cargo.toml | 7 +++- core/bindings/lang/clang/Generate.toml | 11 +++++ core/bindings/lang/clang/build.rs | 34 ++++++++++++++++ core/bindings/lang/clang/headers/ngpd_player.h | 39 ------------------ core/bindings/lang/clang/headers/nogamepads.h | 48 ++++++++++++++++++++++ 7 files changed, 156 insertions(+), 43 deletions(-) create mode 100644 core/bindings/lang/clang/Generate.toml create mode 100644 core/bindings/lang/clang/build.rs delete mode 100644 core/bindings/lang/clang/headers/ngpd_player.h create mode 100644 core/bindings/lang/clang/headers/nogamepads.h diff --git a/Cargo.lock b/Cargo.lock index 459802c..6b30ef4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1580,6 +1580,25 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "cbindgen" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "975982cdb7ad6a142be15bdf84aea7ec6a9e5d4d797c004d43185b24cfe4e684" +dependencies = [ + "clap", + "heck", + "indexmap", + "log", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn", + "tempfile", + "toml 0.8.22", +] + [[package]] name = "cc" version = "1.2.24" @@ -3207,6 +3226,7 @@ dependencies = [ name = "nogamepads-c-bindings" version = "0.1.0" dependencies = [ + "cbindgen", "libc", "nogamepads-core", ] @@ -3261,7 +3281,7 @@ version = "0.0.0" dependencies = [ "nogamepads", "serde", - "toml", + "toml 0.7.8", ] [[package]] @@ -4495,6 +4515,19 @@ dependencies = [ "slotmap", ] +[[package]] +name = "tempfile" +version = "3.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" +dependencies = [ + "fastrand", + "getrandom 0.3.3", + "once_cell", + "rustix 1.0.7", + "windows-sys 0.59.0", +] + [[package]] name = "termcolor" version = "1.4.1" @@ -4622,6 +4655,18 @@ dependencies = [ "toml_edit 0.19.15", ] +[[package]] +name = "toml" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05ae329d1f08c4d17a59bed7ff5b5a769d062e64a62d34a3261b219e62cd5aae" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.22.26", +] + [[package]] name = "toml_datetime" version = "0.6.9" @@ -4651,10 +4696,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "310068873db2c5b3e7659d2cc35d21855dbafa50d1ce336397c666e3cb08137e" dependencies = [ "indexmap", + "serde", + "serde_spanned", "toml_datetime", + "toml_write", "winnow 0.7.10", ] +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + [[package]] name = "tracing" version = "0.1.41" diff --git a/Project_Export.toml b/Project_Export.toml index 5261a7b..5d09f89 100644 --- a/Project_Export.toml +++ b/Project_Export.toml @@ -16,8 +16,8 @@ powershell_complete = { raw = "./.cargo/resources/scripts/", target = "./bin/com ] } # Headers -headers = { raw = "./core/bindings/c_lang/headers/", target = "./lib", files = [ - "ngpd_player.h" +headers = { raw = "./core/bindings/lang/clang/headers/", target = "./lib", files = [ + "nogamepads.h" ] } # Items defined in [release.deps] diff --git a/core/bindings/lang/clang/Cargo.toml b/core/bindings/lang/clang/Cargo.toml index 34d1fed..e094413 100644 --- a/core/bindings/lang/clang/Cargo.toml +++ b/core/bindings/lang/clang/Cargo.toml @@ -3,10 +3,15 @@ name = "nogamepads-c-bindings" edition = "2024" version.workspace = true +build = "build.rs" + [dependencies] nogamepads-core = { path = "./../../../../core" } libc = "0.2.172" +[build-dependencies] +cbindgen = "0.29.0" + [lib] name = "nogamepads_c" -crate-type = ["cdylib"] +crate-type = ["cdylib", "rlib"] diff --git a/core/bindings/lang/clang/Generate.toml b/core/bindings/lang/clang/Generate.toml new file mode 100644 index 0000000..08d8b00 --- /dev/null +++ b/core/bindings/lang/clang/Generate.toml @@ -0,0 +1,11 @@ +language = "C" +include_guard = "NOGAMEPADS_H" +header = "// Auto generated by cbindgen 0.29.0" +cpp_compat = true + +[parse] +parse_deps = true +include = ["*"] + +[fn] +prefix = "ngp_" \ No newline at end of file diff --git a/core/bindings/lang/clang/build.rs b/core/bindings/lang/clang/build.rs new file mode 100644 index 0000000..c43ee7f --- /dev/null +++ b/core/bindings/lang/clang/build.rs @@ -0,0 +1,34 @@ +fn main() { + + // Get the project root directory + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let root_path = std::path::Path::new(&manifest_dir); + + // Input configuration file path + let config_path = root_path.join("Generate.toml"); + + // Output directory path (headers folder in the crate directory) + let headers_dir = root_path.join("headers"); + + // Create the headers directory (if it does not exist) + std::fs::create_dir_all(&headers_dir).expect("Failed to create headers directory"); + + // Output file path + let output_file = headers_dir.join("nogamepads.h"); + + // Load configuration from Generate.toml + let config = cbindgen::Config::from_file(&config_path) + .expect("Failed to parse Generate.toml configuration"); + + // Generate header file + cbindgen::Builder::new() + .with_crate(manifest_dir) + .with_config(config) + .generate() + .expect("Failed to generate C header file") + .write_to_file(&output_file); + + println!("cargo:rerun-if-changed={}", config_path.display()); + println!("cargo:rerun-if-changed=src/mycode.rs"); + println!("cargo:rerun-if-changed=build.rs"); +} \ No newline at end of file diff --git a/core/bindings/lang/clang/headers/ngpd_player.h b/core/bindings/lang/clang/headers/ngpd_player.h deleted file mode 100644 index 00fbe5b..0000000 --- a/core/bindings/lang/clang/headers/ngpd_player.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef PLAYER_FFI_H -#define PLAYER_FFI_H - -#include -#include - -typedef struct FfiAccount { - char* id; - char* player_hash; -} FfiAccount; - -typedef struct FfiCustomize { - char* nickname; - int32_t color_hue; - double color_saturation; - double color_value; -} FfiCustomize; - -typedef struct FfiPlayer { - FfiAccount account; - FfiCustomize* customize; -} FfiPlayer; - -#ifdef __cplusplus -extern "C" { -#endif - -FfiPlayer* player_register(const char* id, const char* password); -bool player_check(const FfiPlayer* player, const char* password); -void player_set_nickname(FfiPlayer* player, const char* nickname); -void player_set_hue(FfiPlayer* player, int hue); -void player_set_hsv(FfiPlayer* player, int hue, double saturation, double value); -void player_free(FfiPlayer* player); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/core/bindings/lang/clang/headers/nogamepads.h b/core/bindings/lang/clang/headers/nogamepads.h new file mode 100644 index 0000000..f1ac85a --- /dev/null +++ b/core/bindings/lang/clang/headers/nogamepads.h @@ -0,0 +1,48 @@ +// 自动生成的头文件 - 请勿手动编辑 + +#ifndef NOGAMEPADS_H +#define NOGAMEPADS_H + +#include +#include +#include +#include + +typedef struct FfiAccount { + char *id; + char *player_hash; +} FfiAccount; + +typedef struct FfiCustomize { + char *nickname; + int color_hue; + double color_saturation; + double color_value; +} FfiCustomize; + +typedef struct FfiPlayer { + struct FfiAccount account; + struct FfiCustomize *customize; +} FfiPlayer; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +ngp_ struct FfiPlayer *player_register(const char *id, const char *password); + +ngp_ bool player_check(const struct FfiPlayer *player, const char *password); + +ngp_ void player_set_nickname(struct FfiPlayer *player, const char *nickname); + +ngp_ void player_set_hue(struct FfiPlayer *player, int hue); + +ngp_ void player_set_hsv(struct FfiPlayer *player, int hue, double saturation, double value); + +ngp_ void player_free(struct FfiPlayer *player); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* NOGAMEPADS_H */ -- cgit