Skip to content

Commit fefe5fc

Browse files
committed
qt-build-utils: fix docs and add new method with QtInstallation arg
1 parent 1ad252a commit fefe5fc

File tree

5 files changed

+58
-51
lines changed

5 files changed

+58
-51
lines changed

crates/cxx-qt-build/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1119,8 +1119,10 @@ extern "C" bool {init_fun}() {{
11191119

11201120
let header_root = dir::header_root();
11211121

1122-
let mut qtbuild = qt_build_utils::QtBuild::new(qt_modules.iter().cloned().collect())
1123-
.expect("Could not find Qt installation");
1122+
let mut qtbuild = qt_build_utils::QtBuild::new_with_default_installation(
1123+
qt_modules.iter().cloned().collect(),
1124+
)
1125+
.expect("Could not find Qt installation");
11241126
qtbuild.cargo_link_libraries(&mut self.cc_builder);
11251127
Self::define_qt_version_cfg_variables(qtbuild.version());
11261128

crates/cxx-qt-lib/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn write_headers() {
9999
}
100100

101101
fn main() {
102-
let qtbuild = qt_build_utils::QtBuild::new(vec!["Core".to_owned()])
102+
let qtbuild = qt_build_utils::QtBuild::new_with_default_installation(vec!["Core".to_owned()])
103103
.expect("Could not find Qt installation");
104104

105105
write_headers();

crates/qt-build-utils/src/installation/qmake.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,46 @@ use std::{
1313

1414
use crate::{parse_cflags, utils, QtBuildError, QtInstallation, QtTool};
1515

16-
/// TODO
16+
/// A implementation of [QtInstallation] using qmake
1717
pub struct QtInstallationQMake {
1818
qmake_path: PathBuf,
1919
qmake_version: Version,
2020
}
2121

2222
impl QtInstallationQMake {
23-
/// TODO
23+
/// The directories specified by the `PATH` environment variable are where qmake is
24+
/// searched for. Alternatively, the `QMAKE` environment variable may be set to specify
25+
/// an explicit path to qmake.
26+
///
27+
/// If multiple major versions (for example, `5` and `6`) of Qt could be installed, set
28+
/// the `QT_VERSION_MAJOR` environment variable to force which one to use. When using Cargo
29+
/// as the build system for the whole build, prefer using `QT_VERSION_MAJOR` over the `QMAKE`
30+
/// environment variable because it will account for different names for the qmake executable
31+
/// that some Linux distributions use.
32+
///
33+
/// However, when building a Rust staticlib that gets linked to C++ code by a C++ build
34+
/// system, it is best to use the `QMAKE` environment variable to ensure that the Rust
35+
/// staticlib is linked to the same installation of Qt that the C++ build system has
36+
/// detected.
37+
/// With CMake, this will automatically be set up for you when using cxxqt_import_crate.
38+
///
39+
/// Alternatively, you can get this from the `Qt::qmake` target's `IMPORTED_LOCATION`
40+
/// property, for example:
41+
/// ```cmake
42+
/// find_package(Qt6 COMPONENTS Core)
43+
/// if(NOT Qt6_FOUND)
44+
/// find_package(Qt5 5.15 COMPONENTS Core REQUIRED)
45+
/// endif()
46+
/// get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
47+
///
48+
/// execute_process(
49+
/// COMMAND cmake -E env
50+
/// "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}/cargo"
51+
/// "QMAKE=${QMAKE}"
52+
/// cargo build
53+
/// WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
54+
/// )
55+
/// ```
2456
pub fn new() -> anyhow::Result<Self> {
2557
// Try the QMAKE variable first
2658
println!("cargo::rerun-if-env-changed=QMAKE");

crates/qt-build-utils/src/lib.rs

+18-45
Original file line numberDiff line numberDiff line change
@@ -236,58 +236,31 @@ pub struct QtBuild {
236236
}
237237

238238
impl QtBuild {
239-
/// Search for where Qt is installed using qmake. Specify the Qt modules you are
240-
/// linking with the `qt_modules` parameter, ommitting the `Qt` prefix (`"Core"`
241-
/// rather than `"QtCore"`). After construction, use the [QtBuild::qmake_query]
242-
/// method to get information about the Qt installation.
243-
///
244-
/// The directories specified by the `PATH` environment variable are where qmake is
245-
/// searched for. Alternatively, the `QMAKE` environment variable may be set to specify
246-
/// an explicit path to qmake.
247-
///
248-
/// If multiple major versions (for example, `5` and `6`) of Qt could be installed, set
249-
/// the `QT_VERSION_MAJOR` environment variable to force which one to use. When using Cargo
250-
/// as the build system for the whole build, prefer using `QT_VERSION_MAJOR` over the `QMAKE`
251-
/// environment variable because it will account for different names for the qmake executable
252-
/// that some Linux distributions use.
253-
///
254-
/// However, when building a Rust staticlib that gets linked to C++ code by a C++ build
255-
/// system, it is best to use the `QMAKE` environment variable to ensure that the Rust
256-
/// staticlib is linked to the same installation of Qt that the C++ build system has
257-
/// detected.
258-
/// With CMake, this will automatically be set up for you when using cxxqt_import_crate.
259-
///
260-
/// Alternatively, you can get this from the `Qt::qmake` target's `IMPORTED_LOCATION`
261-
/// property, for example:
262-
/// ```cmake
263-
/// find_package(Qt6 COMPONENTS Core)
264-
/// if(NOT Qt6_FOUND)
265-
/// find_package(Qt5 5.15 COMPONENTS Core REQUIRED)
266-
/// endif()
267-
/// get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
268-
///
269-
/// execute_process(
270-
/// COMMAND cmake -E env
271-
/// "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}/cargo"
272-
/// "QMAKE=${QMAKE}"
273-
/// cargo build
274-
/// WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
275-
/// )
276-
/// ```
277-
pub fn new(mut qt_modules: Vec<String>) -> anyhow::Result<Self> {
278-
if qt_modules.is_empty() {
279-
qt_modules.push("Core".to_string());
280-
}
281-
239+
/// Create a [QtBuild] using the default [QtInstallation] (currently uses [QtInstallationQMake])
240+
/// and specify which Qt modules you are linking, ommitting the `Qt` prefix (`"Core"`
241+
/// rather than `"QtCore"`).
242+
//
243+
// TODO: is there a better name for this method or a sane way to create a default QtInstallation?
244+
pub fn new_with_default_installation(qt_modules: Vec<String>) -> anyhow::Result<Self> {
282245
#[cfg(feature = "qmake")]
283246
let qt_installation = Box::new(QtInstallationQMake::new()?);
284247
#[cfg(not(feature = "qmake"))]
285248
unsupported!("Only qmake feature is supported");
286249

287-
Ok(Self {
250+
Ok(Self::new(qt_installation, qt_modules))
251+
}
252+
253+
/// Create a [QtBuild] using the given [QtInstallation] and specify which
254+
/// Qt modules you are linking, ommitting the `Qt` prefix (`"Core"` rather than `"QtCore"`).
255+
pub fn new(qt_installation: Box<dyn QtInstallation>, mut qt_modules: Vec<String>) -> Self {
256+
if qt_modules.is_empty() {
257+
qt_modules.push("Core".to_string());
258+
}
259+
260+
Self {
288261
qt_installation,
289262
qt_modules,
290-
})
263+
}
291264
}
292265

293266
/// Tell Cargo to link each Qt module.

tests/qt_types_standalone/rust/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use cxx_qt_build::CxxQtBuilder;
77

88
fn main() {
9-
let qtbuild = qt_build_utils::QtBuild::new(vec!["Core".to_owned()])
9+
let qtbuild = qt_build_utils::QtBuild::new_with_default_installation(vec!["Core".to_owned()])
1010
.expect("Could not find Qt installation");
1111

1212
let mut builder = CxxQtBuilder::new()

0 commit comments

Comments
 (0)