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