Skip to content

Commit

Permalink
cxx-qt-build: consider the folders in rust path
Browse files Browse the repository at this point in the history
Closes #855
  • Loading branch information
ahayzen-kdab committed Apr 18, 2024
1 parent 8b809e8 commit 4fe8ce3
Show file tree
Hide file tree
Showing 57 changed files with 80 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- File name is used for CXX bridges rather than module name to match upstream
- `#[qobject]` attribute is now optional on types in `extern "RustQt"`
- `#[qobject]` attribute is now required on types in `extern "C++Qt"`
- Rust bridges which are in folders are now considered by `CxxQtBuilder`

### Fixed

Expand Down
28 changes: 20 additions & 8 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ struct GeneratedCpp {

impl GeneratedCpp {
/// Generate QObject and cxx header/source C++ file contents
pub fn new(rust_file_path: impl AsRef<Path>) -> Result<Self, Diagnostic> {
pub fn new(
rust_file_path: impl AsRef<Path>,
relative_path: impl AsRef<Path>,
) -> Result<Self, Diagnostic> {
let to_diagnostic = |err| Diagnostic::new(rust_file_path.as_ref().to_owned(), err);

let rust_file_path = rust_file_path.as_ref();
Expand Down Expand Up @@ -91,15 +94,16 @@ impl GeneratedCpp {
rust_file_path.display());
}

// Match upstream where they use the file name as the ident
//
// TODO: what happens if there are folders?
// Match upstream where they use the file name and folders as the ident
//
// TODO: ideally CXX-Qt would also use the file name
// https://github.com/KDAB/cxx-qt/pull/200/commits/4861c92e66c3a022d3f0dedd9f8fd20db064b42b
file_ident = rust_file_path
.file_stem()
.unwrap()
//
// We need the relative path here as we want the folders
file_ident = relative_path
.as_ref()
// Remove the .rs extension
.with_extension("")
.to_str()
.unwrap()
.to_owned();
Expand Down Expand Up @@ -210,6 +214,10 @@ impl GeneratedCpp {
header_directory.display(),
self.file_ident
));
if let Some(directory) = header_path.parent() {
std::fs::create_dir_all(directory)
.expect("Could not create directory to write cxx-qt generated header files");
}
let mut header = File::create(header_path).expect("Could not create cxx header file");
header
.write_all(&self.cxx.header)
Expand All @@ -220,6 +228,10 @@ impl GeneratedCpp {
cpp_directory.display(),
self.file_ident
));
if let Some(directory) = cpp_path.parent() {
std::fs::create_dir_all(directory)
.expect("Could not create directory to write cxx-qt generated source files");
}
let mut cpp = File::create(&cpp_path).expect("Could not create cxx source file");
cpp.write_all(&self.cxx.implementation)
.expect("Could not write cxx source file");
Expand All @@ -242,7 +254,7 @@ fn generate_cxxqt_cpp_files(
let path = format!("{manifest_dir}/{}", rs_path.as_ref().display());
println!("cargo:rerun-if-changed={path}");

let generated_code = match GeneratedCpp::new(&path) {
let generated_code = match GeneratedCpp::new(&path, rs_path) {
Ok(v) => v,
Err(diagnostic) => {
diagnostic.report();
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/writer/cpp/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String {
{includes}
{forward_declare}
#include "cxx-qt-gen/{cxx_file_stem}.cxx.h"
#include "{cxx_file_stem}.cxx.h"
{extern_cxx_qt}
{qobjects}
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/writer/cpp/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn write_cpp_source(generated: &GeneratedCppBlocks) -> String {
.join("\n");

formatdoc! {r#"
#include "cxx-qt-gen/{cxx_file_stem}.cxxqt.h"
#include "{cxx_file_stem}.cxxqt.h"
{extern_cxx_qt}
{qobjects}
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/inheritance.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cxx-qt-gen/inheritance.cxxqt.h"
#include "inheritance.cxxqt.h"

QVariant
MyObject::data(QModelIndex const& _index, ::std::int32_t _role) const
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/inheritance.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class MyObject;

#include "cxx-qt-gen/inheritance.cxx.h"
#include "inheritance.cxx.h"

class MyObject
: public QAbstractItemModel
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/invokables.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cxx-qt-gen/ffi.cxxqt.h"
#include "ffi.cxxqt.h"

namespace cxx_qt::my_object {
void
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/invokables.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using MyObjectCxxQtThread = ::rust::cxxqt1::CxxQtThread<MyObject>;

} // namespace cxx_qt::my_object

#include "cxx-qt-gen/ffi.cxx.h"
#include "ffi.cxx.h"

namespace cxx_qt::my_object {
class MyObject
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/passthrough_and_naming.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cxx-qt-gen/multi_object.cxxqt.h"
#include "multi_object.cxxqt.h"

// Define namespace otherwise we hit a GCC bug
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ using ExternObjectCxxQtSignalHandlererrorOccurred =
struct ExternObjectCxxQtSignalParamserrorOccurred*>;
} // namespace mynamespace::rust::cxxqtgen1

#include "cxx-qt-gen/multi_object.cxx.h"
#include "multi_object.cxx.h"

namespace rust::cxxqtgen1 {
::QMetaObject::Connection
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/properties.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cxx-qt-gen/ffi.cxxqt.h"
#include "ffi.cxxqt.h"

// Define namespace otherwise we hit a GCC bug
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using MyObjectCxxQtSignalHandlertrivialChanged = ::rust::cxxqt1::SignalHandler<
struct MyObjectCxxQtSignalParamstrivialChanged*>;
} // namespace cxx_qt::my_object::rust::cxxqtgen1

#include "cxx-qt-gen/ffi.cxx.h"
#include "ffi.cxx.h"

namespace cxx_qt::my_object::rust::cxxqtgen1 {
::QMetaObject::Connection
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/qenum.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cxx-qt-gen/ffi.cxxqt.h"
#include "ffi.cxxqt.h"

namespace cxx_qt::my_object {
void
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/qenum.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum class MyOtherNamespacedEnum : ::std::int32_t
Q_ENUM_NS(MyOtherNamespacedEnum)
} // namespace other_namespace

#include "cxx-qt-gen/ffi.cxx.h"
#include "ffi.cxx.h"

namespace cxx_qt::my_object {
class MyObject
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/signals.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cxx-qt-gen/ffi.cxxqt.h"
#include "ffi.cxxqt.h"

// Define namespace otherwise we hit a GCC bug
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using QTimerCxxQtSignalHandlertimeout =
::rust::cxxqt1::SignalHandler<struct QTimerCxxQtSignalParamstimeout*>;
} // namespace cxx_qt::my_object::rust::cxxqtgen1

#include "cxx-qt-gen/ffi.cxx.h"
#include "ffi.cxx.h"

namespace cxx_qt::my_object::rust::cxxqtgen1 {
::QMetaObject::Connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <QtCore/QAbstractListModel>
#include <QtCore/QVector>

#include "cxx-qt-gen/energy_usage.cxxqt.h"
#include "cxx-qt-gen/src/energy_usage.cxxqt.h"

class EnergyUsageProxyModel : public QAbstractListModel
{
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_cxx_only/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
#include <QtTest/QTest>

#include "cxx-qt-gen/lib.cxx.h"
#include "cxx-qt-gen/src/lib.cxx.h"
#include "cxx_test.h"

class CxxTest : public QObject
Expand Down
10 changes: 5 additions & 5 deletions tests/basic_cxx_qt/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include <QtTest/QSignalSpy>
#include <QtTest/QTest>

#include "cxx-qt-gen/empty.cxxqt.h"
#include "cxx-qt-gen/locking.cxxqt.h"
#include "cxx-qt-gen/my_data.cxxqt.h"
#include "cxx-qt-gen/my_object.cxxqt.h"
#include "cxx-qt-gen/my_types.cxxqt.h"
#include "cxx-qt-gen/src/empty.cxxqt.h"
#include "cxx-qt-gen/src/locking.cxxqt.h"
#include "cxx-qt-gen/src/my_data.cxxqt.h"
#include "cxx-qt-gen/src/my_object.cxxqt.h"
#include "cxx-qt-gen/src/my_types.cxxqt.h"

class LockingWorkerThread : public QThread
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qbytearray.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QByteArray>
#include <QtTest/QTest>

#include "cxx-qt-gen/qbytearray.cxx.h"
#include "cxx-qt-gen/src/qbytearray.cxx.h"

class QByteArrayTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qcoreapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QCoreApplication>
#include <QtTest/QTest>

#include "cxx-qt-gen/qcoreapplication.cxx.h"
#include "cxx-qt-gen/src/qcoreapplication.cxx.h"

class QCoreApplicationTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QDate>
#include <QtTest/QTest>

#include "cxx-qt-gen/qdate.cxx.h"
#include "cxx-qt-gen/src/qdate.cxx.h"

class QDateTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QDateTime>
#include <QtTest/QTest>

#include "cxx-qt-gen/qdatetime.cxx.h"
#include "cxx-qt-gen/src/qdatetime.cxx.h"

class QDateTimeTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qguiapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtGui/QGuiApplication>
#include <QtTest/QTest>

#include "cxx-qt-gen/qguiapplication.cxx.h"
#include "cxx-qt-gen/src/qguiapplication.cxx.h"

class QGuiApplicationTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <QtCore/QVariant>
#include <QtTest/QTest>

#include "cxx-qt-gen/qhash.cxx.h"
#include "cxx-qt-gen/src/qhash.cxx.h"

class QHashTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qline.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QLine>
#include <QtTest/QTest>

#include "cxx-qt-gen/qline.cxx.h"
#include "cxx-qt-gen/src/qline.cxx.h"

class QLineTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qlinef.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QLineF>
#include <QtTest/QTest>

#include "cxx-qt-gen/qlinef.cxx.h"
#include "cxx-qt-gen/src/qlinef.cxx.h"

class QLineFTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QList>
#include <QtTest/QTest>

#include "cxx-qt-gen/qlist.cxx.h"
#include "cxx-qt-gen/src/qlist.cxx.h"

class QListTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <QtCore/QVariant>
#include <QtTest/QTest>

#include "cxx-qt-gen/qmap.cxx.h"
#include "cxx-qt-gen/src/qmap.cxx.h"

class QMapTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qmargins.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QMargins>
#include <QtTest/QTest>

#include "cxx-qt-gen/qmargins.cxx.h"
#include "cxx-qt-gen/src/qmargins.cxx.h"

class QMarginsTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qmarginsf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QMarginsF>
#include <QtTest/QTest>

#include "cxx-qt-gen/qmarginsf.cxx.h"
#include "cxx-qt-gen/src/qmarginsf.cxx.h"

class QMarginsFTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qmetaobjectconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <QtTest/QTest>
#include <qobjectdefs.h>

#include "cxx-qt-gen/qmetaobjectconnection.cxx.h"
#include "cxx-qt-gen/src/qmetaobjectconnection.cxx.h"

class MyObject : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qmodelindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <QtCore/QStringListModel>
#include <QtTest/QTest>

#include "cxx-qt-gen/qmodelindex.cxx.h"
#include "cxx-qt-gen/src/qmodelindex.cxx.h"

// We subclass from QAbstractListModel to have a valid model to use for
// access to createIndex();
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qpen.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtGui/QPen>
#include <QtTest/QTest>

#include "cxx-qt-gen/qpen.cxx.h"
#include "cxx-qt-gen/src/qpen.cxx.h"

class QPenTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qpersistentmodelindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <QtGui/QStandardItemModel>
#include <QtTest/QTest>

#include "cxx-qt-gen/qpersistentmodelindex.cxx.h"
#include "cxx-qt-gen/src/qpersistentmodelindex.cxx.h"

class QPersistentModelIndexTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QPoint>
#include <QtTest/QTest>

#include "cxx-qt-gen/qpoint.cxx.h"
#include "cxx-qt-gen/src/qpoint.cxx.h"

class QPointTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qpointf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtCore/QPointF>
#include <QtTest/QTest>

#include "cxx-qt-gen/qpointf.cxx.h"
#include "cxx-qt-gen/src/qpointf.cxx.h"

class QPointFTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qpolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtGui/QPolygon>
#include <QtTest/QTest>

#include "cxx-qt-gen/qpolygon.cxx.h"
#include "cxx-qt-gen/src/qpolygon.cxx.h"

class QPolygonTest : public QObject
{
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qpolygonf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QtGui/QPolygonF>
#include <QtTest/QTest>

#include "cxx-qt-gen/qpolygonf.cxx.h"
#include "cxx-qt-gen/src/qpolygonf.cxx.h"

class QPolygonFTest : public QObject
{
Expand Down
Loading

0 comments on commit 4fe8ce3

Please sign in to comment.