Skip to content

Commit 7df274b

Browse files
committed
cxx-qt-lib: use +whole_archive for std_types so that cargo-only works
When building with cargo we need to ensure that the statics aren't optimised out for registering types with Qt 5. Otherwise we cannot use numbers in QML. Also move std_types from -lib into -build, which potentially allows us to have cxx-qt-lib separate in the future. Closes #592
1 parent d95441b commit 7df274b

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

crates/cxx-qt-build/src/lib.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,11 @@ impl CxxQtBuilder {
391391
// Ensure that the linker is setup correctly for Cargo builds
392392
qt_build_utils::setup_linker();
393393

394-
let out_dir = env::var("OUT_DIR").unwrap();
395394
// The include directory needs to be namespaced by crate name when exporting for a C++ build system,
396395
// but for using cargo build without a C++ build system, OUT_DIR is already namespaced by crate name.
397396
let header_root = match env::var("CXXQT_EXPORT_DIR") {
398397
Ok(export_dir) => format!("{export_dir}/{}", env::var("CARGO_PKG_NAME").unwrap()),
399-
Err(_) => out_dir,
398+
Err(_) => env::var("OUT_DIR").unwrap(),
400399
};
401400
let generated_header_dir = format!("{header_root}/cxx-qt-gen");
402401

@@ -448,6 +447,7 @@ impl CxxQtBuilder {
448447
}
449448

450449
for builder in [&mut self.cc_builder, &mut cc_builder_whole_archive] {
450+
// Note, ensure our settings stay in sync across cxx-qt-build and cxx-qt-lib
451451
builder.cpp(true);
452452
// MSVC
453453
builder.flag_if_supported("/std:c++17");
@@ -525,6 +525,25 @@ impl CxxQtBuilder {
525525
cc_builder_whole_archive.file(qtbuild.qrc(&qrc_file));
526526
cc_builder_whole_archive_files_added = true;
527527
}
528+
529+
// If we are using Qt 5 then write the std_types source
530+
// This registers std numbers as a type for use in QML
531+
//
532+
// Note that we need this to be compiled into the whole_archive builder
533+
// as they are stored in statics in the source.
534+
if qtbuild.version().major == 5 {
535+
let std_types_contents = include_str!("std_types_qt5.cpp");
536+
let std_types_path = format!(
537+
"{out_dir}/std_types_qt5.cpp",
538+
out_dir = env::var("OUT_DIR").unwrap()
539+
);
540+
let mut source =
541+
File::create(&std_types_path).expect("Could not create std_types source");
542+
write!(source, "{std_types_contents}").expect("Could not write std_types source");
543+
cc_builder_whole_archive.file(&std_types_path);
544+
cc_builder_whole_archive_files_added = true;
545+
}
546+
528547
if cc_builder_whole_archive_files_added {
529548
cc_builder_whole_archive.compile("qt-static-initializers");
530549
}

crates/cxx-qt-lib/build.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,6 @@ fn main() {
232232
}
233233
builder.file("src/qt_types.cpp");
234234
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");
237235
println!("cargo:rerun-if-changed=src/assertion_utils.h");
238236

239237
// Write this library's manually written C++ headers to files and add them to include paths
@@ -251,11 +249,14 @@ fn main() {
251249
builder.define("CXX_QT_QML_FEATURE", None);
252250
}
253251

252+
// Note, ensure our settings stay in sync across cxx-qt-build and cxx-qt-lib
253+
builder.cpp(true);
254254
// MSVC
255255
builder.flag_if_supported("/std:c++17");
256256
builder.flag_if_supported("/Zc:__cplusplus");
257257
builder.flag_if_supported("/permissive-");
258258
// GCC + Clang
259259
builder.flag_if_supported("-std=c++17");
260+
260261
builder.compile("cxx-qt-lib");
261262
}

0 commit comments

Comments
 (0)