Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cxx: implement slice support for nostd #1123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

twilfredo
Copy link

@twilfredo twilfredo commented Nov 3, 2022

OVERVIEW:

This patch adds experimental support for us to use slices in a nostd/freestanding environment, aim is to run c++ <=> cxx-rs <=> rust on a baremental target environment and use slices, with the intention of supporting more features later.

CFG

In the application Cargo.toml file, the following feature cfgs are required:

[dependencies]
cxx = { ... , default-features = false }

[build-dependencies]
cxx-build = { ... , default-features = false }

Then build with:

RUSTFLAGS='--cfg cxx_experimental_no_alloc' cargo build

Build Notes

and in the particular .cpp file you may need to declare the following

void __assert_func (const char *__a, int, const char *__b, const char *__c) {
	while (true) {}
}

void *__gxx_personality_v0;

FUNCTIONALITY:

Currently, this only supports slices (outside of trivial features).

TESTING:

Tested by compiling:

  • cargo test (run existing tests)

  • cxx/demo/ running it.

  • compiling with a arm toolchain setup with cmake/corrosion and running the binary on QEMU arm with basic logic assertions [1].

Current testing has been done in the direction of C++ -> Rust with a simple callback test to C++. A
simple test setup can be seen here [2].

TODO:

  • Get features such as Results<Ok, Err> working.

  • When we build for the none std case (no default-features), instead of creating a symlink to the original cxx.h file, we copy it over and define the macro to disable stdlib dependencies. Perhaps there's a better way to do this?

NOTES:

By default, all the standard features are enabled gaurded by the #ifndef CXXBRIDGE1_RUST_STD, so this shoudn't break anything.

[1] https://github.com/twilfredo/qemu_misc/tree/master/bm_arm
[2] https://github.com/twilfredo/qemu_misc/blob/master/bm_arm/main.cpp

Signed-off-by: Wilfred Mallawa wilfred.mallawa@wdc.com

OVERVIEW:
This patch adds experimental support for us to use `slices` in a nostd/freestanding environment, aim is to run
c++ <=> cxx-rs <=> rust on a baremental target environment and use
slices, with the intention of supporting more features later.

In the application `Cargo.toml` file, the following feature cfgs are required:

```
[dependencies]
cxx = { ... , default-features = false }

[build-dependencies]
cxx-build = { ... , default-features = false }
```

Then build with:
```
RUSTFLAGS='--cfg cxx_experimental_no_alloc' cargo build
```

and in the particular `.cpp` file you may need to declare the following

```
void __assert_func (const char *__a, int, const char *__b, const char *__c) {
	while (true) {}
}

void *__gxx_personality_v0;
```

FUNCTIONALITY:
Currently, this only supports slices (outside of trivial features).

TESTING:
Tested by compiling:
	- cargo test (run existing tests)
	- cxx/demo/ running it.
	- compiling with a arm toolchain setup with cmake/corrosion
	  and running the binary on QEMU arm with basic logic assertions [1].

Current testing has been done in the direction of C++ -> Rust with a
simple callback test to C++. A
simple test setup can be seen here [2].

TODO:
- Get features such as `Results<Ok, Err>` working.

- When we build for the none `std` case (no `default-features`), instead of creating a symlink
     to the original `cxx.h` file, we copy it over and define the macro to
     disable stdlib dependencies. Perhaps there's a better way to do this?

NOTES:
By default, all the standard features are enabled gaurded by the
`#ifndef CXXBRIDGE1_RUST_STD`, so this **shoudn't** break anything.

[1] https://github.com/twilfredo/qemu_misc/tree/master/bm_arm
[2] https://github.com/twilfredo/qemu_misc/blob/master/bm_arm/main.cpp

Signed-off-by: Wilfred Mallawa <wilfred.mallawa@wdc.com>
@twilfredo
Copy link
Author

twilfredo commented Nov 29, 2022

@dtolnay ping! what are your thoughts on this?

@twilfredo
Copy link
Author

ping!

@GregoryTravis
Copy link

I would love to use this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants