ccgo 3.10.8

A high-performance C++ cross-platform build CLI
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
//! Platform-specific build implementations
//!
//! Each platform has its own builder that implements the `PlatformBuilder` trait.
//! The builders handle:
//! - CMake configuration with platform-specific toolchains
//! - Multi-architecture builds
//! - Library packaging (frameworks, AARs, HARs, etc.)
//! - Archive creation
//!
//! ## Platform Grouping Strategy
//!
//! When building multiple platforms in parallel, we group platforms to avoid
//! toolchain conflicts:
//!
//! - **Apple Group** (iOS, macOS, tvOS, watchOS): Built sequentially to avoid Xcode conflicts
//! - **Gradle Group** (Android, KMP): Built sequentially to avoid Gradle lock/daemon conflicts
//! - **Other Platforms** (Linux, Windows, OHOS): Built in parallel

pub mod android;
mod apple_symbols;
pub mod benches;
pub mod conan;
mod elf_symbols;
pub mod ios;
pub mod kmp;
mod kmp_sdks;
pub mod linux;
pub mod macos;
pub mod ohos;
pub mod openwrt;
pub mod tests;
pub mod tvos;
pub mod watchos;
pub mod windows;

use std::collections::HashSet;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Instant;

use anyhow::{bail, Result};

use super::{BuildContext, BuildResult, PlatformBuilder};
use crate::commands::build::BuildTarget;

/// Get the appropriate platform builder for the target
pub fn get_builder(target: &BuildTarget) -> Result<Box<dyn PlatformBuilder>> {
    match target {
        BuildTarget::Linux => Ok(Box::new(linux::LinuxBuilder::new())),
        BuildTarget::Macos => Ok(Box::new(macos::MacosBuilder::new())),
        BuildTarget::Windows => Ok(Box::new(windows::WindowsBuilder::new())),
        BuildTarget::Ios => Ok(Box::new(ios::IosBuilder::new())),
        BuildTarget::Android => Ok(Box::new(android::AndroidBuilder::new())),
        BuildTarget::Ohos => Ok(Box::new(ohos::OhosBuilder::new())),
        BuildTarget::Tvos => Ok(Box::new(tvos::TvosBuilder::new())),
        BuildTarget::Watchos => Ok(Box::new(watchos::WatchosBuilder::new())),
        BuildTarget::Kmp => Ok(Box::new(kmp::KmpBuilder::new())),
        BuildTarget::Conan => Ok(Box::new(conan::ConanBuilder::new())),
        BuildTarget::Openwrt => Ok(Box::new(openwrt::OpenwrtBuilder::new())),
        BuildTarget::All => bail!("Use build_all() for building all platforms"),
        BuildTarget::Apple => bail!("Use build_apple() for building all Apple platforms"),
    }
}

/// Result of a subprocess platform build
#[derive(Debug)]
struct SubprocessBuildResult {
    platform: String,
    success: bool,
    duration_secs: f64,
    stdout: String,
    stderr: String,
    exit_code: Option<i32>,
}

/// Format duration in human-readable format (e.g., "2m 34s" or "45s")
fn format_duration(secs: f64) -> String {
    let total_secs = secs as u64;
    let mins = total_secs / 60;
    let secs = total_secs % 60;
    if mins > 0 {
        format!("{}m {}s", mins, secs)
    } else {
        format!("{}s", secs)
    }
}

/// Check if a platform can be built natively on the current host
fn can_build_natively(target: &BuildTarget) -> bool {
    let host_os = std::env::consts::OS;

    match target {
        // Linux can only be built natively on Linux
        BuildTarget::Linux => host_os == "linux",

        // Windows can only be built natively on Windows
        BuildTarget::Windows => host_os == "windows",

        // Apple platforms can only be built on macOS (Xcode required)
        BuildTarget::Macos | BuildTarget::Ios | BuildTarget::Tvos | BuildTarget::Watchos => {
            host_os == "macos"
        }

        // OpenWrt requires musl cross-compilers — only native on Linux with toolchains installed
        BuildTarget::Openwrt => host_os == "linux",

        // Android, OHOS, Kmp, Conan can be built on any platform
        _ => true,
    }
}

/// Build a single platform using subprocess to isolate output
/// Returns (platform, success, duration, stdout, stderr)
fn build_platform_subprocess(
    target: &BuildTarget,
    project_root: &PathBuf,
    args: &BuildArgs,
) -> SubprocessBuildResult {
    let start = Instant::now();
    let platform_name = target.to_string().to_lowercase();

    // Get the ccgo executable path
    let ccgo_exe = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("ccgo"));

    let cmd_args = subprocess_command_args(&platform_name, args);

    // Run the subprocess
    let result = Command::new(&ccgo_exe)
        .args(&cmd_args)
        .current_dir(project_root)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output();

    let duration_secs = start.elapsed().as_secs_f64();

    match result {
        Ok(output) => {
            let stdout = String::from_utf8_lossy(&output.stdout).to_string();
            let stderr = String::from_utf8_lossy(&output.stderr).to_string();
            SubprocessBuildResult {
                platform: platform_name,
                success: output.status.success(),
                duration_secs,
                stdout,
                stderr,
                exit_code: output.status.code(),
            }
        }
        Err(e) => SubprocessBuildResult {
            platform: platform_name,
            success: false,
            duration_secs,
            stdout: String::new(),
            stderr: format!("Failed to execute subprocess: {}", e),
            exit_code: None,
        },
    }
}

/// Arguments passed to subprocess builds
#[derive(Clone, Default)]
struct BuildArgs {
    arch: Option<String>,
    native_only: bool,
    docker: bool,
    auto_docker: bool,
    toolchain: Option<String>,
    link_type: Option<String>,
    stl: Option<String>,
    release: bool,
    dev: bool,
    profile: Option<String>,
    asan: bool,
    tsan: bool,
    features: Vec<String>,
    use_default_features: bool,
    all_features: bool,
    variants: Vec<String>,
    native_sdks: std::collections::BTreeMap<String, PathBuf>,
    jobs: Option<usize>,
    ide_project: bool,
    no_fetch: bool,
    verbose: bool,
    cache: Option<String>,
    linkage_default: Option<String>,
    linkage_overrides: Vec<(String, String)>,
    linkage_on_shared_default: Option<String>,
    linkage_on_shared_overrides: Vec<(String, String)>,
    linkage_on_static_default: Option<String>,
    linkage_on_static_overrides: Vec<(String, String)>,
}

impl BuildArgs {
    fn from_context(ctx: &BuildContext) -> Self {
        use crate::builder::sanitizer::SanitizerKind;

        let (asan, tsan) = match ctx.options.sanitizer {
            Some(SanitizerKind::Address) => (true, false),
            Some(SanitizerKind::Thread) => (false, true),
            None => (false, false),
        };

        Self {
            arch: if ctx.options.architectures.is_empty() {
                None
            } else {
                Some(ctx.options.architectures.join(","))
            },
            native_only: ctx.options.native_only,
            docker: ctx.options.use_docker,
            auto_docker: ctx.options.auto_docker,
            toolchain: Some(ctx.options.toolchain.to_string()),
            link_type: Some(ctx.options.link_type.to_string()),
            stl: ctx.options.stl.clone(),
            release: ctx.options.release,
            dev: ctx.options.dev,
            profile: ctx.options.profile_name.clone(),
            asan,
            tsan,
            features: ctx.options.features.clone(),
            use_default_features: ctx.options.use_default_features,
            all_features: ctx.options.all_features,
            variants: ctx.options.variants.clone(),
            native_sdks: ctx.options.native_sdks.clone(),
            jobs: ctx.options.jobs,
            ide_project: ctx.options.ide_project,
            no_fetch: ctx.options.no_fetch,
            verbose: ctx.options.verbose,
            cache: ctx.options.cache.clone().filter(|cache| cache != "auto"),
            linkage_default: ctx.options.linkage_default.map(|value| value.to_string()),
            linkage_overrides: sorted_overrides(&ctx.options.linkage_overrides),
            linkage_on_shared_default: ctx
                .options
                .linkage_on_shared_default
                .map(|value| value.to_string()),
            linkage_on_shared_overrides: sorted_overrides(&ctx.options.linkage_on_shared_overrides),
            linkage_on_static_default: ctx
                .options
                .linkage_on_static_default
                .map(|value| value.to_string()),
            linkage_on_static_overrides: sorted_overrides(&ctx.options.linkage_on_static_overrides),
        }
    }
}

fn sorted_overrides(
    map: &std::collections::HashMap<String, crate::config::Linkage>,
) -> Vec<(String, String)> {
    let mut overrides: Vec<_> = map
        .iter()
        .map(|(name, value)| (name.clone(), value.to_string()))
        .collect();
    overrides.sort_by(|left, right| left.0.cmp(&right.0));
    overrides
}

/// Build the child `ccgo build` argument list for one platform.
fn subprocess_command_args(platform: &str, args: &BuildArgs) -> Vec<String> {
    let mut cmd = vec!["build".to_string(), platform.to_string()];
    push_target_args(&mut cmd, args);
    push_mode_args(&mut cmd, args);
    push_feature_args(&mut cmd, args);
    push_linkage_args(&mut cmd, args);
    if platform == "kmp" {
        for (platform, path) in &args.native_sdks {
            cmd.push("--native-sdk".to_string());
            cmd.push(format!("{platform}={}", path.display()));
        }
    }
    cmd
}

fn push_target_args(cmd: &mut Vec<String>, args: &BuildArgs) {
    if let Some(arch) = &args.arch {
        cmd.push("--arch".to_string());
        cmd.push(arch.clone());
    }
    if args.native_only {
        cmd.push("--native-only".to_string());
    }
    if args.docker {
        cmd.push("--docker".to_string());
    }
    if args.auto_docker {
        cmd.push("--auto-docker".to_string());
    }
    if let Some(toolchain) = &args.toolchain {
        cmd.push("--toolchain".to_string());
        cmd.push(toolchain.clone());
    }
    if let Some(link_type) = &args.link_type {
        cmd.push("--build-as".to_string());
        cmd.push(link_type.clone());
    }
    if let Some(stl) = &args.stl {
        cmd.push("--stl".to_string());
        cmd.push(stl.clone());
    }
    if args.ide_project {
        cmd.push("--ide-project".to_string());
    }
    if let Some(cache) = &args.cache {
        cmd.push("--cache".to_string());
        cmd.push(cache.clone());
    }
}

fn push_mode_args(cmd: &mut Vec<String>, args: &BuildArgs) {
    if args.release {
        cmd.push("--release".to_string());
    }
    if args.dev {
        cmd.push("--dev".to_string());
    }
    if let Some(profile) = &args.profile {
        cmd.push("--profile".to_string());
        cmd.push(profile.clone());
    }
    if args.asan {
        cmd.push("--asan".to_string());
    }
    if args.tsan {
        cmd.push("--tsan".to_string());
    }
    if let Some(jobs) = args.jobs {
        cmd.push("--jobs".to_string());
        cmd.push(jobs.to_string());
    }
    if args.no_fetch {
        cmd.push("--no-fetch".to_string());
    }
    if args.verbose {
        cmd.push("--verbose".to_string());
    }
}

fn push_feature_args(cmd: &mut Vec<String>, args: &BuildArgs) {
    if !args.features.is_empty() {
        cmd.push("--features".to_string());
        cmd.push(args.features.join(","));
    }
    if !args.use_default_features {
        cmd.push("--no-default-features".to_string());
    }
    if args.all_features {
        cmd.push("--all-features".to_string());
    }
    if !args.variants.is_empty() {
        cmd.push("--variant".to_string());
        cmd.push(args.variants.join(","));
    }
}

fn push_linkage_args(cmd: &mut Vec<String>, args: &BuildArgs) {
    push_linkage_values(
        cmd,
        "--linkage",
        &args.linkage_default,
        &args.linkage_overrides,
    );
    push_linkage_values(
        cmd,
        "--linkage-on-shared",
        &args.linkage_on_shared_default,
        &args.linkage_on_shared_overrides,
    );
    push_linkage_values(
        cmd,
        "--linkage-on-static",
        &args.linkage_on_static_default,
        &args.linkage_on_static_overrides,
    );
}

fn push_linkage_values(
    cmd: &mut Vec<String>,
    flag: &str,
    default: &Option<String>,
    overrides: &[(String, String)],
) {
    if let Some(value) = default {
        cmd.push(flag.to_string());
        cmd.push(value.clone());
    }
    for (name, value) in overrides {
        cmd.push(flag.to_string());
        cmd.push(format!("{name}={value}"));
    }
}

/// Print build error summary (last few lines of stderr/stdout)
fn print_build_error(stdout: &str, stderr: &str) {
    // Print last few lines of stderr first (usually contains the actual error)
    if !stderr.is_empty() {
        let stderr_lines: Vec<&str> = stderr.lines().collect();
        let start = stderr_lines.len().saturating_sub(10);
        eprintln!(
            "   Error output (last {} lines):",
            stderr_lines.len().min(10)
        );
        for line in &stderr_lines[start..] {
            eprintln!("   | {}", line);
        }
    } else if !stdout.is_empty() {
        // Fall back to stdout if stderr is empty
        let stdout_lines: Vec<&str> = stdout.lines().collect();
        let start = stdout_lines.len().saturating_sub(5);
        eprintln!("   Output (last {} lines):", stdout_lines.len().min(5));
        for line in &stdout_lines[start..] {
            eprintln!("   | {}", line);
        }
    }
}

/// Platforms included in `ccgo build all`.
fn all_build_platforms() -> Vec<BuildTarget> {
    vec![
        BuildTarget::Linux,
        BuildTarget::Windows,
        BuildTarget::Ohos,
        BuildTarget::Ios,
        BuildTarget::Macos,
        BuildTarget::Tvos,
        BuildTarget::Watchos,
        BuildTarget::Android,
    ]
}

/// Whether the project has a usable KMP module.
fn kmp_configured(project_root: &std::path::Path) -> bool {
    let kmp_dir = project_root.join("kmp");
    kmp_dir.join("gradlew").exists() || kmp_dir.join("gradlew.bat").exists()
}

/// Platforms for `ccgo build all`, including KMP when the project has one.
fn all_platforms_for(ctx: &BuildContext) -> Vec<BuildTarget> {
    let mut platforms = all_build_platforms();
    if kmp_configured(&ctx.project_root) {
        platforms.push(BuildTarget::Kmp);
    }
    platforms
}

/// Platforms built in the parallel phase of `ccgo build all`.
///
/// KMP is excluded because it links against the native libraries produced by
/// the other platforms and therefore runs afterwards.
fn parallel_build_platforms(platforms: &[BuildTarget]) -> Vec<BuildTarget> {
    platforms
        .iter()
        .filter(|target| **target != BuildTarget::Kmp)
        .cloned()
        .collect()
}

/// One `ccgo build all` invocation: which platforms run where.
struct BuildPlan {
    all: Vec<BuildTarget>,
    apple: Vec<BuildTarget>,
    gradle: Vec<BuildTarget>,
    other: Vec<BuildTarget>,
    deferred_kmp: bool,
}

impl BuildPlan {
    fn workers(&self) -> usize {
        usize::from(!self.apple.is_empty())
            + usize::from(!self.gradle.is_empty())
            + self.other.len()
    }
}

fn platform_list(platforms: &[BuildTarget]) -> String {
    platforms
        .iter()
        .map(|platform| platform.to_string().to_lowercase())
        .collect::<Vec<_>>()
        .join(", ")
}

/// Split `all` into the Apple, Gradle and independent worker groups.
fn plan_builds(ctx: &BuildContext) -> BuildPlan {
    let all = all_platforms_for(ctx);
    let deferred_kmp = all.contains(&BuildTarget::Kmp);
    let parallel = parallel_build_platforms(&all);

    let apple_platforms: HashSet<_> = [
        BuildTarget::Ios,
        BuildTarget::Macos,
        BuildTarget::Tvos,
        BuildTarget::Watchos,
    ]
    .into_iter()
    .collect();
    let gradle_platforms: HashSet<_> = [BuildTarget::Android, BuildTarget::Kmp]
        .into_iter()
        .collect();

    let apple = parallel
        .iter()
        .filter(|target| apple_platforms.contains(target))
        .cloned()
        .collect();
    let gradle = parallel
        .iter()
        .filter(|target| gradle_platforms.contains(target))
        .cloned()
        .collect();
    let other = parallel
        .iter()
        .filter(|target| !apple_platforms.contains(target) && !gradle_platforms.contains(target))
        .cloned()
        .collect();

    BuildPlan {
        all,
        apple,
        gradle,
        other,
        deferred_kmp,
    }
}

fn print_build_plan(ctx: &BuildContext, plan: &BuildPlan, num_jobs: usize) {
    eprintln!("\n{}", "=".repeat(80));
    eprintln!("Building library for ALL platforms");
    eprintln!(
        "Build Mode: {}",
        if ctx.options.release {
            "RELEASE"
        } else {
            "DEBUG/BETA"
        }
    );
    eprintln!("{}", "=".repeat(80));

    eprintln!(
        "\nWill build {} platforms: {}",
        plan.all.len(),
        platform_list(&plan.all)
    );
    eprintln!("Parallel platform workers: {}", plan.workers());
    eprintln!("Jobs per platform build: {}", num_jobs);
    eprintln!("{}", "=".repeat(80));

    eprintln!(
        "\n🚀 Starting parallel build with {} platform workers ({} jobs each)...",
        plan.workers(),
        num_jobs
    );
    if !plan.apple.is_empty() {
        eprintln!(
            "   ℹ️  Apple platforms ({}) will be built sequentially to avoid Xcode conflicts",
            platform_list(&plan.apple)
        );
    }
    if !plan.gradle.is_empty() {
        eprintln!(
            "   ℹ️  Gradle platforms ({}) will be built sequentially to avoid Gradle lock conflicts",
            platform_list(&plan.gradle)
        );
    }
    if plan.deferred_kmp {
        eprintln!(
            "   ℹ️  KMP will build after all native platforms so it can reuse their libraries"
        );
    }
    eprintln!();
}

/// Build one platform subprocess and report progress.
fn build_and_report(
    target: BuildTarget,
    args: BuildArgs,
    root: PathBuf,
    completed: Arc<AtomicUsize>,
    in_prog: Arc<Mutex<HashSet<String>>>,
    results_ref: Arc<Mutex<Vec<SubprocessBuildResult>>>,
    total: usize,
) {
    let result = build_platform_subprocess(&target, &root, &args);

    // Update progress atomically
    let count = completed.fetch_add(1, Ordering::SeqCst) + 1;
    {
        let mut progress = in_prog.lock().unwrap();
        progress.remove(&result.platform);

        let status = if result.success { "✅" } else { "❌" };
        let status_text = if result.success {
            "completed"
        } else {
            "failed"
        };

        eprintln!(
            "{} [{}/{}] {} {} ({})",
            status,
            count,
            total,
            result.platform.to_uppercase(),
            status_text,
            format_duration(result.duration_secs)
        );

        if !result.success {
            print_build_error(&result.stdout, &result.stderr);
        }

        if !progress.is_empty() {
            let mut remaining: Vec<_> = progress.iter().cloned().collect();
            remaining.sort();
            eprintln!("   ⏳ Still building: {}", remaining.join(", "));
        }
    }

    results_ref.lock().unwrap().push(result);
}

/// Platform name and elapsed seconds.
type BuildTimes = Vec<(String, f64)>;

fn split_results(results: &[SubprocessBuildResult]) -> (BuildTimes, BuildTimes) {
    let mut successful = Vec::new();
    let mut failed = Vec::new();
    for result in results {
        let entry = (result.platform.clone(), result.duration_secs);
        if result.success {
            successful.push(entry);
        } else {
            failed.push(entry);
        }
    }
    (successful, failed)
}

fn print_build_summary(
    plan: &BuildPlan,
    successful: &[(String, f64)],
    failed: &[(String, f64)],
    total_duration: f64,
    num_jobs: usize,
) {
    eprintln!("\n{}", "=".repeat(80));
    eprintln!("BUILD ALL SUMMARY");
    eprintln!("{}", "=".repeat(80));
    eprintln!("\nTotal platforms: {}", plan.all.len());
    eprintln!("Successful: {}", successful.len());
    eprintln!("Failed: {}", failed.len());
    eprintln!("Parallel platform workers: {}", plan.workers());
    eprintln!("Jobs per platform build: {}", num_jobs);

    if !successful.is_empty() {
        eprintln!("\n✅ Successful builds:");
        for (platform, duration) in successful {
            eprintln!("   - {} ({})", platform, format_duration(*duration));
        }
    }

    if !failed.is_empty() {
        eprintln!("\n❌ Failed builds:");
        for (platform, duration) in failed {
            eprintln!("   - {} ({})", platform, format_duration(*duration));
        }
    }

    eprintln!(
        "\n⏱️  Build completed in {}",
        format_duration(total_duration)
    );

    if failed.is_empty() {
        eprintln!("\n🎉 All platforms built successfully!");
        eprintln!("   💡 To collect all artifacts, run: ccgo package");
    }
}

/// Build all platforms with smart grouping strategy using subprocess isolation
///
/// Platform grouping to avoid toolchain conflicts:
/// - Apple platforms (ios, macos, tvos, watchos): Built sequentially to avoid Xcode conflicts
/// - Gradle platform (android): Runs alongside other groups
/// - Other platforms (linux, windows, ohos): Built in parallel
/// - KMP: Built last, after every native platform, so it can reuse their libraries
///
/// Each platform is built in a subprocess to isolate output and prevent interleaving.
pub fn build_all(ctx: &BuildContext) -> Result<Vec<BuildResult>> {
    let plan = plan_builds(ctx);
    let num_jobs = ctx.jobs();
    print_build_plan(ctx, &plan, num_jobs);

    // Shared state for progress tracking
    let completed_count = Arc::new(AtomicUsize::new(0));
    let in_progress = Arc::new(Mutex::new(
        plan.all
            .iter()
            .map(|platform| platform.to_string().to_lowercase())
            .collect::<HashSet<_>>(),
    ));
    let results = Arc::new(Mutex::new(Vec::<SubprocessBuildResult>::new()));

    let build_args = BuildArgs::from_context(ctx);
    let project_root = ctx.project_root.clone();
    let total = plan.all.len();
    let mut handles = Vec::new();

    // Thread for Apple platforms (sequential within thread)
    if !plan.apple.is_empty() {
        let args = build_args.clone();
        let root = project_root.clone();
        let completed = Arc::clone(&completed_count);
        let in_prog = Arc::clone(&in_progress);
        let results_ref = Arc::clone(&results);
        let platforms = plan.apple.clone();

        handles.push(thread::spawn(move || {
            for target in platforms {
                build_and_report(
                    target,
                    args.clone(),
                    root.clone(),
                    Arc::clone(&completed),
                    Arc::clone(&in_prog),
                    Arc::clone(&results_ref),
                    total,
                );
            }
        }));
    }

    // Thread for Gradle platforms (sequential within thread)
    if !plan.gradle.is_empty() {
        let args = build_args.clone();
        let root = project_root.clone();
        let completed = Arc::clone(&completed_count);
        let in_prog = Arc::clone(&in_progress);
        let results_ref = Arc::clone(&results);
        let platforms = plan.gradle.clone();

        handles.push(thread::spawn(move || {
            for target in platforms {
                build_and_report(
                    target,
                    args.clone(),
                    root.clone(),
                    Arc::clone(&completed),
                    Arc::clone(&in_prog),
                    Arc::clone(&results_ref),
                    total,
                );
            }
        }));
    }

    // Individual threads for other platforms (parallel)
    for target in plan.other.clone() {
        let args = build_args.clone();
        let root = project_root.clone();
        let completed = Arc::clone(&completed_count);
        let in_prog = Arc::clone(&in_progress);
        let results_ref = Arc::clone(&results);

        handles.push(thread::spawn(move || {
            build_and_report(target, args, root, completed, in_prog, results_ref, total);
        }));
    }

    for handle in handles {
        handle.join().expect("Thread panicked");
    }

    // KMP needs the native libraries of the other platforms (including those
    // built through Docker), so it runs after every parallel worker.
    if plan.deferred_kmp {
        eprintln!("\n⏳ Building KMP after native platform builds...");
        build_and_report(
            BuildTarget::Kmp,
            build_args.clone(),
            project_root.clone(),
            Arc::clone(&completed_count),
            Arc::clone(&in_progress),
            Arc::clone(&results),
            total,
        );
    }

    let all_results = Arc::try_unwrap(results)
        .expect("All threads completed")
        .into_inner()
        .unwrap();
    let (successful, failed) = split_results(&all_results);
    let total_duration = all_results
        .iter()
        .map(|result| result.duration_secs)
        .max_by(|left, right| left.partial_cmp(right).unwrap())
        .unwrap_or(0.0);

    print_build_summary(&plan, &successful, &failed, total_duration, num_jobs);

    if !failed.is_empty() {
        bail!("{} platform(s) failed to build", failed.len());
    }

    // Subprocess builds write their own archives; the caller reads target/.
    Ok(Vec::new())
}

/// Build all Apple platforms (iOS, macOS, tvOS, watchOS) sequentially
///
/// Apple platforms share Xcode toolchain and must be built sequentially
/// to avoid conflicts. Uses subprocess to isolate output.
pub fn build_apple(ctx: &BuildContext) -> Result<Vec<BuildResult>> {
    let platforms = [
        BuildTarget::Ios,
        BuildTarget::Macos,
        BuildTarget::Tvos,
        BuildTarget::Watchos,
    ];

    let total_platforms = platforms.len();

    eprintln!("\n{}", "=".repeat(80));
    eprintln!("Building library for APPLE platforms (iOS, macOS, tvOS, watchOS)");
    eprintln!(
        "Build Mode: {}",
        if ctx.options.release {
            "RELEASE"
        } else {
            "DEBUG/BETA"
        }
    );
    eprintln!("{}", "=".repeat(80));

    eprintln!(
        "\nWill build {} Apple platforms: {}",
        total_platforms,
        platforms
            .iter()
            .map(|p| p.to_string())
            .collect::<Vec<_>>()
            .join(", ")
    );
    eprintln!("Build mode: sequential (recommended for Apple platforms)");
    eprintln!("{}", "=".repeat(80));

    let build_args = BuildArgs::from_context(ctx);
    let mut successful: Vec<(String, f64)> = Vec::new();
    let mut failed: Vec<(String, f64)> = Vec::new();

    for (index, target) in platforms.iter().enumerate() {
        eprintln!(
            "\n[{}/{}] Building {}...",
            index + 1,
            total_platforms,
            target.to_string().to_uppercase()
        );

        let result = build_platform_subprocess(target, &ctx.project_root, &build_args);

        if result.success {
            eprintln!(
                "✅ [{}/{}] {} completed ({})",
                index + 1,
                total_platforms,
                result.platform.to_uppercase(),
                format_duration(result.duration_secs)
            );
            successful.push((result.platform, result.duration_secs));
        } else {
            eprintln!(
                "❌ [{}/{}] {} failed ({})",
                index + 1,
                total_platforms,
                result.platform.to_uppercase(),
                format_duration(result.duration_secs)
            );
            print_build_error(&result.stdout, &result.stderr);
            failed.push((result.platform, result.duration_secs));
        }
    }

    // Calculate total duration
    let total_duration: f64 = successful
        .iter()
        .chain(failed.iter())
        .map(|(_, d)| *d)
        .sum();

    // Print summary
    eprintln!("\n{}", "=".repeat(80));
    eprintln!("BUILD APPLE SUMMARY");
    eprintln!("{}", "=".repeat(80));
    eprintln!("\nTotal platforms: {}", total_platforms);
    eprintln!("Successful: {}", successful.len());
    eprintln!("Failed: {}", failed.len());

    if !successful.is_empty() {
        eprintln!("\n✅ Successful builds:");
        for (platform, duration) in &successful {
            eprintln!("   - {} ({})", platform, format_duration(*duration));
        }
    }

    if !failed.is_empty() {
        eprintln!("\n❌ Failed builds:");
        for (platform, duration) in &failed {
            eprintln!("   - {} ({})", platform, format_duration(*duration));
        }
    }

    eprintln!(
        "\n⏱️  Build completed in {}",
        format_duration(total_duration)
    );

    if !failed.is_empty() {
        bail!("{} Apple platform(s) failed to build", failed.len());
    }

    eprintln!("\n🎉 All Apple platforms built successfully!");
    eprintln!("   💡 To publish, run: ccgo publish apple --cocoapods / --spm");

    // Return empty vec - subprocess builds handle their own output
    Ok(Vec::new())
}

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

    fn has_pair(cmd: &[String], flag: &str, value: &str) -> bool {
        cmd.windows(2)
            .any(|pair| pair[0] == flag && pair[1] == value)
    }

    #[test]
    fn all_build_subprocess_forwards_release_and_auto_docker() {
        let args = BuildArgs {
            release: true,
            auto_docker: true,
            ..BuildArgs::default()
        };

        for platform in [
            "linux", "windows", "ohos", "ios", "macos", "tvos", "watchos", "android", "kmp",
        ] {
            let cmd = subprocess_command_args(platform, &args);
            assert_eq!(cmd[0], "build");
            assert_eq!(cmd[1], platform);
            assert!(cmd.iter().any(|arg| arg == "--release"));
            assert!(cmd.iter().any(|arg| arg == "--auto-docker"));
        }
    }

    #[test]
    fn native_sdk_overrides_are_forwarded_only_to_kmp() {
        let args = BuildArgs {
            native_sdks: [(
                "android".to_string(),
                PathBuf::from("/tmp/sdk dir/ANO=1.zip"),
            )]
            .into_iter()
            .collect(),
            ..BuildArgs::default()
        };
        let cmd = subprocess_command_args("kmp", &args);
        assert!(cmd
            .windows(2)
            .any(|pair| pair == ["--native-sdk", "android=/tmp/sdk dir/ANO=1.zip"]));
        assert!(!subprocess_command_args("android", &args)
            .iter()
            .any(|arg| arg == "--native-sdk"));
    }

    #[test]
    fn subprocess_forwards_profile_features_and_linkage_options() {
        let args = BuildArgs {
            profile: Some("ci".to_string()),
            asan: true,
            features: vec!["networking".to_string()],
            variants: vec!["oversea".to_string()],
            all_features: true,
            jobs: Some(4),
            no_fetch: true,
            cache: Some("sccache".to_string()),
            linkage_default: Some("shared-external".to_string()),
            linkage_overrides: vec![
                ("alpha".to_string(), "static-embedded".to_string()),
                ("zeta".to_string(), "shared-external".to_string()),
            ],
            ..BuildArgs::default()
        };

        let cmd = subprocess_command_args("linux", &args);

        assert!(has_pair(&cmd, "--profile", "ci"));
        assert!(has_pair(&cmd, "--features", "networking"));
        assert!(has_pair(&cmd, "--variant", "oversea"));
        assert!(has_pair(&cmd, "--jobs", "4"));
        assert!(has_pair(&cmd, "--cache", "sccache"));
        assert!(has_pair(&cmd, "--linkage", "shared-external"));
        assert!(has_pair(&cmd, "--linkage", "alpha=static-embedded"));
        assert!(has_pair(&cmd, "--linkage", "zeta=shared-external"));
        assert!(cmd.iter().any(|arg| arg == "--asan"));
        assert!(cmd.iter().any(|arg| arg == "--all-features"));
        assert!(cmd.iter().any(|arg| arg == "--no-fetch"));
    }

    fn build_ctx(root: &std::path::Path) -> BuildContext {
        let config = toml::from_str("[package]\nname = \"demo\"\nversion = \"1.0.0\"\n")
            .expect("toml should parse");
        BuildContext::new(
            root.to_path_buf(),
            config,
            crate::builder::BuildOptions::default(),
        )
    }

    #[test]
    fn all_build_includes_kmp_only_when_configured() {
        // A plain C++ project must not fail because `all` suddenly requires KMP.
        let plain = tempfile::tempdir().unwrap();
        let plain_platforms = all_platforms_for(&build_ctx(plain.path()));
        assert_eq!(plain_platforms.len(), 8);
        assert!(!plain_platforms.contains(&BuildTarget::Kmp));

        let kmp_project = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(kmp_project.path().join("kmp")).unwrap();
        std::fs::write(kmp_project.path().join("kmp/gradlew"), b"").unwrap();

        let all = all_platforms_for(&build_ctx(kmp_project.path()));
        assert_eq!(all.len(), 9);
        assert!(all.contains(&BuildTarget::Kmp));

        let unique: HashSet<_> = all.iter().collect();
        assert_eq!(unique.len(), all.len(), "duplicate platform in `all`");

        // KMP is deferred until the native platforms finish.
        let parallel = parallel_build_platforms(&all);
        assert_eq!(parallel.len(), 8);
        assert!(!parallel.contains(&BuildTarget::Kmp));
    }
}