waterui-cli 0.1.4

Cross-platform tooling for WaterUI applications
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
//! `water build` command implementation.

use std::path::PathBuf;

use clap::{Args as ClapArgs, ValueEnum};
use color_eyre::eyre::{Result, bail};
use target_lexicon::{
    Aarch64Architecture, Architecture, BinaryFormat, Environment, OperatingSystem, Triple, Vendor,
};

use crate::shell::Shell;
use crate::toolchain_checks;
use crate::{error, header, success};
use waterui_cli::{
    android::platform::{AndroidAbi, AndroidPlatform},
    apple::platform::build_rust_lib,
    apple::toolchain::AppleSdk,
    backend::reinit_backend,
    build::BuildOptions,
    esp32::{backend::Esp32Backend, platform::build_esp32},
    gtk4::{backend::Gtk4Backend, platform::build_gtk4},
    hydrolysis::{backend::HydrolysisBackend, platform::build_hydrolysis},
    platform::TargetPlatform as LibTargetPlatform,
    project::{PackageType, Project},
};

/// Target platform for building.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum TargetPlatform {
    /// iOS (physical device).
    Ios,
    /// iOS Simulator.
    IosSimulator,
    /// Android.
    Android,
    /// macOS.
    Macos,
    /// Linux.
    Linux,
    /// Windows.
    Windows,
    /// ESP32-S3 (Xtensa firmware).
    Esp32s3,
    /// ESP32-C3 (RISC-V firmware).
    Esp32c3,
    /// ESP32-P4 (RISC-V firmware, hardware FPU).
    Esp32p4,
}

impl TargetPlatform {
    /// The ESP32 chip a platform selects, if it is an ESP32 platform.
    const fn esp32_chip(self) -> Option<waterui_cli::esp32::chip::Esp32Chip> {
        use waterui_cli::esp32::chip::Esp32Chip;
        match self {
            Self::Esp32s3 => Some(Esp32Chip::Esp32S3),
            Self::Esp32c3 => Some(Esp32Chip::Esp32C3),
            Self::Esp32p4 => Some(Esp32Chip::Esp32P4),
            _ => None,
        }
    }
}

/// Target backend for building.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum TargetBackend {
    /// Apple backend (UIKit/AppKit).
    Apple,
    /// Android backend.
    Android,
    /// GTK4 backend.
    Gtk4,
    /// Hydrolysis backend.
    Hydrolysis,
    /// Dew backend (ESP32 firmware).
    Dew,
}

/// Target architecture for building.
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum TargetArch {
    /// ARM64 / `AArch64` (Apple Silicon, modern Android devices).
    Arm64,
    /// `x86_64` (Intel Macs, Android emulators on Intel/AMD).
    X86_64,
    /// `ARMv7` (older 32-bit Android devices).
    Armv7,
    /// x86 (older 32-bit Android emulators).
    X86,
}

/// Arguments for the build command.
#[derive(ClapArgs, Debug)]
pub struct Args {
    /// Target platform to build for.
    #[arg(short, long, value_enum)]
    platform: TargetPlatform,

    /// Backend to use (overrides default for platform).
    #[arg(short, long, value_enum)]
    backend: Option<TargetBackend>,

    /// Target architecture. Defaults to arm64 for iOS/Android, native for macOS/iOS Simulator.
    #[arg(short, long, value_enum)]
    arch: Option<TargetArch>,

    /// Build in release mode (optimized).
    #[arg(long)]
    release: bool,

    /// Project directory path (defaults to current directory).
    #[arg(long, default_value = ".")]
    path: PathBuf,

    /// Output directory to copy the built library to.
    /// Only valid for Apple/Android backends.
    #[arg(long)]
    output_dir: Option<PathBuf>,
}

struct BuildContext {
    project: Project,
    backend: TargetBackend,
    build_options: BuildOptions,
}

/// Run the build command.
pub async fn run(shell: &Shell, args: Args) -> Result<()> {
    let context = prepare_build_context(shell, &args).await?;
    print_build_header(
        shell,
        &context.project,
        args.platform,
        context.backend,
        args.release,
    );
    check_build_toolchain(shell, args.platform, context.backend, args.arch).await?;
    let result = execute_build(shell, &args, &context).await;

    handle_build_result(shell, result, args.output_dir)
}

async fn prepare_build_context(shell: &Shell, args: &Args) -> Result<BuildContext> {
    let project_path = crate::project_path::canonicalize(&args.path)?;
    let mut project = Project::open(&project_path).await?;
    ensure_app_project(&project)?;

    let backend = resolve_and_validate_backend(args)?;
    ensure_backend_configured(&project, backend)?;

    // Selecting an ESP32 platform pins the chip so the generated harness and
    // build target follow the platform.
    if let Some(chip) = args.platform.esp32_chip() {
        project.set_esp32_chip(chip).await?;
    }

    let project = ensure_generated_backend_ready(shell, &project_path, project, backend).await?;
    let build_options = build_options(shell, args, backend).await;

    Ok(BuildContext {
        project,
        backend,
        build_options,
    })
}

fn ensure_app_project(project: &Project) -> Result<()> {
    if project.package_type() != PackageType::App {
        bail!(
            "`water build` is only supported for app mode projects.\n\
             Playground projects are managed by `water run` and `water package`."
        );
    }
    Ok(())
}

fn resolve_and_validate_backend(args: &Args) -> Result<TargetBackend> {
    let backend = resolve_backend(args.platform, args.backend)?;
    validate_desktop_backend_platform_on_host(args.platform, backend)?;
    validate_arch_args(backend, args.arch)?;
    validate_output_dir_args(backend, args.output_dir.as_ref())?;
    Ok(backend)
}

fn ensure_backend_configured(project: &Project, backend: TargetBackend) -> Result<()> {
    match backend {
        TargetBackend::Apple if project.apple_backend().is_none() => {
            bail!("Apple backend is not configured. Run `water backend add apple`.");
        }
        TargetBackend::Android if project.android_backend().is_none() => {
            bail!("Android backend is not configured. Run `water backend add android`.");
        }
        TargetBackend::Gtk4 if project.gtk4_backend().is_none() => {
            bail!("GTK4 backend is not configured. Run `water backend add gtk4`.");
        }
        TargetBackend::Hydrolysis if project.hydrolysis_backend().is_none() => {
            bail!("Hydrolysis backend is not configured. Run `water backend add hydrolysis`.");
        }
        TargetBackend::Dew if project.esp32_backend().is_none() => {
            bail!("ESP32 backend is not configured. Run `water backend add esp32`.");
        }
        _ => Ok(()),
    }
}

async fn ensure_generated_backend_ready(
    shell: &Shell,
    project_path: &PathBuf,
    project: Project,
    backend: TargetBackend,
) -> Result<Project> {
    match backend {
        TargetBackend::Gtk4 if Gtk4Backend::requires_regeneration(&project).await? => {
            reinitialize_generated_backend::<Gtk4Backend>(
                shell,
                project_path,
                &project,
                "Re-initializing GTK4 backend...",
                "GTK4 backend re-initialized",
            )
            .await
        }
        TargetBackend::Hydrolysis if HydrolysisBackend::requires_regeneration(&project).await? => {
            reinitialize_generated_backend::<HydrolysisBackend>(
                shell,
                project_path,
                &project,
                "Re-initializing hydrolysis backend...",
                "Hydrolysis backend re-initialized",
            )
            .await
        }
        TargetBackend::Dew if Esp32Backend::requires_regeneration(&project)? => {
            reinitialize_generated_backend::<Esp32Backend>(
                shell,
                project_path,
                &project,
                "Re-initializing ESP32 backend...",
                "ESP32 backend re-initialized",
            )
            .await
        }
        _ => Ok(project),
    }
}

async fn reinitialize_generated_backend<T>(
    shell: &Shell,
    project_path: &PathBuf,
    project: &Project,
    spinner_message: &str,
    success_message: &str,
) -> Result<Project>
where
    T: waterui_cli::backend::Backend,
{
    let spinner = shell.spinner(spinner_message);
    reinit_backend::<T>(project).await?;
    let project = Project::open(project_path).await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "{success_message}");
    Ok(project)
}

async fn build_options(shell: &Shell, args: &Args, backend: TargetBackend) -> BuildOptions {
    let mut build_options = args.output_dir.as_ref().map_or_else(
        || BuildOptions::development(args.release),
        |output_dir| BuildOptions::development(args.release).with_output_dir(output_dir),
    );

    if let Some(sccache_path) = super::detect_sccache_path(shell).await {
        build_options = build_options.with_sccache(sccache_path);
    }

    if backend == TargetBackend::Apple
        && let Some(triple) = apple_target_triple_override(args.platform, args.arch)
    {
        build_options = build_options.with_target_triple(triple);
    }

    build_options
}

fn print_build_header(
    shell: &Shell,
    project: &Project,
    platform: TargetPlatform,
    backend: TargetBackend,
    release: bool,
) {
    let mode = if release { "release" } else { "debug" };
    header!(
        shell,
        "Building {} for {} via {} ({})",
        project.crate_name(),
        platform_name(platform),
        backend_name(backend),
        mode
    );
}

async fn check_build_toolchain(
    shell: &Shell,
    platform: TargetPlatform,
    backend: TargetBackend,
    arch: Option<TargetArch>,
) -> Result<()> {
    let spinner = shell.spinner("Checking toolchain...");
    check_toolchain_for_backend(platform, backend, arch).await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "Toolchain ready");
    Ok(())
}

async fn execute_build(shell: &Shell, args: &Args, context: &BuildContext) -> Result<PathBuf> {
    let spinner = shell.spinner("Compiling...");
    let result = shell
        .display_output(async {
            match context.backend {
                TargetBackend::Apple => {
                    build_for_apple(
                        &context.project,
                        args.platform,
                        args.arch,
                        context.build_options.clone(),
                    )
                    .await
                }
                TargetBackend::Android => {
                    build_for_android(&context.project, args.arch, context.build_options.clone())
                        .await
                }
                TargetBackend::Gtk4 => {
                    build_gtk4(&context.project, context.build_options.clone()).await
                }
                TargetBackend::Hydrolysis => {
                    build_hydrolysis(
                        &context.project,
                        lib_platform(args.platform),
                        context.build_options.clone(),
                    )
                    .await
                }
                TargetBackend::Dew => {
                    build_esp32(&context.project, context.build_options.clone()).await
                }
            }
        })
        .await;

    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }

    result
}

fn handle_build_result(
    shell: &Shell,
    result: Result<PathBuf>,
    output_dir: Option<PathBuf>,
) -> Result<()> {
    match result {
        Ok(output_path) => {
            success!(shell, "Build output at {}", output_path.display());
            if let Some(output_dir) = output_dir {
                success!(shell, "Copied library to {}", output_dir.display());
            }
            Ok(())
        }
        Err(err) => {
            error!(shell, "Build failed: {err}");
            Err(err)
        }
    }
}

fn resolve_backend(
    platform: TargetPlatform,
    backend_override: Option<TargetBackend>,
) -> Result<TargetBackend> {
    let default_backend = match platform {
        TargetPlatform::Ios | TargetPlatform::IosSimulator | TargetPlatform::Macos => {
            TargetBackend::Apple
        }
        TargetPlatform::Android => TargetBackend::Android,
        TargetPlatform::Linux => TargetBackend::Gtk4,
        TargetPlatform::Windows => TargetBackend::Hydrolysis,
        TargetPlatform::Esp32s3 | TargetPlatform::Esp32c3 | TargetPlatform::Esp32p4 => {
            TargetBackend::Dew
        }
    };
    let backend = backend_override.unwrap_or(default_backend);

    let supported = matches!(
        (platform, backend),
        (
            TargetPlatform::Ios | TargetPlatform::IosSimulator,
            TargetBackend::Apple
        ) | (
            TargetPlatform::Macos,
            TargetBackend::Apple | TargetBackend::Hydrolysis
        ) | (TargetPlatform::Android, TargetBackend::Android)
            | (
                TargetPlatform::Linux,
                TargetBackend::Gtk4 | TargetBackend::Hydrolysis
            )
            | (TargetPlatform::Windows, TargetBackend::Hydrolysis)
            | (
                TargetPlatform::Esp32s3 | TargetPlatform::Esp32c3 | TargetPlatform::Esp32p4,
                TargetBackend::Dew
            )
    );
    if !supported {
        bail!(
            "Backend {:?} does not support platform {:?}.\n\
             Valid combinations:\n  \
             - iOS/iOS Simulator: apple\n  \
             - Android: android\n  \
             - macOS: apple, hydrolysis\n  \
             - Linux: gtk4, hydrolysis\n  \
             - Windows: hydrolysis\n  \
             - ESP32-S3: dew\n  \
             - ESP32-C3: dew\n  \
             - ESP32-P4: dew",
            backend,
            platform
        );
    }
    Ok(backend)
}

fn validate_arch_args(backend: TargetBackend, arch: Option<TargetArch>) -> Result<()> {
    if matches!(
        backend,
        TargetBackend::Gtk4 | TargetBackend::Hydrolysis | TargetBackend::Dew
    ) && arch.is_some()
    {
        bail!("--arch is not supported for gtk4/hydrolysis/dew backends");
    }
    Ok(())
}

fn validate_output_dir_args(backend: TargetBackend, output_dir: Option<&PathBuf>) -> Result<()> {
    if output_dir.is_some()
        && matches!(
            backend,
            TargetBackend::Gtk4 | TargetBackend::Hydrolysis | TargetBackend::Dew
        )
    {
        bail!("--output-dir is only supported for Apple/Android backends");
    }
    Ok(())
}

async fn check_toolchain_for_backend(
    platform: TargetPlatform,
    backend: TargetBackend,
    arch: Option<TargetArch>,
) -> Result<()> {
    match backend {
        TargetBackend::Apple => {
            let sdk = match platform {
                TargetPlatform::Ios => AppleSdk::Ios,
                TargetPlatform::IosSimulator => AppleSdk::IosSimulator,
                TargetPlatform::Macos => AppleSdk::Macos,
                TargetPlatform::Android
                | TargetPlatform::Linux
                | TargetPlatform::Windows
                | TargetPlatform::Esp32s3
                | TargetPlatform::Esp32c3
                | TargetPlatform::Esp32p4 => {
                    bail!("Internal error: Apple backend is not supported on {platform:?}");
                }
            };
            toolchain_checks::check_apple(sdk).await?;
        }
        TargetBackend::Android => {
            if platform != TargetPlatform::Android {
                bail!("Internal error: Android backend is not supported on {platform:?}");
            }
            let requested_abi = android_abi(arch.unwrap_or(TargetArch::Arm64));
            toolchain_checks::check_android_build_or_package_for_abis(&[requested_abi]).await?;
        }
        TargetBackend::Gtk4 => {
            if platform != TargetPlatform::Linux {
                bail!("Internal error: GTK4 backend is not supported on {platform:?}");
            }
            toolchain_checks::check_gtk4().await?;
        }
        TargetBackend::Hydrolysis => {
            if platform != TargetPlatform::Macos
                && platform != TargetPlatform::Linux
                && platform != TargetPlatform::Windows
            {
                bail!("Internal error: hydrolysis backend is not supported on {platform:?}");
            }
        }
        TargetBackend::Dew => {
            if platform.esp32_chip().is_none() {
                bail!("Internal error: dew backend is not supported on {platform:?}");
            }
        }
    }
    Ok(())
}

async fn build_for_apple(
    project: &Project,
    platform: TargetPlatform,
    arch: Option<TargetArch>,
    options: BuildOptions,
) -> Result<PathBuf> {
    match (platform, arch) {
        (TargetPlatform::Ios, None | Some(TargetArch::Arm64)) => {
            build_rust_lib(project, LibTargetPlatform::IOS, options).await
        }
        (TargetPlatform::Ios, Some(target_arch)) => {
            bail!(
                "iOS physical devices only support arm64, not {:?}",
                target_arch
            );
        }
        (TargetPlatform::IosSimulator, None | Some(TargetArch::Arm64 | TargetArch::X86_64)) => {
            build_rust_lib(project, LibTargetPlatform::IOSSimulator, options).await
        }
        (TargetPlatform::IosSimulator, Some(target_arch)) => {
            bail!(
                "iOS Simulator only supports arm64 or x86_64, not {:?}",
                target_arch
            );
        }
        (TargetPlatform::Macos, None | Some(TargetArch::Arm64 | TargetArch::X86_64)) => {
            build_rust_lib(project, LibTargetPlatform::MacOS, options).await
        }
        (TargetPlatform::Macos, Some(target_arch)) => {
            bail!("macOS only supports arm64 or x86_64, not {:?}", target_arch);
        }
        (
            TargetPlatform::Android
            | TargetPlatform::Linux
            | TargetPlatform::Windows
            | TargetPlatform::Esp32s3
            | TargetPlatform::Esp32c3
            | TargetPlatform::Esp32p4,
            _,
        ) => {
            bail!(
                "Internal error: invalid Apple backend platform {:?}",
                platform
            );
        }
    }
}

async fn build_for_android(
    project: &Project,
    arch: Option<TargetArch>,
    options: BuildOptions,
) -> Result<PathBuf> {
    let abi = android_abi(arch.unwrap_or(TargetArch::Arm64));
    AndroidPlatform::new(abi).build(project, options).await
}

fn validate_desktop_backend_platform_on_host(
    platform: TargetPlatform,
    backend: TargetBackend,
) -> Result<()> {
    match backend {
        TargetBackend::Gtk4 => {
            #[cfg(target_os = "linux")]
            {
                if platform != TargetPlatform::Linux {
                    bail!("GTK4 backend on Linux host requires --platform linux");
                }
            }

            #[cfg(not(target_os = "linux"))]
            {
                bail!("GTK4 backend is only supported on Linux hosts");
            }
        }
        TargetBackend::Hydrolysis => {
            #[cfg(target_os = "macos")]
            if platform != TargetPlatform::Macos {
                bail!("Hydrolysis backend on macOS host requires --platform macos");
            }

            #[cfg(target_os = "linux")]
            if platform != TargetPlatform::Linux {
                bail!("Hydrolysis backend on Linux host requires --platform linux");
            }

            #[cfg(target_os = "windows")]
            if platform != TargetPlatform::Windows {
                bail!("Hydrolysis backend on Windows host requires --platform windows");
            }

            #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
            bail!("Hydrolysis backend is only supported on macOS, Linux, or Windows hosts");
        }
        TargetBackend::Apple => {
            #[cfg(not(target_os = "macos"))]
            bail!("Apple backend requires a macOS host");
        }
        // The Dew/ESP32 firmware cross-compiles from any host with espup installed.
        TargetBackend::Android | TargetBackend::Dew => {}
    }

    Ok(())
}

const fn lib_platform(platform: TargetPlatform) -> LibTargetPlatform {
    match platform {
        TargetPlatform::Ios => LibTargetPlatform::IOS,
        TargetPlatform::IosSimulator => LibTargetPlatform::IOSSimulator,
        TargetPlatform::Android => LibTargetPlatform::Android,
        TargetPlatform::Macos => LibTargetPlatform::MacOS,
        TargetPlatform::Linux => LibTargetPlatform::Linux,
        TargetPlatform::Windows => LibTargetPlatform::Windows,
        TargetPlatform::Esp32s3 => LibTargetPlatform::Esp32S3,
        TargetPlatform::Esp32c3 => LibTargetPlatform::Esp32C3,
        TargetPlatform::Esp32p4 => LibTargetPlatform::Esp32P4,
    }
}

const fn android_abi(arch: TargetArch) -> AndroidAbi {
    match arch {
        TargetArch::Arm64 => AndroidAbi::Arm64V8a,
        TargetArch::X86_64 => AndroidAbi::X86_64,
        TargetArch::Armv7 => AndroidAbi::ArmeabiV7a,
        TargetArch::X86 => AndroidAbi::X86,
    }
}

const fn platform_name(platform: TargetPlatform) -> &'static str {
    match platform {
        TargetPlatform::Ios => "iOS",
        TargetPlatform::IosSimulator => "iOS Simulator",
        TargetPlatform::Android => "Android",
        TargetPlatform::Macos => "macOS",
        TargetPlatform::Linux => "Linux",
        TargetPlatform::Windows => "Windows",
        TargetPlatform::Esp32s3 => "ESP32-S3",
        TargetPlatform::Esp32c3 => "ESP32-C3",
        TargetPlatform::Esp32p4 => "ESP32-P4",
    }
}

const fn backend_name(backend: TargetBackend) -> &'static str {
    match backend {
        TargetBackend::Apple => "Apple",
        TargetBackend::Android => "Android",
        TargetBackend::Gtk4 => "GTK4",
        TargetBackend::Hydrolysis => "Hydrolysis",
        TargetBackend::Dew => "Dew",
    }
}

const fn apple_target_triple_override(
    platform: TargetPlatform,
    arch: Option<TargetArch>,
) -> Option<Triple> {
    match (platform, arch) {
        (TargetPlatform::Macos, Some(TargetArch::Arm64)) => Some(Triple {
            architecture: Architecture::Aarch64(Aarch64Architecture::Aarch64),
            vendor: Vendor::Apple,
            operating_system: OperatingSystem::Darwin(None),
            environment: Environment::Unknown,
            binary_format: BinaryFormat::Macho,
        }),
        (TargetPlatform::Macos, Some(TargetArch::X86_64)) => Some(Triple {
            architecture: Architecture::X86_64,
            vendor: Vendor::Apple,
            operating_system: OperatingSystem::Darwin(None),
            environment: Environment::Unknown,
            binary_format: BinaryFormat::Macho,
        }),
        (TargetPlatform::IosSimulator, Some(TargetArch::Arm64)) => Some(Triple {
            architecture: Architecture::Aarch64(Aarch64Architecture::Aarch64),
            vendor: Vendor::Apple,
            operating_system: OperatingSystem::IOS(None),
            environment: Environment::Sim,
            binary_format: BinaryFormat::Macho,
        }),
        (TargetPlatform::IosSimulator, Some(TargetArch::X86_64)) => Some(Triple {
            architecture: Architecture::X86_64,
            vendor: Vendor::Apple,
            operating_system: OperatingSystem::IOS(None),
            environment: Environment::Unknown,
            binary_format: BinaryFormat::Macho,
        }),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{TargetBackend, TargetPlatform, resolve_backend, validate_output_dir_args};

    #[test]
    fn resolve_backend_defaults_match_platforms() {
        assert_eq!(
            resolve_backend(TargetPlatform::Ios, None).expect("ios backend"),
            TargetBackend::Apple
        );
        assert_eq!(
            resolve_backend(TargetPlatform::Android, None).expect("android backend"),
            TargetBackend::Android
        );
        assert_eq!(
            resolve_backend(TargetPlatform::Linux, None).expect("linux backend"),
            TargetBackend::Gtk4
        );
        assert_eq!(
            resolve_backend(TargetPlatform::Windows, None).expect("windows backend"),
            TargetBackend::Hydrolysis
        );
    }

    #[test]
    fn output_dir_rejected_for_desktop_backends() {
        let output = Some(&std::path::PathBuf::from("/tmp/out"));
        assert!(validate_output_dir_args(TargetBackend::Gtk4, output).is_err());
        assert!(validate_output_dir_args(TargetBackend::Hydrolysis, output).is_err());
        assert!(validate_output_dir_args(TargetBackend::Apple, output).is_ok());
    }
}