1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Be Wilson <be.wilson@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#![deny(missing_docs)]

//! 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.
//! This must be a separate crate from cxx-qt-lib because cxx-qt-lib cannot be a build dependency of cxx-qt-build.
//! Otherwise Cargo links the executable compiled from a build.rs that uses cxx-qt-build to Qt, so running
//! build.rs fails when Qt is linked dynamically if the Qt libraries are not in PATH (Windows)/LD_LIBRARY_PATH (Unix).

use std::{fs::File, io::Write, path::Path};

/// Write the cxx-qt-lib headers to the specified directory.
pub fn write_headers(directory: impl AsRef<Path>) {
    let directory = directory.as_ref();
    std::fs::create_dir_all(directory).expect("Could not create cxx-qt-lib header directory");
    for (file_contents, file_name) in [
        (include_str!("../include/core/qbytearray.h"), "qbytearray.h"),
        (
            include_str!("../include/core/qcoreapplication.h"),
            "qcoreapplication.h",
        ),
        (include_str!("../include/core/qdate.h"), "qdate.h"),
        (include_str!("../include/core/qdatetime.h"), "qdatetime.h"),
        (include_str!("../include/core/qhash.h"), "qhash.h"),
        (include_str!("../include/core/qlist.h"), "qlist.h"),
        (
            include_str!("../include/core/qlist_qvector.h"),
            "qlist_qvector.h",
        ),
        (include_str!("../include/core/qmap.h"), "qmap.h"),
        (include_str!("../include/core/qmargins.h"), "qmargins.h"),
        (include_str!("../include/core/qmarginsf.h"), "qmarginsf.h"),
        (
            include_str!("../include/core/qmetaobjectconnection.h"),
            "qmetaobjectconnection.h",
        ),
        (
            include_str!("../include/core/qmodelindex.h"),
            "qmodelindex.h",
        ),
        (
            include_str!("../include/core/qpersistentmodelindex.h"),
            "qpersistentmodelindex.h",
        ),
        (include_str!("../include/core/qpoint.h"), "qpoint.h"),
        (include_str!("../include/core/qpointf.h"), "qpointf.h"),
        (include_str!("../include/core/qrect.h"), "qrect.h"),
        (include_str!("../include/core/qrectf.h"), "qrectf.h"),
        (include_str!("../include/core/qset.h"), "qset.h"),
        (include_str!("../include/core/qsize.h"), "qsize.h"),
        (include_str!("../include/core/qsizef.h"), "qsizef.h"),
        (include_str!("../include/core/qstring.h"), "qstring.h"),
        (
            include_str!("../include/core/qstringlist.h"),
            "qstringlist.h",
        ),
        (include_str!("../include/core/qt.h"), "qt.h"),
        (include_str!("../include/core/qtime.h"), "qtime.h"),
        (include_str!("../include/core/qtimezone.h"), "qtimezone.h"),
        (include_str!("../include/core/qurl.h"), "qurl.h"),
        (include_str!("../include/core/qvariant.h"), "qvariant.h"),
        (include_str!("../include/core/qvector.h"), "qvector.h"),
        #[cfg(feature = "qt_gui")]
        (include_str!("../include/gui/qcolor.h"), "qcolor.h"),
        #[cfg(feature = "qt_gui")]
        (
            include_str!("../include/gui/qguiapplication.h"),
            "qguiapplication.h",
        ),
        #[cfg(feature = "qt_gui")]
        (include_str!("../include/gui/qimage.h"), "qimage.h"),
        #[cfg(feature = "qt_gui")]
        (include_str!("../include/gui/qvector2d.h"), "qvector2d.h"),
        #[cfg(feature = "qt_gui")]
        (include_str!("../include/gui/qvector3d.h"), "qvector3d.h"),
        #[cfg(feature = "qt_gui")]
        (include_str!("../include/gui/qvector4d.h"), "qvector4d.h"),
        #[cfg(feature = "qt_qml")]
        (
            include_str!("../include/qml/qqmlapplicationengine.h"),
            "qqmlapplicationengine.h",
        ),
        #[cfg(feature = "qt_qml")]
        (include_str!("../include/qml/qqmlengine.h"), "qqmlengine.h"),
        (include_str!("../include/common.h"), "common.h"),
    ] {
        // Note that we do not need rerun-if-changed for these files
        // as include_str causes a rerun when the header changes
        // and the files are always written to the target.
        let h_path = format!("{}/{file_name}", directory.display());
        let mut header = File::create(h_path).expect("Could not create cxx-qt-lib header");
        write!(header, "{file_contents}").expect("Could not write cxx-qt-lib header");
    }
}