Skip to main content

euv_cli/platform/
impl.rs

1use super::*;
2
3/// Canonical string helpers for `TargetCategory`.
4impl TargetCategory {
5    /// Returns the canonical lowercase label for this ecosystem category.
6    ///
7    /// # Returns
8    ///
9    /// - `&'static str` - Lowercase category string.
10    #[must_use]
11    pub const fn as_str(self) -> &'static str {
12        match self {
13            Self::Web => CATEGORY_WEB,
14            Self::Desktop => CATEGORY_DESKTOP,
15            Self::Mobile => CATEGORY_MOBILE,
16            Self::Terminal => CATEGORY_TERMINAL,
17            Self::Harmony => CATEGORY_HARMONY,
18            Self::Wearable => CATEGORY_WEARABLE,
19            Self::MiniApp => CATEGORY_MINIAPP,
20            Self::QuickApp => CATEGORY_QUICKAPP,
21        }
22    }
23}
24
25/// Host capability detection and per-target readiness evaluation on `HostCapabilityProbe`.
26impl HostCapabilityProbe {
27    /// Detects the active host OS, installed Rust targets, SDK environment variables, and external build tools.
28    ///
29    /// # Returns
30    ///
31    /// - `Self` - Populated host capability snapshot.
32    #[must_use]
33    pub fn detect() -> Self {
34        let host_os: String = env::consts::OS.to_string();
35        let host_arch: String = env::consts::ARCH.to_string();
36        let installed_triples: Vec<String> = probe_installed_rust_targets();
37        let mut env_sdks: BTreeMap<String, String> = BTreeMap::new();
38        if let Some(android_home) = resolve_first_env(&[ENV_ANDROID_HOME, ENV_ANDROID_SDK_ROOT]) {
39            let _: Option<String> = env_sdks.insert(ENV_ANDROID_HOME.to_string(), android_home);
40        }
41        let discovered_ndk: Option<String> =
42            resolve_first_env(&[ENV_ANDROID_NDK, ENV_ANDROID_NDK_LATEST_HOME]).or_else(|| {
43                env_sdks
44                    .get(ENV_ANDROID_HOME)
45                    .and_then(|sdk: &String| discover_android_ndk_under_sdk(sdk))
46            });
47        if let Some(android_ndk) = discovered_ndk {
48            let _: Option<String> = env_sdks.insert(ENV_ANDROID_NDK.to_string(), android_ndk);
49        }
50        if let Some(ohos_sdk) = resolve_first_env(&[ENV_OHOS_SDK, ENV_DEVECO_SDK]) {
51            let _: Option<String> = env_sdks.insert(ENV_OHOS_SDK.to_string(), ohos_sdk);
52        }
53        let mut available_tools: BTreeMap<String, String> = BTreeMap::new();
54        record_probe_tool(&mut available_tools, BIN_RUSTC, &[ARG_VERSION]);
55        record_probe_tool(&mut available_tools, BIN_CARGO, &[ARG_VERSION]);
56        record_probe_tool(&mut available_tools, BIN_RUSTUP, &[ARG_VERSION]);
57        record_probe_tool(&mut available_tools, BIN_WASM_BINDGEN, &[ARG_VERSION]);
58        record_probe_tool(&mut available_tools, BIN_WASM_OPT, &[ARG_VERSION]);
59        record_probe_tool(&mut available_tools, BIN_WASM_PACK, &[ARG_VERSION]);
60        record_probe_tool(&mut available_tools, BIN_CARGO_NDK, &[ARG_VERSION]);
61        record_probe_tool(&mut available_tools, BIN_JAVA, &[ARG_SHORT_VERSION]);
62        record_probe_tool(&mut available_tools, BIN_JAVAC, &[ARG_SHORT_VERSION]);
63        record_probe_tool(&mut available_tools, BIN_XCRUN, &[ARG_SHOW_SDK]);
64        record_probe_tool(&mut available_tools, BIN_XCODEBUILD, &[ARG_SHORT_VERSION]);
65        record_probe_tool(&mut available_tools, BIN_CODESIGN, &[ARG_VERSION]);
66        record_probe_tool(&mut available_tools, BIN_HVIGORW, &[ARG_VERSION]);
67        record_probe_tool(&mut available_tools, BIN_HDC, &[ARG_VERSION]);
68        record_probe_tool(&mut available_tools, BIN_CRATE_CLI, &[ARG_VERSION]);
69        record_probe_tool(&mut available_tools, BIN_NODE, &[ARG_VERSION]);
70        let simctl_out: String = probe_host_tool(BIN_XCRUN, &[ARG_SIMCTL, ARG_HELP]);
71        if simctl_out != STATUS_MISSING {
72            let _: Option<String> =
73                available_tools.insert(TOOL_KEY_SIMCTL.to_string(), STATUS_OK.to_string());
74        }
75        let android_home_ref: Option<&str> = env_sdks.get(ENV_ANDROID_HOME).map(String::as_str);
76        record_android_sdk_tool(
77            &mut available_tools,
78            BIN_ADB,
79            DIR_PLATFORM_TOOLS,
80            android_home_ref,
81        );
82        for bt_tool in [BIN_AAPT2, BIN_D8, BIN_ZIPALIGN, BIN_APKSIGNER].iter() {
83            record_android_sdk_tool(
84                &mut available_tools,
85                bt_tool,
86                DIR_BUILD_TOOLS,
87                android_home_ref,
88            );
89        }
90        Self::new(
91            host_os,
92            host_arch,
93            installed_triples,
94            available_tools,
95            env_sdks,
96        )
97    }
98
99    /// Returns `true` when `triple` is installed in this capability snapshot.
100    ///
101    /// # Arguments
102    ///
103    /// - `&str` - Rust compiler target triple.
104    ///
105    /// # Returns
106    ///
107    /// - `bool` - Whether the target triple is present.
108    #[must_use]
109    pub fn has_triple(&self, triple: &str) -> bool {
110        self.get_installed_triples()
111            .iter()
112            .any(|installed: &String| installed == triple)
113    }
114
115    /// Returns `true` when tool `name` was detected in this capability snapshot.
116    ///
117    /// # Arguments
118    ///
119    /// - `&str` - Tool key name.
120    ///
121    /// # Returns
122    ///
123    /// - `bool` - Whether the tool is available.
124    #[must_use]
125    pub fn has_tool(&self, name: &str) -> bool {
126        self.get_available_tools().contains_key(name)
127    }
128
129    /// Returns `true` when SDK environment key `name` was detected in this snapshot.
130    ///
131    /// # Arguments
132    ///
133    /// - `&str` - SDK key name (e.g. `ANDROID_NDK_HOME`).
134    ///
135    /// # Returns
136    ///
137    /// - `bool` - Whether the SDK path is configured.
138    #[must_use]
139    pub fn has_sdk(&self, name: &str) -> bool {
140        self.get_env_sdks().contains_key(name)
141    }
142
143    /// Evaluates build readiness, missing prerequisites, and install hints for one concrete `target`.
144    ///
145    /// # Arguments
146    ///
147    /// - `TargetArg` - Concrete platform target.
148    ///
149    /// # Returns
150    ///
151    /// - `TargetCapability` - Detailed readiness report for `target`.
152    #[must_use]
153    pub fn probe_target(&self, target: TargetArg) -> TargetCapability {
154        let concrete: TargetArg = if target == TargetArg::Native || target.is_group() {
155            host_native_target()
156        } else {
157            target
158        };
159        let spec: TargetSpec = spec_for_platform(concrete);
160        let triple: String = spec.get_rust_triple().clone();
161        let category: TargetCategory = category_for_target(concrete);
162        let host_os: &str = self.get_host_os().as_str();
163        let mut missing: Vec<String> = Vec::new();
164        let mut hints: Vec<String> = Vec::new();
165        let host_supported: bool = match concrete {
166            TargetArg::Macos
167            | TargetArg::Ios
168            | TargetArg::IosSim
169            | TargetArg::Ipad
170            | TargetArg::IpadSim
171            | TargetArg::Watchos => host_os == HOST_OS_MACOS,
172            TargetArg::Windows => host_os == HOST_OS_WINDOWS,
173            TargetArg::Linux => host_os == HOST_OS_LINUX,
174            _ => true,
175        };
176        if !host_supported {
177            match concrete {
178                TargetArg::Macos
179                | TargetArg::Ios
180                | TargetArg::IosSim
181                | TargetArg::Ipad
182                | TargetArg::IpadSim
183                | TargetArg::Watchos => {
184                    missing.push(HOST_OS_MACOS.to_string());
185                    hints.push(HINT_MACOS_HOST_ONLY.to_string());
186                }
187                TargetArg::Windows => {
188                    missing.push(HOST_OS_WINDOWS.to_string());
189                    hints.push(HINT_WINDOWS_HOST_ONLY.to_string());
190                }
191                TargetArg::Linux => {
192                    missing.push(HOST_OS_LINUX.to_string());
193                    hints.push(HINT_LINUX_HOST_ONLY.to_string());
194                }
195                _ => {}
196            }
197        }
198        if !self.has_triple(&triple) {
199            missing.push(triple.clone());
200            hints.push(format!("rustup target add {triple}"));
201        }
202        match concrete {
203            TargetArg::Web
204            | TargetArg::VivoWatch
205            | TargetArg::Zeppos
206            | TargetArg::Wechat
207            | TargetArg::Alipay
208            | TargetArg::Douyin
209            | TargetArg::Baidu
210            | TargetArg::Qq
211            | TargetArg::Kuaishou
212            | TargetArg::Jd
213            | TargetArg::Xhs
214            | TargetArg::Dingtalk
215            | TargetArg::Feishu
216            | TargetArg::Taobao
217            | TargetArg::Meituan
218            | TargetArg::Quickapp
219            | TargetArg::QuickappAlliance
220            | TargetArg::QuickappHuawei => {
221                if !self.has_tool(BIN_WASM_BINDGEN) && !self.has_tool(BIN_WASM_PACK) {
222                    missing.push(BIN_WASM_BINDGEN.to_string());
223                    hints.push(HINT_INSTALL_WASM_BINDGEN.to_string());
224                }
225            }
226            TargetArg::Android | TargetArg::Wearos | TargetArg::OppoWatch => {
227                if !self.has_sdk(ENV_ANDROID_NDK) {
228                    missing.push(ENV_ANDROID_NDK.to_string());
229                    hints.push(HINT_INSTALL_ANDROID_NDK.to_string());
230                }
231                let android_tools: [&str; 5] =
232                    [BIN_JAVAC, BIN_AAPT2, BIN_D8, BIN_ZIPALIGN, BIN_APKSIGNER];
233                let mut added_bt_hint: bool = false;
234                for tool in android_tools.iter() {
235                    if !self.has_tool(tool) {
236                        missing.push((*tool).to_string());
237                        if !added_bt_hint {
238                            hints.push(HINT_INSTALL_ANDROID_BUILD_TOOLS.to_string());
239                            added_bt_hint = true;
240                        }
241                    }
242                }
243            }
244            TargetArg::Ios
245            | TargetArg::IosSim
246            | TargetArg::Ipad
247            | TargetArg::IpadSim
248            | TargetArg::Watchos => {
249                if host_supported && !self.has_tool(BIN_XCRUN) {
250                    missing.push(BIN_XCRUN.to_string());
251                    hints.push(HINT_INSTALL_XCODE.to_string());
252                }
253            }
254            TargetArg::Harmony
255            | TargetArg::HarmonyPc
256            | TargetArg::HarmonyWearable
257            | TargetArg::HarmonyAtomic
258                if !self.has_sdk(ENV_OHOS_SDK) =>
259            {
260                missing.push(ENV_OHOS_SDK.to_string());
261                hints.push(HINT_INSTALL_OHOS_SDK.to_string());
262            }
263            _ => {}
264        }
265        let ready: bool = host_supported && missing.is_empty();
266        TargetCapability::new(
267            spec.get_name().clone(),
268            triple,
269            category.as_str().to_string(),
270            host_supported,
271            ready,
272            missing,
273            hints,
274        )
275    }
276
277    /// Evaluates readiness across a deduplicated list of requested targets and groups.
278    ///
279    /// # Arguments
280    ///
281    /// - `&[TargetArg]` - Requested targets or groups.
282    ///
283    /// # Returns
284    ///
285    /// - `Vec<TargetCapability>` - Per-target readiness reports in deterministic order.
286    #[must_use]
287    pub fn probe_targets(&self, targets: &[TargetArg]) -> Vec<TargetCapability> {
288        let concrete: Vec<TargetArg> = resolve_targets_dedup(targets);
289        concrete
290            .into_iter()
291            .map(|item: TargetArg| self.probe_target(item))
292            .collect()
293    }
294
295    /// Evaluates readiness across all 38 concrete euv targets (36 production + `ios-sim` / `ipad-sim`).
296    ///
297    /// # Returns
298    ///
299    /// - `Vec<TargetCapability>` - Complete readiness matrix.
300    #[must_use]
301    pub fn probe_all(&self) -> Vec<TargetCapability> {
302        let concrete: Vec<TargetArg> = resolve_all_concrete_targets();
303        concrete
304            .into_iter()
305            .map(|item: TargetArg| self.probe_target(item))
306            .collect()
307    }
308
309    /// Partitions requested targets into `(ready_targets, skipped_capabilities)` for `--skip-unavailable`.
310    ///
311    /// # Arguments
312    ///
313    /// - `&[TargetArg]` - Requested targets or target groups.
314    ///
315    /// # Returns
316    ///
317    /// - `(Vec<TargetArg>, Vec<TargetCapability>)` - Buildable targets paired with skipped capability reports.
318    #[must_use]
319    pub fn filter_buildable(
320        &self,
321        targets: &[TargetArg],
322    ) -> (Vec<TargetArg>, Vec<TargetCapability>) {
323        let concrete: Vec<TargetArg> = resolve_targets_dedup(targets);
324        let mut ready: Vec<TargetArg> = Vec::new();
325        let mut skipped: Vec<TargetCapability> = Vec::new();
326        for item in concrete.into_iter() {
327            let cap: TargetCapability = self.probe_target(item);
328            if cap.get_ready() {
329                ready.push(item);
330            } else {
331                skipped.push(cap);
332            }
333        }
334        (ready, skipped)
335    }
336
337    /// Returns deduplicated missing `rustup` target triples supported on this host OS for `euv doctor --fix`.
338    ///
339    /// # Returns
340    ///
341    /// - `Vec<String>` - Ordered list of installable missing target triples.
342    #[must_use]
343    pub fn missing_installable_triples(&self) -> Vec<String> {
344        let mut missing_triples: Vec<String> = Vec::new();
345        for cap in self.probe_all().into_iter() {
346            if !cap.get_host_supported() {
347                continue;
348            }
349            let triple: &String = cap.get_triple();
350            if !self.has_triple(triple) && !missing_triples.contains(triple) {
351                missing_triples.push(triple.clone());
352            }
353        }
354        missing_triples
355    }
356}