@@ -61,7 +61,10 @@ struct GeneratedCpp {
6161
6262impl GeneratedCpp {
6363 /// 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 > {
6568 let to_diagnostic = |err| Diagnostic :: new ( rust_file_path. as_ref ( ) . to_owned ( ) , err) ;
6669
6770 let rust_file_path = rust_file_path. as_ref ( ) ;
@@ -91,15 +94,16 @@ impl GeneratedCpp {
9194 rust_file_path. display( ) ) ;
9295 }
9396
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
9798 //
9899 // TODO: ideally CXX-Qt would also use the file name
99100 // 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 ( "" )
103107 . to_str ( )
104108 . unwrap ( )
105109 . to_owned ( ) ;
@@ -210,6 +214,10 @@ impl GeneratedCpp {
210214 header_directory. display( ) ,
211215 self . file_ident
212216 ) ) ;
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+ }
213221 let mut header = File :: create ( header_path) . expect ( "Could not create cxx header file" ) ;
214222 header
215223 . write_all ( & self . cxx . header )
@@ -220,6 +228,10 @@ impl GeneratedCpp {
220228 cpp_directory. display( ) ,
221229 self . file_ident
222230 ) ) ;
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+ }
223235 let mut cpp = File :: create ( & cpp_path) . expect ( "Could not create cxx source file" ) ;
224236 cpp. write_all ( & self . cxx . implementation )
225237 . expect ( "Could not write cxx source file" ) ;
@@ -242,7 +254,7 @@ fn generate_cxxqt_cpp_files(
242254 let path = format ! ( "{manifest_dir}/{}" , rs_path. as_ref( ) . display( ) ) ;
243255 println ! ( "cargo:rerun-if-changed={path}" ) ;
244256
245- let generated_code = match GeneratedCpp :: new ( & path) {
257+ let generated_code = match GeneratedCpp :: new ( & path, rs_path ) {
246258 Ok ( v) => v,
247259 Err ( diagnostic) => {
248260 diagnostic. report ( ) ;
0 commit comments