rsconstruct 0.9.167

Rust based fast build system
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
use crate::common::{run_rsconstruct_json, run_rsconstruct_with_env};
use std::fs;
use tempfile::TempDir;

/// Helper: set up a project with the cc processor enabled and a cc.yaml file.
fn setup_cc_project(project_path: &std::path::Path, cc_yaml: &str) {
    fs::create_dir_all(project_path.join("src")).unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.mass_generator.cc]\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();
    fs::write(project_path.join("cc.yaml"), cc_yaml).unwrap();
}

#[test]
fn cc_no_project_discovered() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    // Enable cc processor but don't create cc.yaml
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.mass_generator.cc]\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();

    let result = run_rsconstruct_json(project_path, &["build"]);
    assert!(result.exit_success, "Build should succeed with no cc.yaml");
    assert_eq!(result.total_products, 0, "No products should be discovered");
}

#[test]
fn cc_build_static_library() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
libraries:
  - name: mymath
    lib_type: static
    sources: [src/math.c]
"#,
    );

    fs::write(
        project_path.join("src/math.c"),
        "int add(int a, int b) { return a + b; }\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        project_path
            .join("out/processor.mass_generator.cc/lib/libmymath.a")
            .exists(),
        "Static library should exist"
    );
}

#[test]
fn cc_build_shared_library() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
libraries:
  - name: mymath
    lib_type: shared
    sources: [src/math.c]
"#,
    );

    fs::write(
        project_path.join("src/math.c"),
        "int add(int a, int b) { return a + b; }\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        project_path
            .join("out/processor.mass_generator.cc/lib/libmymath.so")
            .exists(),
        "Shared library should exist"
    );
}

#[test]
fn cc_build_program_with_library() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
libraries:
  - name: mymath
    lib_type: static
    sources: [src/math.c]

programs:
  - name: main
    sources: [src/main.c]
    link: [mymath]
"#,
    );

    fs::write(
        project_path.join("src/math.c"),
        "int add(int a, int b) { return a + b; }\n",
    )
    .unwrap();

    fs::write(
        project_path.join("src/main.c"),
        r#"extern int add(int, int);
int main() { return add(1, 2) - 3; }
"#,
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        project_path
            .join("out/processor.mass_generator.cc/lib/libmymath.a")
            .exists(),
        "Static library should exist"
    );
    assert!(
        project_path
            .join("out/processor.mass_generator.cc/bin/main")
            .exists(),
        "Executable should exist"
    );

    // Run the executable - should return 0 (add(1,2) - 3 == 0)
    let run_output =
        std::process::Command::new(project_path.join("out/processor.mass_generator.cc/bin/main"))
            .output()
            .expect("Failed to run executable");
    assert!(run_output.status.success(), "Executable should return 0");
}

#[test]
fn cc_incremental_skip() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
programs:
  - name: hello
    sources: [src/hello.c]
"#,
    );

    fs::write(
        project_path.join("src/hello.c"),
        "int main() { return 0; }\n",
    )
    .unwrap();

    // First build
    let output1 = run_rsconstruct_with_env(project_path, &["build", "-v"], &[("NO_COLOR", "1")]);
    assert!(output1.status.success());
    let stdout1 = String::from_utf8_lossy(&output1.stdout);
    assert!(
        stdout1.contains("Processing:"),
        "First build should process: {}",
        stdout1
    );

    // Second build - should skip
    let output2 =
        run_rsconstruct_with_env(project_path, &["build", "--verbose"], &[("NO_COLOR", "1")]);
    assert!(output2.status.success());
    let stdout2 = String::from_utf8_lossy(&output2.stdout);
    assert!(
        stdout2.contains("[processor.mass_generator.cc] Skipping (unchanged):"),
        "Second build should skip: {}",
        stdout2
    );
}

#[test]
fn cc_single_invocation_mode() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("src")).unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.mass_generator.cc]\nsingle_invocation = true\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();
    fs::write(
        project_path.join("cc.yaml"),
        r#"
programs:
  - name: hello
    sources: [src/hello.c]
"#,
    )
    .unwrap();
    fs::write(
        project_path.join("src/hello.c"),
        "#include <stdio.h>\nint main() { printf(\"hello\\n\"); return 0; }\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Build with single_invocation failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        project_path
            .join("out/processor.mass_generator.cc/bin/hello")
            .exists(),
        "Executable should exist"
    );
}

#[test]
fn cc_build_both_library_types() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
libraries:
  - name: myutil
    lib_type: both
    sources: [src/util.c]
"#,
    );

    fs::write(
        project_path.join("src/util.c"),
        "int helper() { return 42; }\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        project_path
            .join("out/processor.mass_generator.cc/lib/libmyutil.a")
            .exists(),
        "Static library should exist"
    );
    assert!(
        project_path
            .join("out/processor.mass_generator.cc/lib/libmyutil.so")
            .exists(),
        "Shared library should exist"
    );
}

#[test]
fn cc_clean_removes_output_dir() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
programs:
  - name: hello
    sources: [src/hello.c]
"#,
    );

    fs::write(
        project_path.join("src/hello.c"),
        "int main() { return 0; }\n",
    )
    .unwrap();

    // Build
    let build_output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(build_output.status.success());
    assert!(
        project_path
            .join("out/processor.mass_generator.cc/bin/hello")
            .exists()
    );

    // Clean
    let clean_output =
        run_rsconstruct_with_env(project_path, &["clean", "outputs"], &[("NO_COLOR", "1")]);
    assert!(clean_output.status.success());
    assert!(
        !project_path
            .join("out/processor.mass_generator.cc/bin/hello")
            .exists(),
        "Output should be removed after clean"
    );
}

/// Two sources with the same stem in different directories must compile to
/// distinct object files — a flat stem-derived name would silently drop one
/// translation unit from the link.
#[test]
fn cc_same_stem_sources_do_not_collide() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
programs:
  - name: app
    sources: [src/main.c, src/a/util.c, src/b/util.c]
"#,
    );

    fs::create_dir_all(project_path.join("src/a")).unwrap();
    fs::create_dir_all(project_path.join("src/b")).unwrap();
    fs::write(
        project_path.join("src/main.c"),
        "int from_a(void); int from_b(void);\nint main(void) { return from_a() + from_b(); }\n",
    )
    .unwrap();
    fs::write(
        project_path.join("src/a/util.c"),
        "int from_a(void) { return 1; }\n",
    )
    .unwrap();
    fs::write(
        project_path.join("src/b/util.c"),
        "int from_b(void) { return -1; }\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "Build failed (same-stem object collision?): stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        project_path
            .join("out/processor.mass_generator.cc/bin/app")
            .exists(),
        "Program should link"
    );
}

/// `[processor.mass_generator.cc]` flags are project-wide defaults for every cc.yaml.
///
/// Regression test: the config-level `cflags`/`cxxflags`/`ldflags`/
/// `include_dirs` fields used to be fully dead — declared, documented, part
/// of the config hash, and read by nothing. The source below only compiles
/// when the config-level define reaches the compiler.
#[test]
fn cc_config_flags_are_manifest_defaults() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("src")).unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.mass_generator.cc]\nsrc_dirs = [\".\"]\ncflags = [\"-DNEED_FLAG=0\"]\n",
    )
    .unwrap();
    fs::write(
        project_path.join("cc.yaml"),
        r#"
programs:
  - name: app
    sources: [src/main.c]
"#,
    )
    .unwrap();
    fs::write(
        project_path.join("src/main.c"),
        "int main(void) { return NEED_FLAG; }\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "config-level cflags must reach the compiler: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

/// A cc.yaml that sets a field explicitly overrides the config default —
/// including setting it to empty.
#[test]
fn cc_manifest_overrides_config_flags() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    fs::create_dir_all(project_path.join("src")).unwrap();
    // The config default is an invalid flag: the build can only succeed if
    // the manifest's explicit empty cflags wins over the config value.
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.mass_generator.cc]\nsrc_dirs = [\".\"]\ncflags = [\"--definitely-not-a-flag\"]\n",
    )
    .unwrap();
    fs::write(
        project_path.join("cc.yaml"),
        r#"
cflags: []
programs:
  - name: app
    sources: [src/main.c]
"#,
    )
    .unwrap();
    fs::write(
        project_path.join("src/main.c"),
        "int main(void) { return 0; }\n",
    )
    .unwrap();

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "manifest cflags must override the config default: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

/// A compiler overridden per cc.yaml must be part of the processor's
/// declared tools.
///
/// Regression test: `required_tools()` used to return only the config-level
/// compilers, so a manifest override tripped the debug-build declared-tools
/// assertion ("executed undeclared tool") and made `tool check` verify the
/// wrong binary. Test binaries run the debug build, so this test fails by
/// panic if the override is not declared.
#[test]
fn cc_manifest_compiler_is_declared() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    // A distinctly-named compiler wrapper on PATH.
    let bin_dir = project_path.join("toolbin");
    fs::create_dir_all(&bin_dir).unwrap();
    let wrapper = bin_dir.join("mycustomcc");
    fs::write(&wrapper, "#!/bin/sh\nexec gcc \"$@\"\n").unwrap();
    crate::common::make_executable(&wrapper);

    fs::create_dir_all(project_path.join("src")).unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.mass_generator.cc]\nsrc_dirs = [\".\"]\n",
    )
    .unwrap();
    fs::write(
        project_path.join("cc.yaml"),
        r#"
cc: mycustomcc
programs:
  - name: app
    sources: [src/main.c]
"#,
    )
    .unwrap();
    fs::write(
        project_path.join("src/main.c"),
        "int main(void) { return 0; }\n",
    )
    .unwrap();

    let path_env = format!(
        "{}:{}",
        bin_dir.display(),
        std::env::var("PATH").unwrap_or_default()
    );
    let output = run_rsconstruct_with_env(
        project_path,
        &["build"],
        &[("NO_COLOR", "1"), ("PATH", &path_env)],
    );
    assert!(
        output.status.success(),
        "manifest-overridden compiler must be declared (debug builds assert on undeclared tools): stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        project_path
            .join("out/processor.mass_generator.cc/bin/app")
            .exists(),
        "Program should link"
    );
}

/// Write `content` to `rel` under the project, creating parent dirs.
fn write_project_file(project_path: &std::path::Path, rel: &str, content: &str) {
    let path = project_path.join(rel);
    fs::create_dir_all(path.parent().unwrap()).unwrap();
    fs::write(path, content).unwrap();
}

/// Run `rsconstruct build` with JSON output, asserting it succeeded.
fn build_json(project_path: &std::path::Path) -> crate::common::BuildResult {
    let result = run_rsconstruct_json(project_path, &["build"]);
    assert!(
        result.exit_success,
        "build failed: {:?} {:?}",
        result.errors, result.products
    );
    result
}

/// A cc.yaml product lists the manifest first and its sources after it,
/// so the C/C++ analyzers used to look at `inputs[0]`, see a `.yaml`, and
/// match nothing: editing a header included by a listed source rebuilt
/// nothing. With `[analyzer.cpp]` every listed source is scanned, a header
/// one of them includes rebuilds the targets built from it, and a header
/// nothing includes does not.
#[test]
fn cc_header_edit_rebuilds_with_cpp_analyzer() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    write_project_file(
        project_path,
        "rsconstruct.toml",
        "[processor.mass_generator.cc]\nsrc_dirs = [\".\"]\n\n[analyzer.cpp]\ninclude_paths = [\"include\"]\n",
    );
    write_project_file(
        project_path,
        "cc.yaml",
        r#"
include_dirs: [include]

libraries:
  - name: mymath
    lib_type: static
    sources: [src/math.c]

programs:
  - name: main
    sources: [src/main.c]
    link: [mymath]
"#,
    );
    write_project_file(project_path, "include/mymath.h", "int add(int a, int b);\n");
    write_project_file(project_path, "include/unrelated.h", "#define UNUSED 1\n");
    write_project_file(
        project_path,
        "src/math.c",
        "#include \"mymath.h\"\nint add(int a, int b) { return a + b; }\n",
    );
    write_project_file(
        project_path,
        "src/main.c",
        "#include \"mymath.h\"\nint main(void) { return add(1, 2) - 3; }\n",
    );

    let first = build_json(project_path);
    assert!(first.total_products > 0, "the manifest must plan products");
    assert_eq!(
        first.success, first.total_products,
        "first build builds everything: {:?}",
        first.products
    );

    let second = build_json(project_path);
    assert_eq!(
        second.skipped, second.total_products,
        "unchanged project must skip everything: {:?}",
        second.products
    );

    // Every target compiles a source that includes mymath.h, so all of them
    // are dirty once it changes.
    std::thread::sleep(std::time::Duration::from_millis(1100));
    write_project_file(
        project_path,
        "include/mymath.h",
        "/* edited */\nint add(int a, int b);\n",
    );
    let third = build_json(project_path);
    assert_eq!(
        third.success, third.total_products,
        "editing an included header must rebuild the targets that include it: {:?}",
        third.products
    );

    std::thread::sleep(std::time::Duration::from_millis(1100));
    write_project_file(project_path, "include/unrelated.h", "#define UNUSED 2\n");
    let fourth = build_json(project_path);
    assert_eq!(
        fourth.skipped, fourth.total_products,
        "editing a header nothing includes must rebuild nothing: {:?}",
        fourth.products
    );
}

/// Each object gets the flags of its own language at both levels: a
/// target's `cxxflags` reach its C++ objects and its `cflags` its C
/// objects, never the other way round. The sources `#error` on a flag from
/// the wrong language, so the build is green only when the split is exact.
/// Regression test: per-target `cxxflags` were parsed and never used, and
/// per-target `cflags` went to every object.
#[test]
fn cc_target_flags_follow_the_source_language() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    setup_cc_project(
        project_path,
        r#"
libraries:
  - name: helper
    lib_type: static
    sources: [src/helper.cpp]
    cflags: [-DLIB_C=1]
    cxxflags: [-DLIB_CXX=1]

programs:
  - name: app
    sources: [src/main.cpp, src/util.c]
    link: [helper]
    cflags: [-DPROG_C=1]
    cxxflags: [-DPROG_CXX=1]
"#,
    );
    write_project_file(
        project_path,
        "src/helper.cpp",
        "#ifndef LIB_CXX\n#error library cxxflags did not reach a C++ object\n#endif\n\
         #ifdef LIB_C\n#error library cflags leaked into a C++ object\n#endif\n\
         extern \"C\" int helper(void) { return 1; }\n",
    );
    write_project_file(
        project_path,
        "src/main.cpp",
        "#ifndef PROG_CXX\n#error program cxxflags did not reach a C++ object\n#endif\n\
         #ifdef PROG_C\n#error program cflags leaked into a C++ object\n#endif\n\
         extern \"C\" int helper(void);\nextern \"C\" int util(void);\n\
         int main() { return helper() + util() - 2; }\n",
    );
    write_project_file(
        project_path,
        "src/util.c",
        "#ifndef PROG_C\n#error program cflags did not reach a C object\n#endif\n\
         #ifdef PROG_CXX\n#error program cxxflags leaked into a C object\n#endif\n\
         int util(void) { return 1; }\n",
    );

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "per-target flags must follow the source language: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    let run_output =
        std::process::Command::new(project_path.join("out/processor.mass_generator.cc/bin/app"))
            .output()
            .expect("Failed to run executable");
    assert!(run_output.status.success(), "Executable should return 0");
}

/// Single-invocation mode used to pass only the global language flags:
/// the manifest's and the program's `include_dirs` and the program's own
/// flags were dropped. Both programs here compile only with them: the C
/// one needs headers from both include-dir levels and a program `cflags`
/// define; the C++ one needs a program `cxxflags` define and would fail on
/// the program's (invalid) `cflags` if they reached a C++ command.
#[test]
fn cc_single_invocation_honours_include_dirs_and_program_flags() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    write_project_file(
        project_path,
        "rsconstruct.toml",
        "[processor.mass_generator.cc]\nsingle_invocation = true\nsrc_dirs = [\".\"]\n",
    );
    write_project_file(
        project_path,
        "cc.yaml",
        r#"
include_dirs: [include]

programs:
  - name: app
    sources: [src/main.c]
    include_dirs: [extra]
    cflags: [-DNEED_FLAG=0]

  - name: appxx
    sources: [src/main.cpp]
    cflags: [--definitely-not-a-flag]
    cxxflags: [-DNEED_CXX=0]
"#,
    );
    write_project_file(project_path, "include/a.h", "#define A 0\n");
    write_project_file(project_path, "extra/b.h", "#define B 0\n");
    write_project_file(
        project_path,
        "src/main.c",
        "#include \"a.h\"\n#include \"b.h\"\nint main(void) { return A + B + NEED_FLAG; }\n",
    );
    write_project_file(
        project_path,
        "src/main.cpp",
        "int main() { return NEED_CXX; }\n",
    );

    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "single invocation must pass include dirs and the program's language flags: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    for exe in ["app", "appxx"] {
        let path = project_path.join(format!("out/processor.mass_generator.cc/bin/{exe}"));
        assert!(path.exists(), "{exe} should link");
        let run_output = std::process::Command::new(&path)
            .output()
            .expect("Failed to run executable");
        assert!(run_output.status.success(), "{exe} should return 0");
    }
}