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

qt-build-utils: Add an option to use CMake instead of QMake #1156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `QDateTime::from_string` to parse `QDateTime` from a `QString`.
- Support for further types: `QUuid`
- Optional feature `cmake` to cxx-qt-build to prefer CMake instead of QMake for finding and linking to Qt.

### Fixed

Expand Down
1 change: 1 addition & 0 deletions crates/cxx-qt-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde_json = "1.0"

[features]
link_qt_object_files = ["qt-build-utils/link_qt_object_files"]
cmake = ["qt-build-utils/cmake"]

[lints]
workspace = true
5 changes: 5 additions & 0 deletions crates/qt-build-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rust-version.workspace = true
cc.workspace = true
versions = "6.3"
thiserror.workspace = true
cmake-package = { version = "0.1.5", optional = true }

[features]
# When Cargo links an executable, whether a bin crate or test executable,
Expand All @@ -31,5 +32,9 @@ thiserror.workspace = true
# When linking Qt dynamically, this makes no difference.
link_qt_object_files = []

# Prefer using CMake to find Qt modules, instead of using qmake.
# This may be desirable in certain installations.
cmake = ["dep:cmake-package"]

[lints]
workspace = true
45 changes: 45 additions & 0 deletions crates/qt-build-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,30 @@ impl QtBuild {
/// Get the include paths for Qt, including Qt module subdirectories. This is intended
/// to be passed to whichever tool you are using to invoke the C++ compiler.
pub fn include_paths(&self) -> Vec<PathBuf> {
#[cfg(feature = "cmake")]
{
let Ok(package) = cmake_package::find_package(format!("Qt{}", self.version.major))
.components(self.qt_modules.clone())
.find()
else {
return Vec::default();
};

let mut paths = Vec::new();
for qt_module in &self.qt_modules {
if let Some(target) = package.target(format!("Qt::{}", qt_module)) {
paths.extend(target.include_directories);
}
}

return paths
.iter()
.map(PathBuf::from)
// Only add paths if they exist
.filter(|path| path.exists())
.collect();
}

let root_path = self.qmake_query("QT_INSTALL_HEADERS");
let lib_path = self.qmake_query("QT_INSTALL_LIBS");
let mut paths = Vec::new();
Expand Down Expand Up @@ -591,6 +615,27 @@ impl QtBuild {
/// Lazy load the path of a Qt executable tool
/// Skip doing this in the constructor because not every user of this crate will use each tool
fn get_qt_tool(&self, tool_name: &str) -> Result<String, ()> {
#[cfg(feature = "cmake")]
{
// QML tools live in the QML target, of course
let tools_component = if tool_name.contains("qml") {
"QmlTools"
} else {
"CoreTools"
};

let Ok(package) = cmake_package::find_package(format!("Qt{}", self.version.major))
.components([tools_component.into()])
.find()
else {
return Err(());
};
let Some(target) = package.target(format!("Qt6::{}", tool_name)) else {
return Err(());
};
return target.location.ok_or(());
}

// "qmake -query" exposes a list of paths that describe where Qt executables and libraries
// are located, as well as where new executables & libraries should be installed to.
// We can use these variables to find any Qt tool.
Expand Down
3 changes: 3 additions & 0 deletions examples/cargo_without_cmake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ cxx-qt-lib = { workspace = true, features = ["full"] }
# Use `cxx-qt-build = "0.7"` here instead!
# The link_qt_object_files feature is required for statically linking Qt 6.
cxx-qt-build = { workspace = true, features = [ "link_qt_object_files" ] }

[features]
cmake = ["cxx-qt-build/cmake"]
Loading