Browse Source

update: rust metadata ffi

master
Dnomd343 2 years ago
parent
commit
701e45dcba
  1. 6
      src/klotski_core/ffi/metadata.cc
  2. 10
      src/klotski_core/klotski.h
  3. 6
      src/klotski_core/utils/metadata.h.in
  4. 21
      src/rust_ffi/build.rs
  5. 12
      src/rust_ffi/src/metadata.rs
  6. 3
      src/rust_ffi/wrapper.h

6
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;
}

10
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

6
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"

21
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");
}

12
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(),
}
}
}

3
src/rust_ffi/wrapper.h

@ -1,3 +0,0 @@
#pragma once
#include "../../bin/klotski.h"
Loading…
Cancel
Save