From c904f8a74f7a8c37e7dffab47a68433dba9ae4de Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Thu, 15 Oct 2020 21:10:55 -0700 Subject: [PATCH 1/2] Make cxx.h available to high level code generators. At present, this file is available only from the git repository, but higher level code generators may wish to ensure that they supply a version of cxx.h corresponding precisely to the version of cxx in use. Specifically, such higher level code generators may wish to use rust::Str and similar types in code which _they_ autogenerate, and thus need a way to include definitions of such types. As this code is autogenerated, it can't reasonably rummage around the cxx git repository to find the correct cxx.h header. To be even more specific, higher level code generators may wish to pass rust::Str and/or rust::String types into C++, in order to create UniquePtrs from Rust strings during function calls. --- gen/README.md | 3 +++ gen/lib/src/lib.rs | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/gen/README.md b/gen/README.md index 9786911b2..50adeca88 100644 --- a/gen/README.md +++ b/gen/README.md @@ -2,3 +2,6 @@ This directory contains CXX's C++ code generator. This code generator has two public frontends, one a command-line application (binary) in the *cmd* directory and the other a library intended to be used from a build.rs in the *build* directory. + +There's also a 'lib' frontend which is intended to allow higher level code generators +to embed cxx. This is not yet recommended for general use. diff --git a/gen/lib/src/lib.rs b/gen/lib/src/lib.rs index dae7e7869..e4d9e717d 100644 --- a/gen/lib/src/lib.rs +++ b/gen/lib/src/lib.rs @@ -22,6 +22,7 @@ mod syntax; pub use crate::error::Error; pub use crate::gen::{GeneratedCode, Opt}; use proc_macro2::TokenStream; +use gen::include::HEADER; /// Generate C++ bindings code from a Rust token stream. This should be a Rust /// token stream which somewhere contains a `#[cxx::bridge] mod {}`. @@ -31,3 +32,8 @@ pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result &'static str { + HEADER +} From 0e01d6406f3591147c1b05de66180d6bc118d3a8 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 16 Oct 2020 12:54:44 -0700 Subject: [PATCH 2/2] Expose cxx_gen::HEADER as a static &'static str --- gen/lib/src/lib.rs | 7 +------ gen/src/include.rs | 1 + 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/gen/lib/src/lib.rs b/gen/lib/src/lib.rs index e4d9e717d..ecfa4361a 100644 --- a/gen/lib/src/lib.rs +++ b/gen/lib/src/lib.rs @@ -20,9 +20,9 @@ mod gen; mod syntax; pub use crate::error::Error; +pub use crate::gen::include::HEADER; pub use crate::gen::{GeneratedCode, Opt}; use proc_macro2::TokenStream; -use gen::include::HEADER; /// Generate C++ bindings code from a Rust token stream. This should be a Rust /// token stream which somewhere contains a `#[cxx::bridge] mod {}`. @@ -32,8 +32,3 @@ pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result &'static str { - HEADER -} diff --git a/gen/src/include.rs b/gen/src/include.rs index 1688a4e3d..3255902ad 100644 --- a/gen/src/include.rs +++ b/gen/src/include.rs @@ -1,6 +1,7 @@ use crate::gen::out::OutFile; use std::fmt::{self, Display}; +/// The complete contents of the "rust/cxx.h" header. pub static HEADER: &str = include_str!("include/cxx.h"); pub(super) fn write(out: &mut OutFile, needed: bool, guard: &str) {