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 KDAB#855
  • Loading branch information
ahayzen-kdab committed Sep 4, 2024
1 parent 34d25db commit ffe80a1
Show file tree
Hide file tree
Showing 44 changed files with 87 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Libraries can pass build information to cxx-qt-build in the form of a `cxx_qt_build::Interface`
- Add CMake wrappers around corrosion to simplify importing crates and qml modules that were built with cxx-qt-build
- CMake code has been extracted into a separate repository for faster downloads (kdab/cxx-qt-cmake)
- Folder structure of Rust bridges is now considered in the same way as CXX in `CxxQtBuilder`

### Removed

Expand Down
54 changes: 39 additions & 15 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 @@ -98,15 +101,13 @@ impl GeneratedCpp {
rust_file_path.display());
}

// Match upstream where they use the file name as the ident
// Match upstream where they use the file name and folders as the ident
//
// TODO: what happens if there are folders?
//
// TODO: ideally CXX-Qt would also use the file name
// https://github.com/KDAB/cxx-qt/pull/200/commits/4861c92e66c3a022d3f0dedd9f8fd20db064b42b
rust_file_path
.file_stem()
.unwrap()
// We need the relative path here as we want the folders
relative_path
.as_ref()
// Remove the .rs extension
.with_extension("")
.to_str()
.unwrap()
.clone_into(&mut file_ident);
Expand Down Expand Up @@ -135,7 +136,18 @@ impl GeneratedCpp {
.map_err(GeneratedError::from)
.map_err(to_diagnostic)?;
let rust_tokens = write_rust(&generated_rust);
file_ident.clone_from(&parser.cxx_file_stem);

// Use the relative path with the cxx_file_stem
//
// TODO: ideally CXX-Qt would also use the file name
// but it uses the module or cxx_file_stem for now
// https://github.com/KDAB/cxx-qt/pull/200/commits/4861c92e66c3a022d3f0dedd9f8fd20db064b42b
file_ident = relative_path
.as_ref()
.with_file_name(parser.cxx_file_stem)
.to_str()
.unwrap()
.clone_into(&mut file_ident);

// We need to do this and can't rely on the macro, as we need to generate the
// CXX bridge Rust code that is then fed into the cxx_gen generation.
Expand Down Expand Up @@ -168,10 +180,6 @@ impl GeneratedCpp {
) -> GeneratedCppFilePaths {
let cpp_directory = cpp_directory.as_ref();
let header_directory = header_directory.as_ref();
for directory in [cpp_directory, header_directory] {
std::fs::create_dir_all(directory)
.expect("Could not create directory to write cxx-qt generated files");
}

let mut cpp_file_paths = GeneratedCppFilePaths {
plain_cpp: PathBuf::new(),
Expand All @@ -184,6 +192,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 files");
}
let mut header =
File::create(&header_path).expect("Could not create cxx-qt header file");
let header_generated = match cxx_qt_generated {
Expand All @@ -201,6 +213,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 files");
}
let mut cpp = File::create(&cpp_path).expect("Could not create cxx-qt source file");
let source_generated = match cxx_qt_generated {
CppFragment::Pair { header: _, source } => source,
Expand All @@ -217,6 +233,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 @@ -227,6 +247,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 Down Expand Up @@ -255,7 +279,7 @@ fn generate_cxxqt_cpp_files(
let path = manifest_dir.join(rs_path);
println!("cargo:rerun-if-changed={}", path.to_string_lossy());

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
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_demo_threading/energy_usage.cxxqt.h"
#include "cxx_qt_demo_threading/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 "basic_cxx_only/lib.cxx.h"
#include "basic_cxx_only/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 "basic_cxx_qt/empty.cxxqt.h"
#include "basic_cxx_qt/locking.cxxqt.h"
#include "basic_cxx_qt/my_data.cxxqt.h"
#include "basic_cxx_qt/my_object.cxxqt.h"
#include "basic_cxx_qt/my_types.cxxqt.h"
#include "basic_cxx_qt/src/empty.cxxqt.h"
#include "basic_cxx_qt/src/locking.cxxqt.h"
#include "basic_cxx_qt/src/my_data.cxxqt.h"
#include "basic_cxx_qt/src/my_object.cxxqt.h"
#include "basic_cxx_qt/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 "qt_types_standalone/qbytearray.cxx.h"
#include "qt_types_standalone/src/qbytearray.cxx.h"

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

#include "qt_types_standalone/qcolor.cxx.h"
#include "qt_types_standalone/src/qcolor.cxx.h"

class QColorTest : 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 "qt_types_standalone/qcoreapplication.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qdate.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qdatetime.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qguiapplication.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qhash.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qline.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qlinef.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qlist.cxx.h"
#include "qt_types_standalone/src/qlist.cxx.h"

class QListTest : public QObject
{
Expand Down
4 changes: 2 additions & 2 deletions 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 "qt_types_standalone/qmap.cxx.h"
#include "qt_types_standalone/src/qmap.cxx.h"

class QMapTest : public QObject
{
Expand Down Expand Up @@ -49,4 +49,4 @@ private Q_SLOTS:
QVERIFY(!c.contains(QStringLiteral("github")));
QCOMPARE(c.size(), 2);
}
};
};
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 "qt_types_standalone/qmargins.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qmarginsf.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qmetaobjectconnection.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qmodelindex.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qpen.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qpersistentmodelindex.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qpoint.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qpointf.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qpolygon.cxx.h"
#include "qt_types_standalone/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 "qt_types_standalone/qpolygonf.cxx.h"
#include "qt_types_standalone/src/qpolygonf.cxx.h"

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

#include "qt_types_standalone/qqmlapplicationengine.cxx.h"
#include "qt_types_standalone/src/qqmlapplicationengine.cxx.h"

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

#include "qt_types_standalone/qqmlengine.cxx.h"
#include "qt_types_standalone/src/qqmlengine.cxx.h"

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

#include "qt_types_standalone/qrect.cxx.h"
#include "qt_types_standalone/src/qrect.cxx.h"

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

#include "qt_types_standalone/qrectf.cxx.h"
#include "qt_types_standalone/src/qrectf.cxx.h"

class QRectFTest : public QObject
{
Expand Down
Loading

0 comments on commit ffe80a1

Please sign in to comment.