blob: 4dbc97ae519a9ce07a4f96a1184be853caffcdfc [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001//! This library provides a **safe** mechanism for calling C++ code from Rust
2//! and Rust code from C++, not subject to the many ways that things can go
3//! wrong when using bindgen or cbindgen to generate unsafe C-style bindings.
4//!
David Tolnayccd39752020-01-08 09:33:51 -08005//! This doesn't change the fact that 100% of C++ code is unsafe. When auditing
6//! a project, you would be on the hook for auditing all the unsafe Rust code
7//! and *all* the C++ code. The core safety claim under this new model is that
8//! auditing just the C++ side would be sufficient to catch all problems, i.e.
9//! the Rust side can be 100% safe.
10//!
David Tolnay7db73692019-10-20 14:51:12 -040011//! <br>
12//!
David Tolnayb606ce32020-03-16 01:16:16 -070013//! *Compiler support: requires rustc 1.42+*
David Tolnay7db73692019-10-20 14:51:12 -040014//!
15//! <br>
16//!
17//! # Overview
18//!
19//! The idea is that we define the signatures of both sides of our FFI boundary
20//! embedded together in one Rust module (the next section shows an example).
21//! From this, CXX receives a complete picture of the boundary to perform static
22//! analyses against the types and function signatures to uphold both Rust's and
23//! C++'s invariants and requirements.
24//!
25//! If everything checks out statically, then CXX uses a pair of code generators
26//! to emit the relevant `extern "C"` signatures on both sides together with any
27//! necessary static assertions for later in the build process to verify
28//! correctness. On the Rust side this code generator is simply an attribute
29//! procedural macro. On the C++ side it can be a small Cargo build script if
30//! your build is managed by Cargo, or for other build systems like Bazel or
31//! Buck we provide a command line tool which generates the header and source
32//! file and should be easy to integrate.
33//!
34//! The resulting FFI bridge operates at zero or negligible overhead, i.e. no
35//! copying, no serialization, no memory allocation, no runtime checks needed.
36//!
37//! The FFI signatures are able to use native types from whichever side they
38//! please, such as Rust's `String` or C++'s `std::string`, Rust's `Box` or
39//! C++'s `std::unique_ptr`, Rust's `Vec` or C++'s `std::vector`, etc in any
40//! combination. CXX guarantees an ABI-compatible signature that both sides
41//! understand, based on builtin bindings for key standard library types to
42//! expose an idiomatic API on those types to the other language. For example
43//! when manipulating a C++ string from Rust, its `len()` method becomes a call
44//! of the `size()` member function defined by C++; when manipulation a Rust
45//! string from C++, its `size()` member function calls Rust's `len()`.
46//!
47//! <br>
48//!
49//! # Example
50//!
51//! A runnable version of this example is provided under the *demo-rs* directory
David Tolnayd763f182020-03-12 00:50:19 -070052//! of [https://github.com/dtolnay/cxx] (with the C++ side of the implementation
David Tolnay7db73692019-10-20 14:51:12 -040053//! in the *demo-cxx* directory). To try it out, jump into demo-rs and run
54//! `cargo run`.
55//!
56//! ```no_run
57//! #[cxx::bridge]
58//! mod ffi {
59//! // Any shared structs, whose fields will be visible to both languages.
60//! struct SharedThing {
61//! z: i32,
62//! y: Box<ThingR>,
63//! x: UniquePtr<ThingC>,
64//! }
65//!
66//! extern "C" {
67//! // One or more headers with the matching C++ declarations. Our code
68//! // generators don't read it but it gets #include'd and used in static
69//! // assertions to ensure our picture of the FFI boundary is accurate.
70//! include!("demo-cxx/demo.h");
71//!
72//! // Zero or more opaque types which both languages can pass around but
73//! // only C++ can see the fields.
74//! type ThingC;
75//!
76//! // Functions implemented in C++.
77//! fn make_demo(appname: &str) -> UniquePtr<ThingC>;
78//! fn get_name(thing: &ThingC) -> &CxxString;
79//! fn do_thing(state: SharedThing);
80//! }
81//!
82//! extern "Rust" {
83//! // Zero or more opaque types which both languages can pass around but
84//! // only Rust can see the fields.
85//! type ThingR;
86//!
87//! // Functions implemented in Rust.
88//! fn print_r(r: &ThingR);
89//! }
90//! }
91//! #
92//! # pub struct ThingR(usize);
93//! #
94//! # fn print_r(r: &ThingR) {
95//! # println!("called back with r={}", r.0);
96//! # }
97//! #
98//! # fn main() {}
99//! ```
100//!
101//! Now we simply provide C++ definitions of all the things in the `extern "C"`
102//! block and Rust definitions of all the things in the `extern "Rust"` block,
103//! and get to call back and forth safely.
104//!
105//! Here are links to the complete set of source files involved in the demo:
106//!
107//! - [demo-rs/src/main.rs](https://github.com/dtolnay/cxx/blob/master/demo-rs/src/main.rs)
108//! - [demo-rs/build.rs](https://github.com/dtolnay/cxx/blob/master/demo-rs/build.rs)
109//! - [demo-cxx/demo.h](https://github.com/dtolnay/cxx/blob/master/demo-cxx/demo.h)
110//! - [demo-cxx/demo.cc](https://github.com/dtolnay/cxx/blob/master/demo-cxx/demo.cc)
111//!
112//! To look at the code generated in both languages for the example by the CXX
113//! code generators:
114//!
115//! ```console
116//! # run Rust code generator and print to stdout
117//! # (requires https://github.com/dtolnay/cargo-expand)
118//! $ cargo expand --manifest-path demo-rs/Cargo.toml
119//!
120//! # run C++ code generator and print to stdout
121//! $ cargo run --manifest-path cmd/Cargo.toml -- demo-rs/src/main.rs
122//! ```
123//!
124//! <br>
125//!
126//! # Details
127//!
128//! As seen in the example, the language of the FFI boundary involves 3 kinds of
129//! items:
130//!
131//! - **Shared structs** &mdash; their fields are made visible to both
132//! languages. The definition written within cxx::bridge is the single source
133//! of truth.
134//!
135//! - **Opaque types** &mdash; their fields are secret from the other language.
136//! These cannot be passed across the FFI by value but only behind an
137//! indirection, such as a reference `&`, a Rust `Box`, or a `UniquePtr`. Can
138//! be a type alias for an arbitrarily complicated generic language-specific
139//! type depending on your use case.
140//!
141//! - **Functions** &mdash; implemented in either language, callable from the
142//! other language.
143//!
144//! Within the `extern "C"` part of the CXX bridge we list the types and
145//! functions for which C++ is the source of truth, as well as the header(s)
146//! that declare those APIs. In the future it's possible that this section could
147//! be generated bindgen-style from the headers but for now we need the
148//! signatures written out; static assertions will verify that they are
149//! accurate.
150//!
151//! Within the `extern "Rust"` part, we list types and functions for which Rust
152//! is the source of truth. These all implicitly refer to the `super` module,
153//! the parent module of the CXX bridge. You can think of the two items listed
154//! in the example above as being like `use super::ThingR` and `use
155//! super::print_r` except re-exported to C++. The parent module will either
156//! contain the definitions directly for simple things, or contain the relevant
157//! `use` statements to bring them into scope from elsewhere.
158//!
159//! Your function implementations themselves, whether in C++ or Rust, *do not*
160//! need to be defined as `extern "C"` ABI or no\_mangle. CXX will put in the
161//! right shims where necessary to make it all work.
162//!
163//! <br>
164//!
165//! # Comparison vs bindgen and cbindgen
166//!
167//! Notice that with CXX there is repetition of all the function signatures:
168//! they are typed out once where the implementation is defined (in C++ or Rust)
169//! and again inside the cxx::bridge module, though compile-time assertions
170//! guarantee these are kept in sync. This is different from [bindgen] and
171//! [cbindgen] where function signatures are typed by a human once and the tool
172//! consumes them in one language and emits them in the other language.
173//!
174//! [bindgen]: https://github.com/rust-lang/rust-bindgen
175//! [cbindgen]: https://github.com/eqrion/cbindgen/
176//!
177//! This is because CXX fills a somewhat different role. It is a lower level
178//! tool than bindgen or cbindgen in a sense; you can think of it as being a
179//! replacement for the concept of `extern "C"` signatures as we know them,
180//! rather than a replacement for a bindgen. It would be reasonable to build a
181//! higher level bindgen-like tool on top of CXX which consumes a C++ header
182//! and/or Rust module (and/or IDL like Thrift) as source of truth and generates
183//! the cxx::bridge, eliminating the repetition while leveraging the static
184//! analysis safety guarantees of CXX.
185//!
186//! But note in other ways CXX is higher level than the bindgens, with rich
187//! support for common standard library types. Frequently with bindgen when we
188//! are dealing with an idiomatic C++ API we would end up manually wrapping that
189//! API in C-style raw pointer functions, applying bindgen to get unsafe raw
190//! pointer Rust functions, and replicating the API again to expose those
191//! idiomatically in Rust. That's a much worse form of repetition because it is
192//! unsafe all the way through.
193//!
194//! By using a CXX bridge as the shared understanding between the languages,
195//! rather than `extern "C"` C-style signatures as the shared understanding,
196//! common FFI use cases become expressible using 100% safe code.
197//!
198//! It would also be reasonable to mix and match, using CXX bridge for the 95%
199//! of your FFI that is straightforward and doing the remaining few oddball
200//! signatures the old fashioned way with bindgen and cbindgen, if for some
201//! reason CXX's static restrictions get in the way. Please file an issue if you
202//! end up taking this approach so that we know what ways it would be worthwhile
203//! to make the tool more expressive.
204//!
205//! <br>
206//!
207//! # Cargo-based setup
208//!
209//! For builds that are orchestrated by Cargo, you will use a build script that
210//! runs CXX's C++ code generator and compiles the resulting C++ code along with
211//! any other C++ code for your crate.
212//!
213//! The canonical build script is as follows. The indicated line returns a
214//! [`cc::Build`] instance (from the usual widely used `cc` crate) on which you
215//! can set up any additional source files and compiler flags as normal.
216//!
217//! [`cc::Build`]: https://docs.rs/cc/1.0/cc/struct.Build.html
218//!
219//! ```no_run
220//! // build.rs
221//!
222//! fn main() {
223//! cxx::Build::new()
224//! .bridge("src/main.rs") // returns a cc::Build
225//! .file("../demo-cxx/demo.cc")
226//! .flag("-std=c++11")
227//! .compile("cxxbridge-demo");
228//!
229//! println!("cargo:rerun-if-changed=src/main.rs");
230//! println!("cargo:rerun-if-changed=../demo-cxx/demo.h");
231//! println!("cargo:rerun-if-changed=../demo-cxx/demo.cc");
232//! }
233//! ```
234//!
235//! <br><br>
236//!
237//! # Non-Cargo setup
238//!
239//! For use in non-Cargo builds like Bazel or Buck, CXX provides an alternate
240//! way of invoking the C++ code generator as a standalone command line tool.
241//! The tool is packaged as the `cxxbridge-cmd` crate on crates.io or can be
David Tolnayd763f182020-03-12 00:50:19 -0700242//! built from the *cmd* directory of [https://github.com/dtolnay/cxx].
David Tolnay7db73692019-10-20 14:51:12 -0400243//!
244//! ```bash
245//! $ cargo install cxxbridge-cmd
246//!
247//! $ cxxbridge src/main.rs --header > path/to/mybridge.h
248//! $ cxxbridge src/main.rs > path/to/mybridge.cc
249//! ```
250//!
251//! <br>
252//!
253//! # Safety
254//!
255//! Be aware that the design of this library is intentionally restrictive and
256//! opinionated! It isn't a goal to be powerful enough to handle arbitrary
257//! signatures in either language. Instead this project is about carving out a
258//! reasonably expressive set of functionality about which we can make useful
259//! safety guarantees today and maybe extend over time. You may find that it
260//! takes some practice to use CXX bridge effectively as it won't work in all
261//! the ways that you are used to.
262//!
263//! Some of the considerations that go into ensuring safety are:
264//!
265//! - By design, our paired code generators work together to control both sides
266//! of the FFI boundary. Ordinarily in Rust writing your own `extern "C"`
267//! blocks is unsafe because the Rust compiler has no way to know whether the
268//! signatures you've written actually match the signatures implemented in the
269//! other language. With CXX we achieve that visibility and know what's on the
270//! other side.
271//!
272//! - Our static analysis detects and prevents passing types by value that
273//! shouldn't be passed by value from C++ to Rust, for example because they
274//! may contain internal pointers that would be screwed up by Rust's move
275//! behavior.
276//!
277//! - To many people's surprise, it is possible to have a struct in Rust and a
278//! struct in C++ with exactly the same layout / fields / alignment /
279//! everything, and still not the same ABI when passed by value. This is a
280//! longstanding bindgen bug that leads to segfaults in absolutely
281//! correct-looking code ([rust-lang/rust-bindgen#778]). CXX knows about this
282//! and can insert the necessary zero-cost workaround transparently where
283//! needed, so go ahead and pass your structs by value without worries. This
284//! is made possible by owning both sides of the boundary rather than just
285//! one.
286//!
287//! - Template instantiations: for example in order to expose a UniquePtr\<T\>
288//! type in Rust backed by a real C++ unique\_ptr, we have a way of using a
289//! Rust trait to connect the behavior back to the template instantiations
290//! performed by the other language.
291//!
292//! [rust-lang/rust-bindgen#778]: https://github.com/rust-lang/rust-bindgen/issues/778
293//!
294//! <br>
295//!
296//! # Builtin types
297//!
David Tolnay559fbb32020-03-17 23:32:20 -0700298//! In addition to all the primitive types (i32 &lt;=&gt; int32_t), the
299//! following common types may be used in the fields of shared structs and the
300//! arguments and returns of functions.
David Tolnay7db73692019-10-20 14:51:12 -0400301//!
302//! <table>
303//! <tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr>
David Tolnay750755e2020-03-01 13:04:08 -0800304//! <tr><td>String</td><td>rust::String</td><td></td></tr>
305//! <tr><td>&amp;str</td><td>rust::Str</td><td></td></tr>
David Tolnayf51dc4d2020-03-12 00:45:30 -0700306//! <tr><td><a href="https://docs.rs/cxx/0.2/cxx/struct.CxxString.html">CxxString</a></td><td>std::string</td><td><sup><i>cannot be passed by value</i></sup></td></tr>
David Tolnay750755e2020-03-01 13:04:08 -0800307//! <tr><td>Box&lt;T&gt;</td><td>rust::Box&lt;T&gt;</td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
David Tolnayf51dc4d2020-03-12 00:45:30 -0700308//! <tr><td><a href="https://docs.rs/cxx/0.2/cxx/struct.UniquePtr.html">UniquePtr&lt;T&gt;</a></td><td>std::unique_ptr&lt;T&gt;</td><td><sup><i>cannot hold opaque Rust type</i></sup></td></tr>
David Tolnay559fbb32020-03-17 23:32:20 -0700309//! <tr><td>Result&lt;T&gt;</td><td>error &lt;=&gt; exception</td><td><sup><i>allowed as return type only</i></sup></td></tr>
David Tolnay7db73692019-10-20 14:51:12 -0400310//! </table>
311//!
David Tolnay736cbca2020-03-11 16:49:18 -0700312//! The C++ API of the `rust` namespace is defined by the *include/cxx.h* file
David Tolnayd763f182020-03-12 00:50:19 -0700313//! in [https://github.com/dtolnay/cxx]. You will need to include this header in
David Tolnay736cbca2020-03-11 16:49:18 -0700314//! your C++ code when working with those types.
David Tolnay7db73692019-10-20 14:51:12 -0400315//!
316//! The following types are intended to be supported "soon" but are just not
317//! implemented yet. I don't expect any of these to be hard to make work but
318//! it's a matter of designing a nice API for each in its non-native language.
319//!
320//! <table>
321//! <tr><th>name in Rust</th><th>name in C++</th></tr>
David Tolnay84f232e2020-01-08 12:22:56 -0800322//! <tr><td>&amp;[T]</td><td><sup><i>tbd</i></sup></td></tr>
323//! <tr><td>Vec&lt;T&gt;</td><td><sup><i>tbd</i></sup></td></tr>
324//! <tr><td>BTreeMap&lt;K, V&gt;</td><td><sup><i>tbd</i></sup></td></tr>
325//! <tr><td>HashMap&lt;K, V&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay239d05f2020-03-13 01:36:50 -0700326//! <tr><td>Arc&lt;T&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay84f232e2020-01-08 12:22:56 -0800327//! <tr><td><sup><i>tbd</i></sup></td><td>std::vector&lt;T&gt;</td></tr>
328//! <tr><td><sup><i>tbd</i></sup></td><td>std::map&lt;K, V&gt;</td></tr>
329//! <tr><td><sup><i>tbd</i></sup></td><td>std::unordered_map&lt;K, V&gt;</td></tr>
David Tolnay239d05f2020-03-13 01:36:50 -0700330//! <tr><td><sup><i>tbd</i></sup></td><td>std::shared_ptr&lt;T&gt;</td></tr>
David Tolnay7db73692019-10-20 14:51:12 -0400331//! </table>
David Tolnayd763f182020-03-12 00:50:19 -0700332//!
333//! [https://github.com/dtolnay/cxx]: https://github.com/dtolnay/cxx
David Tolnay7db73692019-10-20 14:51:12 -0400334
David Tolnaybfc8dce2020-03-12 00:48:04 -0700335#![doc(html_root_url = "https://docs.rs/cxx/0.1.2")]
David Tolnay7db73692019-10-20 14:51:12 -0400336#![deny(improper_ctypes)]
337#![allow(
David Tolnayd2bb3da2020-03-18 17:19:39 -0700338 clippy::declare_interior_mutable_const,
David Tolnay30d214c2020-03-15 23:54:34 -0700339 clippy::inherent_to_string,
David Tolnay7db73692019-10-20 14:51:12 -0400340 clippy::large_enum_variant,
341 clippy::missing_safety_doc,
342 clippy::module_inception,
David Tolnay30d214c2020-03-15 23:54:34 -0700343 clippy::needless_doctest_main,
David Tolnay7db73692019-10-20 14:51:12 -0400344 clippy::new_without_default,
345 clippy::or_fun_call,
346 clippy::ptr_arg,
347 clippy::toplevel_ref_arg,
David Tolnay7db73692019-10-20 14:51:12 -0400348 clippy::useless_let_if_seq
349)]
350
David Tolnayaf60e232020-01-24 15:22:09 -0800351extern crate link_cplusplus;
352
David Tolnayda5bd272020-03-16 21:53:22 -0700353#[macro_use]
354mod assert;
355
David Tolnay7db73692019-10-20 14:51:12 -0400356mod cxx_string;
357mod error;
David Tolnayebef4a22020-03-17 15:33:47 -0700358mod exception;
David Tolnay7db73692019-10-20 14:51:12 -0400359mod gen;
360mod opaque;
361mod paths;
David Tolnay486b6ec2020-03-17 01:19:57 -0700362mod result;
David Tolnay7db73692019-10-20 14:51:12 -0400363mod rust_str;
364mod rust_string;
365mod syntax;
366mod unique_ptr;
367mod unwind;
368
369pub use crate::cxx_string::CxxString;
David Tolnayebef4a22020-03-17 15:33:47 -0700370pub use crate::exception::Exception;
David Tolnay7db73692019-10-20 14:51:12 -0400371pub use crate::unique_ptr::UniquePtr;
372pub use cxxbridge_macro::bridge;
373
374// Not public API.
375#[doc(hidden)]
376pub mod private {
377 pub use crate::opaque::Opaque;
David Tolnay486b6ec2020-03-17 01:19:57 -0700378 pub use crate::result::{r#try, Result};
David Tolnay7db73692019-10-20 14:51:12 -0400379 pub use crate::rust_str::RustStr;
380 pub use crate::rust_string::RustString;
381 pub use crate::unique_ptr::UniquePtrTarget;
382 pub use crate::unwind::catch_unwind;
383}
384
385use crate::error::Result;
David Tolnay366ef8b2020-01-26 14:15:59 -0800386use anyhow::anyhow;
David Tolnay7db73692019-10-20 14:51:12 -0400387use std::fs;
388use std::io::{self, Write};
389use std::path::Path;
390use std::process;
391
392/// The CXX code generator for constructing and compiling C++ code.
393///
394/// This is intended to be used from Cargo build scripts to execute CXX's
395/// C++ code generator, set up any additional compiler flags depending on
396/// the use case, and make the C++ compiler invocation.
397///
398/// <br>
399///
400/// # Example
401///
402/// Example of a canonical Cargo build script that builds a CXX bridge:
403///
404/// ```no_run
405/// // build.rs
406///
407/// fn main() {
408/// cxx::Build::new()
409/// .bridge("src/main.rs")
410/// .file("../demo-cxx/demo.cc")
411/// .flag("-std=c++11")
412/// .compile("cxxbridge-demo");
413///
414/// println!("cargo:rerun-if-changed=src/main.rs");
415/// println!("cargo:rerun-if-changed=../demo-cxx/demo.h");
416/// println!("cargo:rerun-if-changed=../demo-cxx/demo.cc");
417/// }
418/// ```
419///
420/// A runnable working setup with this build script is shown in the
David Tolnayd763f182020-03-12 00:50:19 -0700421/// *demo-rs* and *demo-cxx* directories of [https://github.com/dtolnay/cxx].
422///
423/// [https://github.com/dtolnay/cxx]: https://github.com/dtolnay/cxx
David Tolnay7db73692019-10-20 14:51:12 -0400424///
425/// <br>
426///
427/// # Alternatives
428///
429/// For use in non-Cargo builds like Bazel or Buck, CXX provides an
430/// alternate way of invoking the C++ code generator as a standalone command
431/// line tool. The tool is packaged as the `cxxbridge-cmd` crate.
432///
433/// ```bash
434/// $ cargo install cxxbridge-cmd # or build it from the repo
435///
436/// $ cxxbridge src/main.rs --header > path/to/mybridge.h
437/// $ cxxbridge src/main.rs > path/to/mybridge.cc
438/// ```
439#[must_use]
440pub struct Build {
441 _private: (),
442}
443
444impl Build {
445 /// Begin with a [`cc::Build`] in its default configuration.
446 pub fn new() -> Self {
447 Build { _private: () }
448 }
449
450 /// This returns a [`cc::Build`] on which you should continue to set up
451 /// any additional source files or compiler flags, and lastly call its
452 /// [`compile`] method to execute the C++ build.
453 ///
454 /// [`compile`]: https://docs.rs/cc/1.0.49/cc/struct.Build.html#method.compile
455 #[must_use]
456 pub fn bridge(&self, rust_source_file: impl AsRef<Path>) -> cc::Build {
457 match try_generate_bridge(rust_source_file.as_ref()) {
458 Ok(build) => build,
459 Err(err) => {
David Tolnay366ef8b2020-01-26 14:15:59 -0800460 let _ = writeln!(io::stderr(), "\n\ncxxbridge error: {:?}\n\n", anyhow!(err));
David Tolnay7db73692019-10-20 14:51:12 -0400461 process::exit(1);
462 }
463 }
464 }
465}
466
467fn try_generate_bridge(rust_source_file: &Path) -> Result<cc::Build> {
468 let header = gen::do_generate_header(rust_source_file);
469 let header_path = paths::out_with_extension(rust_source_file, ".h")?;
470 fs::create_dir_all(header_path.parent().unwrap())?;
471 fs::write(&header_path, header)?;
472 paths::symlink_header(&header_path, rust_source_file);
473
474 let bridge = gen::do_generate_bridge(rust_source_file);
475 let bridge_path = paths::out_with_extension(rust_source_file, ".cc")?;
476 fs::write(&bridge_path, bridge)?;
477 let mut build = paths::cc_build();
478 build.file(&bridge_path);
479
David Tolnay736cbca2020-03-11 16:49:18 -0700480 let ref cxx_h = paths::include_dir()?.join("rust").join("cxx.h");
481 let _ = fs::create_dir_all(cxx_h.parent().unwrap());
482 let _ = fs::remove_file(cxx_h);
483 let _ = fs::write(cxx_h, gen::include::HEADER);
David Tolnayc43627a2020-01-28 00:50:25 -0800484
David Tolnay7db73692019-10-20 14:51:12 -0400485 Ok(build)
486}