Skip to content

Commit

Permalink
cxx-qt-lib: use a feature for Gui parts of cxx-qt-lib
Browse files Browse the repository at this point in the history
Closes KDAB#290
  • Loading branch information
ahayzen-kdab committed Jan 10, 2023
1 parent 7ac46df commit 305cbee
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 12 deletions.
8 changes: 6 additions & 2 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ impl CxxQtBuilder {
pub fn new() -> Self {
let mut qt_modules = HashSet::new();
qt_modules.insert("Core".to_owned());
qt_modules.insert("Gui".to_owned());
Self {
rust_sources: vec![],
qobject_headers: vec![],
Expand Down Expand Up @@ -304,7 +303,7 @@ impl CxxQtBuilder {

/// Link additional [Qt modules](https://doc.qt.io/qt-6/qtmodules.html).
/// Specify their names without the `Qt` prefix, for example `"Widgets"`.
/// The Core and Gui modules are linked automatically; there is no need to specify them.
/// The Core module is linked automatically; there is no need to specify it.
pub fn qt_modules(mut self, modules: &[&str]) -> Self {
self.qt_modules
.extend(modules.iter().cloned().map(String::from));
Expand Down Expand Up @@ -351,6 +350,11 @@ impl CxxQtBuilder {
// GCC + Clang
self.cc_builder.flag_if_supported("-std=c++17");

// Enable Qt Gui in C++ if the feature is enabled
if self.qt_modules.contains("Gui") {
self.cc_builder.define("CXX_QT_GUI_FEATURE", None);
}

let mut qtbuild = qt_build_utils::QtBuild::new(self.qt_modules.into_iter().collect())
.expect("Could not find Qt installation");
qtbuild.cargo_link_libraries();
Expand Down
4 changes: 4 additions & 0 deletions crates/cxx-qt-lib-headers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ edition.workspace = true
license.workspace = true
description = "A small crate for cxx-qt-lib and cxx-qt-build to share cxx-qt-lib's C++ headers"
repository.workspace = true

[features]
default = []
qt_gui = []
4 changes: 4 additions & 0 deletions crates/cxx-qt-lib-headers/include/qvariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <QtCore/QTime>
#include <QtCore/QUrl>

#ifdef CXX_QT_GUI_FEATURE
#include <QtGui/QColor>
#endif

#include "rust/cxx.h"

Expand Down Expand Up @@ -62,7 +64,9 @@ CXX_QT_QVARIANT_CAN_CONVERT(I8)
CXX_QT_QVARIANT_CAN_CONVERT(I16)
CXX_QT_QVARIANT_CAN_CONVERT(I32)
CXX_QT_QVARIANT_CAN_CONVERT(I64)
#ifdef CXX_QT_GUI_FEATURE
CXX_QT_QVARIANT_CAN_CONVERT(QColor)
#endif
CXX_QT_QVARIANT_CAN_CONVERT(QDate)
CXX_QT_QVARIANT_CAN_CONVERT(QDateTime)
CXX_QT_QVARIANT_CAN_CONVERT(QPoint)
Expand Down
5 changes: 5 additions & 0 deletions crates/cxx-qt-lib-headers/include/qvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
#include <QtCore/QTime>
#include <QtCore/QUrl>
#include <QtCore/QVariant>

#ifdef CXX_QT_GUI_FEATURE
#include <QtGui/QColor>
#endif

#include "rust/cxx.h"

Expand Down Expand Up @@ -116,7 +119,9 @@ using QVector_i8 = QVector<::std::int8_t>;
using QVector_i16 = QVector<::std::int16_t>;
using QVector_i32 = QVector<::std::int32_t>;
using QVector_i64 = QVector<::std::int64_t>;
#ifdef CXX_QT_GUI_FEATURE
using QVector_QColor = QVector<::QColor>;
#endif
using QVector_QDate = QVector<::QDate>;
using QVector_QDateTime = QVector<::QDateTime>;
using QVector_QPoint = QVector<::QPoint>;
Expand Down
1 change: 1 addition & 0 deletions crates/cxx-qt-lib-headers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn write_headers(directory: impl AsRef<Path>) {
(include_str!("../include/convert.h"), "convert.h"),
(include_str!("../include/cxxqt_thread.h"), "cxxqt_thread.h"),
(include_str!("../include/qbytearray.h"), "qbytearray.h"),
#[cfg(feature = "qt_gui")]
(include_str!("../include/qcolor.h"), "qcolor.h"),
(include_str!("../include/qdate.h"), "qdate.h"),
(include_str!("../include/qdatetime.h"), "qdatetime.h"),
Expand Down
4 changes: 4 additions & 0 deletions crates/cxx-qt-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ cxx.workspace = true
cxx-build.workspace = true
cxx-qt-lib-headers.workspace = true
qt-build-utils.workspace = true

[features]
default = ["qt_gui"]
qt_gui = ["cxx-qt-lib-headers/qt_gui"]
20 changes: 16 additions & 4 deletions crates/cxx-qt-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

fn main() {
let qt_modules = vec!["Core", "Gui"]
.iter()
.map(|m| String::from(*m))
.collect();
let qt_modules = vec![
"Core",
#[cfg(feature = "qt_gui")]
"Gui",
]
.iter()
.map(|m| String::from(*m))
.collect();
let qtbuild = qt_build_utils::QtBuild::new(qt_modules).expect("Could not find Qt installation");
qtbuild.cargo_link_libraries();

Expand All @@ -20,6 +24,7 @@ fn main() {

let rust_bridges = [
"qbytearray",
#[cfg(feature = "qt_gui")]
"qcolor",
"qdate",
"qdatetime",
Expand Down Expand Up @@ -62,6 +67,7 @@ fn main() {
"qvariant/qvariant_i16",
"qvariant/qvariant_i32",
"qvariant/qvariant_i64",
#[cfg(feature = "qt_gui")]
"qvariant/qvariant_qcolor",
"qvariant/qvariant_qdate",
"qvariant/qvariant_qdatetime",
Expand All @@ -85,6 +91,7 @@ fn main() {
"qvector/qvector_i16",
"qvector/qvector_i32",
"qvector/qvector_i64",
#[cfg(feature = "qt_gui")]
"qvector/qvector_qcolor",
"qvector/qvector_qdate",
"qvector/qvector_qdatetime",
Expand Down Expand Up @@ -121,6 +128,7 @@ fn main() {

let cpp_files = [
"qbytearray",
#[cfg(feature = "qt_gui")]
"qcolor",
"qdate",
"qdatetime",
Expand Down Expand Up @@ -154,6 +162,10 @@ fn main() {
cxx_qt_lib_headers::write_headers(format!("{}/cxx-qt-lib", out_dir));
builder.include(out_dir);

// Enable Qt Gui in C++ if the feature is enabled
#[cfg(feature = "qt_gui")]
builder.define("CXX_QT_GUI_FEATURE", None);

// MSVC
builder.flag_if_supported("/std:c++17");
builder.flag_if_supported("/Zc:__cplusplus");
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
mod qbytearray;
pub use qbytearray::QByteArray;

#[cfg(feature = "qt_gui")]
mod qcolor;
#[cfg(feature = "qt_gui")]
pub use qcolor::QColor;

mod qdate;
Expand Down
5 changes: 4 additions & 1 deletion crates/cxx-qt-lib/src/types/qvariant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use cxx::{type_id, ExternType};
use std::mem::MaybeUninit;

#[cfg(feature = "qt_gui")]
use crate::QColor;
use crate::{
QColor, QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl,
QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl,
};

#[cxx::bridge]
Expand Down Expand Up @@ -143,6 +145,7 @@ impl_qvariant_value!(i8, qvariant_i8);
impl_qvariant_value!(i16, qvariant_i16);
impl_qvariant_value!(i32, qvariant_i32);
impl_qvariant_value!(i64, qvariant_i64);
#[cfg(feature = "qt_gui")]
impl_qvariant_value!(QColor, qvariant_qcolor);
impl_qvariant_value!(QDate, qvariant_qdate);
impl_qvariant_value!(QDateTime, qvariant_qdatetime);
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/src/types/qvariant/qvariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int8_t, I8)
CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int16_t, I16)
CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int32_t, I32)
CXX_QT_QVARIANT_CAN_CONVERT_IMPL(::std::int64_t, I64)
#ifdef CXX_QT_GUI_FEATURE
CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QColor, QColor)
#endif
CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QDate, QDate)
CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QDateTime, QDateTime)
CXX_QT_QVARIANT_CAN_CONVERT_IMPL(QPoint, QPoint)
Expand Down
8 changes: 6 additions & 2 deletions crates/cxx-qt-lib/src/types/qvector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#[cfg(feature = "qt_gui")]
use crate::QColor;
use crate::{
QColor, QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl,
QVariant,
QDate, QDateTime, QPoint, QPointF, QRect, QRectF, QSize, QSizeF, QString, QTime, QUrl, QVariant,
};
use core::{marker::PhantomData, mem::MaybeUninit};
use cxx::{type_id, ExternType};
Expand All @@ -16,6 +18,7 @@ mod qvector_i16;
mod qvector_i32;
mod qvector_i64;
mod qvector_i8;
#[cfg(feature = "qt_gui")]
mod qvector_qcolor;
mod qvector_qdate;
mod qvector_qdatetime;
Expand Down Expand Up @@ -313,6 +316,7 @@ impl_qvector_element!(i8, qvector_i8, "QVector_i8");
impl_qvector_element!(i16, qvector_i16, "QVector_i16");
impl_qvector_element!(i32, qvector_i32, "QVector_i32");
impl_qvector_element!(i64, qvector_i64, "QVector_i64");
#[cfg(feature = "qt_gui")]
impl_qvector_element!(QColor, qvector_qcolor, "QVector_QColor");
impl_qvector_element!(QDate, qvector_qdate, "QVector_QDate");
impl_qvector_element!(QDateTime, qvector_qdatetime, "QVector_QDateTime");
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/src/types/qvector/qvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ CXX_QT_QVECTOR_ASSERTS(::std::int8_t, i8);
CXX_QT_QVECTOR_ASSERTS(::std::int16_t, i16);
CXX_QT_QVECTOR_ASSERTS(::std::int32_t, i32);
CXX_QT_QVECTOR_ASSERTS(::std::int64_t, i64);
#ifdef CXX_QT_GUI_FEATURE
CXX_QT_QVECTOR_ASSERTS(::QColor, QColor);
#endif
CXX_QT_QVECTOR_ASSERTS(::QDate, QDate);
CXX_QT_QVECTOR_ASSERTS(::QDateTime, QDateTime);
CXX_QT_QVECTOR_ASSERTS(::QPoint, QPoint);
Expand Down
6 changes: 3 additions & 3 deletions examples/cargo_without_cmake/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fn command_help_output(command: &str) -> std::io::Result<std::process::Output> {

fn main() {
CxxQtBuilder::new()
// Link Qt's Qml and Network libraries. Qt Core and Gui are always
// linked, so there is no need to specify them here.
.qt_modules(&["Qml", "Network"])
// Link Qt's Gui, Qml and Network libraries. Qt Core is always
// linked, so there is no need to specify it here.
.qt_modules(&["Gui", "Qml", "Network"])
// Generate C++ from the `#[cxx_qt::bridge]` module
.file("src/cxxqt_object.rs")
// Generate C++ code from the .qrc file with the rcc tool
Expand Down
1 change: 1 addition & 0 deletions examples/qml_features/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn main() {
.cc_builder(|cc| {
cc.include("../cpp");
})
.qt_modules(&["Gui"])
.file("src/containers.rs")
.file("src/custom_base_class.rs")
.file("src/invokables.rs")
Expand Down
1 change: 1 addition & 0 deletions tests/qt_types_standalone/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cxx_qt_build::CxxQtBuilder;

fn main() {
CxxQtBuilder::new()
.qt_modules(&["Gui"])
.file("src/qbytearray.rs")
.file("src/qcolor.rs")
.file("src/qdate.rs")
Expand Down

0 comments on commit 305cbee

Please sign in to comment.