| 
 | 1 | +use std::collections::{BTreeMap, BTreeSet};  | 
 | 2 | +use std::str::FromStr;  | 
 | 3 | + | 
 | 4 | +use clap::builder::PossibleValue;  | 
 | 5 | +use clap::{Args, Subcommand};  | 
 | 6 | +use cross::docker::{self, CROSS_CUSTOM_DOCKERFILE_IMAGE_PREFIX};  | 
 | 7 | +use cross::shell::MessageInfo;  | 
 | 8 | +use cross::{CommandExt, TargetList};  | 
 | 9 | + | 
 | 10 | +// known image prefixes, with their registry  | 
 | 11 | +// the docker.io registry can also be implicit  | 
 | 12 | +const GHCR_IO: &str = docker::CROSS_IMAGE;  | 
 | 13 | +const RUST_EMBEDDED: &str = "rustembedded/cross";  | 
 | 14 | +const DOCKER_IO: &str = "docker.io/rustembedded/cross";  | 
 | 15 | +const IMAGE_PREFIXES: &[&str] = &[GHCR_IO, DOCKER_IO, RUST_EMBEDDED];  | 
 | 16 | + | 
 | 17 | +#[derive(Args, Debug)]  | 
 | 18 | +pub struct ListTargets {  | 
 | 19 | +    /// Format version  | 
 | 20 | +    #[clap(long)]  | 
 | 21 | +    format_version: Option<FormatVersion>,  | 
 | 22 | +    /// Coloring: auto, always, never  | 
 | 23 | +    #[clap(long)]  | 
 | 24 | +    pub color: Option<String>,  | 
 | 25 | +}  | 
 | 26 | + | 
 | 27 | +#[derive(Debug, Clone, Copy, serde::Serialize)]  | 
 | 28 | +pub enum FormatVersion {  | 
 | 29 | +    #[serde(rename = "1")]  | 
 | 30 | +    One,  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +#[derive(Debug, thiserror::Error)]  | 
 | 34 | +#[error("invalid format version")]  | 
 | 35 | +pub struct FormatVersionError;  | 
 | 36 | + | 
 | 37 | +impl FromStr for FormatVersion {  | 
 | 38 | +    type Err = FormatVersionError;  | 
 | 39 | + | 
 | 40 | +    fn from_str(s: &str) -> Result<Self, Self::Err> {  | 
 | 41 | +        match s {  | 
 | 42 | +            "1" => Ok(FormatVersion::One),  | 
 | 43 | +            _ => Err(FormatVersionError),  | 
 | 44 | +        }  | 
 | 45 | +    }  | 
 | 46 | +}  | 
 | 47 | + | 
 | 48 | +#[derive(serde::Serialize)]  | 
 | 49 | +pub struct Output {  | 
 | 50 | +    format_version: FormatVersion,  | 
 | 51 | +    #[serde(flatten)]  | 
 | 52 | +    other: serde_json::Value,  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +impl ListTargets {  | 
 | 56 | +    pub fn verbose(&self) -> bool {  | 
 | 57 | +        false  | 
 | 58 | +    }  | 
 | 59 | + | 
 | 60 | +    pub fn quiet(&self) -> bool {  | 
 | 61 | +        false  | 
 | 62 | +    }  | 
 | 63 | + | 
 | 64 | +    pub fn color(&self) -> Option<&str> {  | 
 | 65 | +        self.color.as_deref()  | 
 | 66 | +    }  | 
 | 67 | + | 
 | 68 | +    pub fn run(self, msg_info: &mut MessageInfo) -> cross::Result<()> {  | 
 | 69 | +        let toml = if let Some(metadata) = cross::cargo_metadata_with_args(None, None, msg_info)? {  | 
 | 70 | +            cross::toml(&metadata, msg_info)?  | 
 | 71 | +        } else {  | 
 | 72 | +            None  | 
 | 73 | +        };  | 
 | 74 | + | 
 | 75 | +        let config = cross::config::Config::new(toml);  | 
 | 76 | +        let version = if let Some(version) = self.format_version {  | 
 | 77 | +            version  | 
 | 78 | +        } else {  | 
 | 79 | +            msg_info.warn(  | 
 | 80 | +                "please specify `--format-version` flag explicitly to avoid compatibility problems",  | 
 | 81 | +            )?;  | 
 | 82 | +            FormatVersion::One  | 
 | 83 | +        };  | 
 | 84 | +        let data = match version {  | 
 | 85 | +            FormatVersion::One => self.run_v1(&config, msg_info)?,  | 
 | 86 | +        };  | 
 | 87 | +        println!(  | 
 | 88 | +            "{}",  | 
 | 89 | +            serde_json::to_string_pretty(&Output {  | 
 | 90 | +                format_version: version,  | 
 | 91 | +                other: data,  | 
 | 92 | +            })?  | 
 | 93 | +        );  | 
 | 94 | +        Ok(())  | 
 | 95 | +    }  | 
 | 96 | + | 
 | 97 | +    pub fn run_v1(  | 
 | 98 | +        self,  | 
 | 99 | +        config: &cross::config::Config,  | 
 | 100 | +        _msg_info: &mut MessageInfo,  | 
 | 101 | +    ) -> cross::Result<serde_json::Value> {  | 
 | 102 | +        #[derive(serde::Serialize)]  | 
 | 103 | +        struct Target {  | 
 | 104 | +            triplet: String,  | 
 | 105 | +            platforms: Vec<String>,  | 
 | 106 | +        }  | 
 | 107 | +        let mut targets: Vec<_> = cross::docker::PROVIDED_IMAGES  | 
 | 108 | +            .iter()  | 
 | 109 | +            .filter_map(|i| {  | 
 | 110 | +                Some(Target {  | 
 | 111 | +                    triplet: Some(i.name).filter(|i| *i != "zig")?.to_owned(),  | 
 | 112 | +                    platforms: i.platforms.iter().map(ToString::to_string).collect(),  | 
 | 113 | +                })  | 
 | 114 | +            })  | 
 | 115 | +            .collect();  | 
 | 116 | +        if let Some(toml_targets) = config.targets() {  | 
 | 117 | +            for (target, config) in toml_targets {  | 
 | 118 | +                if targets.iter().any(|t| t.triplet == target.triple()) {  | 
 | 119 | +                    continue;  | 
 | 120 | +                }  | 
 | 121 | +                targets.push(Target {  | 
 | 122 | +                    triplet: target.triple().to_owned(),  | 
 | 123 | +                    platforms: config  | 
 | 124 | +                        .image  | 
 | 125 | +                        .as_ref()  | 
 | 126 | +                        .map(|i| {  | 
 | 127 | +                            i.toolchain  | 
 | 128 | +                                .iter()  | 
 | 129 | +                                .map(ToString::to_string)  | 
 | 130 | +                                .collect::<Vec<_>>()  | 
 | 131 | +                        })  | 
 | 132 | +                        .filter(|v| !v.is_empty())  | 
 | 133 | +                        .unwrap_or_else(|| vec![docker::ImagePlatform::DEFAULT.to_string()]),  | 
 | 134 | +                })  | 
 | 135 | +            }  | 
 | 136 | +        }  | 
 | 137 | +        Ok(serde_json::json!({  | 
 | 138 | +            "targets": targets,  | 
 | 139 | +        }))  | 
 | 140 | +    }  | 
 | 141 | +}  | 
0 commit comments