cargo_gazelle/
platforms.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::sync::Arc;
11
12use guppy::platform::{Platform, PlatformSpec, TargetFeatures, Triple};
13use std::sync::LazyLock;
14
15use crate::ToBazelDefinition;
16
17static AARCH64_MAC_OS: LazyLock<PlatformSpec> = LazyLock::new(|| {
18    let triple = Triple::new("aarch64-apple-darwin").unwrap();
19    PlatformSpec::Platform(Arc::new(Platform::from_triple(triple, TargetFeatures::All)))
20});
21static X86_64_MAC_OS: LazyLock<PlatformSpec> = LazyLock::new(|| {
22    let triple = Triple::new("x86_64-apple-darwin").unwrap();
23    PlatformSpec::Platform(Arc::new(Platform::from_triple(triple, TargetFeatures::All)))
24});
25static AARCH64_LINUX_GNU: LazyLock<PlatformSpec> = LazyLock::new(|| {
26    let triple = Triple::new("aarch64-unknown-linux-gnu").unwrap();
27    PlatformSpec::Platform(Arc::new(Platform::from_triple(triple, TargetFeatures::All)))
28});
29static X86_64_LINUX_GNU: LazyLock<PlatformSpec> = LazyLock::new(|| {
30    let triple = Triple::new("x86_64-unknown-linux-gnu").unwrap();
31    PlatformSpec::Platform(Arc::new(Platform::from_triple(triple, TargetFeatures::All)))
32});
33
34/// Defines the various platforms we support building for.
35#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub enum PlatformVariant {
37    /// `aarch64-apple-darwin`
38    Aarch64MacOS,
39    /// `x86_64-apple-darwin`
40    X86_64MacOS,
41    /// `aarch64-unknown-linux-gnu`
42    Aarch64Linux,
43    /// `x86_64-unknown-linux-gnu`
44    X86_64Linux,
45}
46
47impl PlatformVariant {
48    const ALL: &'static [PlatformVariant] = &[
49        PlatformVariant::Aarch64MacOS,
50        PlatformVariant::X86_64MacOS,
51        PlatformVariant::Aarch64Linux,
52        PlatformVariant::X86_64Linux,
53    ];
54
55    pub fn spec(&self) -> &PlatformSpec {
56        match self {
57            PlatformVariant::Aarch64MacOS => &*AARCH64_MAC_OS,
58            PlatformVariant::X86_64MacOS => &*X86_64_MAC_OS,
59            PlatformVariant::Aarch64Linux => &*AARCH64_LINUX_GNU,
60            PlatformVariant::X86_64Linux => &*X86_64_LINUX_GNU,
61        }
62    }
63
64    pub fn all() -> &'static [PlatformVariant] {
65        Self::ALL
66    }
67}
68
69impl ToBazelDefinition for PlatformVariant {
70    fn format(&self, writer: &mut dyn std::fmt::Write) -> Result<(), std::fmt::Error> {
71        use PlatformVariant::*;
72
73        // These names are defined in our own platform spec in `/misc/bazel/platforms`.
74        let s = match self {
75            Aarch64MacOS => "@//misc/bazel/platforms:macos_arm",
76            X86_64MacOS => "@//misc/bazel/platforms:macos_x86_64",
77            Aarch64Linux => "@//misc/bazel/platforms:linux_arm",
78            X86_64Linux => "@//misc/bazel/platforms:linux_x86_64",
79        };
80
81        write!(writer, "\"{s}\"")
82    }
83}