@@ -61,7 +61,10 @@ struct GeneratedCpp {
61
61
62
62
impl GeneratedCpp {
63
63
/// Generate QObject and cxx header/source C++ file contents
64
- pub fn new ( rust_file_path : impl AsRef < Path > ) -> Result < Self , Diagnostic > {
64
+ pub fn new (
65
+ rust_file_path : impl AsRef < Path > ,
66
+ relative_path : impl AsRef < Path > ,
67
+ ) -> Result < Self , Diagnostic > {
65
68
let to_diagnostic = |err| Diagnostic :: new ( rust_file_path. as_ref ( ) . to_owned ( ) , err) ;
66
69
67
70
let rust_file_path = rust_file_path. as_ref ( ) ;
@@ -91,15 +94,16 @@ impl GeneratedCpp {
91
94
rust_file_path. display( ) ) ;
92
95
}
93
96
94
- // Match upstream where they use the file name as the ident
95
- //
96
- // TODO: what happens if there are folders?
97
+ // Match upstream where they use the file name and folders as the ident
97
98
//
98
99
// TODO: ideally CXX-Qt would also use the file name
99
100
// https://github.com/KDAB/cxx-qt/pull/200/commits/4861c92e66c3a022d3f0dedd9f8fd20db064b42b
100
- file_ident = rust_file_path
101
- . file_stem ( )
102
- . unwrap ( )
101
+ //
102
+ // We need the relative path here as we want the folders
103
+ file_ident = relative_path
104
+ . as_ref ( )
105
+ // Remove the .rs extension
106
+ . with_extension ( "" )
103
107
. to_str ( )
104
108
. unwrap ( )
105
109
. to_owned ( ) ;
@@ -210,6 +214,10 @@ impl GeneratedCpp {
210
214
header_directory. display( ) ,
211
215
self . file_ident
212
216
) ) ;
217
+ if let Some ( directory) = header_path. parent ( ) {
218
+ std:: fs:: create_dir_all ( directory)
219
+ . expect ( "Could not create directory to write cxx-qt generated header files" ) ;
220
+ }
213
221
let mut header = File :: create ( header_path) . expect ( "Could not create cxx header file" ) ;
214
222
header
215
223
. write_all ( & self . cxx . header )
@@ -220,6 +228,10 @@ impl GeneratedCpp {
220
228
cpp_directory. display( ) ,
221
229
self . file_ident
222
230
) ) ;
231
+ if let Some ( directory) = cpp_path. parent ( ) {
232
+ std:: fs:: create_dir_all ( directory)
233
+ . expect ( "Could not create directory to write cxx-qt generated source files" ) ;
234
+ }
223
235
let mut cpp = File :: create ( & cpp_path) . expect ( "Could not create cxx source file" ) ;
224
236
cpp. write_all ( & self . cxx . implementation )
225
237
. expect ( "Could not write cxx source file" ) ;
@@ -242,7 +254,7 @@ fn generate_cxxqt_cpp_files(
242
254
let path = format ! ( "{manifest_dir}/{}" , rs_path. as_ref( ) . display( ) ) ;
243
255
println ! ( "cargo:rerun-if-changed={path}" ) ;
244
256
245
- let generated_code = match GeneratedCpp :: new ( & path) {
257
+ let generated_code = match GeneratedCpp :: new ( & path, rs_path ) {
246
258
Ok ( v) => v,
247
259
Err ( diagnostic) => {
248
260
diagnostic. report ( ) ;
0 commit comments