Skip to content

Commit 2aea611

Browse files
committed
cxx-qt: use cxx-qt-build and reexport from cxx-qt-lib
This removes a build depends of cxx-qt-build on cxx-qt
1 parent e6ee353 commit 2aea611

File tree

6 files changed

+55
-55
lines changed

6 files changed

+55
-55
lines changed

crates/cxx-qt-build/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ repository.workspace = true
1515
[dependencies]
1616
cc.workspace = true
1717
cxx-gen.workspace = true
18-
cxx-qt.workspace = true
1918
cxx-qt-gen.workspace = true
2019
proc-macro2.workspace = true
2120
quote.workspace = true

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,7 @@ impl CxxQtBuilder {
585585

586586
fn write_common_headers() {
587587
let header_root = dir::header_root();
588-
// Write cxx-qt and cxx headers
589-
cxx_qt::write_headers(header_root.join("cxx-qt"));
588+
// Write cxx headers
590589
std::fs::create_dir_all(header_root.join("rust"))
591590
.expect("Could not create cxx header directory");
592591
let h_path = header_root.join("rust").join("cxx.h");
@@ -649,7 +648,7 @@ impl CxxQtBuilder {
649648
include_paths: &[impl AsRef<Path>],
650649
defines: &[(String, Option<String>)],
651650
) {
652-
// Note, ensure our settings stay in sync across cxx-qt, cxx-qt-build, and cxx-qt-lib
651+
// Note, ensure our settings stay in sync across cxx-qt and cxx-qt-lib
653652
builder.cpp(true);
654653
builder.std("c++17");
655654
// MSVC

crates/cxx-qt-lib/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ fn main() {
313313
let mut interface = cxx_qt_build::Interface::default()
314314
.initializer("src/core/init.cpp")
315315
.export_include_prefixes([])
316-
.export_include_directory(header_dir(), "cxx-qt-lib");
316+
.export_include_directory(header_dir(), "cxx-qt-lib")
317+
.reexport_dependency("cxx-qt");
317318

318319
if qt_gui_enabled() {
319320
interface = interface

crates/cxx-qt/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ readme = "README.md"
1717
keywords = ["cxx", "ffi", "QML", "Qt"]
1818
categories = ["api-bindings", "gui"]
1919

20+
# When creating a library with cxx-qt-build, we need to set a fake "links" key
21+
# to make sure the build scripts are run in the correct order and the build scripts
22+
# can pass metadata from library to dependent.
23+
# See also: https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key
24+
links = "cxx-qt"
25+
2026
[dependencies]
2127
cxx.workspace = true
2228
cxx-qt-macro.workspace = true
2329
static_assertions = "1.1.0"
2430

2531
[build-dependencies]
26-
cxx-build.workspace = true
32+
cxx-qt-build.workspace = true
2733
qt-build-utils.workspace = true
2834

2935
[dev-dependencies]

crates/cxx-qt/build.rs

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,55 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6-
use std::{fs::File, io::Write};
6+
use std::path::PathBuf;
7+
8+
use cxx_qt_build::{CxxQtBuilder, Interface};
9+
10+
fn header_dir() -> PathBuf {
11+
PathBuf::from(std::env::var("OUT_DIR").unwrap())
12+
.join("include")
13+
.join("cxx-qt")
14+
}
15+
16+
fn write_headers() {
17+
println!("cargo::rerun-if-changed=include/");
18+
std::fs::create_dir_all(header_dir()).expect("Failed to create include directory");
19+
20+
for file_path in [
21+
"connection.h",
22+
"signalhandler.h",
23+
"thread.h",
24+
"threading.h",
25+
"type.h",
26+
] {
27+
println!("cargo::rerun-if-changed=include/{file_path}");
28+
std::fs::copy(format!("include/{file_path}"), header_dir().join(file_path))
29+
.expect("Failed to copy header file!");
30+
}
31+
}
732

833
fn main() {
9-
let qtbuild = qt_build_utils::QtBuild::new(vec!["Core".to_owned()])
10-
.expect("Could not find Qt installation");
34+
write_headers();
35+
36+
let interface = Interface::default()
37+
.export_include_prefixes([])
38+
.export_include_directory(header_dir(), "cxx-qt");
1139

12-
// Required for tests
13-
qt_build_utils::setup_linker();
40+
let mut builder = CxxQtBuilder::library(interface);
1441

1542
let cpp_files = ["src/connection.cpp"];
1643
let rust_bridges = ["src/connection.rs"];
1744

1845
for bridge in &rust_bridges {
19-
println!("cargo:rerun-if-changed={bridge}");
46+
builder = builder.file(bridge);
2047
}
2148

22-
let mut builder = cxx_build::bridges(rust_bridges);
49+
builder = builder.cc_builder(move |cc| {
50+
for cpp_file in &cpp_files {
51+
cc.file(cpp_file);
52+
println!("cargo::rerun-if-changed={cpp_file}");
53+
}
54+
});
2355

24-
qtbuild.cargo_link_libraries(&mut builder);
25-
26-
for cpp_file in &cpp_files {
27-
builder.file(cpp_file);
28-
println!("cargo:rerun-if-changed={cpp_file}");
29-
}
30-
31-
// Write this library's manually written C++ headers to files and add them to include paths
32-
let out_dir = std::env::var("OUT_DIR").unwrap();
33-
let directory = format!("{out_dir}/cxx-qt");
34-
std::fs::create_dir_all(&directory).expect("Could not create cxx-qt header directory");
35-
// Note we only need connection.h for now, but lets move all headers to be consistent
36-
// ensure src/lib write_headers is consistent
37-
for (file_contents, file_name) in [
38-
(include_str!("include/connection.h"), "connection.h"),
39-
(include_str!("include/signalhandler.h"), "signalhandler.h"),
40-
(include_str!("include/thread.h"), "thread.h"),
41-
(include_str!("include/threading.h"), "threading.h"),
42-
(include_str!("include/type.h"), "type.h"),
43-
] {
44-
let h_path = format!("{directory}/{file_name}");
45-
let mut header = File::create(h_path).expect("Could not create cxx-qt header");
46-
write!(header, "{file_contents}").expect("Could not write cxx-qt header");
47-
}
48-
builder.include(out_dir);
49-
builder.includes(qtbuild.include_paths());
50-
51-
// Note, ensure our settings stay in sync across cxx-qt, cxx-qt-build, and cxx-qt-lib
52-
builder.cpp(true);
53-
builder.std("c++17");
54-
// MSVC
55-
builder.flag_if_supported("/Zc:__cplusplus");
56-
builder.flag_if_supported("/permissive-");
57-
builder.flag_if_supported("/bigobj");
58-
// MinGW requires big-obj otherwise debug builds fail
59-
builder.flag_if_supported("-Wa,-mbig-obj");
60-
61-
builder.compile("cxx-qt");
56+
builder.build();
6257
}

scripts/release_crates.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ release_crate "cxx-qt-gen"
7070
# Requires cxx-qt-gen
7171
release_crate "cxx-qt-macro"
7272

73-
# Requires cxx-qt-macro and qt-build-utils
74-
release_crate "cxx-qt"
75-
76-
# Requires cxx-qt, cxx-qt-gen, and qt-build-utils
73+
# Requires cxx-qt-gen and qt-build-utils
7774
release_crate "cxx-qt-build"
7875

76+
# Requires cxx-qt-build, cxx-qt-macro, and qt-build-utils
77+
release_crate "cxx-qt"
78+
7979
# Requires cxx-qt, cxx-qt-build
8080
release_crate "cxx-qt-lib"
8181

0 commit comments

Comments
 (0)