cxx_qt_build/
opts.rs

1// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use std::{
7    collections::HashSet,
8    path::{Path, PathBuf},
9};
10
11use crate::MocArguments;
12
13/// Options for external crates to use
14#[derive(Default)]
15pub struct CxxQtBuildersOpts {
16    /// Any extra definitions
17    pub(crate) defines: HashSet<String>,
18    /// Contents, directory, file name
19    pub(crate) headers: Vec<(String, String, String)>,
20    /// Qt modules that are required
21    pub(crate) qt_modules: HashSet<String>,
22    /// Added initializer code required to be linked into a separate object file
23    pub(crate) initializers: Vec<String>,
24}
25
26impl CxxQtBuildersOpts {
27    /// Any additional defines that are required from this opt
28    pub fn define(mut self, define: &str) -> Self {
29        self.defines.insert(define.to_owned());
30        self
31    }
32
33    /// Any additional headers that are required from this opt
34    ///
35    /// These are placed in the given sub directory with the given file name
36    pub fn header(mut self, contents: &str, directory: &str, file_name: &str) -> Self {
37        self.headers.push((
38            contents.to_owned(),
39            directory.to_owned(),
40            file_name.to_owned(),
41        ));
42        self
43    }
44
45    /// Link additional [Qt modules](https://doc.qt.io/qt-6/qtmodules.html) for this opt.
46    /// Specify their names without the `Qt` prefix, for example `"Widgets"`.
47    pub fn qt_module(mut self, module: &str) -> Self {
48        self.qt_modules.insert(module.to_owned());
49        self
50    }
51
52    /// Add initializer C++ code that must be compiled into an object file or linked with
53    /// whole-archive so that the linker doesn't optimize it out.
54    pub fn initializer(mut self, initializers: &str) -> Self {
55        self.initializers.push(initializers.to_owned());
56        self
57    }
58}
59
60/// Options for qobject_headers
61///
62/// QObjectHeaderOpts can be created using the `From<impl AsRef<Path>>` trait.
63/// ```
64/// # use cxx_qt_build::{QObjectHeaderOpts, MocArguments};
65/// QObjectHeaderOpts::from("path/to/header.h")
66///     .moc_arguments(MocArguments::default());
67/// ```
68pub struct QObjectHeaderOpts {
69    pub(crate) path: PathBuf,
70    pub(crate) moc_arguments: MocArguments,
71}
72
73impl<T> From<T> for QObjectHeaderOpts
74where
75    T: AsRef<Path>,
76{
77    fn from(path: T) -> Self {
78        Self {
79            path: path.as_ref().to_owned(),
80            moc_arguments: MocArguments::default(),
81        }
82    }
83}
84
85impl QObjectHeaderOpts {
86    /// Set the moc arguments for this header
87    ///
88    /// By default this is `MocArguments::default()`
89    pub fn moc_arguments(self, moc_arguments: MocArguments) -> Self {
90        Self {
91            moc_arguments,
92            ..self
93        }
94    }
95}