-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This includes a fix in corrosion_add_cxxbridge, to force generation of the header files for all upstream dependencies, which is important in this circular setup.
- Loading branch information
1 parent
a601d6e
commit 44e7fc5
Showing
10 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# This CMake project tests the setup for circular dependencies that involve a CXX bridge | ||
# While circular dependencies are usually discouraged, with CXX they are reasonbly natural, | ||
# as ideally, Rust should be able to call C++ freely and vice-versa. | ||
# | ||
# The way this project is set up, the rust_lib target acts as the one target that includes | ||
# the mixed C++ and Rust code with the cpp_lib and cxxbridge targets as implementation details, | ||
# which shouldn't be used individually. | ||
cmake_minimum_required(VERSION 3.24) | ||
project(test_project VERSION 0.1.0 LANGUAGES CXX) | ||
include(../../test_header.cmake) | ||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_CXX_STANDARD_REQUIRED 1) | ||
|
||
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml) | ||
corrosion_add_cxxbridge(cxxbridge CRATE rust_lib FILES lib.rs) | ||
|
||
if(MSVC) | ||
set_target_properties(cxxbridge PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreadedDLL") | ||
endif() | ||
|
||
add_library(cpp_lib STATIC cpplib.cpp) | ||
target_include_directories(cpp_lib PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include") | ||
if(MSVC) | ||
set_target_properties(cpp_lib PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreadedDLL") | ||
endif() | ||
|
||
# Make sure the cpp library can access the headers from the bridge and vice-versa | ||
target_link_libraries(cpp_lib PRIVATE cxxbridge) | ||
target_link_libraries(cxxbridge PRIVATE cpp_lib) | ||
|
||
# The 3 libraries (rust, cpp, cxx) have a circular dependency, set this up in the linker | ||
# so that the rust_lib target links to everything and circular references are resolved. | ||
target_link_libraries(rust_lib INTERFACE | ||
"$<LINK_GROUP:RESCAN,cxxbridge,rust_lib-static,cpp_lib>" | ||
) | ||
|
||
add_executable(cpp_bin main.cpp) | ||
target_link_libraries(cpp_bin rust_lib) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "cpplib.h" | ||
#include "cxxbridge/lib.h" | ||
#include "rust/cxx.h" | ||
#include <iostream> | ||
|
||
RsImage read_image(rust::Str path) { | ||
std::cout << "read_image called" << std::endl; | ||
std::cout << path << std::endl; | ||
Rgba c = {1.0, 2.0, 3.0, 4.0}; | ||
RsImage v = {1, 1, c}; | ||
return v; | ||
} | ||
|
||
void assert_equality() { | ||
RsImage expected{.width = 1, | ||
.height = 1, | ||
.raster = Rgba{ | ||
.r = 1.0, | ||
.g = 2.0, | ||
.b = 3.0, | ||
.a = 4.0, | ||
}}; | ||
|
||
if (!expected.equal_to("dummy path")) { | ||
throw std::runtime_error("equality_check failed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
#include "cxxbridge/lib.h" | ||
|
||
::RsImage read_image(::rust::Str path); | ||
|
||
void assert_equality(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "cpplib.h" | ||
|
||
#include <iostream> | ||
|
||
int main(void) { | ||
std::cout << "Testing roundtrip..." << std::endl; | ||
|
||
try { | ||
assert_equality(); | ||
} catch (const std::exception &e) { | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
return 1; | ||
} | ||
|
||
std::cout << "Roundtrip successful!"; | ||
return 0; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "rust_lib" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[lib] | ||
name = "rust_lib" | ||
crate-type = ["staticlib"] | ||
|
||
[dependencies] | ||
cxx = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#[cxx::bridge] | ||
pub mod ffi { | ||
#[derive(Debug, PartialEq)] | ||
pub struct Rgba { | ||
r: f32, | ||
g: f32, | ||
b: f32, | ||
a: f32, | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct RsImage { | ||
width: usize, | ||
height: usize, | ||
raster: Rgba, | ||
} | ||
unsafe extern "C++" { | ||
include!("cpplib.h"); | ||
pub fn read_image(path: &str) -> RsImage; | ||
} | ||
|
||
extern "Rust" { | ||
pub fn equal_to(self: &RsImage, other: &str) -> bool; | ||
} | ||
} | ||
|
||
use ffi::*; | ||
|
||
impl RsImage { | ||
pub fn equal_to(&self, path: &str) -> bool { | ||
println!("equal_to"); | ||
*self == read_image(path) | ||
} | ||
} |