@@ -232,10 +232,27 @@ fn main() {
232
232
}
233
233
builder. file ( "src/qt_types.cpp" ) ;
234
234
println ! ( "cargo:rerun-if-changed=src/qt_types.cpp" ) ;
235
- builder. file ( "src/std_types.cpp" ) ;
236
- println ! ( "cargo:rerun-if-changed=src/std_types.cpp" ) ;
237
235
println ! ( "cargo:rerun-if-changed=src/assertion_utils.h" ) ;
238
236
237
+ // Setup compiler
238
+ // qRegisterMetaType needs to be linked with +whole-archive
239
+ // because they use static variables which need to be initialized before main
240
+ // (regardless of whether main is in Rust or C++). Normally linkers only copy symbols referenced
241
+ // from within main when static linking, which would result in discarding those static variables.
242
+ // Use a separate cc::Build for the little amount of code that needs to be linked with +whole-archive
243
+ // to avoid bloating the binary.
244
+ let mut cc_builder_whole_archive = cc:: Build :: new ( ) ;
245
+ cc_builder_whole_archive. link_lib_modifier ( "+whole-archive" ) ;
246
+ // Workaround for spurious compiler error in Rust 1.69. The bug has been fixed, but keeping this
247
+ // workaround seems to be harmless.
248
+ // https://github.com/rust-lang/rust/issues/110912
249
+ cc_builder_whole_archive. link_lib_modifier ( "-bundle" ) ;
250
+ for include_path in qtbuild. include_paths ( ) {
251
+ cc_builder_whole_archive. include ( include_path) ;
252
+ }
253
+ cc_builder_whole_archive. file ( "src/std_types.cpp" ) ;
254
+ println ! ( "cargo:rerun-if-changed=src/std_types.cpp" ) ;
255
+
239
256
// Write this library's manually written C++ headers to files and add them to include paths
240
257
let out_dir = std:: env:: var ( "OUT_DIR" ) . unwrap ( ) ;
241
258
cxx_qt_lib_headers:: write_headers ( format ! ( "{out_dir}/cxx-qt-lib" ) ) ;
@@ -251,11 +268,17 @@ fn main() {
251
268
builder. define ( "CXX_QT_QML_FEATURE" , None ) ;
252
269
}
253
270
254
- // MSVC
255
- builder. flag_if_supported ( "/std:c++17" ) ;
256
- builder. flag_if_supported ( "/Zc:__cplusplus" ) ;
257
- builder. flag_if_supported ( "/permissive-" ) ;
258
- // GCC + Clang
259
- builder. flag_if_supported ( "-std=c++17" ) ;
271
+ for builder in [ & mut builder, & mut cc_builder_whole_archive] {
272
+ // Note, ensure our settings stay in sync across cxx-qt-build and cxx-qt-lib
273
+ builder. cpp ( true ) ;
274
+ // MSVC
275
+ builder. flag_if_supported ( "/std:c++17" ) ;
276
+ builder. flag_if_supported ( "/Zc:__cplusplus" ) ;
277
+ builder. flag_if_supported ( "/permissive-" ) ;
278
+ // GCC + Clang
279
+ builder. flag_if_supported ( "-std=c++17" ) ;
280
+ }
281
+
282
+ cc_builder_whole_archive. compile ( "cxx-qt-lib-static-initializers" ) ;
260
283
builder. compile ( "cxx-qt-lib" ) ;
261
284
}
0 commit comments