Skip to content

Commit

Permalink
xtask: a first pass at a cargo runner
Browse files Browse the repository at this point in the history
see #6
  • Loading branch information
rrbutani committed Jul 7, 2022
1 parent 95dddc0 commit c39f5a5
Show file tree
Hide file tree
Showing 5 changed files with 471 additions and 7 deletions.
15 changes: 9 additions & 6 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "gdb -q -x .gdbconfig"
runner = "cargo run -q --bin xtask --"

rustflags = [
"-C", "link-arg=-Tlink.x",
# "-C", "linker=flip-link", # TODO
"-C", "force-frame-pointers=yes",
# "-C", "force-frame-pointers=yes",
]


Expand All @@ -15,13 +15,16 @@ rustflags = [
#
# But, it doesn't look like the feature above is headed towards stabilization
# anytime soon and we don't want to tie ourselves to nightly so we stick with
# this and stomach needing to be in the repo root to `cargo run`/`cargo flash`.
# this and use aliases where possible to specify the target triple.
#
# See: https://github.com/ut-utp/tm4c/issues/6 for more details.
[build]
target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)

[alias]
b = "build --release"
r = "run --release"
r = "run --release --target thumbv7em-none-eabihf"
b = "build --release --target thumbv7em-none-eabihf"
ben = "bench --target thumbv7em-none-eabihf"

# Notes:
# call stack: cargo +nightly call-stack --bin utp-tm4c | dot -Tpng > out.png
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
".",
# "xtask",
"xtask",
]

[package]
Expand Down
21 changes: 21 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "xtask"
version = "0.0.0"
edition = "2021"
publish = false
build = "build.rs"

[[bin]]
name = "xtask"
path = "xtask.rs"

[build-dependencies]
which = "4.2"

[dependencies]
crossbeam-utils = "0.8"
downloader = { version = "0.2", features = ["tui"] }
owo-colors = "3.4"
serialport = "4.2"
xshell = "0.2"
which = "4.2"
59 changes: 59 additions & 0 deletions xtask/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::{env, fs, ffi, path::{Path, PathBuf}};

fn var(name: impl AsRef<ffi::OsStr>) -> Result<String, env::VarError> {
println!("cargo:rerun-if-env-changed=RUSTC");
env::var(name)
}

fn which(query: impl AsRef<ffi::OsStr>) -> which::Result<PathBuf> {
println!("cargo:rerun-if-env-changed=PATH");
which::which(query)
}

fn main() {
// Tell `xtask` where rustc is so it can find the sysroot:
let rustc = var("RUSTC").expect("cargo sets $RUSTC for build scripts");
let rustc = Path::new(&rustc);

let rustc = if rustc.exists() && rustc.is_absolute() {
rustc.to_path_buf()
} else {
// Assume it's a command that we need to get the path of with `which`:
//
// (this will work with relative paths too; we'll declare false
// dependence on `$PATH` in these cases but that's fine)
which(rustc).unwrap()
};
println!("cargo:rerun-if-changed={}", rustc.display());
println!("cargo:rustc-env=RUSTC_PATH={}", rustc.display());


// Tell `xtask` where it can stick its artifacts:
let out_dir = var("OUT_DIR").expect("cargo sets $OUT_DIR for build scripts");
let out_dir = Path::new(&out_dir);

// We seem to get paths like `target/debug/build/xtask-55dae522e1d36b80/out`
let potential_target_dir = {
let mut path = Some(out_dir);

// Try to go 4 directories up!
for _ in 0..4 {
if let Some(p) = path {
path = p.parent();
}
}

path
};
let target_dir = if let Some(p) = potential_target_dir.filter(|p| p.file_name().unwrap() == "target") {
p
} else {
out_dir
};
let artifact_dir = target_dir.join("xtask");
fs::create_dir_all(&artifact_dir).unwrap();
println!("cargo:rustc-env=XTASK_ARTIFACT_DIR={}", artifact_dir.display());

// Forward the host target triple:
println!("cargo:rustc-env=HOST_TRIPLE={}", var("HOST").unwrap());
}
Loading

0 comments on commit c39f5a5

Please sign in to comment.