diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index e1ad5be..0e46f7e 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -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 @@ -51,7 +51,7 @@ 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 @@ -59,7 +59,7 @@ jobs: - 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 @@ -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: @@ -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: diff --git a/Cargo.toml b/Cargo.toml index 1fc0d4e..d0ebad6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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: @@ -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"] \ No newline at end of file diff --git a/README.md b/README.md index 4bb0379..3b71dde 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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}" @@ -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. diff --git a/example_no_std/.cargo/config.toml b/example_no_std/.cargo/config.toml new file mode 100644 index 0000000..194b5a1 --- /dev/null +++ b/example_no_std/.cargo/config.toml @@ -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" diff --git a/example_no_std/.gitignore b/example_no_std/.gitignore new file mode 100644 index 0000000..2e04901 --- /dev/null +++ b/example_no_std/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.lock +.idea/ \ No newline at end of file diff --git a/example_no_std/Cargo.toml b/example_no_std/Cargo.toml new file mode 100644 index 0000000..29e03ec --- /dev/null +++ b/example_no_std/Cargo.toml @@ -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"] } diff --git a/example_no_std/build.rs b/example_no_std/build.rs new file mode 100644 index 0000000..18039b8 --- /dev/null +++ b/example_no_std/build.rs @@ -0,0 +1,8 @@ +use shadow_rs::ShadowBuilder; + +fn main() { + ShadowBuilder::builder() + .deny_const(Default::default()) + .build() + .unwrap(); +} diff --git a/example_no_std/rust-toolchain.toml b/example_no_std/rust-toolchain.toml new file mode 100644 index 0000000..3ed56e9 --- /dev/null +++ b/example_no_std/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "stable" +components = ["rust-src"] +targets = ["riscv32imc-unknown-none-elf"] \ No newline at end of file diff --git a/example_no_std/src/main.rs b/example_no_std/src/main.rs new file mode 100644 index 0000000..9976411 --- /dev/null +++ b/example_no_std/src/main.rs @@ -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()); + } +} diff --git a/example_wasm/Cargo.toml b/example_wasm/Cargo.toml index ef5cade..95db7d6 100644 --- a/example_wasm/Cargo.toml +++ b/example_wasm/Cargo.toml @@ -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 diff --git a/src/gen_const.rs b/src/gen_const.rs index 9b0e93c..46619a9 100644 --- a/src/gen_const.rs +++ b/src/gen_const.rs @@ -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; @@ -97,7 +97,7 @@ use shadow_rs::serde_json; #[allow(dead_code)] {} pub fn cargo_metadata() -> Result {{ - 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) }}"#, diff --git a/src/lib.rs b/src/lib.rs index 7df54e2..2bf81bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}" @@ -145,19 +145,31 @@ //! 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 @@ -165,16 +177,22 @@ 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)]"; diff --git a/src/shadow.rs b/src/shadow.rs index 915b925..3a4eb78 100644 --- a/src/shadow.rs +++ b/src/shadow.rs @@ -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::{ @@ -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(()) }