Skip to content

Commit

Permalink
qt-build-utils: Add an option to use CMake instead of QMake
Browse files Browse the repository at this point in the history
This is needed for scenarios where the CMake packaging for Qt is better
than what QMake can handle, e.g. split Qt installations where the
modules live in different directories.

A new optional feature is added to cxx-qt-build to prefer CMake for most
operations related to linking/including Qt modules. QMake is still the
default.

Fixes #1153.
  • Loading branch information
redstrate committed Jan 14, 2025
1 parent f5afefe commit 0fb6d0b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/KDAB/cxx-qt/compare/v0.7.0...HEAD)

### Added

- Optional feature `cmake` to cxx-qt-build to prefer CMake instead of QMake for finding and linking to Qt.

### Fixed

- Build warnings due to unused unsafe blocks since CXX 1.0.130
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 = { path = "/home/josh/sources/cmake-package-rs", 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 = ["cmake-package"]

[lints]
workspace = true
39 changes: 39 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,27 @@ 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 +612,24 @@ 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"]

0 comments on commit 0fb6d0b

Please sign in to comment.