Maintain/Build/GetTauriTargetTriple.rs
1//=============================================================================//
2// File Path: Element/Maintain/Source/Build/GetTauriTargetTriple.rs
3//=============================================================================//
4// Module: GetTauriTargetTriple
5//
6// Brief Description: Returns the Tauri-compatible target triple for the current build environment.
7//
8// RESPONSIBILITIES:
9// ================
10//
11// Primary:
12// - Determine the current OS and architecture
13// - Map to Tauri-compatible target triple strings
14// - Support all major platforms (Windows, Linux, macOS)
15//
16// Secondary:
17// - None
18//
19// ARCHITECTURAL ROLE:
20// ===================
21//
22// Position:
23// - Infrastructure/Utility layer
24// - Platform detection utilities
25//
26// Dependencies (What this module requires):
27// - External crates: std (env)
28// - Internal modules: None
29// - Traits implemented: None
30//
31// Dependents (What depends on this module):
32// - Build orchestration functions
33// - Sidecar bundling logic
34// - Node.js version handling
35//
36// IMPLEMENTATION DETAILS:
37// =======================
38//
39// Design Patterns:
40// - Platform detection pattern
41// - String mapping pattern
42//
43// Performance Considerations:
44// - Complexity: O(1) - simple pattern matching
45// - Memory usage patterns: Allocates a new String for the target triple
46// - Hot path optimizations: None needed
47//
48// Thread Safety:
49// - Thread-safe: Yes (pure function with immutable input from env::consts)
50// - Synchronization mechanisms used: None
51// - Interior mutability considerations: None
52//
53// Error Handling:
54// - Error types returned: None (panics on unsupported platforms)
55// - Recovery strategies: Not applicable (runtime panic indicates build error)
56//
57// EXAMPLES:
58// =========
59//
60// Example 1: Getting target triple on macOS
61/// ```rust
62/// use crate::Maintain::Source::Build::GetTauriTargetTriple;
63/// #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
64/// let triple = GetTauriTargetTriple();
65/// assert_eq!(triple, "x86_64-apple-darwin");
66/// ```
67//
68// Example 2: Getting target triple on Windows
69/// ```rust
70/// use crate::Maintain::Source::Build::GetTauriTargetTriple;
71/// #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
72/// let triple = GetTauriTargetTriple();
73/// assert_eq!(triple, "x86_64-pc-windows-msvc");
74/// ```
75//
76// Example 3: Building sidecar path
77/// ```rust
78/// use crate::Maintain::Source::Build::GetTauriTargetTriple;
79/// let triple = GetTauriTargetTriple();
80/// let path = format!("./Element/SideCar/{}/NODE/22", triple);
81/// ```
82//
83//=============================================================================//
84// IMPLEMENTATION
85//=============================================================================//
86
87use std::env;
88
89/// Gets the Tauri-compatible target triple for the current build environment.
90///
91/// This function determines the appropriate target triple string based on the
92/// current operating system and CPU architecture. Target triples are used to
93/// identify platform binaries and ensure compatibility with Tauri's bundling
94/// system.
95///
96/// # Target Triple Format
97///
98/// Target triples follow the format: `{arch}-{vendor}-{os}-{environment}`
99///
100/// Supported combinations:
101/// - Windows x86_64: `x86_64-pc-windows-msvc`
102/// - Linux x86_64: `x86_64-unknown-linux-gnu`
103/// - Linux aarch64: `aarch64-unknown-linux-gnu`
104/// - macOS x86_64: `x86_64-apple-darwin`
105/// - macOS aarch64 (Apple Silicon): `aarch64-apple-darwin`
106///
107/// # Returns
108///
109/// A string containing the target triple for the current platform.
110///
111/// # Panics
112///
113/// This function will panic if the current platform is not supported. The
114/// panic message indicates the unsupported OS-Arch combination and the
115/// build system should be extended to support new platforms as needed.
116///
117/// # Platform Detection
118///
119/// The function uses `std::env::consts` to detect:
120/// - `OS` - The operating system (windows, linux, macos, etc.)
121/// - `ARCH` - The CPU architecture (x86_64, aarch64, etc.)
122///
123/// These are compile-time constants determined when the Rust compiler builds
124/// the binary, so this function always returns consistent results for a
125/// given build.
126///
127/// # Usage Example
128///
129/// Constructing a path to a Node.js sidecar binary:
130/// ```no_run
131/// # use std::path::PathBuf;
132/// # fn example() {
133/// use crate::Maintain::Source::Build::GetTauriTargetTriple;
134///
135/// let triple = GetTauriTargetTriple();
136/// let version = "22";
137///
138/// # #[cfg(target_os = "windows")]
139/// let node_path = PathBuf::from(format!(
140/// "./Element/SideCar/{}/NODE/{}/node.exe",
141/// triple, version
142/// ));
143/// # #[cfg(not(target_os = "windows"))]
144/// # let node_path = PathBuf::from(format!(
145/// # "./Element/SideCar/{}/NODE/{}/bin/node",
146/// # triple, version
147/// # ));
148///
149/// println!("Node.js path: {:?}", node_path);
150/// # }
151/// ```
152///
153/// # Platform Support
154///
155/// Currently supported platforms:
156///
157/// | OS | Architecture | Target Triple |
158/// |---|---|---|
159/// | Windows | x86_64 | `x86_64-pc-windows-msvc` |
160/// | Linux | x86_64 | `x86_64-unknown-linux-gnu` |
161/// | Linux | aarch64 | `aarch64-unknown-linux-gnu` |
162/// | macOS | x86_64 | `x86_64-apple-darwin` |
163/// | macOS | aarch64 | `aarch64-apple-darwin` |
164///
165/// To add support for a new platform:
166/// 1. Add a match arm for the OS-Arch combination
167/// 2. Return the appropriate target triple string
168/// 3. Ensure Node.js binaries are available at the corresponding paths
169pub fn GetTauriTargetTriple() -> String {
170 let Os = env::consts::OS;
171
172 let Arch = env::consts::ARCH;
173
174 match (Os, Arch) {
175 ("windows", "x86_64") => "x86_64-pc-windows-msvc".to_string(),
176
177 ("linux", "x86_64") => "x86_64-unknown-linux-gnu".to_string(),
178
179 ("linux", "aarch64") => "aarch64-unknown-linux-gnu".to_string(),
180
181 ("macos", "x86_64") => "x86_64-apple-darwin".to_string(),
182
183 ("macos", "aarch64") => "aarch64-apple-darwin".to_string(),
184
185 _ => panic!("Unsupported OS-Arch for sidecar: {}-{}", Os, Arch),
186 }
187}
188
189#[cfg(test)]
190mod tests {
191 use super::*;
192
193 #[test]
194 fn test_known_platforms() {
195 // We can't fully test this without a cross-compilation environment,
196 // but we can verify the function returns a valid-looking string
197 let triple = GetTauriTargetTriple();
198
199 // All valid target triples should contain hyphens and be lowercase alphabetic/numeric
200 let valid_chars = |c: char| c.is_alphanumeric() || c == '-' || c == '_';
201 assert!(triple.chars().all(valid_chars));
202
203 // Should contain at least two hyphens (arch-vendor-os or arch-vendor-os-env)
204 assert!(triple.matches('-').count() >= 2);
205 }
206}