cross 0.2.5

Zero setup cross compilation and cross testing
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
use std::collections::{BTreeMap, BTreeSet};

use clap::{Args, Subcommand};
use cross::docker::{self, CROSS_CUSTOM_DOCKERFILE_IMAGE_PREFIX};
use cross::shell::MessageInfo;
use cross::{CommandExt, TargetList};

// known image prefixes, with their registry
// the docker.io registry can also be implicit
const GHCR_IO: &str = docker::CROSS_IMAGE;
const RUST_EMBEDDED: &str = "rustembedded/cross";
const DOCKER_IO: &str = "docker.io/rustembedded/cross";
const IMAGE_PREFIXES: &[&str] = &[GHCR_IO, DOCKER_IO, RUST_EMBEDDED];

#[derive(Args, Debug)]
pub struct ListImages {
    /// Provide verbose diagnostic output.
    #[clap(short, long)]
    pub verbose: bool,
    /// Do not print cross log messages.
    #[clap(short, long)]
    pub quiet: bool,
    /// Coloring: auto, always, never
    #[clap(long)]
    pub color: Option<String>,
    /// Container engine (such as docker or podman).
    #[clap(long)]
    pub engine: Option<String>,
    /// Only list images for specific target(s). By default, list all targets.
    pub targets: Vec<String>,
}

impl ListImages {
    pub fn run(self, engine: docker::Engine, msg_info: &mut MessageInfo) -> cross::Result<()> {
        list_images(self, &engine, msg_info)
    }
}

#[derive(Args, Debug)]
pub struct RemoveImages {
    /// If not provided, remove all images.
    pub targets: Vec<String>,
    /// Remove images matching provided targets.
    #[clap(short, long)]
    pub verbose: bool,
    /// Do not print cross log messages.
    #[clap(short, long)]
    pub quiet: bool,
    /// Coloring: auto, always, never
    #[clap(long)]
    pub color: Option<String>,
    /// Force removal of images.
    #[clap(short, long)]
    pub force: bool,
    /// Remove local (development) images.
    #[clap(short, long)]
    pub local: bool,
    /// Remove images. Default is a dry run.
    #[clap(short, long)]
    pub execute: bool,
    /// Container engine (such as docker or podman).
    #[clap(long)]
    pub engine: Option<String>,
}

impl RemoveImages {
    pub fn run(self, engine: docker::Engine, msg_info: &mut MessageInfo) -> cross::Result<()> {
        if self.targets.is_empty() {
            remove_all_images(self, &engine, msg_info)
        } else {
            remove_target_images(self, &engine, msg_info)
        }
    }
}

#[derive(Subcommand, Debug)]
pub enum Images {
    /// List cross images in local storage.
    List(ListImages),
    /// Remove cross images in local storage.
    Remove(RemoveImages),
}

impl Images {
    pub fn run(self, engine: docker::Engine, msg_info: &mut MessageInfo) -> cross::Result<()> {
        match self {
            Images::List(args) => args.run(engine, msg_info),
            Images::Remove(args) => args.run(engine, msg_info),
        }
    }

    pub fn engine(&self) -> Option<&str> {
        match self {
            Images::List(l) => l.engine.as_deref(),
            Images::Remove(l) => l.engine.as_deref(),
        }
    }

    pub fn verbose(&self) -> bool {
        match self {
            Images::List(l) => l.verbose,
            Images::Remove(l) => l.verbose,
        }
    }

    pub fn quiet(&self) -> bool {
        match self {
            Images::List(l) => l.quiet,
            Images::Remove(l) => l.quiet,
        }
    }

    pub fn color(&self) -> Option<&str> {
        match self {
            Images::List(l) => l.color.as_deref(),
            Images::Remove(l) => l.color.as_deref(),
        }
    }
}

#[derive(Debug, PartialOrd, Ord, PartialEq, Eq)]
struct Image {
    repository: String,
    tag: String,
    // need to remove images by ID, not just tag
    id: String,
}

impl std::fmt::Display for Image {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.repository == "<none>" {
            f.write_str(&self.id)
        } else {
            f.write_str(&self.name())
        }
    }
}

impl Image {
    fn name(&self) -> String {
        format!("{}:{}", self.repository, self.tag)
    }
}

fn parse_image(image: &str) -> Image {
    // this cannot panic: we've formatted our image list as `${repo}:${tag} ${id}`
    let (repository, rest) = image.split_once(':').unwrap();
    let (tag, id) = rest.split_once(' ').unwrap();
    Image {
        repository: repository.to_string(),
        tag: tag.to_string(),
        id: id.to_string(),
    }
}

fn is_cross_image(repository: &str) -> bool {
    IMAGE_PREFIXES.iter().any(|i| repository.starts_with(i))
}

fn is_local_image(tag: &str) -> bool {
    tag.starts_with("local")
}

fn get_cross_images(
    engine: &docker::Engine,
    msg_info: &mut MessageInfo,
    local: bool,
) -> cross::Result<Vec<Image>> {
    let mut images: BTreeSet<_> = cross::docker::subcommand(engine, "images")
        .args(&["--format", "{{.Repository}}:{{.Tag}} {{.ID}}"])
        .args(&[
            "--filter",
            &format!("label={}.for-cross-target", cross::CROSS_LABEL_DOMAIN),
        ])
        .run_and_get_stdout(msg_info)?
        .lines()
        .map(parse_image)
        .collect();

    let stdout = cross::docker::subcommand(engine, "images")
        .args(&["--format", "{{.Repository}}:{{.Tag}} {{.ID}}"])
        .run_and_get_stdout(msg_info)?;
    let ids: Vec<_> = images.iter().map(|i| i.id.to_string()).collect();
    images.extend(
        stdout
            .lines()
            .map(parse_image)
            .filter(|i| !ids.iter().any(|id| id == &i.id))
            .filter(|image| is_cross_image(&image.repository))
            .filter(|image| local || !is_local_image(&image.tag)),
    );

    Ok(images.into_iter().collect())
}

// the old rustembedded targets had the following format:
//  repository = (${registry}/)?rustembedded/cross
//  tag = ${target}(-${version})?
// the last component must match `[A-Za-z0-9_-]` and
// we must have at least 3 components. the first component
// may contain other characters, such as `thumbv8m.main-none-eabi`.
fn rustembedded_target(tag: &str) -> String {
    let is_target_char = |c: char| c == '_' || c.is_ascii_alphanumeric();
    let mut components = vec![];
    for (index, component) in tag.split('-').enumerate() {
        if index <= 2 || (!component.is_empty() && component.chars().all(is_target_char)) {
            components.push(component)
        } else {
            break;
        }
    }

    components.join("-")
}

fn get_image_target(
    engine: &cross::docker::Engine,
    image: &Image,
    target_list: &TargetList,
    msg_info: &mut MessageInfo,
) -> cross::Result<String> {
    if let Some(stripped) = image.repository.strip_prefix(&format!("{GHCR_IO}/")) {
        return Ok(stripped.to_string());
    } else if let Some(tag) = image.tag.strip_prefix(RUST_EMBEDDED) {
        return Ok(rustembedded_target(tag));
    } else if let Some(tag) = image.tag.strip_prefix(DOCKER_IO) {
        return Ok(rustembedded_target(tag));
    } else if image
        .repository
        .starts_with(CROSS_CUSTOM_DOCKERFILE_IMAGE_PREFIX)
    {
        if let Some(target) = target_list
            .triples
            .iter()
            .find(|target| image.tag.starts_with(target.as_str()))
            .cloned()
        {
            return Ok(target);
        }
    }
    let mut command = cross::docker::subcommand(engine, "inspect");
    command.args(&[
        "--format",
        &format!(
            r#"{{{{index .Config.Labels "{}.for-cross-target"}}}}"#,
            cross::CROSS_LABEL_DOMAIN
        ),
    ]);
    command.arg(&image.id);

    let target = command.run_and_get_stdout(msg_info)?;
    if target.trim().is_empty() {
        eyre::bail!("cannot get target for image {}", image)
    }
    Ok(target.trim().to_string())
}

pub fn list_images(
    ListImages { targets, .. }: ListImages,
    engine: &docker::Engine,
    msg_info: &mut MessageInfo,
) -> cross::Result<()> {
    let cross_images = get_cross_images(engine, msg_info, true)?;
    let target_list = msg_info.as_quiet(cross::rustc::target_list)?;
    let mut map: BTreeMap<String, Vec<Image>> = BTreeMap::new();
    let mut max_target_len = 0;
    let mut max_image_len = 0;
    for image in cross_images {
        let target = get_image_target(engine, &image, &target_list, msg_info)?;
        if targets.is_empty() || targets.contains(&target) {
            if !map.contains_key(&target) {
                map.insert(target.clone(), vec![]);
            }
            max_target_len = target.len().max(max_target_len);
            max_image_len = image.name().len().max(max_image_len);
            map.get_mut(&target).expect("map must have key").push(image);
        }
    }
    let mut keys: Vec<&str> = map.iter().map(|(k, _)| k.as_ref()).collect();
    keys.sort_unstable();

    let print_string =
        |col1: &str, col2: &str, fill: char, info: &mut MessageInfo| -> cross::Result<()> {
            let mut row = String::new();
            row.push('|');
            row.push(fill);
            row.push_str(col1);
            let spaces = max_target_len.max(col1.len()) + 1 - col1.len();
            for _ in 0..spaces {
                row.push(fill);
            }
            row.push('|');
            row.push(fill);
            row.push_str(col2);
            let spaces = max_image_len.max(col2.len()) + 1 - col2.len();
            for _ in 0..spaces {
                row.push(fill);
            }
            row.push('|');
            info.print(row)
        };

    if targets.len() != 1 {
        print_string("Targets", "Images", ' ', msg_info)?;
        print_string("-------", "------", '-', msg_info)?;
    }

    let print_single =
        |_: &str, image: &Image, info: &mut MessageInfo| -> cross::Result<()> { info.print(image) };
    let print_table = |target: &str, image: &Image, info: &mut MessageInfo| -> cross::Result<()> {
        let name = image.name();
        print_string(target, &name, ' ', info)
    };

    for target in keys {
        for image in map.get(target).expect("map must have key").iter() {
            if targets.len() == 1 {
                print_single(target, image, msg_info)?;
            } else {
                print_table(target, image, msg_info)?;
            }
        }
    }

    Ok(())
}

fn remove_images(
    engine: &docker::Engine,
    images: &[Image],
    msg_info: &mut MessageInfo,
    force: bool,
    execute: bool,
) -> cross::Result<()> {
    let mut command = docker::subcommand(engine, "rmi");
    if force {
        command.arg("--force");
    }
    command.args(images.iter().map(|i| &i.id));
    if images.is_empty() {
        Ok(())
    } else if execute {
        command.run(msg_info, false).map_err(Into::into)
    } else {
        msg_info.note("this is a dry run. to remove the images, pass the `--execute` flag.")?;
        command.print(msg_info)?;
        Ok(())
    }
}

pub fn remove_all_images(
    RemoveImages {
        force,
        local,
        execute,
        ..
    }: RemoveImages,
    engine: &docker::Engine,
    msg_info: &mut MessageInfo,
) -> cross::Result<()> {
    let images = get_cross_images(engine, msg_info, local)?;
    remove_images(engine, &images, msg_info, force, execute)
}

pub fn remove_target_images(
    RemoveImages {
        targets,
        force,
        local,
        execute,
        ..
    }: RemoveImages,
    engine: &docker::Engine,
    msg_info: &mut MessageInfo,
) -> cross::Result<()> {
    let cross_images = get_cross_images(engine, msg_info, local)?;
    let target_list = msg_info.as_quiet(cross::rustc::target_list)?;
    let mut images = vec![];
    for image in cross_images {
        let target = dbg!(get_image_target(engine, &image, &target_list, msg_info)?);
        if targets.contains(&target) {
            images.push(image);
        }
    }
    remove_images(engine, &images, msg_info, force, execute)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_rustembedded_target() {
        let targets = [
            "x86_64-unknown-linux-gnu",
            "x86_64-apple-darwin",
            "thumbv8m.main-none-eabi",
        ];
        for target in targets {
            let versioned = format!("{target}-0.2.1");
            assert_eq!(rustembedded_target(target), target.to_string());
            assert_eq!(rustembedded_target(&versioned), target.to_string());
        }
    }
}