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 Apr 18, 2024
1 parent 8b809e8 commit bd6d571
Show file tree
Hide file tree
Showing 66 changed files with 120 additions and 95 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"`
- Folder structure of Rust bridges is now considered in the same way as CXX in `CxxQtBuilder`

### Fixed

Expand Down
64 changes: 44 additions & 20 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,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
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 @@ -128,7 +129,17 @@ impl GeneratedCpp {
.map_err(GeneratedError::from)
.map_err(to_diagnostic)?;
let rust_tokens = write_rust(&generated_rust);
file_ident = parser.cxx_file_stem.clone();
// 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()
.to_owned();

// 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 @@ -161,10 +172,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 @@ -177,6 +184,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 @@ -194,6 +205,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 @@ -210,6 +225,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 +239,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 +265,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 Expand Up @@ -455,11 +478,12 @@ impl CxxQtBuilder {
} else {
format!("{header_root}/{dir_name}")
};
std::fs::create_dir_all(directory.clone())
.expect("Could not create {directory} header directory");

let h_path = format!("{directory}/{file_name}");
let mut header = File::create(h_path).expect("Could not create header: {h_path}");
let h_path = PathBuf::from(format!("{directory}/{file_name}"));
if let Some(directory) = h_path.parent() {
std::fs::create_dir_all(directory)
.expect("Could not create {directory} header directory");
}
let mut header = File::create(&h_path).expect("Could not create header: {h_path}");
write!(header, "{file_contents}").expect("Could not write header: {h_path}");
}

Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/src/generator/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl GeneratedRustBlocks {

/// Generate the include line for this parsed block
fn generate_include(parser: &Parser) -> Result<Item> {
let import_path = format!("cxx-qt-gen/{}.cxxqt.h", parser.cxx_file_stem);
let import_path = format!("{}.cxxqt.h", parser.cxx_file_stem);

syn::parse2(quote! {
unsafe extern "C++" {
Expand Down Expand Up @@ -116,7 +116,7 @@ mod tests {
&rust.cxx_mod_contents[0],
quote! {
unsafe extern "C++" {
include!("cxx-qt-gen/ffi.cxxqt.h");
include!("ffi.cxxqt.h");
}
},
);
Expand Down Expand Up @@ -164,7 +164,7 @@ mod tests {
&rust.cxx_mod_contents[0],
quote! {
unsafe extern "C++" {
include!("cxx-qt-gen/my_object.cxxqt.h");
include!("my_object.cxxqt.h");
}
},
);
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
12 changes: 6 additions & 6 deletions crates/cxx-qt-gen/src/writer/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ mod tests {
#include "cxx-qt-gen/cxx_file_stem.cxx.h"
#include "cxx_file_stem.cxx.h"
Expand Down Expand Up @@ -367,7 +367,7 @@ mod tests {
#include "cxx-qt-gen/cxx_file_stem.cxx.h"
#include "cxx_file_stem.cxx.h"
Expand Down Expand Up @@ -435,7 +435,7 @@ mod tests {
#include "cxx-qt-gen/cxx_file_stem.cxx.h"
#include "cxx_file_stem.cxx.h"
Expand Down Expand Up @@ -474,7 +474,7 @@ mod tests {
/// Helper for the expected source
pub fn expected_source() -> &'static str {
indoc! {r#"
#include "cxx-qt-gen/cxx_file_stem.cxxqt.h"
#include "cxx_file_stem.cxxqt.h"
namespace cxx_qt::my_object {
Expand Down Expand Up @@ -531,7 +531,7 @@ mod tests {
/// Helper for the expected source with multiple QObjects
pub fn expected_source_multi_qobjects() -> &'static str {
indoc! {r#"
#include "cxx-qt-gen/cxx_file_stem.cxxqt.h"
#include "cxx_file_stem.cxxqt.h"
namespace cxx_qt {
Expand Down Expand Up @@ -574,7 +574,7 @@ mod tests {
/// Helper for the expected source with no namespace
pub fn expected_source_no_namespace() -> &'static str {
indoc! {r#"
#include "cxx-qt-gen/cxx_file_stem.cxxqt.h"
#include "cxx_file_stem.cxxqt.h"
int
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/inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod inheritance {
type QMetaObjectConnection = cxx_qt::QMetaObjectConnection;
}
unsafe extern "C++" {
include!("cxx-qt-gen/inheritance.cxxqt.h");
include!("inheritance.cxxqt.h");
}
unsafe extern "C++" {
#[doc = "The C++ type for the QObject "]
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/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod ffi {
type QMetaObjectConnection = cxx_qt::QMetaObjectConnection;
}
unsafe extern "C++" {
include!("cxx-qt-gen/ffi.cxxqt.h");
include!("ffi.cxxqt.h");
}
unsafe extern "C++" {
#[doc = "The C++ type for the QObject "]
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/passthrough_and_naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub mod ffi {
type QMetaObjectConnection = cxx_qt::QMetaObjectConnection;
}
unsafe extern "C++" {
include!("cxx-qt-gen/multi_object.cxxqt.h");
include!("multi_object.cxxqt.h");
}
unsafe extern "C++" {
#[doc = "The C++ type for the QObject "]
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/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod ffi {
type QMetaObjectConnection = cxx_qt::QMetaObjectConnection;
}
unsafe extern "C++" {
include!("cxx-qt-gen/ffi.cxxqt.h");
include!("ffi.cxxqt.h");
}
unsafe extern "C++" {
#[doc = "The C++ type for the QObject "]
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/qenum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod ffi {
type MyOtherNamespacedEnum;
}
unsafe extern "C++" {
include!("cxx-qt-gen/ffi.cxxqt.h");
include!("ffi.cxxqt.h");
}
unsafe extern "C++" {
#[doc = "The C++ type for the QObject "]
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
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod ffi {
type QMetaObjectConnection = cxx_qt::QMetaObjectConnection;
}
unsafe extern "C++" {
include!("cxx-qt-gen/ffi.cxxqt.h");
include!("ffi.cxxqt.h");
}
unsafe extern "C++" {
#[doc = "The C++ type for the QObject "]
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
Loading

0 comments on commit bd6d571

Please sign in to comment.