Skip to content

Commit

Permalink
Merge pull request #275 from dtolnay/demo
Browse files Browse the repository at this point in the history
Combine demo into one root directory
  • Loading branch information
dtolnay authored Sep 2, 2020
2 parents 7273963 + f401e88 commit 9ffb7b6
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 90 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{matrix.rust}}
- run: cargo run --manifest-path demo-rs/Cargo.toml
- run: cargo run --manifest-path demo/Cargo.toml
- run: cargo test --workspace --exclude cxx-test-suite

msrv:
Expand All @@ -43,7 +43,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: dtolnay/[email protected]
- run: cargo run --manifest-path demo-rs/Cargo.toml
- run: cargo run --manifest-path demo/Cargo.toml

buck:
name: Buck
Expand All @@ -66,7 +66,7 @@ jobs:
cp third-party/Cargo.lock .
cargo vendor --versioned-dirs --locked third-party/vendor
- run: buck build :cxx#check --verbose=0
- run: buck run demo-rs --verbose=0
- run: buck run demo --verbose=0
- run: buck test ... --verbose=0

bazel:
Expand All @@ -84,5 +84,5 @@ jobs:
run: |
cp third-party/Cargo.lock .
cargo vendor --versioned-dirs --locked third-party/vendor
- run: bazel run demo-rs --verbose_failures --noshow_progress
- run: bazel run demo --verbose_failures --noshow_progress
- run: bazel test ... --verbose_failures --noshow_progress
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "Safe interop between Rust and C++"
repository = "https://github.com/dtolnay/cxx"
documentation = "https://docs.rs/cxx"
readme = "README.md"
exclude = ["/demo-cxx", "/gen", "/syntax", "/third-party"]
exclude = ["/demo", "/gen", "/syntax", "/third-party"]
keywords = ["ffi"]
categories = ["development-tools::ffi", "api-bindings"]

Expand All @@ -34,7 +34,7 @@ rustversion = "1.0"
trybuild = { version = "1.0.33", features = ["diff"] }

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

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ function calls Rust's `len()`.

## Example

A runnable version of this example is provided under the *demo-rs* directory of
this repo (with the C++ side of the implementation in the *demo-cxx* directory).
To try it out, jump into demo-rs and run `cargo run`.
A runnable version of this example is provided under the *demo* directory of
this repo. To try it out, run `cargo run` from that directory.

```rust
#[cxx::bridge]
Expand All @@ -78,7 +77,7 @@ mod ffi {
// One or more headers with the matching C++ declarations. Our code
// generators don't read it but it gets #include'd and used in static
// assertions to ensure our picture of the FFI boundary is accurate.
include!("demo-cxx/demo.h");
include!("demo/include/demo.h");

// Zero or more opaque types which both languages can pass around but
// only C++ can see the fields.
Expand Down Expand Up @@ -107,21 +106,21 @@ get to call back and forth safely.

Here are links to the complete set of source files involved in the demo:

- [demo-rs/src/main.rs](demo-rs/src/main.rs)
- [demo-rs/build.rs](demo-rs/build.rs)
- [demo-cxx/demo.h](demo-cxx/demo.h)
- [demo-cxx/demo.cc](demo-cxx/demo.cc)
- [demo/src/main.rs](demo/src/main.rs)
- [demo/build.rs](demo/build.rs)
- [demo/include/demo.h](demo/include/demo.h)
- [demo/src/demo.cc](demo/src/demo.cc)

To look at the code generated in both languages for the example by the CXX code
generators:

```console
# run Rust code generator and print to stdout
# (requires https://github.com/dtolnay/cargo-expand)
$ cargo expand --manifest-path demo-rs/Cargo.toml
$ cargo expand --manifest-path demo/Cargo.toml

# run C++ code generator and print to stdout
$ cargo run --manifest-path gen/cmd/Cargo.toml -- demo-rs/src/main.rs
$ cargo run --manifest-path gen/cmd/Cargo.toml -- demo/src/main.rs
```

<br>
Expand Down Expand Up @@ -228,13 +227,13 @@ cxx-build = "0.3"

fn main() {
cxx_build::bridge("src/main.rs") // returns a cc::Build
.file("../demo-cxx/demo.cc")
.file("src/demo.cc")
.flag_if_supported("-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");
println!("cargo:rerun-if-changed=src/demo.cc");
println!("cargo:rerun-if-changed=include/demo.h");
}
```

Expand Down
17 changes: 0 additions & 17 deletions demo-cxx/BUCK

This file was deleted.

17 changes: 0 additions & 17 deletions demo-cxx/BUILD

This file was deleted.

25 changes: 20 additions & 5 deletions demo-rs/BUCK → demo/BUCK
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
rust_binary(
name = "demo-rs",
srcs = glob(["src/**"]),
name = "demo",
srcs = glob(["src/**/*.rs"]),
deps = [
":demo-sys",
":gen",
"//:cxx",
"//demo-cxx:demo-cxx",
],
)

cxx_library(
name = "gen",
srcs = [":gen-source"],
deps = [
":demo-include",
":include",
"//demo-cxx:include",
],
)

Expand All @@ -38,5 +38,20 @@ cxx_library(
exported_headers = {
"src/main.rs.h": ":gen-header",
},
visibility = ["PUBLIC"],
)

cxx_library(
name = "demo-sys",
srcs = ["src/demo.cc"],
compiler_flags = ["-std=c++14"],
deps = [
":demo-include",
":include",
],
)

cxx_library(
name = "demo-include",
exported_headers = ["include/demo.h"],
deps = ["//:core"],
)
27 changes: 21 additions & 6 deletions demo-rs/BUILD → demo/BUILD
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
load("//tools/bazel:rust.bzl", "rust_binary", "rust_library")

rust_binary(
name = "demo-rs",
srcs = glob(["src/**"]),
name = "demo",
srcs = glob(["src/**/*.rs"]),
deps = [
":demo-sys",
":gen",
"//:cxx",
"//demo-cxx",
],
)

cc_library(
name = "gen",
srcs = [":gen-source"],
deps = [
":demo-include",
":include",
"//demo-cxx:include",
],
)

Expand All @@ -38,6 +38,21 @@ genrule(
cc_library(
name = "include",
hdrs = [":gen-header"],
include_prefix = "demo-rs/src",
visibility = ["//visibility:public"],
include_prefix = "demo/src",
)

cc_library(
name = "demo-sys",
srcs = ["src/demo.cc"],
copts = ["-std=c++14"],
deps = [
":demo-include",
":include",
],
)

cc_library(
name = "demo-include",
hdrs = ["include/demo.h"],
deps = ["//:core"],
)
File renamed without changes.
6 changes: 3 additions & 3 deletions demo-rs/build.rs → demo/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn main() {
cxx_build::bridge("src/main.rs")
.file("../demo-cxx/demo.cc")
.file("src/demo.cc")
.flag_if_supported("-std=c++14")
.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");
println!("cargo:rerun-if-changed=src/demo.cc");
println!("cargo:rerun-if-changed=include/demo.h");
}
File renamed without changes.
4 changes: 2 additions & 2 deletions demo-cxx/demo.cc → demo/src/demo.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "demo-cxx/demo.h"
#include "demo-rs/src/main.rs.h"
#include "demo/include/demo.h"
#include "demo/src/main.rs.h"
#include <iostream>

namespace org {
Expand Down
2 changes: 1 addition & 1 deletion demo-rs/src/main.rs → demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod ffi {
}

extern "C" {
include!("demo-cxx/demo.h");
include!("demo/include/demo.h");

type ThingC;
fn make_demo(appname: &str) -> UniquePtr<ThingC>;
Expand Down
12 changes: 6 additions & 6 deletions gen/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
//!
//! fn main() {
//! cxx_build::bridge("src/main.rs")
//! .file("../demo-cxx/demo.cc")
//! .file("src/demo.cc")
//! .flag_if_supported("-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");
//! println!("cargo:rerun-if-changed=src/demo.cc");
//! println!("cargo:rerun-if-changed=include/demo.h");
//! }
//! ```
//!
//! A runnable working setup with this build script is shown in the
//! *demo-rs* and *demo-cxx* directories of [https://github.com/dtolnay/cxx].
//! A runnable working setup with this build script is shown in the *demo*
//! directory of [https://github.com/dtolnay/cxx].
//!
//! [https://github.com/dtolnay/cxx]: https://github.com/dtolnay/cxx
//!
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn bridge(rust_source_file: impl AsRef<Path>) -> Build {
/// ```no_run
/// let source_files = vec!["src/main.rs", "src/path/to/other.rs"];
/// cxx_build::bridges(source_files)
/// .file("../demo-cxx/demo.cc")
/// .file("src/demo.cc")
/// .flag_if_supported("-std=c++11")
/// .compile("cxxbridge-demo");
/// ```
Expand Down
27 changes: 13 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@
//!
//! # Example
//!
//! A runnable version of this example is provided under the *demo-rs* directory
//! of [https://github.com/dtolnay/cxx] (with the C++ side of the implementation
//! in the *demo-cxx* directory). To try it out, jump into demo-rs and run
//! `cargo run`.
//! A runnable version of this example is provided under the *demo* directory of
//! [https://github.com/dtolnay/cxx]. To try it out, run `cargo run` from that
//! directory.
//!
//! ```no_run
//! #[cxx::bridge]
Expand All @@ -76,7 +75,7 @@
//! // One or more headers with the matching C++ declarations. Our code
//! // generators don't read it but it gets #include'd and used in static
//! // assertions to ensure our picture of the FFI boundary is accurate.
//! include!("demo-cxx/demo.h");
//! include!("demo/include/demo.h");
//!
//! // Zero or more opaque types which both languages can pass around but
//! // only C++ can see the fields.
Expand Down Expand Up @@ -113,21 +112,21 @@
//!
//! Here are links to the complete set of source files involved in the demo:
//!
//! - [demo-rs/src/main.rs](https://github.com/dtolnay/cxx/blob/master/demo-rs/src/main.rs)
//! - [demo-rs/build.rs](https://github.com/dtolnay/cxx/blob/master/demo-rs/build.rs)
//! - [demo-cxx/demo.h](https://github.com/dtolnay/cxx/blob/master/demo-cxx/demo.h)
//! - [demo-cxx/demo.cc](https://github.com/dtolnay/cxx/blob/master/demo-cxx/demo.cc)
//! - [demo/src/main.rs](https://github.com/dtolnay/cxx/blob/master/demo/src/main.rs)
//! - [demo/build.rs](https://github.com/dtolnay/cxx/blob/master/demo/build.rs)
//! - [demo/include/demo.h](https://github.com/dtolnay/cxx/blob/master/demo/include/demo.h)
//! - [demo/src/demo.cc](https://github.com/dtolnay/cxx/blob/master/demo/src/demo.cc)
//!
//! To look at the code generated in both languages for the example by the CXX
//! code generators:
//!
//! ```console
//! # run Rust code generator and print to stdout
//! # (requires https://github.com/dtolnay/cargo-expand)
//! $ cargo expand --manifest-path demo-rs/Cargo.toml
//! $ cargo expand --manifest-path demo/Cargo.toml
//!
//! # run C++ code generator and print to stdout
//! $ cargo run --manifest-path gen/cmd/Cargo.toml -- demo-rs/src/main.rs
//! $ cargo run --manifest-path gen/cmd/Cargo.toml -- demo/src/main.rs
//! ```
//!
//! <br>
Expand Down Expand Up @@ -237,13 +236,13 @@
//!
//! fn main() {
//! cxx_build::bridge("src/main.rs") // returns a cc::Build
//! .file("../demo-cxx/demo.cc")
//! .file("src/demo.cc")
//! .flag_if_supported("-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");
//! println!("cargo:rerun-if-changed=src/demo.cc");
//! println!("cargo:rerun-if-changed=include/demo.h");
//! }
//! ```
//!
Expand Down

0 comments on commit 9ffb7b6

Please sign in to comment.