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

Exposing a rust library to C code #55

Closed
SirVer opened this issue Sep 19, 2017 · 8 comments
Closed

Exposing a rust library to C code #55

SirVer opened this issue Sep 19, 2017 · 8 comments

Comments

@SirVer
Copy link

SirVer commented Sep 19, 2017

My use case is to swap out unsafe C with save rust implementations. To keep APIs in tact, I need to provide a C library that bridges to Rust. I was trying to do this:

package(default_visibility = ["//visibility:public"])

load(
    "@io_bazel_rules_rust//rust:rust.bzl",
    "rust_library",
)

rust_library(
    name = "greeter",
    srcs = ["lib.rs"],
)

cc_binary(
   name = "rust_from_c",
   srcs = ["hello_world.cc"],
   deps = [":greeter"],
)

with lib.rs:

#[no_mangle]
pub extern "C" fn greet(){
    println!("Hello from rust!");
}

and hello_world.cc:

void greet();
int main(int argc, char *argv[])
{
	greet();
	return 0;
}

This should conceptually work, but bazel refuses:

ERROR: /usr/src/app/rust_from_c/BUILD:18:14: in deps attribute of cc_library rule //rust_from_c:greeter: rust_library rule '//rust_from_c:greeter_rust' is misplaced here (expected cc_inc_library, cc_library, objc_library or cc_proto_library)

Is there a way to call rust code from C?

@SirVer
Copy link
Author

SirVer commented Sep 19, 2017

@acmcarther I think you know most about these rules, right?

@acmcarther
Copy link
Collaborator

acmcarther commented Sep 19, 2017

From my initial research:

https://doc.rust-lang.org/reference/linkage.html
You either want "staticlib" or "cdylib". Specifying crate types is enabled in #38, which was historically blocked on CI issues (I need to fix the PR urgently).

Once that is done, it is possible to pass this file output into the srcs of a cc_library or cc_binary rule. See the bazel docs for cc_library#srcs: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.

This may not work (either because the providers from the rust_library aren't quite right, or because cc_library#srcs requires inputs to be statically known and not generated via another rule). In that case, we'd want to consider adding a cc_library_from_rust_library rule that reshapes the providers yielded by rust_library into a form that cc_library will accept as a dep.

EDIT: Clarified last sentence to indicate that we'd use the modified dependency as a "dep" not a "src".

@SirVer
Copy link
Author

SirVer commented Sep 19, 2017

Thank you! So this is indeed blocked on #38. Also, having to state a rust_library as a src for a cc_library seems odd - from the semantics, dep would be the better keyword. But if src works I'll take it.

@mfarrugi
Copy link
Collaborator

Added an example of this here c_calling_rust, can tidy it up and create a pr after #38

Can't currently invoke the cc_library rule from rust_library like a macro can, so we could add a macro that calls rust_library and then `cc_library as in the example or figure out what it would take for cc_* rules to be more flexible in accepting native dependencies.

@SirVer
Copy link
Author

SirVer commented Feb 14, 2018

awesome!

/cc @thomasschiwietz

@mfarrugi
Copy link
Collaborator

mfarrugi commented Mar 7, 2018

This is now possible and an example is pushed.
Dylibs are a little trickier but support for them on linux is pending.

@thomasschiwietz
Copy link

very cool!

@mfarrugi
Copy link
Collaborator

mfarrugi commented Jun 7, 2018

Resolved by #74

@mfarrugi mfarrugi closed this as completed Jun 7, 2018
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

No branches or pull requests

4 participants