Skip to content

Commit

Permalink
Project structure update
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Sep 1, 2022
1 parent 2baf376 commit cb2e70e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 122 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
src/cxxbridge_code/target
build

examples/src/cxxbridge_code/target
examples/build
87 changes: 28 additions & 59 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,72 +1,39 @@
# Example of using [CXX](https://cxx.rs/) and [Corrosion](https://github.com/corrosion-rs/corrosion) in CMake project to call Rust functions from C++
# CMake file for using [CXX](https://cxx.rs/) and [Corrosion](https://github.com/corrosion-rs/corrosion) in CMake project to call Rust functions from C++

This is a fork of the [rusty_cmake](https://github.com/trondhe/rusty_cmake) repository to use CXX and Corrosion in a CMake project to use CXX and Corrosion in a CMake project with the following modifications:
This is a fork of the [rusty_cmake](https://github.com/trondhe/rusty_cmake) repository to use CXX and Corrosion in a CMake project with the following modifications:

### `cxx_corrosion.cmake` file:
### `cxx_corrosion.cmake` file:
- Windows-related code is removed (Linux and MacOS are supported) (Linux and MacOS are supported).
- Windows-related code is removed (Linux and MacOS are supported) (Linux and MacOS are supported).
- Simplify by using a simple one CMakeLists file for the project.

### Code examples to call Rust functions from C++:
- [Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns.
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) in return and [Anyhow](https://docs.rs/anyhow/latest/anyhow/).
- [Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust + C++.
### Code examples to call Rust functions from C++:
- [Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns.
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) in return and [Anyhow](https://docs.rs/anyhow/latest/anyhow/).
- [Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust + C++.
- Function to get STEM is removed in a favor of one-line solution;
- Simplify by using a simple one CMakeLists file for the project;
- Windows-related code is removed (Linux and MacOS are supported);
- Minor code changes to simplify and use a modern C++.

# Why?

This repo can be used as a sandbox playground for testing simple ideas of the Rust library integration in the current C++ CMake project and as examples of the usage.

# Usage

## Dependencies
- Linux or MacOS
- CMake
- Clang
## CMake

Run CMake and build commands in a project's root:

`cmake -B build . && make -C build -j4`.
This `cmake/corrosion_cxx.cmake` can be imported in any CMake project to simplify Rust code usage in C++.

Then you can run the example app by calling:
Current solution is a battle-tested and `cmake/corrosion_cxx.cmake` file is used in a [Comm](https://comm.app) application development.

`build/cxx_cmake`.
# Examples

## Nix
Example code is located in `examples` folder.

If you are using [Nix](https://nixos.org/download.html) as a development environment you can use a nix shell with all dependencies:

```
nix-shell -p cmake -p clang -p rustup -p libiconv -p git --pure
```
This repo example can be used as a sandbox playground for testing simple ideas of the Rust library integration in the current C++ CMake project and as examples of the usage.

And then run build commands from CMake above.

## Docker

There is a Docker file to build and run the example app in Docker container.
To build it run the build command from the project's root directory:

```
docker build . -t cxx-corrosion-cmake
```

Then you can run the example app by calling:
### Code examples to call Rust functions from C++:
- [Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns;
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) and [Anyhow](https://docs.rs/anyhow/latest/anyhow/) in return to C++;
- [Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust when calling from C++.

```
docker run cxx-corrosion-cmake
```
## Dependencies
- Linux or MacOS
- CMake
- Clang
- [Corrosion](https://github.com/corrosion-rs/corrosion#installation)
# Todo
- Rustup
## CMake
- [ ] Make it vcpkg package
- [ ] Make it nix package
Run CMake and build commands in a project's root:

Run CMake and build commands in the `examples` folder:

`cmake -B build . && make -C build -j4`.

Expand All @@ -82,12 +49,12 @@ If you are using [Nix](https://nixos.org/download.html) as a development environ
nix-shell -p cmake -p clang -p rustup -p libiconv -p git --pure
```

And then run build commands from CMake above.
And then run build commands from CMake section above.

## Docker

There is a Docker file to build and run the example app in Docker container.
To build it run the build command from the project's root directory:
To build it run the build command from the `examples` folder:

```
docker build . -t cxx-corrosion-cmake
Expand All @@ -101,5 +68,7 @@ docker run cxx-corrosion-cmake

# Todo

- [ ] Make it vcpkg package
- [ ] Make it nix package
Create a packages in package managers to help tracking updates in the project instead of manually track the changes.

- Make it nix package
- Make it vcpkg package
2 changes: 1 addition & 1 deletion CMakeLists.txt → examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ project(cxx_cmake CXX)
set(CMAKE_CXX_STANDARD 17)

find_package(Corrosion REQUIRED)
include(cmake/corrosion_cxx.cmake)
include(../cmake/corrosion_cxx.cmake)

add_executable(${PROJECT_NAME})

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions examples/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "cxxbridge_code/src/lib.rs.h"
#include <iostream>
#include <string>
#include "rust/cxx.h"

int main()
{
// Primitive types:
std::cout << "[1] A value given via generateb cxxbridge for lib_cxxbridge_bool: "
<< lib_cxxbridge_bool(true) << std::endl;
std::cout << "[2] A value given via generated cxxbridge for lib_cxxbridge_integer: "
<< lib_cxxbridge_integer(86) << std::endl;
const rust::String testString = "Max";
std::cout << "[3] A value given via generated cxxbridge for lib_cxxbridge_string: "
<< lib_cxxbridge_string(testString) << std::endl;

// Return Rust `Result` with Ok:
std::cout << "[4] A Rust `Result Ok` value given via generated cxxbridge for lib_cxxbridge_return_result_ok:" << std::endl
<< lib_cxxbridge_return_result_ok() << std::endl;

// Return Rust `Result` with Error:
try
{
std::cout << "[5] A Rust `Result Error` value given via generated cxxbridge for lib_cxxbridge_return_result_error:" << std::endl
<< lib_cxxbridge_return_result_error() << std::endl;
}
catch (rust::Error e)
{
std::cerr << "Got an error from Rust function `Result`:" << std::endl;
std::cerr << e.what() << std::endl;
}

// Panic in a function call:
std::cout << "[6] Testing a case of panic by running lib_cxxbridge_panicked_function:" << std::endl;
std::cout << lib_cxxbridge_panicked_function();

return 0;
}
59 changes: 0 additions & 59 deletions src/main.cpp

This file was deleted.

0 comments on commit cb2e70e

Please sign in to comment.