rustferry-android 0.1.0

Direct no-Gradle Android packaging backend for RustFerry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
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
use std::{collections::BTreeSet, env, fs, path::PathBuf};

use camino::{Utf8Path, Utf8PathBuf};
use rustferry_core::{AndroidAbi, AndroidConfig};
use serde::{Deserialize, Serialize};

use crate::AndroidError;

/// Explicit discovery inputs. [`Default`] reads only conventional path environment variables.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DiscoveryOptions {
    /// Preferred Android SDK root.
    pub sdk_root: Option<Utf8PathBuf>,
    /// Preferred Android NDK root.
    pub ndk_root: Option<Utf8PathBuf>,
    /// Preferred Java home.
    pub java_home: Option<Utf8PathBuf>,
    /// Directories searched for host executables.
    pub executable_search_paths: Vec<Utf8PathBuf>,
    /// Home directory used for OS-standard SDK locations.
    pub home_dir: Option<Utf8PathBuf>,
    /// NDK prebuilt host tag override, useful for deterministic tests.
    pub host_tag: Option<String>,
}

impl DiscoveryOptions {
    /// Capture standard Android, Java, `PATH`, and home path variables.
    pub fn from_environment() -> Self {
        let sdk_root = env_path("ANDROID_SDK_ROOT").or_else(|| env_path("ANDROID_HOME"));
        let ndk_root = env_path("ANDROID_NDK_HOME").or_else(|| env_path("ANDROID_NDK_ROOT"));
        let java_home = env_path("JAVA_HOME");
        let executable_search_paths = env::var_os("PATH")
            .map(|value| {
                env::split_paths(&value)
                    .filter_map(|path| Utf8PathBuf::from_path_buf(path).ok())
                    .collect()
            })
            .unwrap_or_default();
        Self {
            sdk_root,
            ndk_root,
            java_home,
            executable_search_paths,
            home_dir: env_path(home_variable()),
            host_tag: None,
        }
    }
}

impl Default for DiscoveryOptions {
    fn default() -> Self {
        Self::from_environment()
    }
}

/// One installed Android SDK platform.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AndroidPlatform {
    /// Owning SDK root.
    pub sdk_root: Utf8PathBuf,
    /// Numeric API level.
    pub api_level: u32,
    /// Platform directory.
    pub directory: Utf8PathBuf,
    /// Framework resource JAR used by AAPT2 and D8.
    pub android_jar: Utf8PathBuf,
}

/// One Android SDK Build Tools installation.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AndroidBuildTools {
    /// Owning SDK root.
    pub sdk_root: Utf8PathBuf,
    /// SDK directory version.
    pub version: String,
    /// Build Tools directory.
    pub directory: Utf8PathBuf,
    /// Android Asset Packaging Tool 2.
    pub aapt2: Option<Utf8PathBuf>,
    /// D8 bytecode compiler.
    pub d8: Option<Utf8PathBuf>,
    /// ZIP alignment tool.
    pub zipalign: Option<Utf8PathBuf>,
    /// APK signing tool.
    pub apksigner: Option<Utf8PathBuf>,
}

impl AndroidBuildTools {
    /// Whether all direct-pipeline executables are present.
    pub fn is_complete(&self) -> bool {
        self.aapt2.is_some()
            && self.d8.is_some()
            && self.zipalign.is_some()
            && self.apksigner.is_some()
    }
}

/// One Android NDK installation and its LLVM host toolchain.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AndroidNdk {
    /// NDK directory.
    pub root: Utf8PathBuf,
    /// NDK revision from `source.properties`, when readable.
    pub version: String,
    /// Selected `toolchains/llvm/prebuilt/<host>` directory.
    pub llvm_prebuilt: Option<Utf8PathBuf>,
}

impl AndroidNdk {
    /// Resolve the NDK Clang driver for one ABI and minimum API level.
    ///
    /// # Errors
    ///
    /// Returns an error when this NDK has no host prebuilt or matching Clang driver.
    pub fn linker_for(&self, abi: AndroidAbi, min_sdk: u32) -> Result<Utf8PathBuf, AndroidError> {
        let prebuilt = self
            .llvm_prebuilt
            .as_ref()
            .ok_or_else(|| AndroidError::ToolMissing {
                tool: "Android NDK LLVM host prebuilt".to_owned(),
                searched: vec![self.root.join("toolchains/llvm/prebuilt")],
                fix: "Install an NDK package matching this host.".to_owned(),
            })?;
        let stem = match abi {
            AndroidAbi::Arm64V8a => "aarch64-linux-android",
            AndroidAbi::X86_64 => "x86_64-linux-android",
            AndroidAbi::ArmeabiV7a => "armv7a-linux-androideabi",
        };
        let candidate = prebuilt
            .join("bin")
            .join(format!("{stem}{min_sdk}-clang{}", ndk_clang_suffix()));
        if candidate.is_file() {
            Ok(candidate)
        } else {
            Err(AndroidError::NdkLinkerMissing {
                target: abi.rust_target().to_owned(),
                searched: vec![candidate],
                fix: format!(
                    "Install a complete NDK supporting API {min_sdk}, then run `cargo ferry doctor`."
                ),
            })
        }
    }

    /// Resolve this NDK's LLVM archiver.
    ///
    /// # Errors
    ///
    /// Returns an error when this NDK lacks its host prebuilt or `llvm-ar`.
    pub fn llvm_ar(&self) -> Result<Utf8PathBuf, AndroidError> {
        let prebuilt = self
            .llvm_prebuilt
            .as_ref()
            .ok_or_else(|| AndroidError::ToolMissing {
                tool: "Android NDK LLVM host prebuilt".to_owned(),
                searched: vec![self.root.join("toolchains/llvm/prebuilt")],
                fix: "Install an NDK package matching this host.".to_owned(),
            })?;
        let candidate = prebuilt
            .join("bin")
            .join(format!("llvm-ar{}", executable_suffix()));
        if candidate.is_file() {
            Ok(candidate)
        } else {
            Err(AndroidError::ToolMissing {
                tool: "NDK llvm-ar".to_owned(),
                searched: vec![candidate],
                fix: "Reinstall the selected Android NDK, then run `cargo ferry doctor`."
                    .to_owned(),
            })
        }
    }
}

/// Optional host and SDK helper executables found during discovery.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct AndroidHostTools {
    /// Cargo executable.
    pub cargo: Option<Utf8PathBuf>,
    /// Rust compiler.
    pub rustc: Option<Utf8PathBuf>,
    /// Rustup executable.
    pub rustup: Option<Utf8PathBuf>,
    /// Java runtime.
    pub java: Option<Utf8PathBuf>,
    /// Java compiler.
    pub javac: Option<Utf8PathBuf>,
    /// Java key store utility.
    pub keytool: Option<Utf8PathBuf>,
    /// Android SDK manager.
    pub sdkmanager: Option<Utf8PathBuf>,
    /// Android Debug Bridge; not required to build.
    pub adb: Option<Utf8PathBuf>,
    /// Android emulator; not required to build.
    pub emulator: Option<Utf8PathBuf>,
}

/// Complete read-only inventory of Android toolchain candidates.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AndroidDiscovery {
    /// SDK roots inspected.
    pub sdk_roots_searched: Vec<Utf8PathBuf>,
    /// NDK roots inspected.
    pub ndk_roots_searched: Vec<Utf8PathBuf>,
    /// Installed SDK platforms.
    pub platforms: Vec<AndroidPlatform>,
    /// Installed SDK Build Tools, including incomplete directories for diagnostics.
    pub build_tools: Vec<AndroidBuildTools>,
    /// Installed NDKs.
    pub ndks: Vec<AndroidNdk>,
    /// Host and optional SDK tools.
    pub host_tools: AndroidHostTools,
}

impl AndroidDiscovery {
    /// Select a complete direct-build toolchain for a validated Android configuration.
    ///
    /// # Errors
    ///
    /// Returns a typed error naming any missing platform, Build Tools, NDK, Cargo, or key tool.
    pub fn select_toolchain(
        &self,
        config: &AndroidConfig,
    ) -> Result<AndroidToolchain, AndroidError> {
        let platform = select_platform(&self.platforms, &config.target_sdk)?;
        if platform.api_level < config.min_sdk {
            return Err(AndroidError::InvalidRequest(format!(
                "target SDK {} is below min SDK {}",
                platform.api_level, config.min_sdk
            )));
        }
        let build_tools = self
            .build_tools
            .iter()
            .filter(|tools| tools.sdk_root == platform.sdk_root && tools.is_complete())
            .max_by(|left, right| compare_versions(&left.version, &right.version))
            .cloned()
            .ok_or_else(|| AndroidError::BuildToolsMissing {
                searched: self
                    .build_tools
                    .iter()
                    .map(|tools| tools.directory.clone())
                    .chain(
                        self.sdk_roots_searched
                            .iter()
                            .map(|root| root.join("build-tools")),
                    )
                    .collect(),
                fix: "Install Android SDK Build Tools with `sdkmanager \"build-tools;<version>\"`, then run `cargo ferry doctor`.".to_owned(),
            })?;
        let ndk = self
            .ndks
            .iter()
            .filter(|ndk| ndk.llvm_prebuilt.is_some())
            .max_by(|left, right| compare_versions(&left.version, &right.version))
            .cloned()
            .ok_or_else(|| AndroidError::ToolMissing {
                tool: "Android NDK LLVM toolchain".to_owned(),
                searched: self.ndk_roots_searched.clone(),
                fix: "Install an NDK with `sdkmanager \"ndk;<version>\"`, then run `cargo ferry doctor`.".to_owned(),
            })?;
        let cargo = require_host_tool(
            "cargo",
            self.host_tools.cargo.as_ref(),
            &[],
            "Install Rust through rustup, then run `cargo ferry doctor`.",
        )?;
        let keytool = require_host_tool(
            "keytool",
            self.host_tools.keytool.as_ref(),
            &[],
            "Install a JDK and set JAVA_HOME, then run `cargo ferry doctor`.",
        )?;
        Ok(AndroidToolchain {
            sdk_root: platform.sdk_root.clone(),
            platform,
            build_tools,
            ndk,
            cargo,
            rustc: self.host_tools.rustc.clone(),
            rustup: self.host_tools.rustup.clone(),
            java: self.host_tools.java.clone(),
            javac: self.host_tools.javac.clone(),
            keytool,
        })
    }
}

/// Selected tools used by one direct Android build.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AndroidToolchain {
    /// Android SDK root.
    pub sdk_root: Utf8PathBuf,
    /// Selected platform.
    pub platform: AndroidPlatform,
    /// Selected complete Build Tools.
    pub build_tools: AndroidBuildTools,
    /// Selected NDK.
    pub ndk: AndroidNdk,
    /// Cargo executable.
    pub cargo: Utf8PathBuf,
    /// Optional Rust compiler for doctor reporting.
    pub rustc: Option<Utf8PathBuf>,
    /// Optional rustup executable for target checks.
    pub rustup: Option<Utf8PathBuf>,
    /// Optional Java runtime.
    pub java: Option<Utf8PathBuf>,
    /// Optional Java compiler.
    pub javac: Option<Utf8PathBuf>,
    /// Key store utility.
    pub keytool: Utf8PathBuf,
}

impl AndroidToolchain {
    /// Resolve the NDK Clang driver for one ABI and minimum API level.
    ///
    /// # Errors
    ///
    /// Returns an error when the selected NDK has no host prebuilt or matching Clang driver.
    pub fn linker_for(&self, abi: AndroidAbi, min_sdk: u32) -> Result<Utf8PathBuf, AndroidError> {
        self.ndk.linker_for(abi, min_sdk)
    }

    /// Resolve the NDK LLVM archiver.
    ///
    /// # Errors
    ///
    /// Returns an error when the selected NDK lacks its host prebuilt or `llvm-ar`.
    pub fn llvm_ar(&self) -> Result<Utf8PathBuf, AndroidError> {
        self.ndk.llvm_ar()
    }
}

/// Inspect configured and conventional Android SDK/NDK locations without changing the system.
pub fn discover_android(options: &DiscoveryOptions) -> AndroidDiscovery {
    let sdk_roots_searched = sdk_candidates(options);
    let mut platforms = Vec::new();
    let mut build_tools = Vec::new();
    for root in &sdk_roots_searched {
        platforms.extend(discover_platforms(root));
        build_tools.extend(discover_build_tools(root));
    }
    platforms.sort_by_key(|platform| platform.api_level);
    build_tools.sort_by(|left, right| compare_versions(&left.version, &right.version));

    let ndk_roots_searched = ndk_candidates(options, &sdk_roots_searched);
    let mut ndks = ndk_roots_searched
        .iter()
        .filter(|path| path.is_dir())
        .map(|root| discover_ndk(root, options.host_tag.as_deref()))
        .collect::<Vec<_>>();
    ndks.sort_by(|left, right| compare_versions(&left.version, &right.version));

    let sdk_root = sdk_roots_searched
        .iter()
        .find(|root| root.is_dir())
        .cloned();
    AndroidDiscovery {
        sdk_roots_searched,
        ndk_roots_searched,
        platforms,
        build_tools,
        ndks,
        host_tools: discover_host_tools(options, sdk_root.as_ref()),
    }
}

fn sdk_candidates(options: &DiscoveryOptions) -> Vec<Utf8PathBuf> {
    let mut paths = Vec::new();
    if let Some(root) = &options.sdk_root {
        paths.push(root.clone());
    }
    if let Some(home) = &options.home_dir {
        #[cfg(target_os = "macos")]
        paths.push(home.join("Library/Android/sdk"));
        #[cfg(target_os = "linux")]
        paths.push(home.join("Android/Sdk"));
        #[cfg(target_os = "windows")]
        paths.push(home.join("AppData/Local/Android/Sdk"));
    }
    deduplicate(paths)
}

fn ndk_candidates(options: &DiscoveryOptions, sdk_roots: &[Utf8PathBuf]) -> Vec<Utf8PathBuf> {
    let mut paths = Vec::new();
    if let Some(root) = &options.ndk_root {
        paths.push(root.clone());
    }
    for sdk in sdk_roots {
        let versioned = sdk.join("ndk");
        paths.extend(read_directories(&versioned));
        paths.push(sdk.join("ndk-bundle"));
    }
    deduplicate(paths)
}

fn discover_platforms(sdk_root: &Utf8Path) -> Vec<AndroidPlatform> {
    read_directories(&sdk_root.join("platforms"))
        .into_iter()
        .filter_map(|directory| {
            let api_level = directory
                .file_name()?
                .strip_prefix("android-")?
                .parse()
                .ok()?;
            let android_jar = directory.join("android.jar");
            android_jar.is_file().then(|| AndroidPlatform {
                sdk_root: sdk_root.to_owned(),
                api_level,
                directory,
                android_jar,
            })
        })
        .collect()
}

fn discover_build_tools(sdk_root: &Utf8Path) -> Vec<AndroidBuildTools> {
    read_directories(&sdk_root.join("build-tools"))
        .into_iter()
        .filter_map(|directory| {
            let version = directory.file_name()?.to_owned();
            Some(AndroidBuildTools {
                sdk_root: sdk_root.to_owned(),
                version,
                aapt2: executable_in(&directory, "aapt2"),
                d8: script_or_executable_in(&directory, "d8"),
                zipalign: executable_in(&directory, "zipalign"),
                apksigner: script_or_executable_in(&directory, "apksigner"),
                directory,
            })
        })
        .collect()
}

fn discover_ndk(root: &Utf8Path, requested_host: Option<&str>) -> AndroidNdk {
    let version = fs::read_to_string(root.join("source.properties"))
        .ok()
        .and_then(|source| {
            source.lines().find_map(|line| {
                let (key, value) = line.split_once('=')?;
                (key.trim() == "Pkg.Revision").then(|| value.trim().to_owned())
            })
        })
        .or_else(|| root.file_name().map(ToOwned::to_owned))
        .unwrap_or_else(|| "unknown".to_owned());
    let prebuilts = read_directories(&root.join("toolchains/llvm/prebuilt"));
    let llvm_prebuilt = requested_host
        .and_then(|host| {
            prebuilts
                .iter()
                .find(|path| path.file_name() == Some(host))
                .cloned()
        })
        .or_else(|| {
            let prefix = host_prefix();
            prebuilts
                .iter()
                .find(|path| {
                    path.file_name()
                        .is_some_and(|name| name.starts_with(prefix))
                })
                .cloned()
        });
    AndroidNdk {
        root: root.to_owned(),
        version,
        llvm_prebuilt,
    }
}

fn discover_host_tools(
    options: &DiscoveryOptions,
    sdk_root: Option<&Utf8PathBuf>,
) -> AndroidHostTools {
    let java_bin = options.java_home.as_ref().map(|home| home.join("bin"));
    let find_java = |name: &str| {
        java_bin
            .as_ref()
            .and_then(|directory| executable_in(directory, name))
            .or_else(|| find_on_path(name, &options.executable_search_paths))
    };
    let sdkmanager = sdk_root
        .and_then(|root| find_sdkmanager(root))
        .or_else(|| find_on_path("sdkmanager", &options.executable_search_paths));
    AndroidHostTools {
        cargo: find_on_path("cargo", &options.executable_search_paths),
        rustc: find_on_path("rustc", &options.executable_search_paths),
        rustup: find_on_path("rustup", &options.executable_search_paths),
        java: find_java("java"),
        javac: find_java("javac"),
        keytool: find_java("keytool"),
        sdkmanager,
        adb: sdk_root.and_then(|root| executable_in(&root.join("platform-tools"), "adb")),
        emulator: sdk_root.and_then(|root| executable_in(&root.join("emulator"), "emulator")),
    }
}

fn find_sdkmanager(root: &Utf8Path) -> Option<Utf8PathBuf> {
    let command_line_tools = root.join("cmdline-tools");
    let mut candidates = read_directories(&command_line_tools);
    candidates.sort_by(|left, right| {
        compare_versions(
            left.file_name().unwrap_or_default(),
            right.file_name().unwrap_or_default(),
        )
    });
    candidates
        .into_iter()
        .rev()
        .find_map(|directory| script_or_executable_in(&directory.join("bin"), "sdkmanager"))
        .or_else(|| script_or_executable_in(&root.join("tools/bin"), "sdkmanager"))
}

fn select_platform(
    platforms: &[AndroidPlatform],
    requested: &str,
) -> Result<AndroidPlatform, AndroidError> {
    let selected = if requested == "installed" {
        platforms.iter().max_by_key(|platform| platform.api_level)
    } else {
        let api = requested
            .strip_prefix("android-")
            .unwrap_or(requested)
            .parse::<u32>()
            .map_err(|_| {
                AndroidError::InvalidRequest(format!(
                    "android.target_sdk must be `installed`, an API number, or `android-<number>`; got `{requested}`"
                ))
            })?;
        platforms.iter().find(|platform| platform.api_level == api)
    };
    selected.cloned().ok_or_else(|| AndroidError::PlatformMissing {
        requested: requested.to_owned(),
        installed: platforms.iter().map(|platform| platform.api_level).collect(),
        fix: format!(
            "Install it with `sdkmanager \"platforms;android-{}\"`, then run `cargo ferry doctor`.",
            requested.strip_prefix("android-").unwrap_or(requested)
        ),
    })
}

fn require_host_tool(
    name: &str,
    value: Option<&Utf8PathBuf>,
    searched: &[Utf8PathBuf],
    fix: &str,
) -> Result<Utf8PathBuf, AndroidError> {
    value.cloned().ok_or_else(|| AndroidError::ToolMissing {
        tool: name.to_owned(),
        searched: searched.to_vec(),
        fix: fix.to_owned(),
    })
}

fn read_directories(root: &Utf8Path) -> Vec<Utf8PathBuf> {
    let Ok(entries) = fs::read_dir(root) else {
        return Vec::new();
    };
    let mut paths = entries
        .filter_map(Result::ok)
        .filter_map(|entry| Utf8PathBuf::from_path_buf(entry.path()).ok())
        .filter(|path| path.is_dir())
        .collect::<Vec<_>>();
    paths.sort();
    paths
}

fn find_on_path(name: &str, paths: &[Utf8PathBuf]) -> Option<Utf8PathBuf> {
    paths
        .iter()
        .find_map(|directory| script_or_executable_in(directory, name))
}

fn executable_in(directory: &Utf8Path, name: &str) -> Option<Utf8PathBuf> {
    let candidate = directory.join(format!("{name}{}", executable_suffix()));
    candidate.is_file().then_some(candidate)
}

fn script_or_executable_in(directory: &Utf8Path, name: &str) -> Option<Utf8PathBuf> {
    executable_in(directory, name).or_else(|| {
        #[cfg(target_os = "windows")]
        {
            let candidate = directory.join(format!("{name}.bat"));
            candidate.is_file().then_some(candidate)
        }
        #[cfg(not(target_os = "windows"))]
        {
            let candidate = directory.join(name);
            candidate.is_file().then_some(candidate)
        }
    })
}

fn compare_versions(left: &str, right: &str) -> std::cmp::Ordering {
    numeric_version(left)
        .cmp(&numeric_version(right))
        .then_with(|| left.cmp(right))
}

fn numeric_version(value: &str) -> Vec<u64> {
    value
        .split(|character: char| !character.is_ascii_digit())
        .filter(|part| !part.is_empty())
        .filter_map(|part| part.parse().ok())
        .collect()
}

fn deduplicate(paths: Vec<Utf8PathBuf>) -> Vec<Utf8PathBuf> {
    let mut seen = BTreeSet::new();
    paths
        .into_iter()
        .filter(|path| seen.insert(path.clone()))
        .collect()
}

fn env_path(name: &str) -> Option<Utf8PathBuf> {
    env::var_os(name).and_then(|value| Utf8PathBuf::from_path_buf(PathBuf::from(value)).ok())
}

#[cfg(target_os = "windows")]
const fn home_variable() -> &'static str {
    "USERPROFILE"
}

#[cfg(not(target_os = "windows"))]
const fn home_variable() -> &'static str {
    "HOME"
}

#[cfg(target_os = "windows")]
const fn executable_suffix() -> &'static str {
    ".exe"
}

#[cfg(not(target_os = "windows"))]
const fn executable_suffix() -> &'static str {
    ""
}

#[cfg(target_os = "windows")]
const fn ndk_clang_suffix() -> &'static str {
    ".cmd"
}

#[cfg(not(target_os = "windows"))]
const fn ndk_clang_suffix() -> &'static str {
    ""
}

#[cfg(target_os = "macos")]
const fn host_prefix() -> &'static str {
    "darwin-"
}

#[cfg(target_os = "linux")]
const fn host_prefix() -> &'static str {
    "linux-"
}

#[cfg(target_os = "windows")]
const fn host_prefix() -> &'static str {
    "windows-"
}

#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
const fn host_prefix() -> &'static str {
    ""
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;

    fn touch(path: &Utf8Path) {
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, b"").unwrap();
    }

    #[test]
    fn finds_latest_complete_sdk_and_ndk() {
        let temp = TempDir::new().unwrap();
        let root = Utf8PathBuf::from_path_buf(temp.path().to_owned()).unwrap();
        for api in [34, 35] {
            touch(&root.join(format!("platforms/android-{api}/android.jar")));
        }
        for version in ["34.0.0", "35.0.0"] {
            let directory = root.join("build-tools").join(version);
            for tool in ["aapt2", "d8", "zipalign", "apksigner"] {
                touch(&directory.join(format!("{tool}{}", executable_suffix())));
            }
        }
        let ndk = root.join("ndk/27.1.0");
        touch(&ndk.join(format!(
            "toolchains/llvm/prebuilt/test-host/bin/llvm-ar{}",
            executable_suffix()
        )));
        touch(&ndk.join(format!(
            "toolchains/llvm/prebuilt/test-host/bin/aarch64-linux-android26-clang{}",
            ndk_clang_suffix()
        )));
        fs::write(ndk.join("source.properties"), "Pkg.Revision = 27.1.0\n").unwrap();
        let bin = root.join("host-bin");
        for tool in ["cargo", "rustc", "rustup", "java", "javac", "keytool"] {
            touch(&bin.join(format!("{tool}{}", executable_suffix())));
        }
        let options = DiscoveryOptions {
            sdk_root: Some(root.clone()),
            ndk_root: Some(ndk),
            java_home: None,
            executable_search_paths: vec![bin],
            home_dir: None,
            host_tag: Some("test-host".to_owned()),
        };
        let discovery = discover_android(&options);
        let toolchain = discovery
            .select_toolchain(&AndroidConfig::default())
            .unwrap();
        assert_eq!(toolchain.platform.api_level, 35);
        assert_eq!(toolchain.build_tools.version, "35.0.0");
        let expected_linker = format!("aarch64-linux-android26-clang{}", ndk_clang_suffix());
        assert_eq!(
            toolchain
                .linker_for(AndroidAbi::Arm64V8a, 26)
                .unwrap()
                .file_name(),
            Some(expected_linker.as_str())
        );
    }

    #[test]
    fn incomplete_newer_build_tools_are_skipped() {
        let mut discovery = AndroidDiscovery {
            sdk_roots_searched: vec![Utf8PathBuf::from("/sdk")],
            ndk_roots_searched: vec![],
            platforms: vec![AndroidPlatform {
                sdk_root: "/sdk".into(),
                api_level: 35,
                directory: "/sdk/platforms/android-35".into(),
                android_jar: "/sdk/platforms/android-35/android.jar".into(),
            }],
            build_tools: vec![],
            ndks: vec![],
            host_tools: AndroidHostTools::default(),
        };
        let complete = AndroidBuildTools {
            sdk_root: "/sdk".into(),
            version: "34.0.0".into(),
            directory: "/sdk/build-tools/34.0.0".into(),
            aapt2: Some("/aapt2".into()),
            d8: Some("/d8".into()),
            zipalign: Some("/zipalign".into()),
            apksigner: Some("/apksigner".into()),
        };
        let mut incomplete = complete.clone();
        incomplete.version = "35.0.0".into();
        incomplete.d8 = None;
        discovery.build_tools = vec![complete, incomplete];
        assert!(discovery.build_tools[0].is_complete());
        assert!(!discovery.build_tools[1].is_complete());
    }
}