cuno-toolchain 0.1.0

Compiler abstraction, detection, per-compiler implementations, and flag translation.
Documentation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! What can go wrong while finding and preparing a toolchain. Every case is a
//! setup problem (exit code 3): the user's environment, not their code.

use std::fmt;
use std::path::PathBuf;

use cuno_core::{CunoError, ExitClass};

use crate::Version;

const BUILD_TOOLS_HELP: &str = "install Build Tools for Visual Studio with the \"Desktop development with C++\" workload: https://visualstudio.microsoft.com/visual-cpp-build-tools/";

/// A toolchain problem.
#[derive(Debug)]
pub enum ToolchainError {
    /// vswhere.exe is not where every Visual Studio 2017+ installer puts it.
    VswhereMissing {
        /// Where it was looked for.
        path: PathBuf,
    },
    /// vswhere.exe ran but failed, or printed something that is not its JSON.
    VswhereFailed {
        /// What went wrong, with vswhere's own output when it gave any.
        detail: String,
    },
    /// No Visual Studio or Build Tools installation has the C++ x64 tools.
    NoVisualStudio,
    /// cl.exe could not be run, or printed no version.
    ClVersionUnknown {
        /// The cl.exe that was run.
        path: PathBuf,
    },
    /// No Windows SDK with the x64 headers and libraries.
    NoWindowsSdk {
        /// The SDK directories looked in, in order.
        looked_in: Vec<PathBuf>,
    },
    /// A compiler name cuno does not know.
    UnknownCompiler {
        /// The name.
        name: String,
        /// Where it was given (`CUNO_COMPILER`, ...).
        source: String,
    },
    /// A compiler cuno does not support yet (`gcc`, `safec`).
    UnsupportedCompiler {
        /// The name.
        name: String,
        /// Where it was chosen.
        source: String,
    },
    /// No copy of the chosen compiler was found.
    CompilerNotFound {
        /// The compiler (`clang`).
        compiler: &'static str,
    },
    /// Copies were found, but none is eligible (a preview, a MinGW build, one
    /// that does not run).
    NoUsableCompiler {
        /// The compiler.
        compiler: &'static str,
        /// Each copy found, with why it was left out.
        excluded: Vec<String>,
    },
    /// Copies were found, but none satisfies the requirements.
    NoMatchingVersion {
        /// The compiler.
        compiler: &'static str,
        /// Each requirement, with where it comes from.
        requirements: Vec<String>,
        /// The versions found.
        found: Vec<Version>,
    },
    /// A `[toolchain.version]` value that is not a requirement.
    InvalidVersionRequirement {
        /// The compiler it is for.
        compiler: String,
        /// The value as written.
        text: String,
    },
    /// `[toolchain.paths] msvc` names a directory with no usable Visual Studio.
    VisualStudioNotAt {
        /// The directory.
        path: PathBuf,
    },
    /// The MSVC standard library rejects this Clang (STL1000).
    RejectedByStl {
        /// The compiler (`clang`, `clang-cl`).
        compiler: &'static str,
        /// Its version.
        version: Version,
        /// The Clang version the library asks for, as written, if it says.
        needed: Option<String>,
        /// The library's message.
        message: String,
    },
    /// The standard library probe failed for another reason, so no verdict.
    ProbeFailed {
        /// The compiler.
        compiler: &'static str,
        /// What it printed, or why it could not run.
        output: String,
    },
    /// The selected compiler is too old for the package's edition.
    EditionNotSupported {
        /// The compiler.
        compiler: &'static str,
        /// Its version.
        version: Version,
        /// The edition, as written.
        edition: &'static str,
        /// The oldest version that accepts it.
        needed: Version,
    },
    /// A tool path the user configured has no file.
    ConfiguredToolMissing {
        /// The setting, such as `[toolchain.paths] clang-tidy`.
        setting: String,
        /// The path it gives.
        path: PathBuf,
    },
    /// A tool path the user configured names a tool that cannot be used (it
    /// does not run, prints no version, is a preview, or targets MinGW).
    ConfiguredToolUnusable {
        /// The setting, such as `[toolchain.paths] clang-tidy`.
        setting: String,
        /// The path it gives.
        path: PathBuf,
        /// Why it cannot be used, such as `it did not run`.
        why: String,
    },
}

impl fmt::Display for ToolchainError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ToolchainError::VswhereMissing { path } => {
                write!(f, "vswhere.exe was not found at {}", path.display())
            }
            ToolchainError::VswhereFailed { detail } => {
                write!(f, "vswhere.exe failed: {detail}")
            }
            ToolchainError::NoVisualStudio => {
                f.write_str("no Visual Studio or Build Tools with the C++ x64 tools was found")
            }
            ToolchainError::ClVersionUnknown { path } => {
                write!(f, "could not read the version of {}", path.display())
            }
            ToolchainError::NoWindowsSdk { looked_in } => {
                let places: Vec<String> =
                    looked_in.iter().map(|p| p.display().to_string()).collect();
                write!(
                    f,
                    "no Windows SDK with the x64 headers and libraries was found in {}",
                    places.join(" or ")
                )
            }
            ToolchainError::UnknownCompiler { name, source } => {
                write!(
                    f,
                    "{source} names `{name}`, which is not a compiler cuno knows"
                )
            }
            ToolchainError::UnsupportedCompiler { name, source } => {
                write!(f, "{name} is not supported yet (chosen by {source})")
            }
            ToolchainError::CompilerNotFound { compiler } => write!(f, "no {compiler} was found"),
            ToolchainError::NoUsableCompiler { compiler, excluded } => write!(
                f,
                "no usable {compiler} was found; left out: {}",
                excluded.join("; ")
            ),
            ToolchainError::NoMatchingVersion {
                compiler,
                requirements,
                found,
            } => {
                let found: Vec<String> = found.iter().map(Version::to_string).collect();
                write!(
                    f,
                    "no {compiler} found satisfies {}; found {}",
                    requirements.join(" and "),
                    found.join(", ")
                )
            }
            ToolchainError::InvalidVersionRequirement { compiler, text } => write!(
                f,
                "[toolchain.version] {compiler} = \"{text}\" is not a version requirement"
            ),
            ToolchainError::VisualStudioNotAt { path } => write!(
                f,
                "[toolchain.paths] msvc is {}, but no Visual Studio with the C++ x64 tools is installed there",
                path.display()
            ),
            ToolchainError::RejectedByStl {
                compiler, version, ..
            } => write!(
                f,
                "{compiler} {version} is not accepted by the MSVC standard library in use (STL1000)"
            ),
            ToolchainError::ProbeFailed { compiler, output } => write!(
                f,
                "could not tell whether {compiler} works with the MSVC standard library:\n{output}"
            ),
            ToolchainError::EditionNotSupported {
                compiler,
                version,
                edition,
                ..
            } => write!(
                f,
                "{compiler} {version} does not support edition = \"{edition}\""
            ),
            ToolchainError::ConfiguredToolUnusable { setting, path, why } => {
                write!(f, "{setting} is {}, but {why}", path.display())
            }
            ToolchainError::ConfiguredToolMissing { setting, path } => {
                write!(
                    f,
                    "{setting} is {}, but there is no file there",
                    path.display()
                )
            }
        }
    }
}

impl std::error::Error for ToolchainError {}

impl CunoError for ToolchainError {
    fn exit_class(&self) -> ExitClass {
        ExitClass::Setup
    }

    fn help(&self) -> Option<String> {
        match self {
            ToolchainError::UnknownCompiler { .. } | ToolchainError::UnsupportedCompiler { .. } => {
                Some(format!("choose {}", crate::compiler::CompilerKind::names()))
            }
            ToolchainError::CompilerNotFound { compiler } => Some(format!(
                "install LLVM (https://github.com/llvm/llvm-project/releases), or write where {compiler} is in [toolchain.paths] {compiler}"
            )),
            ToolchainError::NoUsableCompiler { compiler, .. } => Some(format!(
                "install a release of LLVM for Windows (https://github.com/llvm/llvm-project/releases), or write where a usable {compiler} is in [toolchain.paths] {compiler}"
            )),
            ToolchainError::NoMatchingVersion { compiler, .. } => Some(format!(
                "install a {compiler} that satisfies it, or change [toolchain.version] {compiler}"
            )),
            ToolchainError::InvalidVersionRequirement { .. } => Some(
                "write conditions such as \">=17\" or \">=19.29, <19.50\"".to_owned(),
            ),
            ToolchainError::VisualStudioNotAt { .. } => Some(
                "correct [toolchain.paths] msvc, or remove it to let cuno find Visual Studio"
                    .to_owned(),
            ),
            ToolchainError::RejectedByStl {
                compiler: "clang-tidy",
                needed: Some(needed),
                ..
            } => Some(format!("install clang-tidy {needed} or newer")),
            ToolchainError::RejectedByStl {
                needed, message, ..
            } => Some(match needed {
                Some(needed) => {
                    format!("install Clang {needed} or newer, or select another compiler")
                }
                None => format!("the standard library says: {message}"),
            }),
            ToolchainError::ProbeFailed { .. } => Some(
                "check `[build.<compiler>] flags` in cuno.toml, and the Visual Studio and LLVM installations".to_owned(),
            ),
            ToolchainError::EditionNotSupported {
                compiler, needed, ..
            } => Some(format!(
                "use {compiler} {needed} or newer, or choose another edition or compiler"
            )),
            ToolchainError::ConfiguredToolMissing { setting, .. }
            | ToolchainError::ConfiguredToolUnusable { setting, .. } => Some(format!(
                "correct {setting}, or remove it to let cuno find the tool"
            )),
            ToolchainError::VswhereMissing { .. } | ToolchainError::NoVisualStudio => {
                Some(BUILD_TOOLS_HELP.to_owned())
            }
            ToolchainError::NoWindowsSdk { .. } => Some(
                "add a Windows SDK in the Visual Studio Installer (a \"Windows 11 SDK\" component of the \"Desktop development with C++\" workload)".to_owned(),
            ),
            ToolchainError::VswhereFailed { .. } | ToolchainError::ClVersionUnknown { .. } => Some(
                "repair the Visual Studio installation with the Visual Studio Installer".to_owned(),
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Mutation: give any variant another exit class; this test fails.
    #[test]
    fn every_toolchain_error_is_a_setup_problem() {
        for e in [
            ToolchainError::VswhereMissing { path: "x".into() },
            ToolchainError::VswhereFailed { detail: "x".into() },
            ToolchainError::NoVisualStudio,
            ToolchainError::ClVersionUnknown { path: "x".into() },
            ToolchainError::ConfiguredToolMissing {
                setting: "x".into(),
                path: "x".into(),
            },
            ToolchainError::NoWindowsSdk {
                looked_in: vec!["x".into()],
            },
            ToolchainError::UnknownCompiler {
                name: "x".into(),
                source: "x".into(),
            },
            ToolchainError::UnsupportedCompiler {
                name: "x".into(),
                source: "x".into(),
            },
            ToolchainError::CompilerNotFound { compiler: "x" },
            ToolchainError::NoUsableCompiler {
                compiler: "x",
                excluded: vec![],
            },
            ToolchainError::NoMatchingVersion {
                compiler: "x",
                requirements: vec![],
                found: vec![],
            },
            ToolchainError::InvalidVersionRequirement {
                compiler: "x".into(),
                text: "x".into(),
            },
            ToolchainError::VisualStudioNotAt { path: "x".into() },
            ToolchainError::RejectedByStl {
                compiler: "x",
                version: Version {
                    major: 0,
                    minor: 0,
                    patch: 0,
                },
                needed: None,
                message: "x".into(),
            },
            ToolchainError::ProbeFailed {
                compiler: "x",
                output: "x".into(),
            },
            ToolchainError::EditionNotSupported {
                compiler: "x",
                version: Version {
                    major: 0,
                    minor: 0,
                    patch: 0,
                },
                edition: "x",
                needed: Version {
                    major: 0,
                    minor: 0,
                    patch: 0,
                },
            },
        ] {
            assert_eq!(e.exit_class(), ExitClass::Setup, "{e}");
            assert!(e.help().is_some(), "{e}");
        }
    }

    /// Mutation: drop what was looked for from any message, or give a
    /// missing Visual Studio the repair advice instead of the Build Tools
    /// workload; this test fails.
    #[test]
    fn messages_name_what_was_looked_for_and_how_to_fix_it() {
        let cases = [
            (
                ToolchainError::VswhereMissing {
                    path: "Z:/x/vswhere.exe".into(),
                },
                "Z:/x/vswhere.exe",
                "Desktop development with C++",
            ),
            (
                ToolchainError::VswhereFailed {
                    detail: "access denied".into(),
                },
                "access denied",
                "Visual Studio Installer",
            ),
            (
                ToolchainError::NoVisualStudio,
                "C++ x64 tools",
                "Desktop development with C++",
            ),
            (
                ToolchainError::ClVersionUnknown {
                    path: "Z:/t/cl.exe".into(),
                },
                "Z:/t/cl.exe",
                "Visual Studio Installer",
            ),
            (
                ToolchainError::ConfiguredToolMissing {
                    setting: "[toolchain.paths] clang".into(),
                    path: "Z:/c/clang.exe".into(),
                },
                "Z:/c/clang.exe",
                "[toolchain.paths] clang",
            ),
            (
                ToolchainError::NoWindowsSdk {
                    looked_in: vec!["Y:/Kits".into(), "Z:/Windows Kits/10".into()],
                },
                "Y:/Kits or Z:/Windows Kits/10",
                "Windows 11 SDK",
            ),
            (
                ToolchainError::UnsupportedCompiler {
                    name: "gcc".into(),
                    source: "CUNO_COMPILER".into(),
                },
                "gcc is not supported yet (chosen by CUNO_COMPILER)",
                "msvc, clang or clang-cl",
            ),
            (
                ToolchainError::NoMatchingVersion {
                    compiler: "msvc",
                    requirements: vec![">=19.29 (cuno's minimum)".into()],
                    found: vec![Version {
                        major: 19,
                        minor: 28,
                        patch: 1,
                    }],
                },
                "satisfies >=19.29 (cuno's minimum); found 19.28.1",
                "[toolchain.version] msvc",
            ),
            (
                ToolchainError::RejectedByStl {
                    compiler: "clang",
                    version: Version {
                        major: 20,
                        minor: 1,
                        patch: 8,
                    },
                    needed: Some("22".into()),
                    message: "STL1000: ...".into(),
                },
                "clang 20.1.8 is not accepted by the MSVC standard library in use (STL1000)",
                "install Clang 22 or newer, or select another compiler",
            ),
            (
                ToolchainError::RejectedByStl {
                    compiler: "clang",
                    version: Version {
                        major: 20,
                        minor: 1,
                        patch: 8,
                    },
                    needed: None,
                    message: "STL1000: odd wording".into(),
                },
                "(STL1000)",
                "STL1000: odd wording",
            ),
            (
                ToolchainError::UnknownCompiler {
                    name: "nope".into(),
                    source: "--compiler".into(),
                },
                "--compiler names `nope`, which is not a compiler cuno knows",
                "msvc, clang or clang-cl",
            ),
            (
                ToolchainError::CompilerNotFound { compiler: "clang" },
                "no clang was found",
                "[toolchain.paths] clang",
            ),
            (
                ToolchainError::NoUsableCompiler {
                    compiler: "clang",
                    excluded: vec!["a (x)".into(), "b (y)".into()],
                },
                "no usable clang was found; left out: a (x); b (y)",
                "release of LLVM",
            ),
            (
                ToolchainError::InvalidVersionRequirement {
                    compiler: "clang".into(),
                    text: "~17".into(),
                },
                "[toolchain.version] clang = \"~17\" is not a version requirement",
                "\">=19.29, <19.50\"",
            ),
            (
                ToolchainError::VisualStudioNotAt {
                    path: "Z:/VS".into(),
                },
                "[toolchain.paths] msvc is Z:/VS, but no Visual Studio",
                "correct [toolchain.paths] msvc",
            ),
            (
                ToolchainError::ProbeFailed {
                    compiler: "clang-cl",
                    output: "fatal error: x".into(),
                },
                "whether clang-cl works with the MSVC standard library:\nfatal error: x",
                "[build.<compiler>] flags",
            ),
        ];
        for (error, in_message, in_help) in cases {
            let message = error.to_string();
            let help = error.help().unwrap();
            assert!(message.contains(in_message), "{message}");
            assert!(help.contains(in_help), "{help}");
        }
    }
}