Skip to content

Commit 1339314

Browse files
ahayzen-kdabLeonMatthesKDAB
authored andcommitted
cxx-qt-lib: use cxx-qt-build to build rather than cxx
1 parent 22af7db commit 1339314

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

crates/cxx-qt-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ time = { version = "0.3.20", optional = true }
2424
url = { version = "2.3", optional = true }
2525

2626
[build-dependencies]
27-
cxx-build.workspace = true
27+
cxx-qt-build.workspace = true
2828
cxx-qt-lib-headers.workspace = true
2929
qt-build-utils.workspace = true
3030

crates/cxx-qt-lib/build.rs

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6+
use cxx_qt_build::CxxQtBuilder;
7+
use qt_build_utils::QtBuild;
68
use std::{fs::File, io::Write};
79

810
fn main() {
@@ -13,24 +15,26 @@ fn main() {
1315
Err(_) => false,
1416
};
1517

16-
let mut qt_modules = vec!["Core".to_owned()];
18+
let mut builder = CxxQtBuilder::new();
19+
// Not strictly required to enable modules as the feature should do this
20+
// build lets be explicit for now.
1721
if feature_qt_gui_enabled {
18-
qt_modules.push("Gui".to_owned());
22+
builder = builder.qt_module("Gui");
1923
}
2024
if feature_qt_qml_enabled {
21-
qt_modules.push("Qml".to_owned());
25+
builder = builder.qt_module("Qml");
2226
}
2327

24-
let qtbuild = qt_build_utils::QtBuild::new(qt_modules).expect("Could not find Qt installation");
25-
26-
// Required for tests
27-
qt_build_utils::setup_linker();
28-
2928
// Find the Qt version and tell the Rust compiler
3029
// this allows us to have conditional Rust code
30+
//
31+
// TODO: is this useful to have in cxx-qt-build?
3132
println!(
3233
"cargo:rustc-cfg=qt_version_major=\"{}\"",
33-
qtbuild.version().major
34+
QtBuild::new(vec!())
35+
.expect("Could not find Qt installation")
36+
.version()
37+
.major
3438
);
3539

3640
let mut rust_bridges = vec![
@@ -189,15 +193,10 @@ fn main() {
189193
]);
190194
}
191195

192-
for bridge in &rust_bridges {
193-
println!("cargo:rerun-if-changed=src/{bridge}.rs");
196+
for rust_source in &rust_bridges {
197+
builder = builder.file(format!("src/{rust_source}.rs"));
194198
}
195199

196-
let mut builder =
197-
cxx_build::bridges(rust_bridges.iter().map(|bridge| format!("src/{bridge}.rs")));
198-
199-
qtbuild.cargo_link_libraries(&mut builder);
200-
201200
let mut cpp_files = vec![
202201
"core/qbytearray",
203202
"core/qcoreapplication",
@@ -251,12 +250,14 @@ fn main() {
251250
cpp_files.extend(["core/qdatetime", "core/qtimezone"]);
252251
}
253252

254-
for cpp_file in &cpp_files {
255-
builder.file(format!("src/{cpp_file}.cpp"));
256-
println!("cargo:rerun-if-changed=src/{cpp_file}.cpp");
257-
}
258-
builder.file("src/qt_types.cpp");
259-
println!("cargo:rerun-if-changed=src/qt_types.cpp");
253+
builder = builder.cc_builder(move |cc| {
254+
for cpp_file in &cpp_files {
255+
cc.file(format!("src/{cpp_file}.cpp"));
256+
println!("cargo:rerun-if-changed=src/{cpp_file}.cpp");
257+
}
258+
cc.file("src/qt_types.cpp");
259+
println!("cargo:rerun-if-changed=src/qt_types.cpp");
260+
});
260261
println!("cargo:rerun-if-changed=src/assertion_utils.h");
261262

262263
// Write this library's manually written C++ headers to files and add them to include paths
@@ -275,33 +276,20 @@ fn main() {
275276
write!(header, "{file_contents}").expect("Could not write header: {h_path}");
276277
}
277278

278-
// Load the include paths
279-
//
280-
// TODO: note once we use cxx-qt-build we don't need to include the Qt paths here
281-
builder.includes(qtbuild.include_paths());
282-
builder.include(header_root);
283-
284-
// Enable Qt Gui in C++ if the feature is enabled
285-
if feature_qt_gui_enabled {
286-
builder.define("CXX_QT_GUI_FEATURE", None);
287-
}
279+
builder = builder.cc_builder(|cc| {
280+
// Load the include path
281+
cc.include(&header_root);
288282

289-
// Enable Qt Qml in C++ if the feature is enabled
290-
if feature_qt_gui_enabled {
291-
builder.define("CXX_QT_QML_FEATURE", None);
292-
}
283+
// Enable Qt Gui in C++ if the feature is enabled
284+
if feature_qt_gui_enabled {
285+
cc.define("CXX_QT_GUI_FEATURE", None);
286+
}
293287

294-
// Note, ensure our settings stay in sync across cxx-qt, cxx-qt-build, and cxx-qt-lib
295-
builder.cpp(true);
296-
// MSVC
297-
builder.flag_if_supported("/std:c++17");
298-
builder.flag_if_supported("/Zc:__cplusplus");
299-
builder.flag_if_supported("/permissive-");
300-
builder.flag_if_supported("/bigobj");
301-
// GCC + Clang
302-
builder.flag_if_supported("-std=c++17");
303-
// MinGW requires big-obj otherwise debug builds fail
304-
builder.flag_if_supported("-Wa,-mbig-obj");
288+
// Enable Qt Qml in C++ if the feature is enabled
289+
if feature_qt_gui_enabled {
290+
cc.define("CXX_QT_QML_FEATURE", None);
291+
}
292+
});
305293

306-
builder.compile("cxx-qt-lib");
294+
builder.build();
307295
}

0 commit comments

Comments
 (0)