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

Support multiple AUTOCXX_RS_JSON_ARCHIVE entries #1147

Merged
merged 2 commits into from
Aug 15, 2022
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
6 changes: 3 additions & 3 deletions engine/src/conversion/codegen_rs/function_wrapper_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl TypeConversionPolicy {
}
RustConversionType::FromPinMaybeUninitToPtr => {
let ty = match self.cxxbridge_type() {
Type::Ptr(TypePtr { elem, .. }) => &*elem,
Type::Ptr(TypePtr { elem, .. }) => elem,
_ => panic!("Not a ptr"),
};
let ty = parse_quote! {
Expand All @@ -80,7 +80,7 @@ impl TypeConversionPolicy {
}
RustConversionType::FromPinMoveRefToPtr => {
let ty = match self.cxxbridge_type() {
Type::Ptr(TypePtr { elem, .. }) => &*elem,
Type::Ptr(TypePtr { elem, .. }) => elem,
_ => panic!("Not a ptr"),
};
let ty = parse_quote! {
Expand All @@ -99,7 +99,7 @@ impl TypeConversionPolicy {
}
RustConversionType::FromTypeToPtr => {
let ty = match self.cxxbridge_type() {
Type::Ptr(TypePtr { elem, .. }) => &*elem,
Type::Ptr(TypePtr { elem, .. }) => elem,
_ => panic!("Not a ptr"),
};
let ty = parse_quote! { &mut #ty };
Expand Down
2 changes: 1 addition & 1 deletion engine/src/known_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub(crate) fn known_types() -> &'static TypeDatabase {
}

/// The type of payload that a cxx generic can contain.
#[derive(PartialEq, Clone, Copy)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum CxxGenericType {
/// Not a generic at all
Not,
Expand Down
6 changes: 5 additions & 1 deletion gen/cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ filenames, then you should use
instead of
--gen-rs-include
and you will need to give AUTOCXX_RS_JSON_ARCHIVE when building the Rust code.
The output filename is named gen.rs.json.
The output filename is named gen.rs.json. AUTOCXX_RS_JSON_ARCHIVE should be set
to the path to gen.rs.json. It may optionally have multiple paths separated the
way as the PATH environment variable for the current platform, see
[`std::env::split_paths`] for details. The first path which is successfully
opened will be used.
This teaches rustc (and the autocxx macro) that all the different Rust bindings
for multiple different autocxx macros have been archived into this single file.
Expand Down
52 changes: 52 additions & 0 deletions gen/cmd/tests/cmd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,58 @@ fn test_gen_archive() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
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"),
vec![tmp_dir.path().join("gen.rs.json")],
&["gen0.cc"],
RsFindMode::Custom(Box::new(|path: &Path| {
std::env::set_var(
"AUTOCXX_RS_JSON_ARCHIVE",
std::env::join_paths([&path.join("gen.rs.json"), Path::new("/nonexistent")])
.unwrap(),
)
})),
);
if KEEP_TEMPDIRS {
println!("Tempdir: {:?}", tmp_dir.into_path().to_str());
}
r.unwrap();
Ok(())
}

#[test]
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"),
vec![tmp_dir.path().join("gen.rs.json")],
&["gen0.cc"],
RsFindMode::Custom(Box::new(|path: &Path| {
std::env::set_var(
"AUTOCXX_RS_JSON_ARCHIVE",
std::env::join_paths([Path::new("/nonexistent"), &path.join("gen.rs.json")])
.unwrap(),
)
})),
);
if KEEP_TEMPDIRS {
println!("Tempdir: {:?}", tmp_dir.into_path().to_str());
}
r.unwrap();
Ok(())
}

#[test]
fn test_gen_multiple_in_archive() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub enum RsFindMode {
AutocxxRs,
AutocxxRsArchive,
AutocxxRsFile,
/// This just calls the callback instead of setting any environment variables. The callback
/// receives the path to the temporary directory.
Custom(Box<dyn FnOnce(&Path)>),
}

/// API to test building pre-generated files.
Expand Down Expand Up @@ -174,6 +177,7 @@ impl LinkableTryBuilder {
"AUTOCXX_RS_FILE",
self.temp_dir.path().join("gen0.include.rs"),
),
RsFindMode::Custom(f) => f(self.temp_dir.path()),
};
std::panic::catch_unwind(|| {
let test_cases = trybuild::TestCases::new();
Expand Down
2 changes: 1 addition & 1 deletion parser/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{directives::get_directives, RustPath};

use quote::quote;

#[derive(PartialEq, Clone, Debug, Hash)]
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum UnsafePolicy {
AllFunctionsSafe,
AllFunctionsUnsafe,
Expand Down
8 changes: 4 additions & 4 deletions parser/src/file_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ impl FileLocationStrategy {
include!( #fname );
}
}
FileLocationStrategy::FromAutocxxRsJsonArchive(fname) => {
let archive = File::open(fname).unwrap_or_else(|_| panic!("Unable to open {}. This may mean you didn't run the codegen tool (autocxx_gen) before building the Rust code.", fname.to_string_lossy()));
FileLocationStrategy::FromAutocxxRsJsonArchive(fnames) => {
let archive = std::env::split_paths(fnames).flat_map(File::open).next().unwrap_or_else(|| panic!("Unable to open any of the paths listed in {}. This may mean you didn't run the codegen tool (autocxx_gen) before building the Rust code.", fnames.to_string_lossy()));
let multi_bindings: MultiBindings = serde_json::from_reader(archive)
.unwrap_or_else(|_| {
panic!("Unable to interpret {} as JSON", fname.to_string_lossy())
panic!("Unable to interpret {} as JSON", fnames.to_string_lossy())
});
multi_bindings.get(config).unwrap_or_else(|err| panic!("Unable to find a suitable set of bindings within the JSON archive {} ({}). This likely means that the codegen tool hasn't been rerun since some changes in your include_cpp! macro.", fname.to_string_lossy(), err))
multi_bindings.get(config).unwrap_or_else(|err| panic!("Unable to find a suitable set of bindings within the JSON archive {} ({}). This likely means that the codegen tool hasn't been rerun since some changes in your include_cpp! macro.", fnames.to_string_lossy(), err))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/subclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use autocxx_macro::subclass as is_subclass;
/// #[subclass(superclass("MyCppSuperclass"))]
/// struct Bar {};
/// ```
/// * as a directive within the [include_cpp] macro, in which case you
/// * as a directive within the [crate::include_cpp] macro, in which case you
/// must provide two arguments of the superclass and then the
/// subclass:
/// ```
Expand Down