From 701e45dcbadece232f6003312d78fbcf6c026029 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 30 Jan 2023 15:47:23 +0800 Subject: [PATCH] update: rust metadata ffi --- src/klotski_core/ffi/metadata.cc | 6 +++--- src/klotski_core/klotski.h | 10 +++++----- src/klotski_core/utils/metadata.h.in | 6 +++--- src/rust_ffi/build.rs | 21 ++++++++++++++------- src/rust_ffi/src/metadata.rs | 12 +++++++++++- src/rust_ffi/wrapper.h | 3 --- 6 files changed, 36 insertions(+), 22 deletions(-) delete mode 100644 src/rust_ffi/wrapper.h diff --git a/src/klotski_core/ffi/metadata.cc b/src/klotski_core/ffi/metadata.cc index 0bf2937..eb03e74 100644 --- a/src/klotski_core/ffi/metadata.cc +++ b/src/klotski_core/ffi/metadata.cc @@ -1,15 +1,15 @@ #include "klotski.h" #include "metadata.h" -int get_version_major() { +uint32_t get_version_major() { return VERSION_MAJOR; } -int get_version_minor() { +uint32_t get_version_minor() { return VERSION_MINOR; } -int get_version_patch() { +uint32_t get_version_patch() { return VERSION_PATCH; } diff --git a/src/klotski_core/klotski.h b/src/klotski_core/klotski.h index 5ba9a8b..6e29f9f 100644 --- a/src/klotski_core/klotski.h +++ b/src/klotski_core/klotski.h @@ -22,11 +22,6 @@ extern "C" { #ifdef __cplusplus extern "C" { #endif - // version -> major.minor.patch - extern int get_version_major(); - extern int get_version_minor(); - extern int get_version_patch(); - extern const char* get_author(); extern const char* get_git_tag(); extern const char* get_version(); @@ -36,6 +31,11 @@ extern "C" { extern const char* get_project_url(); extern const char* get_system_info(); extern const char* get_compiler_info(); + + // version -> major.minor.patch + extern uint32_t get_version_major(); + extern uint32_t get_version_minor(); + extern uint32_t get_version_patch(); #ifdef __cplusplus } #endif diff --git a/src/klotski_core/utils/metadata.h.in b/src/klotski_core/utils/metadata.h.in index 130ee7f..d2d17d4 100644 --- a/src/klotski_core/utils/metadata.h.in +++ b/src/klotski_core/utils/metadata.h.in @@ -1,8 +1,8 @@ #pragma once -#define VERSION_MAJOR (@VERSION_MAJOR@) -#define VERSION_MINOR (@VERSION_MINOR@) -#define VERSION_PATCH (@VERSION_ALTER@) +#define VERSION_MAJOR uint32_t(@VERSION_MAJOR@) +#define VERSION_MINOR uint32_t(@VERSION_MINOR@) +#define VERSION_PATCH uint32_t(@VERSION_ALTER@) #define VERSION_STR "v@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_ALTER@" #define AUTHOR "dnomd343" diff --git a/src/rust_ffi/build.rs b/src/rust_ffi/build.rs index a1e1803..89f7a79 100644 --- a/src/rust_ffi/build.rs +++ b/src/rust_ffi/build.rs @@ -3,24 +3,31 @@ extern crate bindgen; use std::env; use std::path::PathBuf; -const LIB_CORE_DIR: &str = "../../bin"; +const KLOTSKI_LIB: &str = "klotski"; +const KLOTSKI_HEADER: &str = "klotski.h"; +const KLOTSKI_RUST_ABI: &str = "bindings.rs"; +const KLOTSKI_RELEASE_DIR: &str = "../../bin/"; fn main() { - // basic compile options - println!("cargo:rustc-link-search={}", LIB_CORE_DIR); - println!("cargo:rustc-link-lib=static=klotski"); - println!("cargo:rerun-if-changed=wrapper.h"); + // path of klotski c-header file + let header_path = PathBuf::from(KLOTSKI_RELEASE_DIR).join(KLOTSKI_HEADER); + let klotski_c_header = header_path.to_str().unwrap(); + + // basic cargo compile options + println!("cargo:rustc-link-search={}", KLOTSKI_RELEASE_DIR); + println!("cargo:rustc-link-lib=static={}", KLOTSKI_LIB); + println!("cargo:rerun-if-changed={}", klotski_c_header); println!("cargo:rustc-link-lib=stdc++"); // build binding info from c-header file let bindings = bindgen::Builder::default() - .header("wrapper.h") + .header(klotski_c_header) .parse_callbacks(Box::new(bindgen::CargoCallbacks)) .generate() .expect("Unable to generate bindings"); // release binding rust source code let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - bindings.write_to_file(out_path.join("bindings.rs")) + bindings.write_to_file(out_path.join(KLOTSKI_RUST_ABI)) .expect("Couldn't write bindings"); } diff --git a/src/rust_ffi/src/metadata.rs b/src/rust_ffi/src/metadata.rs index f815509..c94c371 100644 --- a/src/rust_ffi/src/metadata.rs +++ b/src/rust_ffi/src/metadata.rs @@ -5,23 +5,33 @@ use Core::CStringDump; pub struct Metadata { pub author: String, pub system: String, + pub git_tag: String, pub version: String, pub compiler: String, pub commit_id: String, pub build_time: String, + pub git_branch: String, pub project_url: String, + pub version_major: u32, + pub version_minor: u32, + pub version_patch: u32, } pub fn load_metadata() -> Metadata { unsafe { Metadata { - author: Core::get_author().dump(), + author: Core::get_author().dump(), // const c_char* -> String system: Core::get_system_info().dump(), + git_tag: Core::get_git_tag().dump(), version: Core::get_version().dump(), compiler: Core::get_compiler_info().dump(), commit_id: Core::get_commit_id().dump(), build_time: Core::get_build_time().dump(), + git_branch: Core::get_git_branch().dump(), project_url: Core::get_project_url().dump(), + version_major: Core::get_version_major(), // uint32_t -> u32 + version_minor: Core::get_version_minor(), + version_patch: Core::get_version_patch(), } } } diff --git a/src/rust_ffi/wrapper.h b/src/rust_ffi/wrapper.h deleted file mode 100644 index 2fcfc5e..0000000 --- a/src/rust_ffi/wrapper.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#include "../../bin/klotski.h"