Skip to content

Basic crate for accessing task names at build time. #2052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions sys/task-names/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "hubris-task-names"
version = "0.1.0"
edition = "2021"

[build-dependencies]
build-util = { path = "../../build/util" }

[features]

[lib]
test = false
doctest = false
bench = false

[lints]
workspace = true
29 changes: 29 additions & 0 deletions sys/task-names/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::fs::File;
use std::io::Write;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let out = build_util::out_dir();

let task_env = build_util::env_var("HUBRIS_TASKS").unwrap_or_else(|_| {
panic!("can't build this crate outside of the build system.")
});

let task_names = task_env.split(',').collect::<Vec<_>>();
let count = task_names.len();

let mut task_file = File::create(out.join("tasks.rs")).unwrap();
writeln!(task_file, "pub static TASK_NAMES: [&str; {count}] = [").unwrap();
for name in &task_names {
writeln!(task_file, " {name:?},").unwrap();
}
writeln!(task_file, "];").unwrap();

let longest = task_names.iter().map(|s| s.len()).max().unwrap_or(0);
writeln!(task_file, "pub const MAX_TASK_NAME: usize = {longest};").unwrap();

Ok(())
}
17 changes: 17 additions & 0 deletions sys/task-names/src/lib.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually the ideal lib.rs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk, it could have more comments.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Allows compile-time retrieval of task names in the current image.
//!
//! The code is generated, but here's what you can expect:
//!
//! `TASK_NAMES` is a `static` array of `&str`.
//!
//! `MAX_TASK_NAME` is a `const` `usize` giving the number of bytes in the
//! longest task name. This can be useful for sizing buffers.

#![no_std]
#![forbid(clippy::wildcard_imports)]

include!(concat!(env!("OUT_DIR"), "/tasks.rs"));