Skip to content

Commit

Permalink
Merge pull request #792 from bsilver8192/skip-cxx-gen
Browse files Browse the repository at this point in the history
Implement an option for not generating `cxx::bridge` sources
  • Loading branch information
adetaylor authored Feb 14, 2022
2 parents 2037677 + 8c29d68 commit 90c2aa1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 7 deletions.
7 changes: 7 additions & 0 deletions engine/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ impl<CTX: BuilderContext> Builder<CTX> {
self
}

/// Whether to skip using [`cxx_gen`] to generate the C++ code,
/// so that some other process can handle that.
pub fn skip_cxx_gen(mut self, skip_cxx_gen: bool) -> Self {
self.cpp_codegen_options.skip_cxx_gen = skip_cxx_gen;
self
}

/// Build autocxx C++ files and return a cc::Build you can use to build
/// more from a build.rs file.
pub fn build(self) -> Result<BuilderBuild, BuilderError> {
Expand Down
7 changes: 6 additions & 1 deletion engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ impl CppBuildable for IncludeCppEngine {
State::NotGenerated => panic!("Call generate() first"),
State::Generated(gen_results) => {
let rs = gen_results.item_mod.to_token_stream();
files.push(do_cxx_cpp_generation(rs, cpp_codegen_options)?);
if !cpp_codegen_options.skip_cxx_gen {
files.push(do_cxx_cpp_generation(rs, cpp_codegen_options)?);
}
if let Some(cpp_file_pair) = &gen_results.cpp {
files.push(cpp_file_pair.clone());
}
Expand Down Expand Up @@ -642,4 +644,7 @@ pub struct CppCodegenOptions {
/// An annotation optionally to include on each C++ function.
/// For example to export the symbol from a library.
pub cxx_impl_annotations: Option<String>,
/// Whether to skip using [`cxx_gen`] to generate the C++ code,
/// so that some other process can handle that.
pub skip_cxx_gen: bool,
}
47 changes: 45 additions & 2 deletions gen/cmd/src/cmd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ fn test_help() -> Result<(), Box<dyn std::error::Error>> {
}

fn base_test<F>(tmp_dir: &TempDir, arg_modifier: F) -> Result<(), Box<dyn std::error::Error>>
where
F: FnOnce(&mut Command),
{
let result = base_test_ex(tmp_dir, arg_modifier);
assert_contentful(&tmp_dir, "gen0.cc");
result
}

fn base_test_ex<F>(tmp_dir: &TempDir, arg_modifier: F) -> Result<(), Box<dyn std::error::Error>>
where
F: FnOnce(&mut Command),
{
Expand All @@ -47,7 +56,6 @@ where
.arg("--gen-rs-include")
.assert()
.success();
assert_contentful(&tmp_dir, "gen0.cc");
Ok(())
}

Expand Down Expand Up @@ -119,6 +127,25 @@ fn test_gen_repro() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn test_skip_cxx_gen() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = TempDir::new("example")?;
base_test_ex(&tmp_dir, |cmd| {
cmd.arg("--generate-exact")
.arg("3")
.arg("--fix-rs-include-name")
.arg("--skip-cxx-gen");
})?;
assert_contentful(&tmp_dir, "autocxxgen_ffi.h");
assert_not_contentful(&tmp_dir, "gen0.cc");
assert_exists(&tmp_dir, "gen1.cc");
assert_exists(&tmp_dir, "gen2.cc");
assert_contentful(&tmp_dir, "gen0.include.rs");
assert_exists(&tmp_dir, "gen1.include.rs");
assert_exists(&tmp_dir, "gen2.include.rs");
Ok(())
}

fn write_to_file(dir: &Path, filename: &str, content: &[u8]) {
let path = dir.join(filename);
let mut f = File::create(&path).expect("Unable to create file");
Expand All @@ -130,7 +157,23 @@ fn assert_contentful(outdir: &TempDir, fname: &str) {
if !p.exists() {
panic!("File {} didn't exist", p.to_string_lossy());
}
assert!(p.metadata().unwrap().len() > super::BLANK.len().try_into().unwrap());
assert!(
p.metadata().unwrap().len() > super::BLANK.len().try_into().unwrap(),
"File {} is empty",
fname
);
}

fn assert_not_contentful(outdir: &TempDir, fname: &str) {
let p = outdir.path().join(fname);
if !p.exists() {
panic!("File {} didn't exist", p.to_string_lossy());
}
assert!(
p.metadata().unwrap().len() <= super::BLANK.len().try_into().unwrap(),
"File {} is not empty",
fname
);
}

fn assert_exists(outdir: &TempDir, fname: &str) {
Expand Down
7 changes: 4 additions & 3 deletions gen/cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ fn main() {
.arg("gen-rs-include")
)
.arg(
Arg::with_name("cxx-gen")
.long("cxx-gen")
.help("Perform C++ codegen also for #[cxx::bridge] blocks. Only applies for --gen-cpp")
Arg::with_name("skip-cxx-gen")
.long("skip-cxx-gen")
.help("Skip performing C++ codegen for #[cxx::bridge] blocks. Only applies for --gen-cpp")
.requires("gen-cpp")
)
.arg(
Expand Down Expand Up @@ -211,6 +211,7 @@ fn main() {
cpp_codegen_options.cxx_impl_annotations = get_option_string("cxx-impl-annotations", &matches);
cpp_codegen_options.path_to_cxx_h = get_option_string("cxx-h-path", &matches);
cpp_codegen_options.path_to_cxxgen_h = get_option_string("cxxgen-h-path", &matches);
cpp_codegen_options.skip_cxx_gen = matches.is_present("skip-cxx-gen");
// In future, we should provide an option to write a .d file here
// by passing a callback into the dep_recorder parameter here.
// https://github.com/google/autocxx/issues/56
Expand Down
36 changes: 36 additions & 0 deletions integration-tests/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,17 @@ impl BuilderModifierFns for EnableAutodiscover {
}
}

pub(crate) struct SkipCxxGen;

impl BuilderModifierFns for SkipCxxGen {
fn modify_autocxx_builder(
&self,
builder: Builder<TestBuilderContext>,
) -> Builder<TestBuilderContext> {
builder.skip_cxx_gen(true)
}
}

/// Searches generated C++ for strings we want to find, or want _not_ to find,
/// or both.
pub(crate) struct CppMatcher<'a> {
Expand Down Expand Up @@ -582,3 +593,28 @@ impl<'a> CodeCheckerFns for CppMatcher<'a> {
}
}
}

/// Counts the number of generated C++ files.
pub(crate) struct CppCounter {
cpp_count: usize,
}

impl CppCounter {
pub(crate) fn new(cpp_count: usize) -> Self {
Self { cpp_count }
}
}

impl CodeCheckerFns for CppCounter {
fn check_cpp(&self, cpp: &[PathBuf]) -> Result<(), TestError> {
if cpp.len() == self.cpp_count {
Ok(())
} else {
Err(TestError::CppCodeExaminationFail)
}
}

fn skip_build(&self) -> bool {
true
}
}
24 changes: 23 additions & 1 deletion integration-tests/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use crate::test_utils::{
directives_from_lists, do_run_test_manual, make_clang_arg_adder, make_error_finder,
make_string_finder, run_test, run_test_ex, run_test_expect_fail, run_test_expect_fail_ex,
CppMatcher, EnableAutodiscover, NoSystemHeadersChecker, SetSuppressSystemHeaders,
CppCounter, CppMatcher, EnableAutodiscover, NoSystemHeadersChecker, SetSuppressSystemHeaders,
SkipCxxGen,
};
use indoc::indoc;
use itertools::Itertools;
Expand Down Expand Up @@ -7911,6 +7912,27 @@ fn test_issue486_multi_types() {
);
}

#[test]
fn test_skip_cxx_gen() {
let cxx = indoc! {"
void do_nothing() {
}
"};
let hdr = indoc! {"
void do_nothing();
"};
let rs = quote! {};
run_test_ex(
cxx,
hdr,
rs,
directives_from_lists(&["do_nothing"], &[], None),
Some(Box::new(SkipCxxGen)),
Some(Box::new(CppCounter::new(1))),
None,
);
}

// Yet to test:
// - Ifdef
// - Out param pointers
Expand Down

0 comments on commit 90c2aa1

Please sign in to comment.