1use std::ffi::OsString;
5use std::path::{Path, PathBuf};
6
7use super::{BuildSettings, Edition, Lang, OptLevel, ProfileSettings, Runtime, Warnings};
8use crate::compiler::{CompilerKind, Dependencies, Family, Syntax};
9use crate::{ToolchainError, Version};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum Mode {
14 Compile,
16 Check,
18}
19
20macro_rules! target {
23 () => {
24 "x86_64-pc-windows-msvc"
25 };
26}
27
28pub const TARGET_SWITCH: &str = concat!("--target=", target!());
30
31#[derive(Clone, Copy, Debug)]
33pub struct CompileInput<'a> {
34 pub kind: CompilerKind,
36 pub lang: Lang,
39 pub edition: Edition,
42 pub build: &'a BuildSettings,
44 pub profile: &'a ProfileSettings,
46 pub include_dirs: &'a [PathBuf],
49 pub test: bool,
51 pub mode: Mode,
53}
54
55pub fn compile_flags(input: &CompileInput<'_>) -> Vec<OsString> {
58 let mut out = Flags::default();
59 let msvc_style = match input.kind.syntax() {
60 Syntax::Cl => true,
61 Syntax::Gnu => false,
62 };
63 let cxx = input.lang == Lang::Cxx;
64 let build = input.build;
65 let profile = input.profile;
66
67 if msvc_style {
69 out.push(&["/nologo", "/utf-8"]);
70 }
71 match input.kind.family() {
72 Family::Llvm => out.push(&[TARGET_SWITCH]),
73 Family::Msvc => {}
75 Family::Gnu => out.push(&["-pipe"]),
79 }
80 if msvc_style && cxx {
81 out.push(&["/Zc:__cplusplus"]);
82 }
83
84 out.push(&[standard(input.kind, input.edition)]);
85
86 out.push(match (build.warnings, input.kind) {
87 (Warnings::Default, _) => &[][..],
88 (Warnings::All, CompilerKind::Clang | CompilerKind::Gcc) => &["-Wall", "-Wextra"],
89 (Warnings::All, CompilerKind::Msvc | CompilerKind::ClangCl) => &["/W4"],
90 (Warnings::Pedantic, CompilerKind::Msvc) => &["/W4", "/permissive-"],
91 (Warnings::Pedantic, CompilerKind::ClangCl) => &["/W4", "-Wpedantic"],
92 (Warnings::Pedantic, CompilerKind::Clang | CompilerKind::Gcc) => {
93 &["-Wall", "-Wextra", "-Wpedantic"]
94 }
95 });
96 if build.warnings_as_errors {
97 out.push(&[if msvc_style { "/WX" } else { "-Werror" }]);
98 }
99
100 out.push(match (build.runtime, input.kind) {
102 (Runtime::Dynamic, CompilerKind::Msvc | CompilerKind::ClangCl) => &["/MD"][..],
103 (Runtime::Static, CompilerKind::Msvc | CompilerKind::ClangCl) => &["/MT"],
104 (Runtime::Dynamic, CompilerKind::Clang) => &["-fms-runtime-lib=dll"],
105 (Runtime::Static, CompilerKind::Clang) => &["-fms-runtime-lib=static"],
106 (Runtime::Dynamic | Runtime::Static, CompilerKind::Gcc) => &[],
107 });
108
109 if cxx {
110 out.push(match (build.exceptions, input.kind) {
114 (true, CompilerKind::Clang | CompilerKind::Gcc) => &[][..],
115 (true, CompilerKind::Msvc | CompilerKind::ClangCl) => &["/EHsc"],
116 (false, CompilerKind::Msvc) => &["/we4530"],
117 (false, CompilerKind::ClangCl) => &[],
118 (false, CompilerKind::Clang | CompilerKind::Gcc) => &["-fno-exceptions"],
119 });
120 if !build.rtti {
121 out.push(match input.kind {
122 CompilerKind::Msvc => &["/GR-", "/we4541"][..],
123 CompilerKind::ClangCl => &["/GR-", "-Werror=rtti"],
124 CompilerKind::Clang | CompilerKind::Gcc => &["-fno-rtti"],
125 });
126 }
127 }
128
129 out.push(&[match (profile.opt_level, msvc_style) {
130 (OptLevel::O0, true) => "/Od",
131 (OptLevel::O1 | OptLevel::O2 | OptLevel::O3, true) => "/O2",
132 (OptLevel::Size, true) => "/O1",
133 (OptLevel::O0, false) => "-O0",
134 (OptLevel::O1, false) => "-O1",
135 (OptLevel::O2, false) => "-O2",
136 (OptLevel::O3, false) => "-O3",
137 (OptLevel::Size, false) => "-Os",
138 }]);
139 if profile.debug_info {
140 out.push(&[if msvc_style { "/Z7" } else { "-g" }]);
141 }
142 if profile.lto {
143 out.push(match input.kind.family() {
144 Family::Msvc => &["/GL"][..],
145 Family::Llvm => &["-flto"],
146 Family::Gnu => &["-flto", "-ffat-lto-objects"],
150 });
151 }
152
153 let define = if msvc_style { "/D" } else { "-D" };
154 let mut defines: Vec<&str> = build.defines.iter().map(String::as_str).collect();
155 if !profile.debug_assertions {
156 defines.push("NDEBUG");
157 }
158 if input.test {
159 defines.push("CUNO_TEST");
160 }
161 if cxx && !build.exceptions && input.kind.uses_msvc_stl() {
164 defines.push("_HAS_EXCEPTIONS=0");
165 }
166 for d in defines {
167 out.0.push(format!("{define}{d}").into());
168 }
169
170 let include = if msvc_style { "/I" } else { "-I" };
171 for dir in input.include_dirs.iter().chain(&build.include_dirs) {
172 let mut arg = OsString::from(include);
173 arg.push(dir);
174 out.0.push(arg);
175 }
176
177 out.0.extend(build.flags.iter().map(OsString::from));
178 out.0
179}
180
181pub fn compile_args(
185 input: &CompileInput<'_>,
186 source: &Path,
187 object: Option<&Path>,
188) -> Vec<OsString> {
189 let msvc_style = match input.kind.syntax() {
190 Syntax::Cl => true,
191 Syntax::Gnu => false,
192 };
193 let mut args: Vec<OsString> = vec![
194 match (input.mode, msvc_style) {
195 (Mode::Compile, true) => "/c",
196 (Mode::Check, true) => "/Zs",
197 (Mode::Compile, false) => "-c",
198 (Mode::Check, false) => "-fsyntax-only",
199 }
200 .into(),
201 ];
202 args.extend(compile_flags(input));
203 match (input.lang, msvc_style) {
206 (Lang::C, true) => args.push("/TC".into()),
207 (Lang::Cxx, true) => args.push("/TP".into()),
208 (Lang::C, false) => args.extend(["-x".into(), "c".into()]),
209 (Lang::Cxx, false) => args.extend(["-x".into(), "c++".into()]),
210 }
211 args.push(source.into());
212 if let (Mode::Compile, Some(object)) = (input.mode, object) {
213 if msvc_style {
214 let mut arg = OsString::from("/Fo");
215 arg.push(object);
216 args.push(arg);
217 } else {
218 args.extend(["-o".into(), object.into()]);
219 }
220 }
221 args
222}
223
224pub fn color_args(kind: CompilerKind, color: bool) -> Vec<OsString> {
232 match (color, kind.family()) {
233 (true, Family::Llvm) => ["-fcolor-diagnostics", "-fansi-escape-codes"]
234 .map(OsString::from)
235 .to_vec(),
236 (true, Family::Gnu) => vec!["-fdiagnostics-color=always".into()],
237 (false, _) | (true, Family::Msvc) => Vec::new(),
238 }
239}
240
241pub fn dependency_args(kind: CompilerKind, file: &Path) -> Vec<OsString> {
246 match (kind.dependencies(), kind.syntax()) {
247 (Dependencies::SourceDependencies, _) => vec!["/sourceDependencies".into(), file.into()],
248 (Dependencies::DepFile, Syntax::Cl) => {
249 let mut to = OsString::from("/clang:-MF");
250 to.push(file);
251 vec!["/clang:-MD".into(), to]
252 }
253 (Dependencies::DepFile, Syntax::Gnu) => vec!["-MD".into(), "-MF".into(), file.into()],
254 }
255}
256
257pub(super) fn standard(kind: CompilerKind, edition: Edition) -> &'static str {
262 match (kind, edition) {
263 (CompilerKind::Clang | CompilerKind::Gcc, Edition::Cxx17) => "-std=c++17",
264 (CompilerKind::Clang | CompilerKind::Gcc, Edition::Cxx20) => "-std=c++20",
265 (CompilerKind::Clang | CompilerKind::Gcc, Edition::Cxx23) => "-std=c++23",
266 (CompilerKind::Clang | CompilerKind::Gcc, Edition::C11) => "-std=c11",
267 (CompilerKind::Clang | CompilerKind::Gcc, Edition::C17) => "-std=c17",
268 (CompilerKind::Clang | CompilerKind::Gcc, Edition::C23) => "-std=c2x",
269 (CompilerKind::ClangCl, Edition::Cxx23) => "/clang:-std=c++23",
270 (CompilerKind::ClangCl, Edition::C23) => "/clang:-std=c2x",
271 (CompilerKind::Msvc, Edition::Cxx23) => "/std:c++23",
272 (CompilerKind::Msvc, Edition::C23) => "/std:clatest",
273 (CompilerKind::Msvc | CompilerKind::ClangCl, Edition::Cxx17) => "/std:c++17",
274 (CompilerKind::Msvc | CompilerKind::ClangCl, Edition::Cxx20) => "/std:c++20",
275 (CompilerKind::Msvc | CompilerKind::ClangCl, Edition::C11) => "/std:c11",
276 (CompilerKind::Msvc | CompilerKind::ClangCl, Edition::C17) => "/std:c17",
277 }
278}
279
280pub fn check_edition(
288 kind: CompilerKind,
289 version: Version,
290 edition: Edition,
291) -> Result<(), ToolchainError> {
292 let needed = match kind {
293 CompilerKind::Msvc => match edition {
295 Edition::Cxx23 => Version {
296 major: 19,
297 minor: 52,
298 patch: 0,
299 },
300 Edition::C23 => Version {
301 major: 19,
302 minor: 37,
303 patch: 0,
304 },
305 _ => return Ok(()),
306 },
307 CompilerKind::Gcc => match edition {
308 Edition::Cxx23 => Version {
309 major: 11,
310 minor: 0,
311 patch: 0,
312 },
313 _ => return Ok(()),
314 },
315 CompilerKind::Clang | CompilerKind::ClangCl => return Ok(()),
317 };
318
319 if version >= needed {
320 return Ok(());
321 }
322 Err(ToolchainError::EditionNotSupported {
323 compiler: kind.name(),
324 version,
325 edition: edition.as_str(),
326 needed,
327 })
328}
329
330#[derive(Default)]
331struct Flags(Vec<OsString>);
332
333impl Flags {
334 fn push(&mut self, flags: &[&str]) {
335 self.0.extend(flags.iter().map(OsString::from));
336 }
337}
338
339#[cfg(test)]
340mod tests {
341 use super::*;
342
343 #[test]
349 fn color_args_follow_the_family() {
350 let flags = ["-fcolor-diagnostics", "-fansi-escape-codes"];
351 for kind in [CompilerKind::Clang, CompilerKind::ClangCl] {
352 assert_eq!(strings(color_args(kind, true)), flags, "{kind:?}");
353 assert!(color_args(kind, false).is_empty(), "{kind:?}");
354 }
355 assert_eq!(
356 strings(color_args(CompilerKind::Gcc, true)),
357 ["-fdiagnostics-color=always"]
358 );
359 assert!(color_args(CompilerKind::Gcc, false).is_empty());
360 assert!(color_args(CompilerKind::Msvc, true).is_empty());
361 assert!(color_args(CompilerKind::Msvc, false).is_empty());
362 }
363
364 const DRIVERS: [CompilerKind; 4] = [
365 CompilerKind::Msvc,
366 CompilerKind::ClangCl,
367 CompilerKind::Clang,
368 CompilerKind::Gcc,
369 ];
370
371 fn build() -> BuildSettings {
372 BuildSettings {
373 warnings: Warnings::Default,
374 warnings_as_errors: false,
375 defines: vec![],
376 include_dirs: vec![],
377 links: vec![],
378 lib_dirs: vec![],
379 link_flags: vec![],
380 flags: vec![],
381 runtime: Runtime::Dynamic,
382 exceptions: true,
383 rtti: true,
384 }
385 }
386
387 fn profile() -> ProfileSettings {
388 ProfileSettings {
389 opt_level: OptLevel::O0,
390 debug_info: false,
391 debug_assertions: true,
392 lto: false,
393 }
394 }
395
396 struct Case {
398 lang: Lang,
399 edition: Edition,
400 build: BuildSettings,
401 profile: ProfileSettings,
402 include_dirs: Vec<PathBuf>,
403 test: bool,
404 }
405
406 impl Case {
407 fn new() -> Case {
408 Case {
409 lang: Lang::Cxx,
410 edition: Edition::Cxx20,
411 build: build(),
412 profile: profile(),
413 include_dirs: vec![],
414 test: false,
415 }
416 }
417
418 fn flags(&self, kind: CompilerKind) -> Vec<String> {
419 let input = CompileInput {
420 kind,
421 lang: self.lang,
422 edition: self.edition,
423 build: &self.build,
424 profile: &self.profile,
425 include_dirs: &self.include_dirs,
426 test: self.test,
427 mode: Mode::Compile,
428 };
429 strings(compile_flags(&input))
430 }
431 }
432
433 fn strings(args: Vec<OsString>) -> Vec<String> {
434 args.into_iter().map(|a| a.into_string().unwrap()).collect()
435 }
436
437 #[test]
440 fn the_defaults_give_the_always_on_flags() {
441 let case = Case::new();
442 let expected: [&[&str]; 4] = [
443 &[
444 "/nologo",
445 "/utf-8",
446 "/Zc:__cplusplus",
447 "/std:c++20",
448 "/MD",
449 "/EHsc",
450 "/Od",
451 ],
452 &[
453 "/nologo",
454 "/utf-8",
455 "--target=x86_64-pc-windows-msvc",
456 "/Zc:__cplusplus",
457 "/std:c++20",
458 "/MD",
459 "/EHsc",
460 "/Od",
461 ],
462 &[
463 "--target=x86_64-pc-windows-msvc",
464 "-std=c++20",
465 "-fms-runtime-lib=dll",
466 "-O0",
467 ],
468 &["-pipe", "-std=c++20", "-O0"],
469 ];
470 for (kind, expected) in DRIVERS.into_iter().zip(expected) {
471 assert_eq!(case.flags(kind), expected, "{kind:?}");
472 }
473 }
474
475 fn assert_row(name: &str, change: impl Fn(&mut Case), rows: [(&[&str], &[&str]); 4]) {
478 assert_row_for(Case::new, name, change, rows);
479 }
480
481 fn c_case() -> Case {
483 let mut case = Case::new();
484 case.lang = Lang::C;
485 case.edition = Edition::C17;
486 case
487 }
488
489 fn assert_row_for(
490 start: fn() -> Case,
491 name: &str,
492 change: impl Fn(&mut Case),
493 rows: [(&[&str], &[&str]); 4],
494 ) {
495 let base = start();
496 let mut case = start();
497 change(&mut case);
498 for (kind, (removed, added)) in DRIVERS.into_iter().zip(rows) {
499 let before = base.flags(kind);
500 let after = case.flags(kind);
501 let mut expected: Vec<String> = before
502 .iter()
503 .filter(|f| !removed.contains(&f.as_str()))
504 .cloned()
505 .collect();
506 for flag in added {
507 assert!(
508 after.iter().any(|f| f == flag),
509 "{name} {kind:?}: `{flag}` missing from {after:?}"
510 );
511 if !expected.iter().any(|f| f == flag) {
512 expected.push((*flag).to_owned());
513 }
514 }
515 let mut sorted_after = after.clone();
516 sorted_after.sort();
517 expected.sort();
518 assert_eq!(sorted_after, expected, "{name} {kind:?}");
519 }
520 }
521
522 #[test]
525 fn every_setting_gives_the_tables_flags_on_every_driver() {
526 type Row = (
527 &'static str,
528 fn(&mut Case),
529 [(&'static [&'static str], &'static [&'static str]); 4],
530 );
531 let rows: Vec<Row> = vec![
532 (
533 "c++17",
534 |c| c.edition = Edition::Cxx17,
535 [
536 (&["/std:c++20"], &["/std:c++17"]),
537 (&["/std:c++20"], &["/std:c++17"]),
538 (&["-std=c++20"], &["-std=c++17"]),
539 (&["-std=c++20"], &["-std=c++17"]),
540 ],
541 ),
542 (
543 "c++23",
544 |c| c.edition = Edition::Cxx23,
545 [
546 (&["/std:c++20"], &["/std:c++23"]),
547 (&["/std:c++20"], &["/clang:-std=c++23"]),
548 (&["-std=c++20"], &["-std=c++23"]),
549 (&["-std=c++20"], &["-std=c++23"]),
550 ],
551 ),
552 (
553 "warnings all",
554 |c| c.build.warnings = Warnings::All,
555 [
556 (&[], &["/W4"]),
557 (&[], &["/W4"]),
558 (&[], &["-Wall", "-Wextra"]),
559 (&[], &["-Wall", "-Wextra"]),
560 ],
561 ),
562 (
563 "warnings pedantic",
564 |c| c.build.warnings = Warnings::Pedantic,
565 [
566 (&[], &["/W4", "/permissive-"]),
567 (&[], &["/W4", "-Wpedantic"]),
568 (&[], &["-Wall", "-Wextra", "-Wpedantic"]),
569 (&[], &["-Wall", "-Wextra", "-Wpedantic"]),
570 ],
571 ),
572 (
573 "warnings-as-errors",
574 |c| c.build.warnings_as_errors = true,
575 [
576 (&[], &["/WX"]),
577 (&[], &["/WX"]),
578 (&[], &["-Werror"]),
579 (&[], &["-Werror"]),
580 ],
581 ),
582 (
583 "defines",
584 |c| c.build.defines = vec!["A=1".into()],
585 [
586 (&[], &["/DA=1"]),
587 (&[], &["/DA=1"]),
588 (&[], &["-DA=1"]),
589 (&[], &["-DA=1"]),
590 ],
591 ),
592 (
593 "test",
594 |c| c.test = true,
595 [
596 (&[], &["/DCUNO_TEST"]),
597 (&[], &["/DCUNO_TEST"]),
598 (&[], &["-DCUNO_TEST"]),
599 (&[], &["-DCUNO_TEST"]),
600 ],
601 ),
602 (
603 "include dirs",
604 |c| {
605 c.include_dirs = vec!["src".into()];
606 c.build.include_dirs = vec!["inc".into()];
607 },
608 [
609 (&[], &["/Isrc", "/Iinc"]),
610 (&[], &["/Isrc", "/Iinc"]),
611 (&[], &["-Isrc", "-Iinc"]),
612 (&[], &["-Isrc", "-Iinc"]),
613 ],
614 ),
615 (
616 "runtime static",
617 |c| c.build.runtime = Runtime::Static,
618 [
619 (&["/MD"], &["/MT"]),
620 (&["/MD"], &["/MT"]),
621 (&["-fms-runtime-lib=dll"], &["-fms-runtime-lib=static"]),
622 (&[], &[]),
624 ],
625 ),
626 (
627 "exceptions false",
628 |c| c.build.exceptions = false,
629 [
630 (&["/EHsc"], &["/we4530", "/D_HAS_EXCEPTIONS=0"]),
631 (&["/EHsc"], &["/D_HAS_EXCEPTIONS=0"]),
632 (&[], &["-fno-exceptions", "-D_HAS_EXCEPTIONS=0"]),
633 (&[], &["-fno-exceptions"]),
635 ],
636 ),
637 (
638 "rtti false",
639 |c| c.build.rtti = false,
640 [
641 (&[], &["/GR-", "/we4541"]),
642 (&[], &["/GR-", "-Werror=rtti"]),
643 (&[], &["-fno-rtti"]),
644 (&[], &["-fno-rtti"]),
645 ],
646 ),
647 (
648 "opt-level 1",
649 |c| c.profile.opt_level = OptLevel::O1,
650 [
651 (&["/Od"], &["/O2"]),
652 (&["/Od"], &["/O2"]),
653 (&["-O0"], &["-O1"]),
654 (&["-O0"], &["-O1"]),
655 ],
656 ),
657 (
658 "opt-level 2",
659 |c| c.profile.opt_level = OptLevel::O2,
660 [
661 (&["/Od"], &["/O2"]),
662 (&["/Od"], &["/O2"]),
663 (&["-O0"], &["-O2"]),
664 (&["-O0"], &["-O2"]),
665 ],
666 ),
667 (
668 "opt-level 3",
669 |c| c.profile.opt_level = OptLevel::O3,
670 [
671 (&["/Od"], &["/O2"]),
672 (&["/Od"], &["/O2"]),
673 (&["-O0"], &["-O3"]),
674 (&["-O0"], &["-O3"]),
675 ],
676 ),
677 (
678 "opt-level s",
679 |c| c.profile.opt_level = OptLevel::Size,
680 [
681 (&["/Od"], &["/O1"]),
682 (&["/Od"], &["/O1"]),
683 (&["-O0"], &["-Os"]),
684 (&["-O0"], &["-Os"]),
685 ],
686 ),
687 (
688 "debug-info",
689 |c| c.profile.debug_info = true,
690 [
691 (&[], &["/Z7"]),
692 (&[], &["/Z7"]),
693 (&[], &["-g"]),
694 (&[], &["-g"]),
695 ],
696 ),
697 (
698 "debug-assertions false",
699 |c| c.profile.debug_assertions = false,
700 [
701 (&[], &["/DNDEBUG"]),
702 (&[], &["/DNDEBUG"]),
703 (&[], &["-DNDEBUG"]),
704 (&[], &["-DNDEBUG"]),
705 ],
706 ),
707 (
708 "lto",
709 |c| c.profile.lto = true,
710 [
711 (&[], &["/GL"]),
712 (&[], &["-flto"]),
713 (&[], &["-flto"]),
714 (&[], &["-flto", "-ffat-lto-objects"]),
715 ],
716 ),
717 (
718 "raw flags",
719 |c| c.build.flags = vec!["/raw".into()],
720 [
721 (&[], &["/raw"]),
722 (&[], &["/raw"]),
723 (&[], &["/raw"]),
724 (&[], &["/raw"]),
725 ],
726 ),
727 ];
728 for (name, change, row) in rows {
729 assert_row(name, change, row);
730 }
731 }
732
733 #[test]
736 fn c_sources_get_the_c_standard_and_no_cxx_only_flag() {
737 for (edition, msvc, clang) in [
738 (Edition::C11, "/std:c11", "-std=c11"),
739 (Edition::C17, "/std:c17", "-std=c17"),
740 (Edition::C23, "/std:clatest", "-std=c2x"),
741 ] {
742 let mut case = Case::new();
743 case.lang = Lang::C;
744 case.edition = edition;
745 case.build.exceptions = false;
746 case.build.rtti = false;
747 for kind in DRIVERS {
748 let flags = case.flags(kind);
749 let expected = match (kind, edition) {
750 (CompilerKind::Clang | CompilerKind::Gcc, _) => clang,
751 (CompilerKind::ClangCl, Edition::C23) => "/clang:-std=c2x",
752 _ => msvc,
753 };
754 assert!(flags.iter().any(|f| f == expected), "{kind:?} {flags:?}");
755 for cxx_only in [
756 "/Zc:__cplusplus",
757 "/EHsc",
758 "/GR-",
759 "-fno-rtti",
760 "-fno-exceptions",
761 "/D_HAS_EXCEPTIONS=0",
762 "-D_HAS_EXCEPTIONS=0",
763 "/we4530",
764 "/we4541",
765 "-Werror=rtti",
766 ] {
767 assert!(
768 !flags.iter().any(|f| f == cxx_only),
769 "{kind:?} {edition:?} {flags:?}"
770 );
771 }
772 }
773 }
774 }
775
776 #[test]
780 fn the_order_is_the_documented_one() {
781 let mut case = Case::new();
782 case.build.flags = vec!["/raw".into()];
783 case.build.defines = vec!["USER".into()];
784 case.build.include_dirs = vec!["user".into()];
785 case.include_dirs = vec!["src".into()];
786 case.test = true;
787 case.profile.debug_assertions = false;
788 for kind in DRIVERS {
789 let flags = case.flags(kind);
790 assert_eq!(flags.last().map(String::as_str), Some("/raw"), "{kind:?}");
791 let at = |suffix: &str| flags.iter().position(|f| f.ends_with(suffix)).unwrap();
792 assert!(
793 at("USER") < at("NDEBUG") && at("NDEBUG") < at("CUNO_TEST"),
794 "{flags:?}"
795 );
796 assert!(at("src") < at("user"), "{flags:?}");
797 assert!(at("CUNO_TEST") < at("src"), "{flags:?}");
798 }
799 }
800
801 #[test]
804 fn a_source_is_compiled_or_checked_in_the_packages_language() {
805 let case = Case::new();
806 let source = Path::new("a.h");
807 let object = Path::new("a.obj");
808 for (kind, mode, lang, first, language, output) in [
809 (
810 CompilerKind::Msvc,
811 Mode::Compile,
812 Lang::Cxx,
813 "/c",
814 vec!["/TP"],
815 vec!["/Foa.obj"],
816 ),
817 (
818 CompilerKind::ClangCl,
819 Mode::Compile,
820 Lang::C,
821 "/c",
822 vec!["/TC"],
823 vec!["/Foa.obj"],
824 ),
825 (
826 CompilerKind::Clang,
827 Mode::Compile,
828 Lang::Cxx,
829 "-c",
830 vec!["-x", "c++"],
831 vec!["-o", "a.obj"],
832 ),
833 (
834 CompilerKind::Clang,
835 Mode::Check,
836 Lang::C,
837 "-fsyntax-only",
838 vec!["-x", "c"],
839 vec![],
840 ),
841 (
842 CompilerKind::Msvc,
843 Mode::Check,
844 Lang::C,
845 "/Zs",
846 vec!["/TC"],
847 vec![],
848 ),
849 ] {
850 let input = CompileInput {
851 kind,
852 lang,
853 edition: if lang == Lang::C {
854 Edition::C17
855 } else {
856 Edition::Cxx20
857 },
858 build: &case.build,
859 profile: &case.profile,
860 include_dirs: &[],
861 test: false,
862 mode,
863 };
864 let args = strings(compile_args(&input, source, Some(object)));
865 let flags = strings(compile_flags(&input));
866 let mut expected = vec![first.to_owned()];
867 expected.extend(flags);
868 expected.extend(language.into_iter().map(str::to_owned));
869 expected.push("a.h".to_owned());
870 expected.extend(output.into_iter().map(str::to_owned));
871 assert_eq!(args, expected, "{kind:?} {mode:?}");
872 }
873 }
874
875 #[test]
879 fn a_c_package_gets_every_shared_setting() {
880 type Row = (
881 &'static str,
882 fn(&mut Case),
883 [(&'static [&'static str], &'static [&'static str]); 4],
884 );
885 let same = |a: &'static [&'static str], c: &'static [&'static str]| {
886 [(&[][..], a), (&[][..], a), (&[][..], c), (&[][..], c)]
887 };
888 let rows: Vec<Row> = vec![
889 (
890 "warnings all",
891 |c| c.build.warnings = Warnings::All,
892 same(&["/W4"], &["-Wall", "-Wextra"]),
893 ),
894 (
895 "warnings-as-errors",
896 |c| c.build.warnings_as_errors = true,
897 same(&["/WX"], &["-Werror"]),
898 ),
899 (
900 "defines",
901 |c| c.build.defines = vec!["A".into()],
902 same(&["/DA"], &["-DA"]),
903 ),
904 (
905 "test",
906 |c| c.test = true,
907 same(&["/DCUNO_TEST"], &["-DCUNO_TEST"]),
908 ),
909 (
910 "include dirs",
911 |c| c.build.include_dirs = vec!["inc".into()],
912 same(&["/Iinc"], &["-Iinc"]),
913 ),
914 (
915 "debug-info",
916 |c| c.profile.debug_info = true,
917 same(&["/Z7"], &["-g"]),
918 ),
919 (
920 "debug-assertions false",
921 |c| c.profile.debug_assertions = false,
922 same(&["/DNDEBUG"], &["-DNDEBUG"]),
923 ),
924 (
925 "raw flags",
926 |c| c.build.flags = vec!["/raw".into()],
927 same(&["/raw"], &["/raw"]),
928 ),
929 (
930 "runtime static",
931 |c| c.build.runtime = Runtime::Static,
932 [
933 (&["/MD"], &["/MT"]),
934 (&["/MD"], &["/MT"]),
935 (&["-fms-runtime-lib=dll"], &["-fms-runtime-lib=static"]),
936 (&[], &[]),
937 ],
938 ),
939 (
940 "opt-level s",
941 |c| c.profile.opt_level = OptLevel::Size,
942 [
943 (&["/Od"], &["/O1"]),
944 (&["/Od"], &["/O1"]),
945 (&["-O0"], &["-Os"]),
946 (&["-O0"], &["-Os"]),
947 ],
948 ),
949 (
950 "lto",
951 |c| c.profile.lto = true,
952 [
953 (&[], &["/GL"]),
954 (&[], &["-flto"]),
955 (&[], &["-flto"]),
956 (&[], &["-flto", "-ffat-lto-objects"]),
957 ],
958 ),
959 (
960 "exceptions false",
961 |c| c.build.exceptions = false,
962 [(&[], &[]), (&[], &[]), (&[], &[]), (&[], &[])],
963 ),
964 (
965 "rtti false",
966 |c| c.build.rtti = false,
967 [(&[], &[]), (&[], &[]), (&[], &[]), (&[], &[])],
968 ),
969 ];
970 for (name, change, row) in rows {
971 assert_row_for(c_case, name, change, row);
972 }
973 }
974
975 #[test]
977 fn compiling_without_an_object_names_none() {
978 let case = Case::new();
979 for kind in DRIVERS {
980 let input = CompileInput {
981 kind,
982 lang: Lang::Cxx,
983 edition: Edition::Cxx20,
984 build: &case.build,
985 profile: &case.profile,
986 include_dirs: &[],
987 test: false,
988 mode: Mode::Compile,
989 };
990 let args = strings(compile_args(&input, Path::new("a.cpp"), None));
991 assert_eq!(args.last().map(String::as_str), Some("a.cpp"), "{kind:?}");
992 }
993 }
994
995 #[test]
999 fn an_edition_cl_does_not_know_is_an_error() {
1000 let v = |minor| Version {
1001 major: 19,
1002 minor,
1003 patch: 0,
1004 };
1005 let old = Version {
1006 major: 17,
1007 minor: 0,
1008 patch: 0,
1009 };
1010 assert!(check_edition(CompilerKind::Msvc, v(52), Edition::Cxx23).is_ok());
1011 assert!(check_edition(CompilerKind::Msvc, v(37), Edition::C23).is_ok());
1012 let err = check_edition(CompilerKind::Msvc, v(51), Edition::Cxx23).unwrap_err();
1013 assert_eq!(
1014 err.to_string(),
1015 "msvc 19.51.0 does not support edition = \"c++23\""
1016 );
1017 assert_eq!(
1018 cuno_core::CunoError::help(&err).as_deref(),
1019 Some("use msvc 19.52.0 or newer, or choose another edition or compiler")
1020 );
1021 assert!(check_edition(CompilerKind::Msvc, v(36), Edition::C23).is_err());
1022 assert!(check_edition(CompilerKind::Msvc, v(29), Edition::Cxx20).is_ok());
1023 for kind in [CompilerKind::Clang, CompilerKind::ClangCl] {
1024 for edition in [Edition::Cxx23, Edition::C23] {
1025 assert!(
1026 check_edition(kind, old, edition).is_ok(),
1027 "{kind:?} {edition:?}"
1028 );
1029 }
1030 }
1031 let gcc = |major| Version {
1032 major,
1033 minor: 0,
1034 patch: 0,
1035 };
1036 assert!(check_edition(CompilerKind::Gcc, gcc(11), Edition::Cxx23).is_ok());
1037 let err = check_edition(CompilerKind::Gcc, gcc(10), Edition::Cxx23).unwrap_err();
1038 assert_eq!(
1039 err.to_string(),
1040 "gcc 10.0.0 does not support edition = \"c++23\""
1041 );
1042 assert!(check_edition(CompilerKind::Gcc, gcc(10), Edition::C23).is_ok());
1043 }
1044
1045 #[test]
1052 fn each_driver_reports_its_headers_its_own_way() {
1053 let file = Path::new("C:/p/a.cpp.d");
1054 let args = |kind| strings(dependency_args(kind, file));
1055 assert_eq!(
1056 args(CompilerKind::Msvc),
1057 ["/sourceDependencies", "C:/p/a.cpp.d"]
1058 );
1059 assert_eq!(
1060 args(CompilerKind::ClangCl),
1061 ["/clang:-MD", "/clang:-MFC:/p/a.cpp.d"]
1062 );
1063 assert_eq!(args(CompilerKind::Clang), ["-MD", "-MF", "C:/p/a.cpp.d"]);
1064 assert_eq!(args(CompilerKind::Gcc), ["-MD", "-MF", "C:/p/a.cpp.d"]);
1065 }
1066}