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