Browse Source

feat: add rust ffi demo

master
Dnomd343 4 months ago
parent
commit
1aa0e3a8f5
  1. 1
      CMakeLists.txt
  2. 0
      legacy/Cargo.lock
  3. 0
      legacy/Cargo.toml
  4. 15
      src/core_ffi/CMakeLists.txt
  5. 1
      src/core_ffi/rust_ffi/.gitignore
  6. 224
      src/core_ffi/rust_ffi/Cargo.lock
  7. 12
      src/core_ffi/rust_ffi/Cargo.toml
  8. 8
      src/core_ffi/rust_ffi/build.rs
  9. 18
      src/core_ffi/rust_ffi/src/common_code.rs
  10. 8
      src/core_ffi/rust_ffi/src/demo.cc
  11. 5
      src/core_ffi/rust_ffi/src/klotski.h
  12. 12
      src/core_ffi/rust_ffi/src/main.rs

1
CMakeLists.txt

@ -10,6 +10,7 @@ option(KLSK_ENABLE_BENCHMARK "Enable benchmark of the klotski project." ON)
option(KLSK_C_FFI "" ON) option(KLSK_C_FFI "" ON)
option(KLSK_PYTHON_FFI "" ON) option(KLSK_PYTHON_FFI "" ON)
option(KLSK_RUST_FFI "" ON)
# ------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------ #

0
src/Cargo.lock → legacy/Cargo.lock

0
src/Cargo.toml → legacy/Cargo.toml

15
src/core_ffi/CMakeLists.txt

@ -12,11 +12,20 @@ endif()
if (KLSK_PYTHON_FFI) if (KLSK_PYTHON_FFI)
pybind11_add_module(klotski_py pybind11_add_module(klotski_py
py_ffi/binder.cc py_ffi/binder.cc
py_ffi/codec/short_code.cc py_ffi/codec/short_code.cc
py_ffi/codec/common_codec.cc py_ffi/codec/common_codec.cc
) )
target_include_directories(klotski_py PRIVATE py_ffi/include) target_include_directories(klotski_py PRIVATE py_ffi/include)
target_link_libraries(klotski_py PRIVATE klotski::core) target_link_libraries(klotski_py PRIVATE klotski::core)
set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski) set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski)
endif() endif()
if (KLSK_RUST_FFI)
# just for IDE highlight
add_library(klotski_rust
rust_ffi/src/demo.cc
rust_ffi/target/cxxbridge/klotski/src/common_code.rs.cc
)
target_include_directories(klotski_rust PRIVATE rust_ffi/target/cxxbridge)
endif()

1
src/core_ffi/rust_ffi/.gitignore

@ -0,0 +1 @@
/target/

224
src/core_ffi/rust_ffi/Cargo.lock

@ -0,0 +1,224 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cc"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907d8581360765417f8f2e0e7d602733bbed60156b4465b7617243689ef9b83d"
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]]
name = "cxx"
version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "273dcfd3acd4e1e276af13ed2a43eea7001318823e7a726a6b3ed39b4acc0b82"
dependencies = [
"cc",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
]
[[package]]
name = "cxx-build"
version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b2766fbd92be34e9ed143898fce6c572dc009de39506ed6903e5a05b68914e"
dependencies = [
"cc",
"codespan-reporting",
"once_cell",
"proc-macro2",
"quote",
"scratch",
"syn",
]
[[package]]
name = "cxxbridge-flags"
version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "839fcd5e43464614ffaa989eaf1c139ef1f0c51672a1ed08023307fa1b909ccd"
[[package]]
name = "cxxbridge-macro"
version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b2c1c1776b986979be68bb2285da855f8d8a35851a769fca8740df7c3d07877"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "klotski"
version = "0.0.1"
dependencies = [
"cxx",
"cxx-build",
]
[[package]]
name = "link-cplusplus"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d240c6f7e1ba3a28b0249f774e6a9dd0175054b52dfbb61b16eb8505c3785c9"
dependencies = [
"cc",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "scratch"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3cf7c11c38cb994f3d40e8a8cde3bbd1f72a435e4c49e85d6553d8312306152"
[[package]]
name = "syn"
version = "2.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-width"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
[[package]]
name = "winapi-util"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

12
src/core_ffi/rust_ffi/Cargo.toml

@ -0,0 +1,12 @@
[package]
name = "klotski"
version = "0.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cxx = "1.0.124"
[build-dependencies]
cxx-build = "1.0.124"

8
src/core_ffi/rust_ffi/build.rs

@ -0,0 +1,8 @@
fn main() {
cxx_build::bridge("src/common_code.rs")
.file("src/demo.cc")
.compile("klotski");
println!("cargo:rerun-if-changed=src/demo.cc");
println!("cargo:rerun-if-changed=src/common_code.rs");
}

18
src/core_ffi/rust_ffi/src/common_code.rs

@ -0,0 +1,18 @@
#[cxx::bridge]
pub mod ffi {
#[derive(Debug)]
pub struct CommonCode {
code: u64
}
extern "Rust" {}
unsafe extern "C++" {
include!("klotski/src/klotski.h");
// fn unwrap(self: &CommonCode) -> u64;
// fn from_string(s: &str) -> CommonCode;
fn from_string() -> CommonCode;
}
}

8
src/core_ffi/rust_ffi/src/demo.cc

@ -0,0 +1,8 @@
#include "klotski/src/klotski.h"
#include "klotski/src/common_code.rs.h"
#include <iostream>
CommonCode from_string() {
return CommonCode {};
}

5
src/core_ffi/rust_ffi/src/klotski.h

@ -0,0 +1,5 @@
#pragma once
#include "klotski/src/common_code.rs.h"
CommonCode from_string();

12
src/core_ffi/rust_ffi/src/main.rs

@ -0,0 +1,12 @@
mod common_code;
use common_code::ffi::CommonCode;
fn main() {
// let code = CommonCode { code: 123 };
// let val = code.unwrap();
// println!("val = {}", val);
let code = common_code::ffi::from_string();
println!("code = {:?}", code);
}
Loading…
Cancel
Save