Skip to content

Commit 24e624b

Browse files
committed
WIP: cxx-qt-build: include headers directory manually
1 parent ba76e47 commit 24e624b

File tree

15 files changed

+26
-93
lines changed

15 files changed

+26
-93
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ 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+
// If the path is relative then use the CARGO_MANIFEST_DIR as the root
143+
let header_dir = if header_dir.is_relative() {
144+
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join(header_dir)
145+
} else {
146+
PathBuf::from(header_dir)
147+
};
148+
match dir::symlink_or_copy_directory(&header_dir, &dest_dir) {
143149
Ok(true) => {},
144150
Ok(false) => panic!("Failed to create symlink folder for `{dest}`"),
145151
Err(e) => panic!(

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

+5-37
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,8 @@
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

66
use cxx_qt_build::CxxQtBuilder;
7-
use std::path::PathBuf;
8-
9-
fn header_dir() -> PathBuf {
10-
PathBuf::from(std::env::var("OUT_DIR").unwrap())
11-
.join("include")
12-
.join("cxx-qt-lib-extras")
13-
}
14-
15-
fn write_headers_in(subfolder: &str) {
16-
println!("cargo::rerun-if-changed=include/{subfolder}");
17-
18-
for entry in
19-
std::fs::read_dir(format!("include/{subfolder}")).expect("Failed to read include directory")
20-
{
21-
let entry = entry.expect("Failed to read header file!");
22-
let header_name = entry.file_name();
23-
println!(
24-
"cargo::rerun-if-changed=include/{subfolder}/{header_name}",
25-
header_name = header_name.to_string_lossy()
26-
);
27-
28-
// TODO: Do we want to add the headers into a subdirectory?
29-
std::fs::copy(entry.path(), header_dir().join(header_name))
30-
.expect("Failed to copy header file!");
31-
}
32-
}
33-
34-
fn write_headers() {
35-
println!("cargo::rerun-if-changed=include/");
36-
std::fs::create_dir_all(header_dir()).expect("Failed to create include directory");
37-
38-
write_headers_in("core");
39-
write_headers_in("gui");
40-
}
417

428
fn main() {
43-
write_headers();
44-
459
let mut builder = CxxQtBuilder::library()
4610
.qt_module("Gui")
4711
.qt_module("Widgets");
@@ -71,6 +35,10 @@ fn main() {
7135
}
7236
cc.file("src/qt_types.cpp");
7337
println!("cargo::rerun-if-changed=src/qt_types.cpp");
38+
39+
// TODO: automatically add any include/ dir like best_effort_copy_headers
40+
println!("cargo::rerun-if-changed=include/");
41+
cc.include("include");
7442
});
7543
println!("cargo::rerun-if-changed=src/assertion_utils.h");
7644

@@ -83,7 +51,7 @@ fn main() {
8351
// Disable exporting the standard include directory, as we are exporting custom header
8452
interface
8553
.export_include_prefixes([])
86-
.export_include_directory(header_dir(), "cxx-qt-lib-extras")
54+
.export_include_directory("include/cxx-qt-lib-extras", "cxx-qt-lib-extras")
8755
.reexport_dependency("cxx-qt-lib")
8856
.export();
8957
}

crates/cxx-qt-lib/build.rs

+8
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ fn main() {
383383
}
384384
cc.file("src/qt_types.cpp");
385385
println!("cargo::rerun-if-changed=src/qt_types.cpp");
386+
387+
// With cxx-qt-lib we need to have generated headers in the include folder
388+
// so we copy them into the OUT_DIR and construct a folder
389+
//
390+
// TODO: can this be improved in anyway?
391+
// if we included the include folder from the source dir how do we combine
392+
// the generated headers into the export include dir?
393+
cc.include(header_dir().parent().expect("header_dir has a parent"));
386394
});
387395

388396
let interface = builder.build();

crates/cxx-qt/build.rs

+6-29
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,9 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6-
use std::path::PathBuf;
7-
86
use cxx_qt_build::CxxQtBuilder;
97

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-
"casting.h",
23-
"signalhandler.h",
24-
"thread.h",
25-
"threading.h",
26-
"type.h",
27-
] {
28-
println!("cargo::rerun-if-changed=include/{file_path}");
29-
std::fs::copy(format!("include/{file_path}"), header_dir().join(file_path))
30-
.expect("Failed to copy header file!");
31-
}
32-
}
33-
348
fn main() {
35-
write_headers();
36-
379
let mut builder = CxxQtBuilder::library();
3810

3911
let cpp_files = ["src/connection.cpp"];
@@ -48,6 +20,10 @@ fn main() {
4820
cc.file(cpp_file);
4921
println!("cargo::rerun-if-changed={cpp_file}");
5022
}
23+
24+
// TODO: automatically add any include/ dir like best_effort_copy_headers
25+
println!("cargo::rerun-if-changed=include/");
26+
cc.include("include");
5127
});
5228
builder = builder.initializer(qt_build_utils::Initializer {
5329
file: Some("src/init.cpp".into()),
@@ -57,6 +33,7 @@ fn main() {
5733
let interface = builder.build();
5834
interface
5935
.export_include_prefixes([])
60-
.export_include_directory(header_dir(), "cxx-qt")
36+
// TODO: should we always export or is this fine?
37+
.export_include_directory("include/cxx-qt", "cxx-qt")
6138
.export();
6239
}
File renamed without changes.

crates/cxx-qt/src/lib.rs

-26
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
1212
use std::ops::Deref;
1313
use std::pin::Pin;
14-
use std::{fs::File, io::Write, path::Path};
1514

1615
mod connection;
1716
mod connectionguard;
@@ -433,28 +432,3 @@ where
433432
Self::initialize(self);
434433
}
435434
}
436-
437-
#[doc(hidden)]
438-
// Write the cxx-qt headers to the specified directory.
439-
pub fn write_headers(directory: impl AsRef<Path>) {
440-
let directory = directory.as_ref();
441-
std::fs::create_dir_all(directory).expect("Could not create cxx-qt header directory");
442-
// Note ensure that the build script is consistent with files that are copied
443-
for (file_contents, file_name) in [
444-
(include_str!("../include/connection.h"), "connection.h"),
445-
(
446-
include_str!("../include/signalhandler.h"),
447-
"signalhandler.h",
448-
),
449-
(include_str!("../include/thread.h"), "thread.h"),
450-
(include_str!("../include/threading.h"), "threading.h"),
451-
(include_str!("../include/type.h"), "type.h"),
452-
] {
453-
// Note that we do not need rerun-if-changed for these files
454-
// as include_str causes a rerun when the header changes
455-
// and the files are always written to the target.
456-
let h_path = format!("{}/{file_name}", directory.display());
457-
let mut header = File::create(h_path).expect("Could not create cxx-qt header");
458-
write!(header, "{file_contents}").expect("Could not write cxx-qt header");
459-
}
460-
}

0 commit comments

Comments
 (0)