Skip to content

Commit

Permalink
Add an option to autocxx_gen to create cxx.h
Browse files Browse the repository at this point in the history
  • Loading branch information
nak3 committed Feb 21, 2023
1 parent 79c7293 commit 252608f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
8 changes: 2 additions & 6 deletions engine/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use miette::Diagnostic;
use thiserror::Error;

use crate::{generate_rs_single, CodegenOptions};
use crate::{strip_system_headers, CppCodegenOptions, ParseError, RebuildDependencyRecorder};
use crate::{get_cxx_header_bytes, CppCodegenOptions, ParseError, RebuildDependencyRecorder};
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs::File;
Expand Down Expand Up @@ -221,7 +221,7 @@ impl<CTX: BuilderContext> Builder<'_, CTX> {
write_to_file(
&incdir,
"cxx.h",
&Self::get_cxx_header_bytes(
&get_cxx_header_bytes(
self.codegen_options
.cpp_codegen_options
.suppress_system_headers,
Expand Down Expand Up @@ -277,10 +277,6 @@ impl<CTX: BuilderContext> Builder<'_, CTX> {
Ok(BuilderSuccess(builder, generated_rs, generated_cpp))
}
}

fn get_cxx_header_bytes(suppress_system_headers: bool) -> Vec<u8> {
strip_system_headers(crate::HEADER.as_bytes().to_vec(), suppress_system_headers)
}
}

fn ensure_created(dir: &Path) -> Result<(), BuilderError> {
Expand Down
6 changes: 5 additions & 1 deletion engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,11 @@ pub fn do_cxx_cpp_generation(
})
}

pub(crate) fn strip_system_headers(input: Vec<u8>, suppress_system_headers: bool) -> Vec<u8> {
pub fn get_cxx_header_bytes(suppress_system_headers: bool) -> Vec<u8> {
strip_system_headers(cxx_gen::HEADER.as_bytes().to_vec(), suppress_system_headers)
}

fn strip_system_headers(input: Vec<u8>, suppress_system_headers: bool) -> Vec<u8> {
if suppress_system_headers {
std::str::from_utf8(&input)
.unwrap()
Expand Down
17 changes: 15 additions & 2 deletions gen/cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
mod depfile;

use autocxx_engine::{
generate_rs_archive, generate_rs_single, parse_file, AutocxxgenHeaderNamer, CxxgenHeaderNamer,
RebuildDependencyRecorder,
generate_rs_archive, generate_rs_single, get_cxx_header_bytes, parse_file,
AutocxxgenHeaderNamer, CxxgenHeaderNamer, RebuildDependencyRecorder,
};
use clap::{crate_authors, crate_version, Arg, ArgGroup, Command};
use depfile::Depfile;
Expand Down Expand Up @@ -176,6 +176,11 @@ fn main() -> miette::Result<()> {
.help("prefix for symbols to be exported from C++ bindings, e.g. __attribute__ ((visibility (\"default\")))")
.takes_value(true),
)
.arg(
Arg::new("generate-cxx-h")
.long("generate-cxx-h")
.help("whether to generate cxx.h header file. If you already knew where to find cxx.h, consider using --cxx-h-path")
)
.arg(
Arg::new("cxx-h-path")
.long("cxx-h-path")
Expand Down Expand Up @@ -332,6 +337,14 @@ fn main() -> miette::Result<()> {
name_autocxxgen_h,
)?;
}

if matches.is_present("generate-cxx-h") {
writer.write_to_file(
"cxx.h".to_string(),
&get_cxx_header_bytes(suppress_system_headers),
)?;
}

if matches.is_present("gen-rs-include") {
if !matches.is_present("fix-rs-include-name") && desired_number.is_some() {
return Err(miette::Report::msg(
Expand Down
15 changes: 2 additions & 13 deletions gen/cmd/tests/cmd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ where
.arg(demo_code_dir.to_str().unwrap())
.arg("--outdir")
.arg(tmp_dir.path().to_str().unwrap())
.arg("--gen-cpp");
.arg("--gen-cpp")
.arg("--generate-cxx-h");
cmd.arg(match rs_gen_mode {
RsGenMode::Single => "--gen-rs-include",
RsGenMode::Archive => "--gen-rs-archive",
Expand All @@ -109,8 +110,6 @@ where
fn test_gen() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
base_test(&tmp_dir, RsGenMode::Single, |_| {})?;
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
std::env::set_var("OUT_DIR", tmp_dir.path().to_str().unwrap());
let r = build_from_folder(
tmp_dir.path(),
Expand All @@ -130,8 +129,6 @@ fn test_gen() -> Result<(), Box<dyn std::error::Error>> {
fn test_gen_archive() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
base_test(&tmp_dir, RsGenMode::Archive, |_| {})?;
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
let r = build_from_folder(
tmp_dir.path(),
&tmp_dir.path().join("demo/main.rs"),
Expand All @@ -150,8 +147,6 @@ fn test_gen_archive() -> Result<(), Box<dyn std::error::Error>> {
fn test_gen_archive_first_entry() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
base_test(&tmp_dir, RsGenMode::Archive, |_| {})?;
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
let r = build_from_folder(
tmp_dir.path(),
&tmp_dir.path().join("demo/main.rs"),
Expand All @@ -176,8 +171,6 @@ fn test_gen_archive_first_entry() -> Result<(), Box<dyn std::error::Error>> {
fn test_gen_archive_second_entry() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
base_test(&tmp_dir, RsGenMode::Archive, |_| {})?;
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
let r = build_from_folder(
tmp_dir.path(),
&tmp_dir.path().join("demo/main.rs"),
Expand Down Expand Up @@ -217,8 +210,6 @@ fn test_gen_multiple_in_archive() -> Result<(), Box<dyn std::error::Error>> {
files,
vec!["directive1.rs", "directive2.rs"],
)?;
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
// We've asked to create 8 C++ files, mostly blank. Build 'em all.
let cpp_files = (0..7).map(|id| format!("gen{id}.cc")).collect_vec();
let cpp_files = cpp_files.iter().map(|s| s.as_str()).collect_vec();
Expand Down Expand Up @@ -273,8 +264,6 @@ fn test_gen_fixed_num() -> Result<(), Box<dyn std::error::Error>> {
assert_not_contentful(&tmp_dir, "autocxxgen1.h");
assert_contentful(&tmp_dir, "gen0.include.rs");
assert_contentful(&tmp_dir, "test.d");
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
let r = build_from_folder(
tmp_dir.path(),
&tmp_dir.path().join("demo/main.rs"),
Expand Down

0 comments on commit 252608f

Please sign in to comment.