diff --git a/crates/cxx-qt-build/src/lib.rs b/crates/cxx-qt-build/src/lib.rs index c85870a63..0de739f2f 100644 --- a/crates/cxx-qt-build/src/lib.rs +++ b/crates/cxx-qt-build/src/lib.rs @@ -287,6 +287,7 @@ fn panic_duplicate_file_and_qml_module( pub struct CxxQtBuilder { rust_sources: Vec, qobject_headers: Vec, + qobject_header_includes: Vec, qrc_files: Vec, qt_modules: HashSet, qml_modules: Vec, @@ -305,6 +306,7 @@ impl CxxQtBuilder { Self { rust_sources: vec![], qobject_headers: vec![], + qobject_header_includes: vec![], qrc_files: vec![], qt_modules, qml_modules: vec![], @@ -417,6 +419,13 @@ impl CxxQtBuilder { self } + /// Specify additional include paths when running the moc compiler (qobject_header). + pub fn qobject_header_include(mut self, path: impl AsRef) -> Self { + let path = path.as_ref(); + self.qobject_header_includes.push(path.to_owned()); + self + } + /// Use a closure to run additional customization on [CxxQtBuilder]'s internal [cc::Build] /// before calling [CxxQtBuilder::build]. This allows to add extra include paths, compiler flags, /// or anything else available via [cc::Build]'s API. For example, to add an include path for @@ -534,7 +543,7 @@ impl CxxQtBuilder { // Run moc on C++ headers with Q_OBJECT macro for qobject_header in self.qobject_headers { - let moc_products = qtbuild.moc(&qobject_header, None); + let moc_products = qtbuild.moc(&qobject_header, None, &self.qobject_header_includes); self.cc_builder.file(moc_products.cpp); } @@ -552,7 +561,7 @@ impl CxxQtBuilder { if let (Some(qobject), Some(qobject_header)) = (files.qobject, files.qobject_header) { self.cc_builder.file(&qobject); - let moc_products = qtbuild.moc(qobject_header, Some(&qml_module.uri)); + let moc_products = qtbuild.moc(qobject_header, Some(&qml_module.uri), &vec![]); self.cc_builder.file(moc_products.cpp); qml_metatypes_json.push(moc_products.metatypes_json); } diff --git a/crates/qt-build-utils/src/lib.rs b/crates/qt-build-utils/src/lib.rs index 82e5fe63c..635646b0f 100644 --- a/crates/qt-build-utils/src/lib.rs +++ b/crates/qt-build-utils/src/lib.rs @@ -531,7 +531,8 @@ impl QtBuild { /// as well as the path to the generated metatypes.json file, which can be passed to [register_qml_module](Self::register_qml_module). /// /// * uri - Should be passed if the input_file is part of a QML module - pub fn moc(&mut self, input_file: impl AsRef, uri: Option<&str>) -> MocProducts { + /// * include_paths - Additional include paths to pass to moc + pub fn moc(&mut self, input_file: impl AsRef, uri: Option<&str>, include_paths: &Vec) -> MocProducts { if self.moc_executable.is_none() { self.moc_executable = Some(self.get_qt_tool("moc").expect("Could not find moc")); } @@ -546,9 +547,14 @@ impl QtBuild { let metatypes_json_path = PathBuf::from(&format!("{}.json", output_path.display())); let mut include_args = String::new(); + // Qt includes for include_path in self.include_paths() { include_args += &format!("-I {} ", include_path.display()); } + // User includes + for include_path in include_paths { + include_args += &format!("-I {} ", include_path.display()); + } let mut cmd = Command::new(self.moc_executable.as_ref().unwrap()); @@ -824,7 +830,7 @@ public: "# ) .unwrap(); - self.moc(&qml_plugin_cpp_path, Some(uri)); + self.moc(&qml_plugin_cpp_path, Some(uri), &vec![]); // Generate file to load static QQmlExtensionPlugin let mut qml_plugin_init = File::create(&qml_plugin_init_path).unwrap();