Skip to content

Commit

Permalink
Add no_std support
Browse files Browse the repository at this point in the history
  • Loading branch information
mchodzikiewicz committed Dec 30, 2024
1 parent 60df9d8 commit 2aef1f9
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 17 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ jobs:
cargo build --release
cargo +nightly clippy --all --all-features -- -D warnings -A clippy::literal_string_with_formatting_args
build_no_std:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: clippy, rustfmt
target: x86_64-unknown-none
- name: Build
run: cd example_no_std && cargo rustc --target x86_64-unknown-none -- -D warnings

test:
strategy:
matrix:
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ members = [
"example_wasm",
]

resolver = "2"
exclude = [
"example_no_std"
]

resolver = "2"
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ fn main() {
}
```

## Setup for `no_std`
Add this to your Cargo.toml
```toml
[dependencies]
shadow-rs-consumer = "{latest version}"

[build-dependencies]
shadow-rs = { version = "{latest version}", default-features = false }
```

Add this line to your `build.rs`:
```rust
fn main() {
ShadowBuilder::builder().build().unwrap();
}
```

In your project code, add this:

```rust
use shadow_rs_consumer::shadow;

shadow!(build);
```

Make use of your build constants:
```rust
log::info!("{}", build::SHORT_COMMIT); //8405e28e
```


#### Reproducibility

This tool includes the current time in the binary which would normally make it non-reproducible.
Expand Down
12 changes: 12 additions & 0 deletions example_no_std/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
.idea
shadow.rs
15 changes: 15 additions & 0 deletions example_no_std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "example_no_std"
version = "0.1.8"
authors = ["baoyachi <[email protected]>", "mchodzikiewicz <[email protected]>"]
edition = "2021"
build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
shadow-rs-consumer = { path = "../shadow-rs-consumer" }
log = "0.4.22"

[build-dependencies]
shadow-rs = { path = "../shadow-rs", default-features = false }
8 changes: 8 additions & 0 deletions example_no_std/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use shadow_rs::ShadowBuilder;

fn main() {
ShadowBuilder::builder()
.deny_const(Default::default())
.build()
.unwrap();
}
18 changes: 18 additions & 0 deletions example_no_std/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![no_std]
#![no_main]

use shadow_rs_consumer::shadow;

shadow!(build);

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]
pub fn func() {
log::info!("short_commit: {}", build::SHORT_COMMIT);
}
2 changes: 1 addition & 1 deletion example_shadow_hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ build = "build.rs"
shadow-rs = { path = "../shadow-rs" }

[build-dependencies]
shadow-rs = { path = "../shadow-rs" }
shadow-rs = { path = "../shadow-rs" }
2 changes: 1 addition & 1 deletion shadow-rs-consumer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
const_format = "0.2.34"
const_format = "0.2.34"
3 changes: 2 additions & 1 deletion shadow-rs-consumer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![no_std]
/// Add a module with the provided name which contains the build information generated by `shadow-rs`.
///
/// # Example
Expand All @@ -23,4 +24,4 @@ macro_rules! shadow {
};
}

pub use const_format::concatcp;
pub use const_format::formatcp;
3 changes: 2 additions & 1 deletion shadow-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ cargo_metadata = { version = "0.18.1", optional = true, default-features = false
serde_json = { version = "1", default-features = false, optional = true }

[features]
default = ["git2", "tzdb"]
default = ["std", "git2", "tzdb"]
metadata = ["cargo_metadata", "serde_json"]
std = []

[dev-dependencies]
winnow = "0.6"
23 changes: 17 additions & 6 deletions shadow-rs/src/gen_const.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[cfg(feature = "std")]
use crate::{Shadow, CARGO_CLIPPY_ALLOW_ALL, CARGO_METADATA};

macro_rules! gen_const {
($fn_name:ident, $fn_body:expr) => {
#[allow(unused)]
pub fn $fn_name() -> String {
let (doc, content) = $fn_body;
format!(
Expand All @@ -15,11 +17,12 @@ macro_rules! gen_const {
};
}

#[allow(dead_code)]
const VERSION_BRANCH_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, branch, commit hash, build time, and build environment on separate lines.
/// This constant is suitable for printing to the user."#,
r##"pub const VERSION:&str = shadow_rs::formatcp!(r#"
r##"pub const VERSION:&str = shadow_rs_consumer::formatcp!(r#"
pkg_version:{}
branch:{}
commit_hash:{}
Expand All @@ -28,11 +31,12 @@ build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, R
);"##,
);

#[allow(dead_code)]
const VERSION_TAG_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, current Git tag, commit hash, build time, and build environment on separate lines.
/// This constant is suitable for printing to the user."#,
r##"pub const VERSION:&str = shadow_rs::formatcp!(r#"
r##"pub const VERSION:&str = shadow_rs_consumer::formatcp!(r#"
pkg_version:{}
tag:{}
commit_hash:{}
Expand All @@ -41,43 +45,47 @@ build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST
);"##,
);

#[allow(dead_code)]
const CLAP_VERSION_BRANCH_CONST: (&str, &str) = (
r#"#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]"#,
r##"pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
r##"pub const CLAP_VERSION:&str = shadow_rs_consumer::formatcp!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##,
);

#[allow(dead_code)]
const CLAP_VERSION_TAG_CONST: (&str, &str) = (
r#"#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]"#,
r##"pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
r##"pub const CLAP_VERSION:&str = shadow_rs_consumer::formatcp!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##,
);

#[allow(dead_code)]
const CLAP_LONG_VERSION_BRANCH_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, branch, commit hash, build time, and build environment on separate lines.
/// This constant is intended to be used by clap or other CLI tools as a long version string."#,
r##"pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
r##"pub const CLAP_LONG_VERSION:&str = shadow_rs_consumer::formatcp!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##,
);

#[allow(dead_code)]
const CLAP_LONG_VERSION_TAG_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, current Git tag, commit hash, build time, and build environment on separate lines.
/// This constant is intended to be used by clap or other CLI tools as a long version string."#,
r##"pub const CLAP_LONG_VERSION:&str = shadow_rs::formatcp!(r#"{}
r##"pub const CLAP_LONG_VERSION:&str = shadow_rs_consumer::formatcp!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
Expand All @@ -95,9 +103,12 @@ gen_const!(
);
gen_const!(clap_long_version_tag_const, CLAP_LONG_VERSION_TAG_CONST);

#[allow(dead_code)]
pub(crate) const BUILD_CONST_VERSION: &str = "VERSION";
#[allow(dead_code)]
pub(crate) const BUILD_CONST_CLAP_LONG_VERSION: &str = "CLAP_LONG_VERSION";

#[cfg(feature = "std")]
pub(crate) fn cargo_metadata_fn(shadow: &Shadow) -> String {
if !shadow.map.contains_key(CARGO_METADATA) {
return "".to_string();
Expand Down
23 changes: 17 additions & 6 deletions shadow-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,24 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;

#[cfg(feature = "std")]
use crate::gen_const::{
cargo_metadata_fn, clap_long_version_branch_const, clap_long_version_tag_const,
clap_version_branch_const, clap_version_tag_const, version_branch_const, version_tag_const,
BUILD_CONST_CLAP_LONG_VERSION, BUILD_CONST_VERSION,
clap_long_version_branch_const, clap_long_version_tag_const, clap_version_branch_const,
clap_version_tag_const, version_branch_const, version_tag_const, BUILD_CONST_CLAP_LONG_VERSION,
BUILD_CONST_VERSION,
};
pub use err::{SdResult, ShadowError};

pub use crate::build::{BuildPattern, ShadowBuilder};
use crate::hook::HookExt;
pub use {build::ShadowConst, env::*, git::*};

pub use shadow_rs_consumer::shadow;
pub use shadow_rs_consumer;

#[cfg(feature = "std")]
use crate::gen_const::cargo_metadata_fn;
#[cfg(feature = "std")]
pub use shadow_rs_consumer::{formatcp, shadow};

pub trait Format {
fn human_format(&self) -> String;
Expand All @@ -196,7 +202,6 @@ const SHADOW_RS: &str = "shadow.rs";
pub const CARGO_CLIPPY_ALLOW_ALL: &str =
"#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]";


/// Generates build information for the current project.
/// This function must be called from `build.rs`.
///
Expand Down Expand Up @@ -502,8 +507,10 @@ impl Shadow {
self.gen_const()?;

//write version function
#[cfg(feature = "std")]
let gen_version = self.gen_version()?;

#[cfg(feature = "std")]
self.gen_build_in(gen_version)?;

Ok(())
Expand Down Expand Up @@ -555,7 +562,9 @@ impl Shadow {
}

fn gen_use_shadow_consumer(&self) -> SdResult<()> {
writeln!(&self.f, "use shadow_rs::formatcp;")?;
#[cfg(feature = "std")]
writeln!(&self.f, "use shadow_rs::shadow_rs_consumer;")?;

Ok(())
}

Expand Down Expand Up @@ -596,6 +605,7 @@ impl Shadow {
Ok(())
}

#[cfg(feature = "std")]
fn gen_version(&mut self) -> SdResult<Vec<&'static str>> {
let (ver_fn, clap_ver_fn, clap_long_ver_fn) = match self.map.get(TAG) {
None => (
Expand Down Expand Up @@ -626,6 +636,7 @@ impl Shadow {
Ok(vec![BUILD_CONST_VERSION, BUILD_CONST_CLAP_LONG_VERSION])
}

#[cfg(feature = "std")]
fn gen_build_in(&self, gen_const: Vec<&'static str>) -> SdResult<()> {
let mut print_val = String::from("\n");

Expand Down

0 comments on commit 2aef1f9

Please sign in to comment.