Skip to content

Commit c2bcf1e

Browse files
author
Jonas Schievink
committed
Use cargo-xtask in CI
1 parent c3d01a2 commit c2bcf1e

File tree

9 files changed

+155
-114
lines changed

9 files changed

+155
-114
lines changed

.cargo/config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
[alias]
2+
xtask = "run -p xtask --"
3+
14
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
25
runner = 'arm-none-eabi-gdb'
36
rustflags = [
47
"-C", "link-arg=-Tlink.x",
58
]
6-
7-
[build]
8-
target = "thumbv7em-none-eabihf"

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
thumbv7em-none-eabihf
3434
thumbv8m.main-none-eabi
3535
- name: Build Crates
36-
run: scripts/build.sh
36+
run: cargo test -p xtask
3737

3838
strategy:
3939
matrix:

Cargo.toml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
[workspace]
22
members = [
3+
"xtask",
34
"nrf52810-hal",
45
"nrf52832-hal",
56
"nrf52833-hal",
67
"nrf52840-hal",
78
"nrf9160-hal",
8-
"examples/rtic-demo",
9-
"examples/spi-demo",
10-
"examples/twi-ssd1306",
11-
"examples/ecb-demo",
12-
"examples/ccm-demo",
13-
"examples/ppi-demo",
14-
"examples/gpiote-demo",
15-
"examples/wdt-demo",
16-
"examples/lpcomp-demo",
17-
"examples/qdec-demo",
18-
"examples/comp-demo",
19-
"examples/i2s-controller-demo",
20-
"examples/i2s-peripheral-demo",
21-
"examples/pwm-demo",
22-
"examples/twim-demo",
23-
"examples/twis-demo",
9+
"examples/*",
2410
]
2511

2612
[profile.dev]

examples/rtic-demo/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ optional = true
3131
52810 = ["nrf52810-hal"]
3232
52832 = ["nrf52832-hal"]
3333
52840 = ["nrf52840-hal"]
34-
default = ["52832"]

examples/twi-ssd1306/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ optional = true
2929
[features]
3030
52832 = ["nrf52832-hal"]
3131
52840 = ["nrf52840-hal"]
32-
default = ["52832"]

scripts/build.sh

Lines changed: 0 additions & 92 deletions
This file was deleted.

xtask/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "xtask"
3+
version = "0.0.0"
4+
authors = ["Jonas Schievink <[email protected]>"]
5+
edition = "2018"
6+
publish = false
7+
8+
[[test]]
9+
name = "ci"
10+
harness = false

xtask/src/lib.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::process::Command;
2+
3+
pub static HALS: &[(&str, &str)] = &[
4+
("nrf51-hal", "thumbv6m-none-eabi"),
5+
("nrf9160-hal", "thumbv8m.main-none-eabihf"),
6+
("nrf52810-hal", "thumbv7em-none-eabi"),
7+
("nrf52832-hal", "thumbv7em-none-eabihf"),
8+
("nrf52833-hal", "thumbv7em-none-eabihf"),
9+
("nrf52840-hal", "thumbv7em-none-eabihf"),
10+
];
11+
12+
pub static EXAMPLES: &[(&str, &[&str])] = &[
13+
("ccm-demo", &["52810", "52832", "52833", "52840"]),
14+
("comp-demo", &[]),
15+
("ecb-demo", &["51", "52810", "52832", "52833", "52840"]),
16+
("gpiote-demo", &[]),
17+
("i2s-controller-demo", &[]),
18+
("lpcomp-demo", &[]),
19+
("ppi-demo", &["51", "52810", "52832", "52833", "52840"]),
20+
("pwm-demo", &[]),
21+
("qdec-demo", &[]),
22+
("rtic-demo", &["51", "52810", "52832", "52840"]),
23+
("spi-demo", &[]),
24+
("twi-ssd1306", &["52832", "52840"]),
25+
("twim-demo", &[]),
26+
("twis-demo", &[]),
27+
("wdt-demo", &[]),
28+
];
29+
30+
pub fn feature_to_target(feat: &str) -> &str {
31+
match feat {
32+
"51" => "thumbv6m-none-eabi",
33+
"52810" => "thumbv7em-none-eabi",
34+
_ if feat.starts_with("52") => "thumbv7em-none-eabihf",
35+
_ => panic!("unknown Cargo feature `{}`", feat),
36+
}
37+
}
38+
39+
pub fn install_targets() {
40+
let mut targets = HALS
41+
.iter()
42+
.map(|(_, target)| *target)
43+
.chain(
44+
EXAMPLES
45+
.iter()
46+
.flat_map(|(_, features)| features.iter().map(|feat| feature_to_target(feat))),
47+
)
48+
.collect::<Vec<_>>();
49+
targets.sort();
50+
targets.dedup();
51+
52+
let mut cmd = Command::new("rustup");
53+
cmd.args(&["target", "add"]).args(&targets);
54+
let status = cmd
55+
.status()
56+
.map_err(|e| format!("couldn't execute {:?}: {}", cmd, e))
57+
.unwrap();
58+
assert!(
59+
status.success(),
60+
"failed to install targets with rustup: {:?}",
61+
cmd
62+
);
63+
}

xtask/tests/ci.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::env;
2+
use std::{fs, process::Command};
3+
use xtask::{EXAMPLES, HALS};
4+
5+
fn build_example(example: &str, target: &str, feature: Option<&str>) {
6+
println!("building `{}` for `{}`", example, target);
7+
let mut cargo = Command::new("cargo");
8+
let toml_path = format!("examples/{}/Cargo.toml", example);
9+
cargo.args(&["build", "--target", target, "--manifest-path", &toml_path]);
10+
if let Some(feature) = feature {
11+
cargo.args(&["--features", feature]);
12+
}
13+
14+
let status = cargo
15+
.status()
16+
.map_err(|e| format!("could not execute {:?}: {}", cargo, e))
17+
.unwrap();
18+
assert!(
19+
status.success(),
20+
"command exited with error status: {:?}",
21+
cargo
22+
);
23+
}
24+
25+
fn main() {
26+
xtask::install_targets();
27+
28+
// We execute from the `xtask` dir, so `cd ..` so that we can find `examples` etc.
29+
env::set_current_dir("..").unwrap();
30+
31+
// Build-test every HAL.
32+
for (hal, target) in HALS {
33+
let mut cargo = Command::new("cargo");
34+
let toml_path = format!("{}/Cargo.toml", hal);
35+
let status = cargo
36+
.args(&["build", "--manifest-path", &toml_path, "--target", target])
37+
.status()
38+
.map_err(|e| format!("could not execute {:?}: {}", cargo, e))
39+
.unwrap();
40+
assert!(
41+
status.success(),
42+
"command exited with error status: {:?}",
43+
cargo
44+
);
45+
}
46+
47+
// Build-test every example with each supported feature.
48+
for (example, features) in EXAMPLES {
49+
// Features are exclusive (they select the target chip), so we test each one
50+
// individually.
51+
if features.is_empty() {
52+
// Use a default target.
53+
build_example(example, "thumbv7em-none-eabihf", None);
54+
} else {
55+
for feature in *features {
56+
let target = xtask::feature_to_target(feature);
57+
build_example(example, target, Some(*feature));
58+
}
59+
}
60+
}
61+
62+
// Ensure that no examples get added without an entry in EXAMPLES.
63+
for entry in fs::read_dir("examples").unwrap() {
64+
let entry = entry.unwrap();
65+
let name = entry.file_name();
66+
let name = name.to_str().unwrap();
67+
68+
if EXAMPLES
69+
.iter()
70+
.find(|(example, ..)| *example == name)
71+
.is_none()
72+
{
73+
panic!("example `{}` is missing an entry in xtask `EXAMPLES`", name);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)