Skip to content
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

Add no_std support #197

Merged
merged 4 commits into from
Jan 3, 2025
Merged
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
18 changes: 17 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,23 @@ jobs:
- name: Build on nightly
run: |
cargo build --release
cargo +nightly clippy --all --all-features -- -D warnings
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:
Expand Down
59 changes: 13 additions & 46 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,47 +1,14 @@
[package]
name = "shadow-rs"
version = "0.37.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
keywords = ["cargo", "build-script", "build", "shadow", "compile"]
readme = "README.md"
categories = ["development-tools", "development-tools::build-utils"]
repository = "https://github.com/baoyachi/shadow-rs"
documentation = "https://docs.rs/shadow-rs"
homepage = "https://github.com/baoyachi/shadow-rs"
license = "MIT AND Apache-2.0"
exclude = ["shadow-rs.png", "build_module.png"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[package.metadata.docs.rs]
all-features = true

[dependencies]
is_debug = "1.0.2"
const_format = { version = "0.2.22", default-features = false }
time = { version = "0.3.36", features = ["formatting", "local-offset", "parsing"], default-features = false }


#! Optional Dependencies:

## Use `libgit2` as a backend for git operations
git2 = { version = "0.19.0", default-features = false, optional = true }

## Better support for querying the local system time
tzdb = { version = "0.6.0", optional = true, default-features = false, features = ["local"] }

document-features = { version = "0.2", optional = true }

cargo_metadata = { version = "0.18.1", optional = true, default-features = false }
serde_json = { version = "1", default-features = false, optional = true }

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

[dev-dependencies]
winnow = "0.6"

[workspace]
members = ["example_shadow", "example_shadow_hook", "example_wasm"]
members = [
"shadow-rs",
"shadow-rs-consumer",
"example_shadow",
"example_shadow_hook",
"example_wasm",
]

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);
}
4 changes: 2 additions & 2 deletions example_shadow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ build = "build.rs"

[dependencies]
clap = "4.0.1"
shadow-rs = { path = "../",features = ["metadata"] }
shadow-rs = { path = "../shadow-rs",features = ["metadata"] }

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

# Test cargo-clippy. For more detailed information about the issue, visit: https://github.com/baoyachi/shadow-rs/issues/151
[lints.clippy]
Expand Down
4 changes: 2 additions & 2 deletions example_shadow_hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

[build-dependencies]
shadow-rs = { path = "../" }
shadow-rs = { path = "../shadow-rs" }
2 changes: 1 addition & 1 deletion example_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]


[dependencies]
shadow-rs = { path = "../", default-features = false, features = ["tzdb"] }
shadow-rs = { path = "../shadow-rs", default-features = false, features = ["tzdb"] }


[package.metadata.wasm-pack.profile.release]
Expand Down
7 changes: 7 additions & 0 deletions shadow-rs-consumer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "shadow-rs-consumer"
version = "0.1.0"
edition = "2021"

[dependencies]
const_format = "0.2.34"
27 changes: 27 additions & 0 deletions shadow-rs-consumer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![no_std]
/// Add a module with the provided name which contains the build information generated by `shadow-rs`.
///
/// # Example
///
/// ```ignore
/// use shadow_rs::shadow;
///
/// shadow!(my_build_information);
///
/// fn main() {
/// println!("I'm version {}!", my_build_information::VERSION);
/// }
/// ```
///
/// The convention, however, is to use `shadow!(build);`.
#[macro_export]
macro_rules! shadow {
($build_mod:ident) => {
#[doc = r#"shadow-rs mod"#]
pub mod $build_mod {
include!(concat!(env!("OUT_DIR"), "/shadow.rs"));
}
};
}

pub use const_format::formatcp;
46 changes: 46 additions & 0 deletions shadow-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "shadow-rs"
version = "0.37.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
keywords = ["cargo", "build-script", "build", "shadow", "compile"]
readme = "README.md"
categories = ["development-tools", "development-tools::build-utils"]
repository = "https://github.com/baoyachi/shadow-rs"
documentation = "https://docs.rs/shadow-rs"
homepage = "https://github.com/baoyachi/shadow-rs"
license = "MIT AND Apache-2.0"
exclude = ["shadow-rs.png", "build_module.png"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[package.metadata.docs.rs]
all-features = true

[dependencies]
is_debug = "1.0.2"
const_format = { version = "0.2.22", default-features = false }
time = { version = "0.3.36", features = ["formatting", "local-offset", "parsing"], default-features = false }
#TODO: not sure how to bootstrap this, but once shadow-rs-consumer is published on crates.io, this dep should point to that version
shadow-rs-consumer = { path = "../shadow-rs-consumer"}

#! Optional Dependencies:

## Use `libgit2` as a backend for git operations
git2 = { version = "0.19.0", default-features = false, optional = true }

## Better support for querying the local system time
tzdb = { version = "0.6.0", optional = true, default-features = false, features = ["local"] }

document-features = { version = "0.2", optional = true }

cargo_metadata = { version = "0.18.1", optional = true, default-features = false }
serde_json = { version = "1", default-features = false, optional = true }

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

[dev-dependencies]
winnow = "0.6"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 17 additions & 6 deletions src/gen_const.rs → 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
Loading
Loading