Struct qt_build_utils::QtBuild
source · pub struct QtBuild { /* private fields */ }Expand description
Helper for build.rs scripts using Qt
let qt_modules = vec!["Core", "Gui"]
.iter()
.map(|m| String::from(*m))
.collect();
let qtbuild = qt_build_utils::QtBuild::new(qt_modules).expect("Could not find Qt installation");Implementations§
source§impl QtBuild
impl QtBuild
sourcepub fn new(qt_modules: Vec<String>) -> Result<Self, QtBuildError>
pub fn new(qt_modules: Vec<String>) -> Result<Self, QtBuildError>
Search for where Qt is installed using qmake. Specify the Qt modules you are
linking with the qt_modules parameter, ommitting the Qt prefix ("Core"
rather than "QtCore"). After construction, use the QtBuild::qmake_query
method to get information about the Qt installation.
The directories specified by the PATH environment variable are where qmake is
searched for. Alternatively, the QMAKE environment variable may be set to specify
an explicit path to qmake.
If multiple major versions (for example, 5 and 6) of Qt could be installed, set
the QT_VERSION_MAJOR environment variable to force which one to use. When using Cargo
as the build system for the whole build, prefer using QT_VERSION_MAJOR over the QMAKE
environment variable because it will account for different names for the qmake executable
that some Linux distributions use.
However, when building a Rust staticlib that gets linked to C++ code by a C++ build
system, it is best to use the QMAKE environment variable to ensure that the Rust
staticlib is linked to the same installation of Qt that the C++ build system has
detected. With CMake, you can get this from the Qt::qmake target’s IMPORTED_LOCATION
property, for example:
find_package(Qt6 COMPONENTS Core)
if(NOT Qt6_FOUND)
find_package(Qt5 5.15 COMPONENTS Core REQUIRED)
endif()
get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
execute_process(
COMMAND cmake -E env
"CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}/cargo"
"QMAKE=${QMAKE}"
cargo build
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
sourcepub fn qmake_query(&self, var_name: &str) -> String
pub fn qmake_query(&self, var_name: &str) -> String
Get the output of running qmake -query var_name
sourcepub fn cargo_link_libraries(&self, builder: &mut Build)
pub fn cargo_link_libraries(&self, builder: &mut Build)
Tell Cargo to link each Qt module.
sourcepub fn include_paths(&self) -> Vec<PathBuf>
pub fn include_paths(&self) -> Vec<PathBuf>
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.
sourcepub fn moc(
&mut self,
input_file: impl AsRef<Path>,
uri: Option<&str>
) -> MocProducts
pub fn moc( &mut self, input_file: impl AsRef<Path>, uri: Option<&str> ) -> MocProducts
Run moc on a C++ header file and save the output into cargo’s OUT_DIR. The return value contains the path to the generated C++ file, which can then be passed to cc::Build::files, as well as the path to the generated metatypes.json file, which can be passed to register_qml_module.
- uri - Should be passed if the input_file is part of a QML module
sourcepub fn register_qml_module(
&mut self,
metatypes_json: &[impl AsRef<Path>],
uri: &str,
version_major: usize,
version_minor: usize,
plugin_name: &str,
qml_files: &[impl AsRef<Path>],
qrc_files: &[impl AsRef<Path>]
) -> QmlModuleRegistrationFiles
pub fn register_qml_module( &mut self, metatypes_json: &[impl AsRef<Path>], uri: &str, version_major: usize, version_minor: usize, plugin_name: &str, qml_files: &[impl AsRef<Path>], qrc_files: &[impl AsRef<Path>] ) -> QmlModuleRegistrationFiles
Generate C++ files to automatically register a QML module at build time using the JSON output from moc.
This generates a qmldir file for the QML module.
The qml_files and qrc_files are registered with the Qt Resource System in
the default QML import path qrc:/qt/qml/uri/of/module/.
When using Qt 6, this will run qmlcachegen to compile the specified .qml files ahead-of-time.
sourcepub fn qrc(&mut self, input_file: &impl AsRef<Path>) -> PathBuf
pub fn qrc(&mut self, input_file: &impl AsRef<Path>) -> PathBuf
Run rcc on a .qrc file and save the output into cargo’s OUT_DIR.
The path to the generated C++ file is returned, which can then be passed to cc::Build::files.
The compiled static library must be linked with +whole-archive
or the linker will discard the generated static variables because they are not referenced from main.