crit 0.0.8

Rust cross-compiler
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
//! crit provides predicates for conveniently managing multiple cross target builds.

extern crate lazy_static;
extern crate pad;
extern crate regex;
extern crate toml;

use pad::PadStr;
use std::collections;
use std::fmt::Write;
use std::fs;
use std::path;
use std::process;

/// CRIT_ARTIFACT_ROOT denotes the directory housing crit internal files during porting.
pub static CRIT_ARTIFACT_ROOT: &str = ".crit";

lazy_static::lazy_static! {
    /// RUSTUP_TARGET_PATTERN matches Rust target triples from rustup target list output.
    pub static ref RUSTUP_TARGET_PATTERN: regex::Regex = regex::Regex::new(r"(\S+)").unwrap();

    /// DEFAULT_TARGET_EXCLUSION_PATTERN collects patterns for problematic target triples,
    /// such as bare metal targets that may lack support for the `std` package,
    /// or targets without community supported cross images.
    pub static ref DEFAULT_TARGET_EXCLUSION_PATTERN: regex::Regex = regex::Regex::new(
        &[
            "android",
            "cuda",
            "emscripten",
            "fortanix",
            "fuchsia",
            "gnux32",
            "ios",
            "loongarch",
            "msvc",
            "none-eabi",
            "pc-solaris",
            "redox",
            "uefi",
            "unknown-none",
            "wasm",
        ].join("|")
    ).unwrap();

    /// DEFAULT_FEATURE_EXCLUSION_PATTERN collects patterns for problematic binary features,
    /// such as internal development programs.
    pub static ref DEFAULT_FEATURE_EXCLUSION_PATTERN: regex::Regex = regex::Regex::new(
        &[
            "letmeout",
        ].join("|")
    ).unwrap();

    /// BUILD_MODES enumerates cargo's major build modes.
    pub static ref BUILD_MODES: Vec<String> = [
        "debug",
        "release",
    ]
        .iter()
        .map(|e| e.to_string())
        .collect();


    /// BINARY_FILE_EXTENSIONS enumerates potential cargo build binary file extensions.
    pub static ref BINARY_FILE_EXTENSIONS: Vec<String> = [
        "",
        "exe",
        "js",
        "wasm",
    ]
        .iter()
        .map(|e| e.to_string())
        .collect();
}

/// get_targets queries rustup for the list of available Rust target triples.
pub fn get_targets(
    target_exclusion_pattern: regex::Regex,
) -> Result<collections::BTreeMap<String, bool>, String> {
    process::Command::new("rustup")
        .args(["target", "list"])
        .stdout(process::Stdio::piped())
        .stderr(process::Stdio::piped())
        .output()
        .map_err(|_| "unable to run rustup".to_string())
        .and_then(|output| match output.status.success() {
            // work around rustup writing error messages to stdout
            false => Err("error: unable to query rustup target list".to_string()),
            _ => String::from_utf8(output.stdout)
                .map_err(|_| "error: unable to decode rustup stdout stream".to_string()),
        })
        .map(|text| {
            text.lines()
                .filter(|line| RUSTUP_TARGET_PATTERN.is_match(line))
                .map(|line| {
                    RUSTUP_TARGET_PATTERN
                        .captures(line)
                        .and_then(|e| e.get(1))
                        .map(|e| e.as_str())
                        .unwrap()
                })
                .map(|target| {
                    (
                        target.to_string(),
                        !target_exclusion_pattern.is_match(target),
                    )
                })
                .collect()
        })
}

/// format_targets renders a target table.
pub fn format_targets(targets: collections::BTreeMap<String, bool>) -> Result<String, String> {
    let target_col_header: String = "TARGET".to_string();
    let target_col_header_len: usize = target_col_header.len();

    let mut target_col_values: Vec<&String> = targets.keys().collect();
    target_col_values.push(&target_col_header);

    let max_target_len: usize = target_col_values
        .iter()
        .map(|e| e.len())
        .max()
        .unwrap_or(target_col_header_len);

    let mut buf: String = String::new();
    write!(
        buf,
        "{} ENABLED",
        target_col_header.pad_to_width(max_target_len)
    )
    .map_err(|_| "error: unable to render target table format header".to_string())?;

    for (target, enabled) in targets {
        write!(buf, "\n{} {}", target.pad_to_width(max_target_len), enabled)
            .map_err(|_| "error: unable to render target table format row".to_string())?;
    }

    Ok(buf)
}

/// get_applications queries Cargo.toml for the list of binary application names.
pub fn get_applications(feature_exclusion_pattern: regex::Regex) -> Result<Vec<String>, String> {
    let bin_sections_result: Result<Vec<toml::Value>, String> = fs::read_to_string("Cargo.toml")
        .map_err(|_| "error: unable to read Cargo.toml".to_string())
        .and_then(|e| e.parse::<toml::Table>().map_err(|err| err.to_string()))
        .and_then(|e| {
            e.get("bin")
                .ok_or("error: no binaries declared in Cargo.toml".to_string())
                .map(|e2| e2.clone())
        })
        .and_then(|e| {
            e.as_array()
                .ok_or("error: binary section not an array in Cargo.toml".to_string())
                .map(|e2| e2.clone())
        });

    let bin_sections: Vec<toml::Value> = bin_sections_result?;

    let name_options: Vec<Option<&toml::Value>> = bin_sections
        .iter()
        .filter(|e| {
            let feature_values_result: Option<&Vec<toml::Value>> =
                e.get("required-features").and_then(|e2| e2.as_array());

            if feature_values_result.is_none() {
                return true;
            }

            let feature_values: &Vec<toml::Value> = feature_values_result.unwrap();

            let feature_options: Vec<Option<&str>> =
                feature_values.iter().map(|e2| e2.as_str()).collect();

            feature_options.iter().any(|e| match e {
                Some(feature) => feature_exclusion_pattern.is_match(feature),
                None => false,
            })
        })
        .map(|e| e.get("name"))
        .collect();

    if name_options.contains(&None) {
        return Err("error: binary missing name field in Cargo.toml".to_string());
    }

    let name_str_results: Vec<Option<&str>> = name_options
        .iter()
        .map(|e| {
            let e2 = e.unwrap();
            e2.as_str()
        })
        .collect();

    if name_str_results.contains(&None) {
        return Err("error: binary name not a string in Cargo.toml".to_string());
    }

    Ok(name_str_results
        .iter()
        .map(|e| e.unwrap())
        .map(|e| e.to_string())
        .collect())
}

/// TargetConfig models a cross build operation.
pub struct TargetConfig<'a> {
    /// target denotes a Rust target triple.
    pub target: &'a str,

    /// cross_dir_pathbuf denotes the cross notion of target directory.
    pub cross_dir_pathbuf: &'a path::PathBuf,

    /// bin_dir_pathbuf denotes the location of a destination directory
    /// for copying artifacts into a recursive archive friendly
    /// subdirectory tree.
    pub bin_dir_pathbuf: &'a path::PathBuf,

    /// cross_args denotes any passthrough arguments to forward to cross.
    pub cross_args: &'a Vec<String>,

    /// applications denotes the names of cargo binaries
    /// expected to be produced during a cross/cargo build.
    pub applications: &'a Vec<String>,
}

impl TargetConfig<'_> {
    /// build executes a cross build.
    pub fn build(&self) -> Result<(), String> {
        let target_dir_pathbuf: path::PathBuf = self.cross_dir_pathbuf.join(self.target);
        let target_dir_str: &str = &target_dir_pathbuf.display().to_string();

        let cross_output_result: Result<process::Output, String> = process::Command::new("cross")
            .args([
                "build",
                "--target-dir",
                target_dir_str,
                "--target",
                self.target,
            ])
            .args(self.cross_args.clone())
            .stdout(process::Stdio::piped())
            .stderr(process::Stdio::piped())
            .output()
            .map_err(|err| err.to_string());

        let cross_output: process::Output = cross_output_result?;

        if !cross_output.status.success() {
            let cross_stderr: String =
                String::from_utf8(cross_output.stderr).map_err(|err| err.to_string())?;

            return Err(cross_stderr);
        }

        for application in self.applications {
            let dest_dir_pathbuf: path::PathBuf = self.bin_dir_pathbuf.join(self.target);
            let dest_dir_str: &str = &dest_dir_pathbuf.display().to_string();

            fs::create_dir_all(dest_dir_str).map_err(|err| err.to_string())?;

            for extension in BINARY_FILE_EXTENSIONS.iter() {
                for mode in BUILD_MODES.iter() {
                    let mut source_pathbuf: path::PathBuf = target_dir_pathbuf
                        .join(self.target)
                        .join(mode)
                        .join(application);
                    source_pathbuf.set_extension(extension);

                    if source_pathbuf.exists() {
                        let source_str: &str = &source_pathbuf.display().to_string();

                        let mut dest_pathbuf: path::PathBuf = dest_dir_pathbuf.join(application);
                        dest_pathbuf.set_extension(extension);
                        let dest_str: &str = &dest_pathbuf.display().to_string();

                        fs::copy(source_str, dest_str).map_err(|err| err.to_string())?;
                    }
                }
            }
        }

        Ok(())
    }
}

/// clean_containers removes leftover cross Docker containers.
pub fn clean_containers() -> Result<(), String> {
    let cross_toml_path: &path::Path = path::Path::new("Cross.toml");

    if !cross_toml_path.exists() {
        return Ok(());
    }

    let cross_config: toml::Table = fs::read_to_string("Cross.toml")
        .map_err(|_| "error: unable to read Cross.toml".to_string())
        .and_then(|e| e.parse::<toml::Table>().map_err(|err| err.to_string()))?;

    if !cross_config.contains_key("target") {
        return Ok(());
    }

    let blank_table: toml::Value = toml::Value::Table(toml::Table::new());

    let targets_result: Result<&toml::Table, String> = cross_config
        .get("target")
        .unwrap_or(&blank_table)
        .as_table()
        .ok_or("target section not a table in Cross.toml".to_string());

    let targets: &toml::Table = targets_result?;

    let target_options: Vec<Option<&toml::Table>> = targets
        .iter()
        .map(|(_, target)| target.as_table())
        .collect();

    if target_options.iter().any(|e| e.is_none()) {
        return Err("error: target entry not a table in Cross.toml".to_string());
    }

    let image_options: Vec<Option<String>> = target_options
        .iter()
        .map(|e| {
            e.unwrap_or(&toml::Table::new())
                .get("image")
                .unwrap_or(&toml::Value::String(String::new()))
                .as_str()
                .map(|e2| e2.to_string())
        })
        .collect();

    if image_options.iter().any(|e| e.is_none()) {
        return Err("error: target image not a string in Cross.toml".to_string());
    }

    let mut images: Vec<String> = image_options
        .iter()
        .map(|e| {
            let blank_string = String::new();
            e.clone().unwrap_or(blank_string)
        })
        .collect();

    // cross default image prefix
    images.push("ghcr.io/cross-rs".to_string());

    let docker_ps_output: process::Output = process::Command::new("docker")
        .args(["ps", "-a"])
        .output()
        .map_err(|_| "error: unable to run docker process list".to_string())?;

    if !docker_ps_output.status.success() {
        let docker_ps_stderr = String::from_utf8(docker_ps_output.stderr)
            .map_err(|_| "error: unable to decode docker process list stderr stream")?;

        return Err(docker_ps_stderr);
    }

    let docker_ps_stdout: String = String::from_utf8(docker_ps_output.stdout)
        .map_err(|_| "error: unable to decode docker process list stdout stream")?;

    for line in docker_ps_stdout.lines() {
        let pattern: String = format!("([[:xdigit:]]{{12}})\\s+({})", images.join("|"));

        let re: regex::Regex = regex::Regex::new(&pattern).map_err(|_| {
            "image name introduced invalid Rust regular expression syntax".to_string()
        })?;

        if !re.is_match(line) {
            continue;
        }

        let container_id: &str = re
            .captures(line)
            .and_then(|e| e.get(1))
            .map(|e| e.as_str())
            .ok_or("error: container id not a string in docker process list output".to_string())?;

        let docker_rm_output: process::Output = process::Command::new("docker")
            .args(["rm", "-f", container_id])
            .output()
            .map_err(|_| "error: unable to run docker container removal".to_string())?;

        if !docker_rm_output.status.success() {
            let docker_rm_stderr: String =
                String::from_utf8(docker_rm_output.stderr).map_err(|_| {
                    "error: unable to decode docker container removal stderr stream".to_string()
                })?;

            return Err(docker_rm_stderr);
        }
    }

    Ok(())
}

/// clean_artifact_root removes CRIT_ARTIFACT_ROOT directory.
pub fn clean_artifact_root(artifact_root_path: &path::Path) -> Result<(), String> {
    if !artifact_root_path.exists() {
        return Ok(());
    }

    fs::remove_dir_all(CRIT_ARTIFACT_ROOT)
        .map_err(|_| "error: unable to remove crit artifact root directory".to_string())
}

/// clean removes:
///
/// * cross Docker containers
/// * CRIT_ARTIFACT_ROOT directory
///
pub fn clean(artifact_root_path: &path::Path) {
    _ = clean_containers();
    _ = clean_artifact_root(artifact_root_path);
}