-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split cxx runtime and build components
- Loading branch information
Showing
42 changed files
with
193 additions
and
156 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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,4 @@ | ||
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. |
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,22 @@ | ||
[package] | ||
name = "cxx-build" | ||
version = "0.2.12" | ||
authors = ["David Tolnay <dtolnay@gmail.com>"] | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
description = "C++ code generator for integrating `cxx` crate into a Cargo build." | ||
repository = "https://github.com/dtolnay/cxx" | ||
|
||
[badges] | ||
travis-ci = { repository = "dtolnay/cxx" } | ||
|
||
[dependencies] | ||
anyhow = "1.0" | ||
cc = "1.0.49" | ||
codespan-reporting = "0.9" | ||
proc-macro2 = { version = "1.0", features = ["span-locations"] } | ||
quote = "1.0" | ||
syn = { version = "1.0", features = ["full"] } | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
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 @@ | ||
../../LICENSE-APACHE |
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 @@ | ||
../../LICENSE-MIT |
File renamed without changes.
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 @@ | ||
../../src |
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,96 @@ | ||
//! The CXX code generator for constructing and compiling C++ code. | ||
//! | ||
//! This is intended to be used from Cargo build scripts to execute CXX's | ||
//! C++ code generator, set up any additional compiler flags depending on | ||
//! the use case, and make the C++ compiler invocation. | ||
//! | ||
//! <br> | ||
//! | ||
//! # Example | ||
//! | ||
//! Example of a canonical Cargo build script that builds a CXX bridge: | ||
//! | ||
//! ```no_run | ||
//! // build.rs | ||
//! | ||
//! fn main() { | ||
//! cxx_build::bridge("src/main.rs") | ||
//! .file("../demo-cxx/demo.cc") | ||
//! .flag("-std=c++11") | ||
//! .compile("cxxbridge-demo"); | ||
//! | ||
//! println!("cargo:rerun-if-changed=src/main.rs"); | ||
//! println!("cargo:rerun-if-changed=../demo-cxx/demo.h"); | ||
//! println!("cargo:rerun-if-changed=../demo-cxx/demo.cc"); | ||
//! } | ||
//! ``` | ||
//! | ||
//! A runnable working setup with this build script is shown in the | ||
//! *demo-rs* and *demo-cxx* directories of [https://github.com/dtolnay/cxx]. | ||
//! | ||
//! [https://github.com/dtolnay/cxx]: https://github.com/dtolnay/cxx | ||
//! | ||
//! <br> | ||
//! | ||
//! # Alternatives | ||
//! | ||
//! For use in non-Cargo builds like Bazel or Buck, CXX provides an | ||
//! alternate way of invoking the C++ code generator as a standalone command | ||
//! line tool. The tool is packaged as the `cxxbridge-cmd` crate. | ||
//! | ||
//! ```bash | ||
//! $ cargo install cxxbridge-cmd # or build it from the repo | ||
//! | ||
//! $ cxxbridge src/main.rs --header > path/to/mybridge.h | ||
//! $ cxxbridge src/main.rs > path/to/mybridge.cc | ||
//! ``` | ||
|
||
mod error; | ||
mod gen; | ||
mod paths; | ||
mod syntax; | ||
|
||
use crate::error::Result; | ||
use crate::gen::Opt; | ||
use anyhow::anyhow; | ||
use std::fs; | ||
use std::io::{self, Write}; | ||
use std::path::Path; | ||
use std::process; | ||
|
||
/// This returns a [`cc::Build`] on which you should continue to set up any | ||
/// additional source files or compiler flags, and lastly call its [`compile`] | ||
/// method to execute the C++ build. | ||
/// | ||
/// [`compile`]: https://docs.rs/cc/1.0.49/cc/struct.Build.html#method.compile | ||
#[must_use] | ||
pub fn bridge(rust_source_file: impl AsRef<Path>) -> cc::Build { | ||
match try_generate_bridge(rust_source_file.as_ref()) { | ||
Ok(build) => build, | ||
Err(err) => { | ||
let _ = writeln!(io::stderr(), "\n\ncxxbridge error: {:?}\n\n", anyhow!(err)); | ||
process::exit(1); | ||
} | ||
} | ||
} | ||
|
||
fn try_generate_bridge(rust_source_file: &Path) -> Result<cc::Build> { | ||
let header = gen::do_generate_header(rust_source_file, Opt::default()); | ||
let header_path = paths::out_with_extension(rust_source_file, ".h")?; | ||
fs::create_dir_all(header_path.parent().unwrap())?; | ||
fs::write(&header_path, header)?; | ||
paths::symlink_header(&header_path, rust_source_file); | ||
|
||
let bridge = gen::do_generate_bridge(rust_source_file, Opt::default()); | ||
let bridge_path = paths::out_with_extension(rust_source_file, ".cc")?; | ||
fs::write(&bridge_path, bridge)?; | ||
let mut build = paths::cc_build(); | ||
build.file(&bridge_path); | ||
|
||
let ref cxx_h = paths::include_dir()?.join("rust").join("cxx.h"); | ||
let _ = fs::create_dir_all(cxx_h.parent().unwrap()); | ||
let _ = fs::remove_file(cxx_h); | ||
let _ = fs::write(cxx_h, gen::include::HEADER); | ||
|
||
Ok(build) | ||
} |
File renamed without changes.
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 @@ | ||
../../../syntax |
File renamed without changes.
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 @@ | ||
../../LICENSE-APACHE |
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 @@ | ||
../../LICENSE-MIT |
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 @@ | ||
../../src |
File renamed without changes.
File renamed without changes.
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 @@ | ||
../../../syntax |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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 @@ | ||
../../include |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,3 @@ | ||
This directory contains CXX's Rust code generator, which is a procedural macro. | ||
Users won't depend on this crate directly. Instead they'll invoke its macro | ||
through the reexport in the main `cxx` crate. |
Oops, something went wrong.