From bd99dfa4ae0179b7683065a49a03287f5cd9657a Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 18 Apr 2022 17:49:14 -0300 Subject: [PATCH] Testing how CXX works (and how to integrate with C++ code) --- cxxtest/Cargo.lock | 173 ++++++++++++++++++++++++ cxxtest/Cargo.toml | 12 ++ cxxtest/build.rs | 5 + cxxtest/src/external/aclass.cpp | 12 ++ cxxtest/src/external/headers/aclass.hpp | 19 +++ cxxtest/src/main.rs | 17 +++ 6 files changed, 238 insertions(+) create mode 100644 cxxtest/Cargo.lock create mode 100644 cxxtest/Cargo.toml create mode 100644 cxxtest/build.rs create mode 100644 cxxtest/src/external/aclass.cpp create mode 100644 cxxtest/src/external/headers/aclass.hpp create mode 100644 cxxtest/src/main.rs diff --git a/cxxtest/Cargo.lock b/cxxtest/Cargo.lock new file mode 100644 index 0000000..4ae959d --- /dev/null +++ b/cxxtest/Cargo.lock @@ -0,0 +1,173 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + +[[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.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2295fe8865279f404147e9b2328e5af0ad11a2c016e58c13acfd48a07d8a55" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0aaaa055d4908326f1b4524b23ae53758019b806c0c4f382ea240982e9766b26" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a670224c6686471df12560a0b97a08145082e70bd38e2b0b5383b79e46c3da7" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b700096ca0dece28d9535fdb17ab784a8ae155d7f29d39c273643e6292c9620" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "cxxtest" +version = "0.1.0" +dependencies = [ + "cxx", + "cxx-build", +] + +[[package]] +name = "link-cplusplus" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8cae2cd7ba2f3f63938b9c724475dfb7b9861b545a90324476324ed21dbc8c8" +dependencies = [ + "cc", +] + +[[package]] +name = "once_cell" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" + +[[package]] +name = "proc-macro2" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "scratch" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96311ef4a16462c757bb6a39152c40f58f31cd2602a40fceb937e2bc34e6cbab" + +[[package]] +name = "syn" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/cxxtest/Cargo.toml b/cxxtest/Cargo.toml new file mode 100644 index 0000000..d75e815 --- /dev/null +++ b/cxxtest/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "cxxtest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cxx = "1.0.66" + +[build-dependencies] +cxx-build = "1.0.66" diff --git a/cxxtest/build.rs b/cxxtest/build.rs new file mode 100644 index 0000000..4103028 --- /dev/null +++ b/cxxtest/build.rs @@ -0,0 +1,5 @@ +fn main() { + cxx_build::bridge("src/main.rs") + .file("src/external/aclass.cpp") + .compile("cxxtest"); +} diff --git a/cxxtest/src/external/aclass.cpp b/cxxtest/src/external/aclass.cpp new file mode 100644 index 0000000..b9cd459 --- /dev/null +++ b/cxxtest/src/external/aclass.cpp @@ -0,0 +1,12 @@ +#include "headers/aclass.hpp" +#include + +std::unique_ptr new_aclass(uint32_t v) { + return std::unique_ptr(new AClass(v)); +} + +AClass::AClass(uint32_t p_value) : a_value(p_value) {} + +void AClass::process() const { + std::cout << "Processing " << this->a_value << std::endl; +} diff --git a/cxxtest/src/external/headers/aclass.hpp b/cxxtest/src/external/headers/aclass.hpp new file mode 100644 index 0000000..86c5989 --- /dev/null +++ b/cxxtest/src/external/headers/aclass.hpp @@ -0,0 +1,19 @@ +#ifndef ACLASS_HPP +#define ACLASS_HPP + +#include +#include + +class AClass { +private: + uint32_t a_value; + +public: + AClass(uint32_t p_value); + + void process() const; +}; + +std::unique_ptr new_aclass(uint32_t v); + +#endif diff --git a/cxxtest/src/main.rs b/cxxtest/src/main.rs new file mode 100644 index 0000000..496f8ab --- /dev/null +++ b/cxxtest/src/main.rs @@ -0,0 +1,17 @@ +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("cxxtest/src/external/headers/aclass.hpp"); + + type AClass; + + fn new_aclass(value: u32) -> UniquePtr; + fn process(&self); + } +} + +fn main() { + println!("Hello, world!"); + let aclass = ffi::new_aclass(12); + aclass.process(); +}