Skip to content

Commit 7d8b903

Browse files
committed
cxx-qt-build: include headers automatically like CXX
1 parent 253b7af commit 7d8b903

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+149
-146
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,15 @@ impl Interface {
139139
.expect("Failed to create header root for interface");
140140
for (header_dir, dest) in &self.exported_include_directories {
141141
let dest_dir = header_root_interface.join(dest);
142-
match dir::symlink_or_copy_directory(header_dir, &dest_dir) {
142+
// Relative paths are take from under the header root
143+
//
144+
// TODO: is this correct?
145+
let header_dir = if header_dir.is_relative() {
146+
dir::header_root().join(header_dir)
147+
} else {
148+
header_dir.clone()
149+
};
150+
match dir::symlink_or_copy_directory(&header_dir, &dest_dir) {
143151
Ok(true) => {},
144152
Ok(false) => panic!("Failed to create symlink folder for `{dest}`"),
145153
Err(e) => panic!(

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

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mod qml_modules;
3333
use qml_modules::OwningQmlModule;
3434
pub use qml_modules::QmlModule;
3535

36+
mod utils;
37+
3638
pub use qt_build_utils::MocArguments;
3739
use qt_build_utils::SemVer;
3840
use quote::ToTokens;
@@ -1083,6 +1085,18 @@ extern "C" bool {init_fun}() {{
10831085
// to the generated files without any namespacing.
10841086
include_paths.push(header_root.join(&self.include_prefix));
10851087

1088+
// Automatically add any header looking files to the header_root, so that they are
1089+
// a) in the include path
1090+
// b) able to be reexported
1091+
let manifest_dir =
1092+
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("No CARGO_MANIFEST_DIR set"));
1093+
let max_depth = 6;
1094+
utils::best_effort_copy_headers(
1095+
manifest_dir.as_path(),
1096+
header_root.join(crate_name()).as_path(),
1097+
max_depth,
1098+
);
1099+
10861100
Self::setup_cc_builder(&mut self.cc_builder, &include_paths);
10871101

10881102
// Generate files

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

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: CXX Authors
2+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
3+
// SPDX-FileContributor: David Tolnay <[email protected]>
4+
//
5+
// SPDX-License-Identifier: MIT OR Apache-2.0
6+
7+
// Modified from
8+
// https://github.com/dtolnay/cxx/blob/0608b11f31c40d6ca11abbf51395f16c4c16ad5e/gen/build/src/lib.rs#L426
9+
10+
use std::{ffi::OsStr, fs, path::Path};
11+
12+
pub(crate) fn best_effort_copy_headers(src: &Path, dst: &Path, max_depth: usize) {
13+
let mut dst_created = false;
14+
let Ok(mut entries) = fs::read_dir(src) else {
15+
return;
16+
};
17+
18+
while let Some(Ok(entry)) = entries.next() {
19+
let file_name = entry.file_name();
20+
if file_name.to_string_lossy().starts_with('.') {
21+
continue;
22+
}
23+
match entry.file_type() {
24+
Ok(file_type) if file_type.is_dir() && max_depth > 0 => {
25+
let src = entry.path();
26+
if src.join("Cargo.toml").exists() || src.join("CACHEDIR.TAG").exists() {
27+
continue;
28+
}
29+
let dst = dst.join(file_name);
30+
best_effort_copy_headers(&src, &dst, max_depth - 1);
31+
}
32+
Ok(file_type) if file_type.is_file() => {
33+
let src = entry.path();
34+
match src.extension().and_then(OsStr::to_str) {
35+
Some("h" | "hh" | "hpp") => {}
36+
_ => continue,
37+
}
38+
if !dst_created && fs::create_dir_all(dst).is_err() {
39+
return;
40+
}
41+
42+
// Ensure that we rebuild if there are changes
43+
println!("cargo::rerun-if-changed={}", src.to_string_lossy());
44+
45+
dst_created = true;
46+
let dst = dst.join(file_name);
47+
let _ = fs::remove_file(&dst);
48+
let _ = fs::copy(src, dst);
49+
}
50+
_ => {}
51+
}
52+
}
53+
}

crates/cxx-qt-gen/src/generator/cpp/cxxqttype.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn generate(qobject_idents: &QObjectNames) -> Result<GeneratedCppQObjectBloc
1313

1414
result
1515
.includes
16-
.insert("#include <cxx-qt/type.h>".to_owned());
16+
.insert("#include <cxx-qt/include/type.h>".to_owned());
1717

1818
result
1919
.base_classes
@@ -36,7 +36,9 @@ mod tests {
3636

3737
// includes
3838
assert_eq!(generated.includes.len(), 1);
39-
assert!(generated.includes.contains("#include <cxx-qt/type.h>"));
39+
assert!(generated
40+
.includes
41+
.contains("#include <cxx-qt/include/type.h>"));
4042

4143
// base class
4244
assert_eq!(generated.base_classes.len(), 1);

crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ pub fn generate(
5151

5252
// Include casting header
5353
let mut result = GeneratedCppExternCxxQtBlocks::default();
54-
result.includes.insert("#include <cxx-qt/casting.h>".into());
54+
result
55+
.includes
56+
.insert("#include <cxx-qt/include/casting.h>".into());
5557

5658
out.push(result);
5759

crates/cxx-qt-gen/src/generator/cpp/qobject.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ impl GeneratedCppQObject {
176176

177177
// Include casting header
178178
let mut result = GeneratedCppQObjectBlocks::default();
179-
result.includes.insert("#include <cxx-qt/casting.h>".into());
179+
result
180+
.includes
181+
.insert("#include <cxx-qt/include/casting.h>".into());
180182

181183
generated.blocks.append(&mut result);
182184

crates/cxx-qt-gen/src/generator/cpp/signal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn generate_cpp_signal(
9696
// Add the include we need
9797
generated
9898
.includes
99-
.insert("#include <cxx-qt/signalhandler.h>".to_owned());
99+
.insert("#include <cxx-qt/include/signalhandler.h>".to_owned());
100100

101101
// Build a namespace that includes any namespace for the T
102102
let qobject_ident_namespaced = qobject_name.cxx_qualified();

crates/cxx-qt-gen/src/generator/cpp/threading.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn generate(qobject_idents: &QObjectNames) -> Result<(String, GeneratedCppQO
3030

3131
result
3232
.includes
33-
.insert("#include <cxx-qt/threading.h>".to_owned());
33+
.insert("#include <cxx-qt/include/threading.h>".to_owned());
3434

3535
result
3636
.base_classes
@@ -84,7 +84,9 @@ mod tests {
8484

8585
// includes
8686
assert_eq!(generated.includes.len(), 1);
87-
assert!(generated.includes.contains("#include <cxx-qt/threading.h>"));
87+
assert!(generated
88+
.includes
89+
.contains("#include <cxx-qt/include/threading.h>"));
8890

8991
// base class
9092
assert_eq!(generated.base_classes.len(), 1);

crates/cxx-qt-gen/src/generator/rust/threading.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn generate(
6262
#[namespace = #cxx_qt_thread_namespace]
6363
#(#cfgs)*
6464
type #cxx_qt_thread_ident = cxx_qt::CxxQtThread<#cpp_struct_ident>;
65-
include!("cxx-qt/thread.h");
65+
include!("cxx-qt/include/thread.h");
6666

6767
#[doc(hidden)]
6868
#(#thread_fn_attrs)*
@@ -209,7 +209,7 @@ mod tests {
209209
#[doc(hidden)]
210210
#[namespace = ""]
211211
type MyObjectCxxQtThread = cxx_qt::CxxQtThread<MyObject>;
212-
include!("cxx-qt/thread.h");
212+
include!("cxx-qt/include/thread.h");
213213

214214
#[doc(hidden)]
215215
#[cxx_name = "qtThread"]

crates/cxx-qt-gen/src/writer/cpp/header.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ mod tests {
256256
let expected = indoc! {r#"
257257
#pragma once
258258
259-
#include <cxx-qt/casting.h>
260-
#include <cxx-qt/type.h>
259+
#include <cxx-qt/include/casting.h>
260+
#include <cxx-qt/include/type.h>
261261
262262
class MyObject;
263263

crates/cxx-qt-gen/src/writer/rust/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn signal_boilerplate() -> TokenStream {
6767
unsafe extern "C++" {
6868
include ! (< QtCore / QObject >);
6969

70-
include!("cxx-qt/connection.h");
70+
include!("cxx-qt/include/connection.h");
7171
#[doc(hidden)]
7272
#[namespace = "Qt"]
7373
#[rust_name = "CxxQtConnectionType"]

crates/cxx-qt-gen/test_outputs/cfgs.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

33
#include <cstdint>
4-
#include <cxx-qt/casting.h>
5-
#include <cxx-qt/signalhandler.h>
6-
#include <cxx-qt/type.h>
4+
#include <cxx-qt/include/casting.h>
5+
#include <cxx-qt/include/signalhandler.h>
6+
#include <cxx-qt/include/type.h>
77

88
class QObjectEnabled;
99

crates/cxx-qt-gen/test_outputs/cfgs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod ffi {
44
unsafe extern "C++" {
55
include ! (< QtCore / QObject >);
6-
include!("cxx-qt/connection.h");
6+
include!("cxx-qt/include/connection.h");
77
#[doc(hidden)]
88
#[namespace = "Qt"]
99
#[rust_name = "CxxQtConnectionType"]

crates/cxx-qt-gen/test_outputs/inheritance.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <cxx-qt/casting.h>
4-
#include <cxx-qt/type.h>
3+
#include <cxx-qt/include/casting.h>
4+
#include <cxx-qt/include/type.h>
55

66
class MyObject;
77

crates/cxx-qt-gen/test_outputs/inheritance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod inheritance {
1010
}
1111
unsafe extern "C++" {
1212
include ! (< QtCore / QObject >);
13-
include!("cxx-qt/connection.h");
13+
include!("cxx-qt/include/connection.h");
1414
#[doc(hidden)]
1515
#[namespace = "Qt"]
1616
#[rust_name = "CxxQtConnectionType"]

crates/cxx-qt-gen/test_outputs/invokables.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include <cxx-qt/casting.h>
4-
#include <cxx-qt/threading.h>
5-
#include <cxx-qt/type.h>
3+
#include <cxx-qt/include/casting.h>
4+
#include <cxx-qt/include/threading.h>
5+
#include <cxx-qt/include/type.h>
66

77
namespace cxx_qt::my_object {
88
class MyObject;

crates/cxx-qt-gen/test_outputs/invokables.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod ffi {
1515
}
1616
unsafe extern "C++" {
1717
include ! (< QtCore / QObject >);
18-
include!("cxx-qt/connection.h");
18+
include!("cxx-qt/include/connection.h");
1919
#[doc(hidden)]
2020
#[namespace = "Qt"]
2121
#[rust_name = "CxxQtConnectionType"]
@@ -125,7 +125,7 @@ mod ffi {
125125
#[doc(hidden)]
126126
#[namespace = "cxx_qt::my_object"]
127127
type MyObjectCxxQtThread = cxx_qt::CxxQtThread<MyObject>;
128-
include!("cxx-qt/thread.h");
128+
include!("cxx-qt/include/thread.h");
129129
#[doc(hidden)]
130130
#[cxx_name = "qtThread"]
131131
#[namespace = "rust::cxxqt1"]

crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include <cxx-qt/casting.h>
4-
#include <cxx-qt/signalhandler.h>
5-
#include <cxx-qt/type.h>
3+
#include <cxx-qt/include/casting.h>
4+
#include <cxx-qt/include/signalhandler.h>
5+
#include <cxx-qt/include/type.h>
66

77
namespace cxx_qt::multi_object {
88
class MyObject;

crates/cxx-qt-gen/test_outputs/passthrough_and_naming.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub mod ffi {
5757
}
5858
unsafe extern "C++" {
5959
include ! (< QtCore / QObject >);
60-
include!("cxx-qt/connection.h");
60+
include!("cxx-qt/include/connection.h");
6161
#[doc(hidden)]
6262
#[namespace = "Qt"]
6363
#[rust_name = "CxxQtConnectionType"]

crates/cxx-qt-gen/test_outputs/properties.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include <cxx-qt/casting.h>
4-
#include <cxx-qt/signalhandler.h>
5-
#include <cxx-qt/type.h>
3+
#include <cxx-qt/include/casting.h>
4+
#include <cxx-qt/include/signalhandler.h>
5+
#include <cxx-qt/include/type.h>
66

77
namespace cxx_qt::my_object {
88
class MyObject;

crates/cxx-qt-gen/test_outputs/properties.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod ffi {
88
}
99
unsafe extern "C++" {
1010
include ! (< QtCore / QObject >);
11-
include!("cxx-qt/connection.h");
11+
include!("cxx-qt/include/connection.h");
1212
#[doc(hidden)]
1313
#[namespace = "Qt"]
1414
#[rust_name = "CxxQtConnectionType"]

crates/cxx-qt-gen/test_outputs/qenum.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <QtCore/QObject>
44
#include <QtQml/QQmlEngine>
55
#include <cstdint>
6-
#include <cxx-qt/casting.h>
7-
#include <cxx-qt/type.h>
6+
#include <cxx-qt/include/casting.h>
7+
#include <cxx-qt/include/type.h>
88

99
namespace cxx_qt::my_object {
1010
class MyObject;

crates/cxx-qt-gen/test_outputs/qenum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod ffi {
44
unsafe extern "C++" {
55
include ! (< QtCore / QObject >);
6-
include!("cxx-qt/connection.h");
6+
include!("cxx-qt/include/connection.h");
77
#[doc(hidden)]
88
#[namespace = "Qt"]
99
#[rust_name = "CxxQtConnectionType"]

crates/cxx-qt-gen/test_outputs/signals.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include <cxx-qt/casting.h>
4-
#include <cxx-qt/signalhandler.h>
5-
#include <cxx-qt/type.h>
3+
#include <cxx-qt/include/casting.h>
4+
#include <cxx-qt/include/signalhandler.h>
5+
#include <cxx-qt/include/type.h>
66

77
namespace cxx_qt::my_object {
88
class MyObject;

crates/cxx-qt-gen/test_outputs/signals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod ffi {
99
}
1010
unsafe extern "C++" {
1111
include ! (< QtCore / QObject >);
12-
include!("cxx-qt/connection.h");
12+
include!("cxx-qt/include/connection.h");
1313
#[doc(hidden)]
1414
#[namespace = "Qt"]
1515
#[rust_name = "CxxQtConnectionType"]

0 commit comments

Comments
 (0)