@@ -232,9 +232,28 @@ fn main() {
232
232
}
233
233
builder. file ( "src/qt_types.cpp" ) ;
234
234
println ! ( "cargo:rerun-if-changed=src/qt_types.cpp" ) ;
235
+ println ! ( "cargo:rerun-if-changed=src/assertion_utils.h" ) ;
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
+ // Still expose std_types.cpp to the builder so that CMake builds work
235
255
builder. file ( "src/std_types.cpp" ) ;
236
256
println ! ( "cargo:rerun-if-changed=src/std_types.cpp" ) ;
237
- println ! ( "cargo:rerun-if-changed=src/assertion_utils.h" ) ;
238
257
239
258
// Write this library's manually written C++ headers to files and add them to include paths
240
259
let out_dir = std:: env:: var ( "OUT_DIR" ) . unwrap ( ) ;
@@ -251,11 +270,17 @@ fn main() {
251
270
builder. define ( "CXX_QT_QML_FEATURE" , None ) ;
252
271
}
253
272
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" ) ;
273
+ for builder in [ & mut builder, & mut cc_builder_whole_archive] {
274
+ // Note, ensure our settings stay in sync across cxx-qt-build and cxx-qt-lib
275
+ builder. cpp ( true ) ;
276
+ // MSVC
277
+ builder. flag_if_supported ( "/std:c++17" ) ;
278
+ builder. flag_if_supported ( "/Zc:__cplusplus" ) ;
279
+ builder. flag_if_supported ( "/permissive-" ) ;
280
+ // GCC + Clang
281
+ builder. flag_if_supported ( "-std=c++17" ) ;
282
+ }
283
+
284
+ cc_builder_whole_archive. compile ( "cxx-qt-lib-static-initializers" ) ;
260
285
builder. compile ( "cxx-qt-lib" ) ;
261
286
}
0 commit comments