From 4f929ab3864c3b799cf03e9a46e3e68b634654f7 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Wed, 28 Jun 2023 18:38:40 +0100 Subject: [PATCH] WIP: examples: create a multi project to test multiple crates --- Cargo.toml | 3 + examples/CMakeLists.txt | 1 + examples/meta_project/CMakeLists.txt | 60 +++++++++++++++++++ examples/meta_project/cpp/main.cpp | 31 ++++++++++ examples/meta_project/qml/main.qml | 60 +++++++++++++++++++ examples/meta_project/qml/qml.qrc | 12 ++++ examples/meta_project/rust/main/Cargo.toml | 24 ++++++++ examples/meta_project/rust/main/build.rs | 10 ++++ examples/meta_project/rust/main/src/lib.rs | 6 ++ .../meta_project/rust/main/src/main_object.rs | 42 +++++++++++++ examples/meta_project/rust/sub1/Cargo.toml | 24 ++++++++ examples/meta_project/rust/sub1/build.rs | 10 ++++ examples/meta_project/rust/sub1/src/lib.rs | 16 +++++ .../meta_project/rust/sub1/src/sub1_object.rs | 39 ++++++++++++ examples/meta_project/rust/sub2/Cargo.toml | 24 ++++++++ examples/meta_project/rust/sub2/build.rs | 10 ++++ examples/meta_project/rust/sub2/src/lib.rs | 16 +++++ .../meta_project/rust/sub2/src/sub2_object.rs | 39 ++++++++++++ 18 files changed, 427 insertions(+) create mode 100644 examples/meta_project/CMakeLists.txt create mode 100644 examples/meta_project/cpp/main.cpp create mode 100644 examples/meta_project/qml/main.qml create mode 100644 examples/meta_project/qml/qml.qrc create mode 100644 examples/meta_project/rust/main/Cargo.toml create mode 100644 examples/meta_project/rust/main/build.rs create mode 100644 examples/meta_project/rust/main/src/lib.rs create mode 100644 examples/meta_project/rust/main/src/main_object.rs create mode 100644 examples/meta_project/rust/sub1/Cargo.toml create mode 100644 examples/meta_project/rust/sub1/build.rs create mode 100644 examples/meta_project/rust/sub1/src/lib.rs create mode 100644 examples/meta_project/rust/sub1/src/sub1_object.rs create mode 100644 examples/meta_project/rust/sub2/Cargo.toml create mode 100644 examples/meta_project/rust/sub2/build.rs create mode 100644 examples/meta_project/rust/sub2/src/lib.rs create mode 100644 examples/meta_project/rust/sub2/src/sub2_object.rs diff --git a/Cargo.toml b/Cargo.toml index ccd960d76..522284f69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,9 @@ members = [ "examples/qml_extension_plugin/plugin/rust", "examples/qml_features/rust", "examples/qml_minimal/rust", + "examples/meta_project/rust/main", + "examples/meta_project/rust/sub1", + "examples/meta_project/rust/sub2", "tests/basic_cxx_only/rust", "tests/basic_cxx_qt/rust", diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8e606d65b..318180b8d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -9,3 +9,4 @@ add_subdirectory(qml_extension_plugin) add_subdirectory(qml_features) add_subdirectory(qml_minimal) add_subdirectory(demo_threading) +add_subdirectory(meta_project) diff --git a/examples/meta_project/CMakeLists.txt b/examples/meta_project/CMakeLists.txt new file mode 100644 index 000000000..e513de3aa --- /dev/null +++ b/examples/meta_project/CMakeLists.txt @@ -0,0 +1,60 @@ +# SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +# SPDX-FileContributor: Andrew Hayzen +# +# SPDX-License-Identifier: MIT OR Apache-2.0 + +cmake_minimum_required(VERSION 3.24) + +project(example_meta_project) +set(APP_NAME ${PROJECT_NAME}) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +if(NOT USE_QT5) + find_package(Qt6 COMPONENTS Core Gui Qml QuickControls2 QmlImportScanner QuickTest Test) +endif() +if(NOT Qt6_FOUND) + find_package(Qt5 5.15 COMPONENTS Core Gui Qml QuickControls2 QmlImportScanner QuickTest Test REQUIRED) +endif() +get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION) + +find_package(Corrosion QUIET) +if(NOT Corrosion_FOUND) + include(FetchContent) + FetchContent_Declare( + Corrosion + GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git + GIT_TAG v0.4.1 + ) + + FetchContent_MakeAvailable(Corrosion) +endif() + +set(CRATE qml-meta-project) +corrosion_import_crate(MANIFEST_PATH rust/main/Cargo.toml CRATES ${CRATE}) +set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt") +corrosion_set_env_vars(${CRATE} + "CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}" + "QMAKE=${QMAKE}" +) +add_library(${APP_NAME}_lib INTERFACE) +target_include_directories(${APP_NAME}_lib INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}") +target_link_libraries(${APP_NAME}_lib INTERFACE + "$" + Qt::Core + Qt::Gui + Qt::Qml + Qt::QuickControls2 +) + +add_executable(${APP_NAME} + cpp/main.cpp + qml/qml.qrc +) + +target_include_directories(${APP_NAME} PRIVATE cpp) +target_link_libraries(${APP_NAME} PRIVATE ${APP_NAME}_lib) +qt_import_qml_plugins(${APP_NAME}) diff --git a/examples/meta_project/cpp/main.cpp b/examples/meta_project/cpp/main.cpp new file mode 100644 index 000000000..cf615d210 --- /dev/null +++ b/examples/meta_project/cpp/main.cpp @@ -0,0 +1,31 @@ +// clang-format off +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#include +#include + +int +main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + + const QUrl url(QStringLiteral("qrc:/main.qml")); + QObject::connect( + &engine, + &QQmlApplicationEngine::objectCreated, + &app, + [url](QObject* obj, const QUrl& objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, + Qt::QueuedConnection); + + engine.load(url); + + return app.exec(); +} diff --git a/examples/meta_project/qml/main.qml b/examples/meta_project/qml/main.qml new file mode 100644 index 000000000..18301e50f --- /dev/null +++ b/examples/meta_project/qml/main.qml @@ -0,0 +1,60 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +import QtQuick 2.12 +import QtQuick.Controls 2.12 +import QtQuick.Layouts 1.12 +import QtQuick.Window 2.12 + +import com.kdab.cxx_qt.demo 1.0 +import com.kdab.cxx_qt.demo.sub1 1.0 +import com.kdab.cxx_qt.demo.sub2 1.0 + +ApplicationWindow { + id: window + minimumHeight: 480 + minimumWidth: 640 + title: qsTr("CXX-Qt: Hello World") + visible: true + + MainObject { + id: main + } + + Sub1Object { + id: sub1 + } + + Sub2Object { + id: sub2 + } + + Column { + anchors.fill: parent + anchors.margins: 10 + spacing: 10 + + Label { + text: "Main: " + main.string + } + + Label { + text: "Sub1: " + sub1.string + } + + Label { + text: "Sub2: " + sub2.string + } + + Button { + text: "Increment Number" + + onClicked: { + main.increment(); + sub1.increment(); + sub2.increment(); + } + } + } +} diff --git a/examples/meta_project/qml/qml.qrc b/examples/meta_project/qml/qml.qrc new file mode 100644 index 000000000..69e2bfdf5 --- /dev/null +++ b/examples/meta_project/qml/qml.qrc @@ -0,0 +1,12 @@ + + + + + main.qml + + diff --git a/examples/meta_project/rust/main/Cargo.toml b/examples/meta_project/rust/main/Cargo.toml new file mode 100644 index 000000000..674e023a6 --- /dev/null +++ b/examples/meta_project/rust/main/Cargo.toml @@ -0,0 +1,24 @@ +# SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +# SPDX-FileContributor: Andrew Hayzen +# +# SPDX-License-Identifier: MIT OR Apache-2.0 +[package] +name = "qml-meta-project" +version = "0.1.0" +authors = ["Andrew Hayzen "] +edition = "2021" +license = "MIT OR Apache-2.0" + +[lib] +crate-type = ["staticlib"] + +[dependencies] +sub1 = { path = "../sub1" } +sub2 = { path = "../sub2" } + +cxx.workspace = true +cxx-qt.workspace = true +cxx-qt-lib.workspace = true + +[build-dependencies] +cxx-qt-build.workspace = true diff --git a/examples/meta_project/rust/main/build.rs b/examples/meta_project/rust/main/build.rs new file mode 100644 index 000000000..796d649b1 --- /dev/null +++ b/examples/meta_project/rust/main/build.rs @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx_qt_build::CxxQtBuilder; + +fn main() { + CxxQtBuilder::new().file("src/main_object.rs").build(); +} diff --git a/examples/meta_project/rust/main/src/lib.rs b/examples/meta_project/rust/main/src/lib.rs new file mode 100644 index 000000000..a609c11c6 --- /dev/null +++ b/examples/meta_project/rust/main/src/lib.rs @@ -0,0 +1,6 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +mod main_object; diff --git a/examples/meta_project/rust/main/src/main_object.rs b/examples/meta_project/rust/main/src/main_object.rs new file mode 100644 index 000000000..44e0db56b --- /dev/null +++ b/examples/meta_project/rust/main/src/main_object.rs @@ -0,0 +1,42 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cxx_qt::bridge] +pub mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/qstring.h"); + type QString = cxx_qt_lib::QString; + } + + #[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")] + #[derive(Default)] + pub struct MainObject { + #[qproperty] + string: QString, + + pub counter: u32, + } + + unsafe extern "RustQt" { + #[qinvokable] + fn increment(self: Pin<&mut qobject::MainObject>); + } +} + +use core::pin::Pin; +use cxx_qt::CxxQtType; +use cxx_qt_lib::QString; + +impl ffi::MainObjectQt { + pub fn increment(mut self: Pin<&mut Self>) { + let counter = self.rust().counter; + let counter = sub1::increment(counter); + let counter = sub2::increment(counter); + self.as_mut().rust_mut().counter = counter; + + let new_string = QString::from(&self.rust().counter.to_string()); + self.as_mut().set_string(new_string); + } +} diff --git a/examples/meta_project/rust/sub1/Cargo.toml b/examples/meta_project/rust/sub1/Cargo.toml new file mode 100644 index 000000000..b2d1ff728 --- /dev/null +++ b/examples/meta_project/rust/sub1/Cargo.toml @@ -0,0 +1,24 @@ +# SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +# SPDX-FileContributor: Andrew Hayzen +# +# SPDX-License-Identifier: MIT OR Apache-2.0 +[package] +name = "sub1" +version = "0.1.0" +authors = [ + "Andrew Hayzen ", +] +edition = "2021" +license = "MIT OR Apache-2.0" + +# This will instruct Cargo to create an rlib our main crate is the staticlib +[lib] +crate-type = ["rlib"] + +[dependencies] +cxx.workspace = true +cxx-qt.workspace = true +cxx-qt-lib.workspace = true + +[build-dependencies] +cxx-qt-build.workspace = true diff --git a/examples/meta_project/rust/sub1/build.rs b/examples/meta_project/rust/sub1/build.rs new file mode 100644 index 000000000..f8d180b57 --- /dev/null +++ b/examples/meta_project/rust/sub1/build.rs @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx_qt_build::CxxQtBuilder; + +fn main() { + CxxQtBuilder::new().file("src/sub1_object.rs").build(); +} diff --git a/examples/meta_project/rust/sub1/src/lib.rs b/examples/meta_project/rust/sub1/src/lib.rs new file mode 100644 index 000000000..4c7f893dc --- /dev/null +++ b/examples/meta_project/rust/sub1/src/lib.rs @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +// We need to enable packed bundled libs to allow for +bundle and +whole-archive +// +// Note that this is a nightly feature +// https://github.com/rust-lang/rust/issues/108081 +#![feature(packed_bundled_libs)] + +mod sub1_object; + +pub fn increment(number: u32) -> u32 { + number + 1 +} diff --git a/examples/meta_project/rust/sub1/src/sub1_object.rs b/examples/meta_project/rust/sub1/src/sub1_object.rs new file mode 100644 index 000000000..172332fa8 --- /dev/null +++ b/examples/meta_project/rust/sub1/src/sub1_object.rs @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cxx_qt::bridge] +pub mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/qstring.h"); + type QString = cxx_qt_lib::QString; + } + + #[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo.sub1", qml_version = "1.0")] + #[derive(Default)] + pub struct Sub1Object { + #[qproperty] + string: QString, + + pub counter: u32, + } + + unsafe extern "RustQt" { + #[qinvokable] + fn increment(self: Pin<&mut qobject::Sub1Object>); + } +} + +use core::pin::Pin; +use cxx_qt::CxxQtType; +use cxx_qt_lib::QString; + +impl ffi::Sub1ObjectQt { + pub fn increment(mut self: Pin<&mut Self>) { + self.as_mut().rust_mut().counter = crate::increment(self.rust().counter); + + let new_string = QString::from(&self.rust().counter.to_string()); + self.as_mut().set_string(new_string); + } +} diff --git a/examples/meta_project/rust/sub2/Cargo.toml b/examples/meta_project/rust/sub2/Cargo.toml new file mode 100644 index 000000000..0bbf603d0 --- /dev/null +++ b/examples/meta_project/rust/sub2/Cargo.toml @@ -0,0 +1,24 @@ +# SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +# SPDX-FileContributor: Andrew Hayzen +# +# SPDX-License-Identifier: MIT OR Apache-2.0 +[package] +name = "sub2" +version = "0.1.0" +authors = [ + "Andrew Hayzen ", +] +edition = "2021" +license = "MIT OR Apache-2.0" + +# This will instruct Cargo to create an rlib our main crate is the staticlib +[lib] +crate-type = ["rlib"] + +[dependencies] +cxx.workspace = true +cxx-qt.workspace = true +cxx-qt-lib.workspace = true + +[build-dependencies] +cxx-qt-build.workspace = true diff --git a/examples/meta_project/rust/sub2/build.rs b/examples/meta_project/rust/sub2/build.rs new file mode 100644 index 000000000..270ba7a04 --- /dev/null +++ b/examples/meta_project/rust/sub2/build.rs @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx_qt_build::CxxQtBuilder; + +fn main() { + CxxQtBuilder::new().file("src/sub2_object.rs").build(); +} diff --git a/examples/meta_project/rust/sub2/src/lib.rs b/examples/meta_project/rust/sub2/src/lib.rs new file mode 100644 index 000000000..28b7dde30 --- /dev/null +++ b/examples/meta_project/rust/sub2/src/lib.rs @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +// We need to enable packed bundled libs to allow for +bundle and +whole-archive +// +// Note that this is a nightly feature +// https://github.com/rust-lang/rust/issues/108081 +#![feature(packed_bundled_libs)] + +mod sub2_object; + +pub fn increment(number: u32) -> u32 { + number + 2 +} diff --git a/examples/meta_project/rust/sub2/src/sub2_object.rs b/examples/meta_project/rust/sub2/src/sub2_object.rs new file mode 100644 index 000000000..df94a2dac --- /dev/null +++ b/examples/meta_project/rust/sub2/src/sub2_object.rs @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#[cxx_qt::bridge] +pub mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/qstring.h"); + type QString = cxx_qt_lib::QString; + } + + #[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo.sub2", qml_version = "1.0")] + #[derive(Default)] + pub struct Sub2Object { + #[qproperty] + string: QString, + + pub counter: u32, + } + + unsafe extern "RustQt" { + #[qinvokable] + fn increment(self: Pin<&mut qobject::Sub2Object>); + } +} + +use core::pin::Pin; +use cxx_qt::CxxQtType; +use cxx_qt_lib::QString; + +impl ffi::Sub2ObjectQt { + pub fn increment(mut self: Pin<&mut Self>) { + self.as_mut().rust_mut().counter = crate::increment(self.rust().counter); + + let new_string = QString::from(&self.rust().counter.to_string()); + self.as_mut().set_string(new_string); + } +}