alien-core 3.3.12

Deploy software into your customers' cloud accounts and keep it fully managed
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
//! Cross-compilation build target types
//!
//! These types identify target OS/architecture combinations used by the open-source
//! build system (alien-build) for cross-compilation.

use serde::{Deserialize, Serialize};
#[cfg(feature = "openapi")]
use utoipa::ToSchema;

/// Build strategy for cross-compilation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CargoBuildStrategy {
    /// Plain `cargo build` — native toolchain, no cross-compilation.
    Native,
    /// `cargo zigbuild` — uses Zig as the C toolchain for Linux musl targets.
    Zigbuild,
    /// `cargo xwin build` — uses xwin to provide MSVC CRT/SDK for Windows targets from non-Windows hosts.
    Xwin,
}

/// Operating system and architecture of the machine running a build.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuildHost {
    /// Linux x86-64.
    LinuxX64,
    /// Linux ARM64.
    LinuxArm64,
    /// Windows x86-64.
    WindowsX64,
    /// macOS ARM64.
    DarwinArm64,
    /// A host Alien does not support. This includes Darwin x64 and Windows ARM64, for which
    /// Alien does not publish native runtime targets.
    Unsupported,
}

impl BuildHost {
    /// Detect the build host without coercing unknown machines into a supported target.
    pub fn current() -> Self {
        match (std::env::consts::OS, std::env::consts::ARCH) {
            ("linux", "x86_64") => Self::LinuxX64,
            ("linux", "aarch64") => Self::LinuxArm64,
            ("windows", "x86_64") => Self::WindowsX64,
            ("macos", "aarch64") => Self::DarwinArm64,
            _ => Self::Unsupported,
        }
    }

    /// Human-readable host identifier for diagnostics and cache keys.
    pub fn id(self) -> &'static str {
        match self {
            Self::LinuxX64 => "linux-x64",
            Self::LinuxArm64 => "linux-arm64",
            Self::WindowsX64 => "windows-x64",
            Self::DarwinArm64 => "darwin-arm64",
            Self::Unsupported => "unsupported",
        }
    }
}

impl CargoBuildStrategy {
    /// The cargo subcommand to use (e.g. "build", "zigbuild", "xwin").
    pub fn cargo_subcommand(&self) -> &'static str {
        match self {
            Self::Native => "build",
            Self::Zigbuild => "zigbuild",
            Self::Xwin => "xwin",
        }
    }

    /// Cargo args: for xwin, the subcommand is `cargo xwin build` (two args).
    pub fn cargo_args(&self) -> Vec<&'static str> {
        match self {
            Self::Native => vec!["build"],
            Self::Zigbuild => vec!["zigbuild"],
            Self::Xwin => vec!["xwin", "build"],
        }
    }

    /// Human-readable name for logging.
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Native => "cargo build",
            Self::Zigbuild => "cargo zigbuild",
            Self::Xwin => "cargo xwin build",
        }
    }

    /// The cargo tool binary to install (None for native builds).
    pub fn install_package(&self) -> Option<&'static str> {
        match self {
            Self::Native => None,
            Self::Zigbuild => Some("cargo-zigbuild"),
            Self::Xwin => Some("cargo-xwin"),
        }
    }
}

/// Types of source binaries used for package building
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "kebab-case")]
pub enum SourceBinaryType {
    /// alien-deploy binary
    Cli,
    /// alien-terraform binary
    Terraform,
    /// alien-operator binary
    Operator,
}

impl SourceBinaryType {
    /// Returns the binary filename (without extension)
    pub fn binary_name(&self) -> &'static str {
        match self {
            SourceBinaryType::Cli => "alien-deploy",
            SourceBinaryType::Terraform => "alien-terraform",
            SourceBinaryType::Operator => "alien-operator",
        }
    }
}

impl std::fmt::Display for SourceBinaryType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SourceBinaryType::Cli => write!(f, "cli"),
            SourceBinaryType::Terraform => write!(f, "terraform"),
            SourceBinaryType::Operator => write!(f, "operator"),
        }
    }
}

/// Target OS and architecture for compiled binaries.
///
/// Used as keys in package output maps (CLI binaries, Terraform providers, etc.)
/// and for cross-compilation target selection during builds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "kebab-case")]
pub enum BinaryTarget {
    /// Windows x64 (x86_64-pc-windows-msvc)
    WindowsX64,
    /// Linux x86_64 (musl)
    LinuxX64,
    /// Linux ARM64 (musl)
    LinuxArm64,
    /// macOS ARM64 (Apple Silicon)
    DarwinArm64,
}

impl BinaryTarget {
    /// All supported binary targets
    pub const ALL: &'static [BinaryTarget] = &[
        BinaryTarget::WindowsX64,
        BinaryTarget::LinuxX64,
        BinaryTarget::LinuxArm64,
        BinaryTarget::DarwinArm64,
    ];

    /// Linux-only targets (for container/operator builds)
    pub const LINUX: &'static [BinaryTarget] = &[BinaryTarget::LinuxX64, BinaryTarget::LinuxArm64];

    /// Get the Rust target triple for this platform
    pub fn rust_target_triple(&self) -> &'static str {
        match self {
            Self::WindowsX64 => "x86_64-pc-windows-msvc",
            Self::LinuxX64 => "x86_64-unknown-linux-musl",
            Self::LinuxArm64 => "aarch64-unknown-linux-musl",
            Self::DarwinArm64 => "aarch64-apple-darwin",
        }
    }

    /// Returns the cargo subcommand and tool name needed to build for this target.
    ///
    /// - Linux musl on a matching Linux host: `cargo build` (native musl toolchain)
    /// - Linux musl from another host or architecture: `cargo zigbuild`
    /// - Windows MSVC from non-Windows: `cargo xwin build` (xwin provides MSVC CRT/SDK)
    /// - Windows MSVC on Windows: `cargo build` (native MSVC toolchain)
    /// - macOS on macOS: `cargo build` (native Apple toolchain)
    pub fn cargo_build_strategy(&self) -> CargoBuildStrategy {
        self.cargo_build_strategy_for(BuildHost::current())
    }

    /// Select the cheapest correct Rust compiler for an explicit build host.
    pub fn cargo_build_strategy_for(&self, host: BuildHost) -> CargoBuildStrategy {
        match (self, host) {
            (Self::LinuxX64, BuildHost::LinuxX64)
            | (Self::LinuxArm64, BuildHost::LinuxArm64)
            | (Self::WindowsX64, BuildHost::WindowsX64)
            | (Self::DarwinArm64, BuildHost::DarwinArm64) => CargoBuildStrategy::Native,
            (Self::WindowsX64, _) => CargoBuildStrategy::Xwin,
            (Self::LinuxX64 | Self::LinuxArm64, _) => CargoBuildStrategy::Zigbuild,
            // The Rust target alone is insufficient off macOS: an Apple SDK is also required.
            (Self::DarwinArm64, _) => CargoBuildStrategy::Native,
        }
    }

    /// Get the binary extension for this platform
    pub fn binary_extension(&self) -> &'static str {
        match self {
            Self::WindowsX64 => ".exe",
            _ => "",
        }
    }

    /// Get the platform identifier for runtime downloads (e.g., "linux-x64")
    pub fn runtime_platform_id(&self) -> &'static str {
        match self {
            Self::WindowsX64 => "windows-x64",
            Self::LinuxX64 => "linux-x64",
            Self::LinuxArm64 => "linux-aarch64",
            Self::DarwinArm64 => "darwin-aarch64",
        }
    }

    /// Inverse of [`runtime_platform_id`] — the `.oci.tar` filename spelling
    /// (`linux-aarch64`/`darwin-aarch64`), not the `linux-arm64`/`darwin-arm64` CLI names.
    pub fn from_runtime_platform_id(id: &str) -> Option<Self> {
        match id {
            "windows-x64" => Some(Self::WindowsX64),
            "linux-x64" => Some(Self::LinuxX64),
            "linux-aarch64" => Some(Self::LinuxArm64),
            "darwin-aarch64" => Some(Self::DarwinArm64),
            _ => None,
        }
    }

    /// Get the OCI os string for this target
    pub fn oci_os(&self) -> &'static str {
        match self {
            Self::WindowsX64 => "windows",
            Self::LinuxX64 | Self::LinuxArm64 => "linux",
            Self::DarwinArm64 => "darwin",
        }
    }

    /// Get the OCI architecture string for this target
    pub fn oci_arch(&self) -> &'static str {
        match self {
            Self::WindowsX64 | Self::LinuxX64 => "amd64",
            Self::LinuxArm64 | Self::DarwinArm64 => "arm64",
        }
    }

    /// Get the Bun cross-compilation target for `bun build --compile --target`
    pub fn bun_target(&self) -> &'static str {
        match self {
            Self::WindowsX64 => "bun-windows-x64",
            Self::LinuxX64 => "bun-linux-x64",
            Self::LinuxArm64 => "bun-linux-arm64",
            Self::DarwinArm64 => "bun-darwin-arm64",
        }
    }

    /// Terraform registry platform key (os_arch format)
    pub fn terraform_key(&self) -> &'static str {
        match self {
            BinaryTarget::LinuxX64 => "linux_amd64",
            BinaryTarget::LinuxArm64 => "linux_arm64",
            BinaryTarget::DarwinArm64 => "darwin_arm64",
            BinaryTarget::WindowsX64 => "windows_amd64",
        }
    }

    /// Terraform OS string
    pub fn terraform_os(&self) -> &'static str {
        match self {
            BinaryTarget::LinuxX64 | BinaryTarget::LinuxArm64 => "linux",
            BinaryTarget::DarwinArm64 => "darwin",
            BinaryTarget::WindowsX64 => "windows",
        }
    }

    /// Terraform architecture string
    pub fn terraform_arch(&self) -> &'static str {
        match self {
            BinaryTarget::LinuxX64 | BinaryTarget::WindowsX64 => "amd64",
            BinaryTarget::LinuxArm64 | BinaryTarget::DarwinArm64 => "arm64",
        }
    }

    /// Check if this target is a Darwin/macOS target
    pub fn is_darwin(&self) -> bool {
        matches!(self, Self::DarwinArm64)
    }

    /// Check if this is a Windows target
    pub fn is_windows(&self) -> bool {
        matches!(self, Self::WindowsX64)
    }

    /// Get the Linux container target matching the current host architecture.
    /// Containers always run Linux (even on macOS via Docker's Linux VM),
    /// so we map the host architecture to the corresponding Linux target.
    pub fn linux_container_target() -> Self {
        match Self::current_os() {
            Self::DarwinArm64 | Self::LinuxArm64 => Self::LinuxArm64,
            Self::LinuxX64 | Self::WindowsX64 => Self::LinuxX64,
        }
    }

    /// Get all possible targets as a Vec
    pub fn all() -> Vec<Self> {
        Self::ALL.to_vec()
    }

    /// Get default targets for a platform
    pub fn defaults_for_platform(platform: crate::Platform) -> Vec<Self> {
        match platform {
            crate::Platform::Aws => vec![Self::LinuxArm64],
            crate::Platform::Gcp => vec![Self::LinuxX64],
            crate::Platform::Azure => vec![Self::LinuxX64],
            crate::Platform::Kubernetes => Self::LINUX.to_vec(),
            crate::Platform::Machines => Self::LINUX.to_vec(),
            crate::Platform::Local => vec![Self::current_os()],
            crate::Platform::Test => vec![Self::LinuxX64],
        }
    }

    /// Detect the current OS target
    pub fn current_os() -> Self {
        #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
        return Self::WindowsX64;

        #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
        return Self::LinuxX64;

        #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
        return Self::LinuxArm64;

        #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
        return Self::DarwinArm64;

        #[cfg(not(any(
            all(target_os = "windows", target_arch = "x86_64"),
            all(target_os = "linux", target_arch = "x86_64"),
            all(target_os = "linux", target_arch = "aarch64"),
            all(target_os = "macos", target_arch = "aarch64")
        )))]
        {
            panic!("Alien does not support native binaries on this host")
        }
    }
}

impl std::fmt::Display for BinaryTarget {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BinaryTarget::WindowsX64 => write!(f, "windows-x64"),
            BinaryTarget::LinuxX64 => write!(f, "linux-x64"),
            BinaryTarget::LinuxArm64 => write!(f, "linux-arm64"),
            BinaryTarget::DarwinArm64 => write!(f, "darwin-arm64"),
        }
    }
}

impl std::str::FromStr for BinaryTarget {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "windows-x64" => Ok(BinaryTarget::WindowsX64),
            "linux-x64" => Ok(BinaryTarget::LinuxX64),
            "linux-arm64" => Ok(BinaryTarget::LinuxArm64),
            "darwin-arm64" => Ok(BinaryTarget::DarwinArm64),
            _ => Err(format!("Unknown binary target: {}", s)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{BinaryTarget, BuildHost, CargoBuildStrategy};
    use crate::Platform;

    #[test]
    fn local_platform_defaults_to_current_host_target() {
        assert_eq!(
            BinaryTarget::defaults_for_platform(Platform::Local),
            vec![BinaryTarget::current_os()]
        );
    }

    #[test]
    fn runtime_platform_id_round_trips() {
        for target in BinaryTarget::ALL {
            assert_eq!(
                BinaryTarget::from_runtime_platform_id(target.runtime_platform_id()),
                Some(*target),
                "round-trip failed for {target}"
            );
        }
        // The tarball spelling differs from the CLI/FromStr spelling.
        assert_eq!(
            BinaryTarget::from_runtime_platform_id("linux-aarch64"),
            Some(BinaryTarget::LinuxArm64)
        );
        assert_eq!(BinaryTarget::from_runtime_platform_id("linux-arm64"), None);
        assert_eq!(BinaryTarget::from_runtime_platform_id("nonsense"), None);
    }

    #[test]
    fn cloud_platform_defaults_remain_stable() {
        assert_eq!(
            BinaryTarget::defaults_for_platform(Platform::Aws),
            vec![BinaryTarget::LinuxArm64]
        );
        assert_eq!(
            BinaryTarget::defaults_for_platform(Platform::Gcp),
            vec![BinaryTarget::LinuxX64]
        );
        assert_eq!(
            BinaryTarget::defaults_for_platform(Platform::Azure),
            vec![BinaryTarget::LinuxX64]
        );
        assert_eq!(
            BinaryTarget::defaults_for_platform(Platform::Kubernetes),
            vec![BinaryTarget::LinuxX64, BinaryTarget::LinuxArm64]
        );
    }

    #[test]
    fn cargo_build_strategy_uses_native_tools_only_on_compatible_hosts() {
        assert_eq!(
            BinaryTarget::LinuxX64.cargo_build_strategy_for(BuildHost::LinuxX64),
            CargoBuildStrategy::Native
        );
        assert_eq!(
            BinaryTarget::LinuxArm64.cargo_build_strategy_for(BuildHost::LinuxArm64),
            CargoBuildStrategy::Native
        );
        assert_eq!(
            BinaryTarget::LinuxArm64.cargo_build_strategy_for(BuildHost::LinuxX64),
            CargoBuildStrategy::Zigbuild
        );
        assert_eq!(
            BinaryTarget::WindowsX64.cargo_build_strategy_for(BuildHost::WindowsX64),
            CargoBuildStrategy::Native
        );
        assert_eq!(
            BinaryTarget::WindowsX64.cargo_build_strategy_for(BuildHost::DarwinArm64),
            CargoBuildStrategy::Xwin
        );
        assert_eq!(
            BinaryTarget::DarwinArm64.cargo_build_strategy_for(BuildHost::DarwinArm64),
            CargoBuildStrategy::Native
        );
    }
}