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

Add qobject_header_include() to add support for moc -I<path> #680

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 11 additions & 2 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ fn panic_duplicate_file_and_qml_module(
pub struct CxxQtBuilder {
rust_sources: Vec<PathBuf>,
qobject_headers: Vec<PathBuf>,
qobject_header_includes: Vec<PathBuf>,
qrc_files: Vec<PathBuf>,
qt_modules: HashSet<String>,
qml_modules: Vec<OwningQmlModule>,
Expand All @@ -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![],
Expand Down Expand Up @@ -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<Path>) -> 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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions crates/qt-build-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>, uri: Option<&str>) -> MocProducts {
/// * include_paths - Additional include paths to pass to moc
pub fn moc(&mut self, input_file: impl AsRef<Path>, uri: Option<&str>, include_paths: &Vec<PathBuf>) -> MocProducts {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should go through the other moc arguments while we are here and consider which others might be useful.

if self.moc_executable.is_none() {
self.moc_executable = Some(self.get_qt_tool("moc").expect("Could not find moc"));
}
Expand All @@ -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());

Expand Down Expand Up @@ -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();
Expand Down