Skip to content

Commit

Permalink
Merge pull request #210 from baoyachi/no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi authored Feb 23, 2025
2 parents df29dc1 + 1c47fe9 commit b7b42e6
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 32 deletions.
20 changes: 16 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Build Release
run: cargo build --release
- name: Run tests
run: cargo test --all -- --nocapture
run: cargo test

# Run examples with debug
- name: Run examples with debug
Expand All @@ -51,15 +51,15 @@ jobs:
- name: Check example_shadow
run: |
cargo fmt --all -- --check
cargo clippy --all -- -D warnings
cargo clippy --all-targets --all-features -- -D warnings
cargo run
working-directory: ./example_shadow

# example_shadow_hook check
- name: Check example_shadow_hook
run: |
cargo fmt --all -- --check
cargo clippy --all -- -D warnings
cargo clippy --all-targets --all-features -- -D warnings
cargo run
working-directory: ./example_shadow_hook

Expand All @@ -74,6 +74,18 @@ jobs:
wasm-pack build --target web
working-directory: ./example_wasm

- uses: dtolnay/rust-toolchain@v1
with:
target: riscv32imc-unknown-none-elf
toolchain: stable
components: rust-src
- name: Run no_std example
run: |
cargo fmt --all -- --check
cargo clippy --release -- -D warnings
cargo b --release
working-directory: ./example_no_std

# build on nightly
- uses: actions-rs/toolchain@v1
with:
Expand All @@ -83,7 +95,7 @@ jobs:
- name: Build on nightly
run: |
cargo build --release
cargo +nightly clippy --all --all-features -- -D warnings -A clippy::literal_string_with_formatting_args
cargo +nightly clippy --all-features -- -D warnings -A clippy::literal_string_with_formatting_args
test:
strategy:
Expand Down
14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ exclude = ["shadow-rs.png", "build_module.png"]
all-features = true

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


#! Optional Dependencies:
Expand All @@ -37,11 +37,17 @@ 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 = ["git2", "tzdb", "build"]
metadata = ["cargo_metadata", "serde_json"]

std = []
no_std = []

build = ["time", "tzdb", "is_debug/std", "std"]


[dev-dependencies]
winnow = "0.6"

[workspace]
members = ["example_shadow", "example_shadow_hook", "example_wasm"]
members = ["example_shadow", "example_shadow_hook", "example_wasm", "example_no_std"]
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Strongly recommend using **shadow-rs** on the [LSP](https://microsoft.github.io/

You can use this crate to programmatically check where a binary came from and how it was built.

Currently, integration into **wasm** is also supported. For detailed settings, please refer to the
link [example_wasm](https://github.com/baoyachi/shadow-rs/tree/master/example_wasm).


Currently, integration into **wasm**,**no_std** is also supported. For detailed settings, please refer to the
link:
* [example_wasm](https://github.com/baoyachi/shadow-rs/tree/master/example_wasm)
* [example_no_std](https://github.com/baoyachi/shadow-rs/tree/master/example_no_std)

![build_module](./build_module.png)

Expand Down Expand Up @@ -68,7 +68,7 @@ Modify your `Cargo.toml` like so:
build = "build.rs"

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

[build-dependencies]
shadow-rs = "{latest version}"
Expand Down Expand Up @@ -140,6 +140,17 @@ fn main() {
}
```

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

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


#### Reproducibility

This tool includes the current time in the binary which would normally make it non-reproducible.
Expand Down
19 changes: 19 additions & 0 deletions example_no_std/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

[target.riscv32imc-unknown-none-elf]
runner = "espflash flash --monitor"

[build]
rustflags = [
"-C", "link-arg=-Tlinkall.x",
# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
# NOTE: May negatively impact performance of produced code
"-C", "force-frame-pointers",
]

target = "riscv32imc-unknown-none-elf"

[unstable]
build-std = ["core"]

[env]
ESP_LOG="INFO"
3 changes: 3 additions & 0 deletions example_no_std/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
Cargo.lock
.idea/
25 changes: 25 additions & 0 deletions example_no_std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "example_no_std"
version = "0.1.8"
edition = "2021"
build = "build.rs"

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

[dependencies]
esp-backtrace = { version = "0.14.2", features = [
"esp32c3",
"exception-handler",
"panic-handler",
"println",
] }

esp-hal = { version = "0.22.0", features = [
"esp32c3",
] }
esp-println = { version = "0.12.0", features = ["esp32c3", "log"] }
shadow-rs = { path = "../", default-features = false }
log = "0.4.22"

[build-dependencies]
shadow-rs = { path = "../", features = ["no_std"] }
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();
}
4 changes: 4 additions & 0 deletions example_no_std/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = ["rust-src"]
targets = ["riscv32imc-unknown-none-elf"]
24 changes: 24 additions & 0 deletions example_no_std/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_hal::delay::Delay;
use esp_hal::prelude::*;
use log::info;

use shadow_rs::shadow;

shadow!(build);
#[entry]
fn main() -> ! {
esp_println::logger::init_logger_from_env();

let delay = Delay::new();

info!("{}", build::VERSION);

loop {
info!("Hello world!");
delay.delay(500.millis());
}
}
4 changes: 2 additions & 2 deletions example_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ crate-type = ["cdylib", "rlib"]
serde-wasm-bindgen = "0.5"
serde_json = "1.0.108"
wasm-bindgen = "0.2.92"
shadow-rs = { path = "../", default-features = false, features = ["tzdb"] }
shadow-rs = { path = "../", default-features = false }

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

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
4 changes: 2 additions & 2 deletions src/gen_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ gen_const!(clap_long_version_tag_const, CLAP_LONG_VERSION_TAG_CONST);
pub(crate) const BUILD_CONST_VERSION: &str = "VERSION";
pub(crate) const BUILD_CONST_CLAP_LONG_VERSION: &str = "CLAP_LONG_VERSION";

#[allow(dead_code)]
pub(crate) fn cargo_metadata_fn(shadow: &Shadow) -> String {
if !shadow.map.contains_key(CARGO_METADATA) {
return "".to_string();
}
format!(
r#"
use std::str::from_utf8;
use shadow_rs::cargo_metadata::Metadata;
use shadow_rs::serde_json;
Expand All @@ -97,7 +97,7 @@ use shadow_rs::serde_json;
#[allow(dead_code)]
{}
pub fn cargo_metadata() -> Result<Metadata, String> {{
let metadata_json = from_utf8(CARGO_METADATA.as_ref()).map_err(|err| format!("generate 'CARGO_METADATA' value from UTF8 error:{{}}",err))?;
let metadata_json = std::str::from_utf8(CARGO_METADATA.as_ref()).map_err(|err| format!("generate 'CARGO_METADATA' value from UTF8 error:{{}}",err))?;
let meta: Metadata = serde_json::from_str(metadata_json).map_err(|err| err.to_string())?;
Ok(meta)
}}"#,
Expand Down
34 changes: 26 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! build = "build.rs"
//!
//! [dependencies]
//! shadow-rs = "{latest version}"
//! shadow-rs = { version = "{latest version}", default-features = false }
//!
//! [build-dependencies]
//! shadow-rs = "{latest version}"
Expand Down Expand Up @@ -145,36 +145,54 @@
//! pub const GIT_STATUS_FILE: &str = "* src/lib.rs (dirty)";
//! ```
//!
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "metadata")]
pub extern crate cargo_metadata;
#[cfg(feature = "metadata")]
pub extern crate serde_json;

#[cfg(feature = "build")]
mod build;
#[cfg(feature = "build")]
mod ci;
#[cfg(feature = "build")]
mod date_time;
#[cfg(feature = "build")]
mod env;
#[cfg(feature = "build")]
mod err;
#[cfg(feature = "build")]
mod gen_const;
#[cfg(feature = "build")]
mod git;
#[cfg(feature = "build")]
mod hook;
#[cfg(feature = "build")]
mod shadow;

/// Re-exported from the const_format crate
pub use const_format::*;
/// Re-exported from the is_debug crate
pub use is_debug::*;

pub use crate::build::{BuildPattern, ShadowBuilder};
pub use crate::date_time::DateTime;
pub use err::{SdResult, ShadowError};
pub use shadow::Shadow;
pub use {build::default_deny, build::ShadowConst, env::*, git::*};
#[cfg(feature = "build")]
mod pub_export {
pub use crate::build::{BuildPattern, ShadowBuilder};
pub use crate::date_time::DateTime;
pub use crate::err::{SdResult, ShadowError};
pub use crate::shadow::Shadow;
pub use {crate::build::default_deny, crate::build::ShadowConst, crate::env::*, crate::git::*};

pub trait Format {
fn human_format(&self) -> String;
pub trait Format {
fn human_format(&self) -> String;
}
}

#[cfg(feature = "build")]
pub use pub_export::*;

pub const CARGO_CLIPPY_ALLOW_ALL: &str =
"#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]";

Expand Down
19 changes: 12 additions & 7 deletions src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::build::{ConstType, ConstVal};
use crate::ci::CiType;
use crate::env::{new_project, new_system_env};
use crate::gen_const::{
cargo_metadata_fn, clap_long_version_branch_const, clap_long_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, version_branch_const,
version_tag_const, BUILD_CONST_CLAP_LONG_VERSION, BUILD_CONST_VERSION,
};
use crate::git::new_git;
use crate::{
Expand Down Expand Up @@ -291,17 +291,22 @@ impl Shadow {
print_val.push_str(tmp.as_str());
}

let everything_define = format!(
"/// Prints all built-in `shadow-rs` build constants to standard output.\n\
#[cfg(not(feature = "no_std"))]
{
let everything_define = format!(
"/// Prints all built-in `shadow-rs` build constants to standard output.\n\
#[allow(dead_code)]\n\
{CARGO_CLIPPY_ALLOW_ALL}\n\
pub fn print_build_in() {\
{{print_val}}\
}\n",
);
writeln!(&self.f, "{everything_define}")?;
);

writeln!(&self.f, "{everything_define}")?;

writeln!(&self.f, "{}", cargo_metadata_fn(self))?;
use crate::gen_const::cargo_metadata_fn;
writeln!(&self.f, "{}", cargo_metadata_fn(self))?;
}

Ok(())
}
Expand Down

0 comments on commit b7b42e6

Please sign in to comment.