Skip to content

Commit

Permalink
cxx-qt-build: don't fail build script on parse error
Browse files Browse the repository at this point in the history
so rust-analyzer can make use of the error during macro expansion
  • Loading branch information
Be-ing committed Feb 9, 2023
1 parent 49fce91 commit fea483d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 24 additions & 14 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct GeneratedCpp {

impl GeneratedCpp {
/// Generate QObject and cxx header/source C++ file contents
pub fn new(rust_file_path: impl AsRef<Path>) -> Self {
pub fn new(rust_file_path: impl AsRef<Path>) -> cxx_qt_gen::Result<Self> {
let rust_file_path = rust_file_path.as_ref();
let file = parse_qt_file(rust_file_path).unwrap();

Expand Down Expand Up @@ -90,13 +90,13 @@ impl GeneratedCpp {
rust_file_path.display());
}

let parser = Parser::from(m.clone()).unwrap();
let generated_cpp = GeneratedCppBlocks::from(&parser).unwrap();
let parser = Parser::from(m.clone())?;
let generated_cpp = GeneratedCppBlocks::from(&parser)?;
// TODO: we'll have to extend the C++ data here rather than overwriting
// assuming we share the same file
cxx_qt = Some(write_cpp(&generated_cpp));

let generated_rust = GeneratedRustBlocks::from(&parser).unwrap();
let generated_rust = GeneratedRustBlocks::from(&parser)?;
let rust_tokens = write_rust(&generated_rust);
file_ident = parser.cxx_file_stem.clone();
for (_, qobject) in parser.cxx_qt_data.qobjects {
Expand All @@ -119,12 +119,12 @@ impl GeneratedCpp {
let cxx = cxx_gen::generate_header_and_cc(tokens, &opt)
.expect("Could not generate C++ from Rust file");

GeneratedCpp {
Ok(GeneratedCpp {
cxx_qt,
cxx,
file_ident,
qml_metadata,
}
})
}

/// Write generated .cpp and .h files to specified directories. Returns the paths of all files written.
Expand Down Expand Up @@ -210,7 +210,7 @@ impl GeneratedCpp {
fn generate_cxxqt_cpp_files(
rs_source: &[PathBuf],
header_dir: impl AsRef<Path>,
) -> Vec<GeneratedCppFilePaths> {
) -> cxx_qt_gen::Result<Vec<GeneratedCppFilePaths>> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

let mut generated_file_paths: Vec<GeneratedCppFilePaths> = Vec::with_capacity(rs_source.len());
Expand All @@ -219,11 +219,11 @@ fn generate_cxxqt_cpp_files(
let path = format!("{manifest_dir}/{}", rs_path.display());
println!("cargo:rerun-if-changed={path}");

let generated_code = GeneratedCpp::new(&path);
let generated_code = GeneratedCpp::new(&path)?;
generated_file_paths.push(generated_code.write_to_directories(cpp_directory, &header_dir));
}

generated_file_paths
Ok(generated_file_paths)
}

/// Run cxx-qt's C++ code generator on Rust modules marked with the [cxx_qt::bridge] macro, compile
Expand Down Expand Up @@ -426,11 +426,21 @@ impl CxxQtBuilder {
}

// Generate files
for files in generate_cxxqt_cpp_files(&self.rust_sources, &generated_header_dir) {
self.cc_builder.file(files.plain_cpp);
if let (Some(qobject), Some(qobject_header)) = (files.qobject, files.qobject_header) {
self.cc_builder.file(&qobject);
self.qobject_headers.push(qobject_header);
match generate_cxxqt_cpp_files(&self.rust_sources, &generated_header_dir) {
Ok(generated_files) => for files in generated_files {
self.cc_builder.file(files.plain_cpp);
if let (Some(qobject), Some(qobject_header)) = (files.qobject, files.qobject_header) {
self.cc_builder.file(&qobject);
self.qobject_headers.push(qobject_header);
}
},
Err(e) => {
// Don't panic here because rust-analyzer would fail with a verbose backtrace
// when running the build script. The same error will be encountered when the proc_macro
// expands after the build script runs, which allows rust-analyzer to make sense of the
// error and point the user to the code causing the problem.
println!("cargo:warning=cxx-qt-build failed to parse cxx_qt::bridge: {e:?}");
return;
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub use parser::{qobject::QmlElementMetadata, Parser};
pub use syntax::{parse_qt_file, CxxQtItem};
pub use writer::{cpp::write_cpp, rust::write_rust};

pub use syn::{Error, Result};

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit fea483d

Please sign in to comment.