Skip to main content

cuno_toolchain/flags/
settings.rs

1//! The settings a compile and a link are made from, resolved from
2//! `Cuno.toml` by `cuno-manifest` (`docs/en/specs/manifest.md`). They live
3//! here because turning them into flags is this crate's job
4//! (`docs/en/specs/architecture.md`).
5
6use std::path::PathBuf;
7
8macro_rules! choices {
9    ($(#[$doc:meta])* $name:ident { $($(#[$vdoc:meta])* $variant:ident = $text:literal),+ $(,)? }) => {
10        $(#[$doc])*
11        #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
12        pub enum $name { $($(#[$vdoc])* $variant),+ }
13
14        impl $name {
15            /// Every value, as written in `Cuno.toml`.
16            pub const ALL: &[($name, &str)] = &[$(($name::$variant, $text)),+];
17
18            /// The value as written in `Cuno.toml`.
19            pub fn as_str(self) -> &'static str {
20                match self { $($name::$variant => $text),+ }
21            }
22
23            /// The value `text` names in `Cuno.toml`.
24            pub fn parse(text: &str) -> Option<$name> {
25                Self::ALL.iter().find(|(_, t)| *t == text).map(|(v, _)| *v)
26            }
27        }
28    };
29}
30
31choices!(
32    /// `[package] lang`.
33    Lang { /// C.
34    C = "c", /// C++.
35    Cxx = "c++" }
36);
37choices!(
38    /// `[package] edition`.
39    Edition { /// C++17.
40    Cxx17 = "c++17", /// C++20.
41    Cxx20 = "c++20", /// C++23.
42    Cxx23 = "c++23", /// C11.
43    C11 = "c11", /// C17.
44    C17 = "c17", /// C23.
45    C23 = "c23" }
46);
47choices!(
48    /// `warnings`.
49    Warnings { /// The compiler's own.
50    Default = "default", /// The recommended set.
51    All = "all", /// Strict conformance too.
52    Pedantic = "pedantic" }
53);
54choices!(
55    /// `runtime`.
56    Runtime { /// The C runtime as a DLL.
57    Dynamic = "dynamic", /// The C runtime in the executable.
58    Static = "static" }
59);
60choices!(
61    /// `[[bin]] subsystem`.
62    Subsystem { /// A console program.
63    Console = "console", /// A program without a console window.
64    Windows = "windows" }
65);
66/// `opt-level`: `0` to `3`, or `"s"` for size.
67#[derive(Clone, Copy, Debug, PartialEq, Eq)]
68pub enum OptLevel {
69    /// `0`.
70    O0,
71    /// `1`.
72    O1,
73    /// `2`.
74    O2,
75    /// `3`.
76    O3,
77    /// `"s"`.
78    Size,
79}
80
81impl Lang {
82    /// The entry file a package of this language has when `Cuno.toml` names
83    /// none, relative to the package root as `Cuno.toml` writes paths
84    /// (`docs/en/specs/project-layout.md`).
85    pub fn default_entry(self) -> &'static str {
86        match self {
87            Lang::C => "src/main.c",
88            Lang::Cxx => "src/main.cpp",
89        }
90    }
91
92    /// The edition a package of this language gets by default.
93    pub fn default_edition(self) -> Edition {
94        match self {
95            Lang::C => Edition::C17,
96            Lang::Cxx => Edition::Cxx20,
97        }
98    }
99}
100
101impl Edition {
102    /// The language this edition is a standard of.
103    pub fn lang(self) -> Lang {
104        match self {
105            Edition::C11 | Edition::C17 | Edition::C23 => Lang::C,
106            _ => Lang::Cxx,
107        }
108    }
109}
110
111/// The build settings for one build, with every layer applied and the
112/// defaults of `manifest.md` filled in.
113#[derive(Clone, Debug, PartialEq, Eq)]
114pub struct BuildSettings {
115    /// `warnings`.
116    pub warnings: Warnings,
117    /// `warnings-as-errors`.
118    pub warnings_as_errors: bool,
119    /// `defines`.
120    pub defines: Vec<String>,
121    /// `include-dirs`.
122    pub include_dirs: Vec<PathBuf>,
123    /// `links`.
124    pub links: Vec<String>,
125    /// `lib-dirs`.
126    pub lib_dirs: Vec<PathBuf>,
127    /// `link-flags`.
128    pub link_flags: Vec<String>,
129    /// `[build.<compiler>] flags`.
130    pub flags: Vec<String>,
131    /// `runtime`.
132    pub runtime: Runtime,
133    /// `exceptions`.
134    pub exceptions: bool,
135    /// `rtti`.
136    pub rtti: bool,
137}
138
139/// A profile's settings with the defaults of `manifest.md` filled in.
140#[derive(Clone, Copy, Debug, PartialEq, Eq)]
141pub struct ProfileSettings {
142    /// `opt-level`.
143    pub opt_level: OptLevel,
144    /// `debug-info`.
145    pub debug_info: bool,
146    /// `debug-assertions`.
147    pub debug_assertions: bool,
148    /// `lto`.
149    pub lto: bool,
150}