cxx_qt_lib_headers/
lib.rs

1// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Be Wilson <be.wilson@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6#![deny(missing_docs)]
7
8//! This crate is a hack so build.rs for cxx-qt-lib and cxx-qt-build both have access to cxx-qt-lib's C++ headers.
9//! This must be a separate crate from cxx-qt-lib because cxx-qt-lib cannot be a build dependency of cxx-qt-build.
10//! Otherwise Cargo links the executable compiled from a build.rs that uses cxx-qt-build to Qt, so running
11//! build.rs fails when Qt is linked dynamically if the Qt libraries are not in PATH (Windows)/LD_LIBRARY_PATH (Unix).
12
13use std::{fs::File, io::Write, path::Path};
14
15/// Write the cxx-qt-lib headers to the specified directory.
16pub fn write_headers(directory: impl AsRef<Path>) {
17    let directory = directory.as_ref();
18    std::fs::create_dir_all(directory).expect("Could not create cxx-qt-lib header directory");
19    for (file_contents, file_name) in [
20        (include_str!("../include/core/qbytearray.h"), "qbytearray.h"),
21        (
22            include_str!("../include/core/qcoreapplication.h"),
23            "qcoreapplication.h",
24        ),
25        (include_str!("../include/core/qdate.h"), "qdate.h"),
26        (include_str!("../include/core/qdatetime.h"), "qdatetime.h"),
27        (include_str!("../include/core/qhash.h"), "qhash.h"),
28        (include_str!("../include/core/qlist.h"), "qlist.h"),
29        (
30            include_str!("../include/core/qlist_qvector.h"),
31            "qlist_qvector.h",
32        ),
33        (include_str!("../include/core/qmap.h"), "qmap.h"),
34        (include_str!("../include/core/qmargins.h"), "qmargins.h"),
35        (include_str!("../include/core/qmarginsf.h"), "qmarginsf.h"),
36        (
37            include_str!("../include/core/qmetaobjectconnection.h"),
38            "qmetaobjectconnection.h",
39        ),
40        (
41            include_str!("../include/core/qmodelindex.h"),
42            "qmodelindex.h",
43        ),
44        (
45            include_str!("../include/core/qpersistentmodelindex.h"),
46            "qpersistentmodelindex.h",
47        ),
48        (include_str!("../include/core/qpoint.h"), "qpoint.h"),
49        (include_str!("../include/core/qpointf.h"), "qpointf.h"),
50        (include_str!("../include/core/qrect.h"), "qrect.h"),
51        (include_str!("../include/core/qrectf.h"), "qrectf.h"),
52        (include_str!("../include/core/qset.h"), "qset.h"),
53        (include_str!("../include/core/qsize.h"), "qsize.h"),
54        (include_str!("../include/core/qsizef.h"), "qsizef.h"),
55        (include_str!("../include/core/qstring.h"), "qstring.h"),
56        (
57            include_str!("../include/core/qstringlist.h"),
58            "qstringlist.h",
59        ),
60        (include_str!("../include/core/qt.h"), "qt.h"),
61        (include_str!("../include/core/qtime.h"), "qtime.h"),
62        (include_str!("../include/core/qtimezone.h"), "qtimezone.h"),
63        (include_str!("../include/core/qurl.h"), "qurl.h"),
64        (include_str!("../include/core/qvariant.h"), "qvariant.h"),
65        (include_str!("../include/core/qvector.h"), "qvector.h"),
66        #[cfg(feature = "qt_gui")]
67        (include_str!("../include/gui/qcolor.h"), "qcolor.h"),
68        #[cfg(feature = "qt_gui")]
69        (
70            include_str!("../include/gui/qguiapplication.h"),
71            "qguiapplication.h",
72        ),
73        #[cfg(feature = "qt_gui")]
74        (include_str!("../include/gui/qimage.h"), "qimage.h"),
75        #[cfg(feature = "qt_gui")]
76        (include_str!("../include/gui/qvector2d.h"), "qvector2d.h"),
77        #[cfg(feature = "qt_gui")]
78        (include_str!("../include/gui/qvector3d.h"), "qvector3d.h"),
79        #[cfg(feature = "qt_gui")]
80        (include_str!("../include/gui/qvector4d.h"), "qvector4d.h"),
81        #[cfg(feature = "qt_qml")]
82        (
83            include_str!("../include/qml/qqmlapplicationengine.h"),
84            "qqmlapplicationengine.h",
85        ),
86        #[cfg(feature = "qt_qml")]
87        (include_str!("../include/qml/qqmlengine.h"), "qqmlengine.h"),
88        (include_str!("../include/common.h"), "common.h"),
89    ] {
90        // Note that we do not need rerun-if-changed for these files
91        // as include_str causes a rerun when the header changes
92        // and the files are always written to the target.
93        let h_path = format!("{}/{file_name}", directory.display());
94        let mut header = File::create(h_path).expect("Could not create cxx-qt-lib header");
95        write!(header, "{file_contents}").expect("Could not write cxx-qt-lib header");
96    }
97}