Skip to content

Commit

Permalink
Support multiple AUTOCXX_RS_JSON_ARCHIVE entries
Browse files Browse the repository at this point in the history
This is helpful when using relative paths with doctests, because
doctests are compiled from a different working directory than the crate
itself. Bazel requires relative paths in environment variables so that
caching works across machines, for example.
  • Loading branch information
bsilver8192 committed Aug 14, 2022
1 parent 37353fa commit c3c0204
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
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
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

0 comments on commit c3c0204

Please sign in to comment.