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

Is it possible to support no_std for this project? #91

Open
YdrMaster opened this issue Apr 29, 2022 · 8 comments
Open

Is it possible to support no_std for this project? #91

YdrMaster opened this issue Apr 29, 2022 · 8 comments

Comments

@YdrMaster
Copy link

Support no_std will be useful.

@baoyachi
Copy link
Owner

baoyachi commented Apr 29, 2022

Recently,not yet. I'm not clear about rust 'no_std' develop

@baoyachi
Copy link
Owner

For long time, It's should be support.

@YdrMaster
Copy link
Author

thx.

@mchodzikiewicz
Copy link
Contributor

Hi,

I am missing this crate so badly in my no_std projects 🙁

Having a quick look it should be fairly simple introducing a split between generation part in build.rs and usage part in final binary. If I see correctly, most of what's available in the later is const &str which is 100% no_std and no-alloc compliant

This way you could add something like shadow-rs-gen to [build-dependencies] and shadow-rs-consts to [dependencies]

@mchodzikiewicz
Copy link
Contributor

Oh! I just made it work in 5mins (in a messy way)

I am planning to prepare a cleaner PR soon with additional crate in a workspace and feature gates, but for now if anybody is interested in hacking it up, you need to:

  • Disable generation of print_build_in() because it uses println!() inside
  • Substitute shadow::formatcp with const_format::formatcp
  • Add shadow-rs you modified this way to your [build-dependencies] instead of [dependencies]
  • Add const_format crate to your project
  • Copy shadow macro to your project:
#[macro_export]
macro_rules! shadow {
    ($build_mod:ident) => {
        #[doc = r#"shadow-rs mod"#]
        pub mod $build_mod {
            include!(concat!(env!("OUT_DIR"), "/shadow.rs"));
        }
    };
}

First two points as diff of shadow-rs:

diff --git a/src/gen_const.rs b/src/gen_const.rs
index d433da4..5548b2e 100644
--- a/src/gen_const.rs
+++ b/src/gen_const.rs
@@ -19,7 +19,7 @@ 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 = const_format::formatcp!(r#"
 pkg_version:{}
 branch:{}
 commit_hash:{}
@@ -32,7 +32,7 @@ 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 = const_format::formatcp!(r#"
 pkg_version:{}
 tag:{}
 commit_hash:{}
@@ -43,7 +43,7 @@ build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST
 
 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 = const_format::formatcp!(r#"{}
 branch:{}
 commit_hash:{}
 build_time:{}
@@ -53,7 +53,7 @@ build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, R
 
 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 = const_format::formatcp!(r#"{}
 tag:{}
 commit_hash:{}
 build_time:{}
@@ -65,7 +65,7 @@ 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 = const_format::formatcp!(r#"{}
 branch:{}
 commit_hash:{}
 build_time:{}
@@ -77,7 +77,7 @@ 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 = const_format::formatcp!(r#"{}
 tag:{}
 commit_hash:{}
 build_time:{}
diff --git a/src/lib.rs b/src/lib.rs
index f0f0c1c..745ad99 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -663,15 +663,15 @@ 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\
-            #[allow(dead_code)]\n\
-            {CARGO_CLIPPY_ALLOW_ALL}\n\
-            pub fn print_build_in() {\
-            {{print_val}}\
-            }\n",
-        );
-        writeln!(&self.f, "{everything_define}")?;
+        // 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, "{}", cargo_metadata_fn(self))?;

@baoyachi
Copy link
Owner

@mchodzikiewicz

  1. Using no_std requires adding a check in the GitHub action, which is currently missing
  2. For parts that do not support no_std, consider using feature management

@mchodzikiewicz
Copy link
Contributor

mchodzikiewicz commented Dec 29, 2024

Thanks, that's how I do it (still wip, but I have good progress)

Do you mind if I move main crate content to shadow-rs/ and add it to the workspace along with shadow-rs-consumer that contains shadow!() macro?

This way it is possible to cleanly define what is and what isn't a workspace member which is necessary for no_std example (because workspace feature resolver enables all features required by any member to all other members and no_std requires default-features = false.

As a sidenote, workspace Cargo.toml is pretty "special" and doesn't like to be a manifest for a crate at the same time and it works by chance. (https://doc.rust-lang.org/cargo/reference/workspaces.html)

Additional property of the no_std feature is that there will be two crates and main one will depend on shadow-rs-consumer (to avoid duplication of the macro)

@mchodzikiewicz
Copy link
Contributor

Hi, any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants