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

Split cxx runtime and build components #167

Merged
merged 2 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 20,15 @@ matrix:
rust: nightly-x86_64-pc-windows-gnu
before_script:
# windows is bad at symlinks
- rm cmd/src/gen cmd/src/syntax gen/include macro/src/syntax src/gen src/syntax
- cp -r include gen; cp -r gen cmd/src; cp -r syntax cmd/src; cp -r syntax macro/src; cp -r gen src; cp -r syntax src
- rm gen/build/src/gen gen/build/src/syntax gen/cmd/src/gen gen/cmd/src/syntax gen/src/include macro/src/syntax
- cp -r include gen/src; cp -r gen/src gen/build/src/gen; cp -r gen/src gen/cmd/src/gen; cp -r syntax gen/build/src; cp -r syntax gen/cmd/src; cp -r syntax macro/src

- name: Windows (msvc)
os: windows
rust: nightly-x86_64-pc-windows-msvc
before_script:
- rm cmd/src/gen cmd/src/syntax gen/include macro/src/syntax src/gen src/syntax
- cp -r include gen; cp -r gen cmd/src; cp -r syntax cmd/src; cp -r syntax macro/src; cp -r gen src; cp -r syntax src
- rm gen/build/src/gen gen/build/src/syntax gen/cmd/src/gen gen/cmd/src/syntax gen/src/include macro/src/syntax
- cp -r include gen/src; cp -r gen/src gen/build/src/gen; cp -r gen/src gen/cmd/src/gen; cp -r syntax gen/build/src; cp -r syntax gen/cmd/src; cp -r syntax macro/src

- name: Buck
rust: nightly
Expand Down Expand Up @@ -62,3 62,7 @@ matrix:
rust: 1.42.0
script:
- cargo run --manifest-path demo-rs/Cargo.toml

# https://github.com/dtolnay/cxx/pull/167
allow_failures:
- name: Buck
22 changes: 15 additions & 7 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 5,13 @@ rust_library(
deps = [
":core",
":macro",
"//third-party:anyhow",
"//third-party:cc",
"//third-party:codespan-reporting",
"//third-party:link-cplusplus",
"//third-party:proc-macro2",
"//third-party:quote",
"//third-party:syn",
],
)

rust_binary(
name = "codegen",
srcs = glob(["cmd/src/**"]),
srcs = glob(["gen/cmd/src/**"]),
crate = "cxxbridge",
visibility = ["PUBLIC"],
deps = [
Expand Down Expand Up @@ -52,3 46,17 @@ rust_library(
"//third-party:syn",
],
)

rust_library(
name = "build",
srcs = glob(["gen/build/src/**"]),
visibility = ["PUBLIC"],
deps = [
"//third-party:anyhow",
"//third-party:cc",
"//third-party:codespan-reporting",
"//third-party:proc-macro2",
"//third-party:quote",
"//third-party:syn",
],
)
26 changes: 17 additions & 9 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 3,18 @@ load("//tools/bazel:rust.bzl", "rust_binary", "rust_library")
rust_library(
name = "cxx",
srcs = glob(["src/**/*.rs"]),
data = ["src/gen/include/cxx.h"],
visibility = ["//visibility:public"],
deps = [
":core-lib",
":cxxbridge-macro",
"//third-party:anyhow",
"//third-party:cc",
"//third-party:codespan-reporting",
"//third-party:link-cplusplus",
"//third-party:proc-macro2",
"//third-party:quote",
"//third-party:syn",
],
)

rust_binary(
name = "codegen",
srcs = glob(["cmd/src/**/*.rs"]),
data = ["cmd/src/gen/include/cxx.h"],
srcs = glob(["gen/cmd/src/**/*.rs"]),
data = ["gen/cmd/src/gen/include/cxx.h"],
visibility = ["//visibility:public"],
deps = [
"//third-party:anyhow",
Expand Down Expand Up @@ -57,3 50,18 @@ rust_library(
"//third-party:syn",
],
)

rust_library(
name = "build",
srcs = glob(["gen/build/src/**/*.rs"]),
data = ["gen/build/src/gen/include/cxx.h"],
visibility = ["//visibility:public"],
deps = [
"//third-party:anyhow",
"//third-party:cc",
"//third-party:codespan-reporting",
"//third-party:proc-macro2",
"//third-party:quote",
"//third-party:syn",
],
)
9 changes: 2 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 15,20 @@ exclude = ["/demo-cxx", "/gen", "/syntax", "/third-party"]
travis-ci = { repository = "dtolnay/cxx" }

[dependencies]
anyhow = "1.0"
cc = "1.0.49"
codespan-reporting = "0.9"
cxxbridge-macro = { version = "=0.2.12", path = "macro" }
link-cplusplus = "1.0"
proc-macro2 = { version = "1.0", features = ["span-locations"] }
quote = "1.0"
syn = { version = "1.0", features = ["full"] }

[build-dependencies]
cc = "1.0.49"

[dev-dependencies]
cxx-build = { version = "=0.2.12", path = "gen/build" }
cxx-test-suite = { version = "0", path = "tests/ffi" }
rustversion = "1.0"
trybuild = { version = "1.0.21", features = ["diff"] }

[workspace]
members = ["cmd", "demo-rs", "macro", "tests/ffi"]
members = ["demo-rs", "gen/build", "gen/cmd", "macro", "tests/ffi"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 219,7 @@ set up any additional source files and compiler flags as normal.
// build.rs

fn main() {
cxx::Build::new()
.bridge("src/main.rs") // returns a cc::Build
cxx_build::bridge("src/main.rs") // returns a cc::Build
.file("../demo-cxx/demo.cc")
.flag("-std=c 11")
.compile("cxxbridge-demo");
Expand Down
1 change: 0 additions & 1 deletion cmd/LICENSE-APACHE

This file was deleted.

1 change: 0 additions & 1 deletion cmd/LICENSE-MIT

This file was deleted.

1 change: 0 additions & 1 deletion cmd/src/gen

This file was deleted.

1 change: 0 additions & 1 deletion cmd/src/syntax

This file was deleted.

2 changes: 1 addition & 1 deletion demo-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 9,4 @@ publish = false
cxx = { path = ".." }

[build-dependencies]
cxx = { path = ".." }
cxx-build = { path = "../gen/build" }
3 changes: 1 addition & 2 deletions demo-rs/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 1,5 @@
fn main() {
cxx::Build::new()
.bridge("src/main.rs")
cxx_build::bridge("src/main.rs")
.file("../demo-cxx/demo.cc")
.flag("-std=c 11")
.compile("cxxbridge-demo");
Expand Down
4 changes: 4 additions & 0 deletions gen/README.md
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.
22 changes: 22 additions & 0 deletions gen/build/Cargo.toml
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 <[email protected]>"]
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"]
1 change: 1 addition & 0 deletions gen/build/LICENSE-APACHE
1 change: 1 addition & 0 deletions gen/build/LICENSE-MIT
File renamed without changes.
1 change: 1 addition & 0 deletions gen/build/src/gen
96 changes: 96 additions & 0 deletions gen/build/src/lib.rs
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.
1 change: 1 addition & 0 deletions gen/build/src/syntax
File renamed without changes.
1 change: 1 addition & 0 deletions gen/cmd/LICENSE-APACHE
1 change: 1 addition & 0 deletions gen/cmd/LICENSE-MIT
1 change: 1 addition & 0 deletions gen/cmd/src/gen
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions gen/cmd/src/syntax
1 change: 0 additions & 1 deletion gen/include

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions gen/src/include
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@ edition = "2018"
license = "MIT OR Apache-2.0"
description = "Implementation detail of the `cxx` crate."
repository = "https://github.com/dtolnay/cxx"
exclude = ["README.md"]

[lib]
proc-macro = true
Expand Down
3 changes: 3 additions & 0 deletions macro/README.md
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.
1 change: 0 additions & 1 deletion src/gen

This file was deleted.

Loading