From 712230b06bf579190360e4dfceffab09ef6257be Mon Sep 17 00:00:00 2001 From: baoyachi Date: Fri, 25 Nov 2022 00:23:15 +0800 Subject: [PATCH] support CARGO_MANIFEST_DIR const generate, fix #119 --- README.md | 19 +++++++++++++------ src/env.rs | 8 ++++++++ src/lib.rs | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 946cb57..707e6d1 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,12 @@ You can use this tool to check in production exactly where a binary came from an ![build_module](./build_module.png) # Notice ⚠️ -> The build.rs **is not rebuilt** every-time if the repository has been history builld. -The recommended way is to run `cargo clean` first, then execute `cargo build`, or use a CI/CD pipeline tool to help you perform this operation. -For more details, see https://github.com/baoyachi/shadow-rs/issues/95. +> The build.rs **is not rebuilt** every-time if the repository has been history builld. +> The recommended way is to run `cargo clean` first, then execute `cargo build`, or use a CI/CD pipeline tool to help +> you +> perform this operation. +> For more details, see https://github.com/baoyachi/shadow-rs/issues/95. # Full Examples @@ -67,7 +69,9 @@ shadow-rs = "{latest version}" ### 2) Create `build.rs` file Now in the root of your project (same directory as `Cargo.toml`) add a file `build.rs`: - * with add custom `const` or `fn` see:[example_shadow_hook](https://github.com/baoyachi/shadow-rs/blob/master/example_shadow_hook/build.rs) + +* with add custom `const` or `fn` + see:[example_shadow_hook](https://github.com/baoyachi/shadow-rs/blob/master/example_shadow_hook/build.rs) ```rust fn main() -> shadow_rs::SdResult<()> { @@ -125,6 +129,7 @@ fn main() { println!("{}", build::RUST_CHANNEL); // rust toolchain e.g. 'stable-x86_64-apple-darwin (default)' println!("{}", build::CARGO_VERSION); // cargo version e.g. 'cargo 1.45.0 (744bd1fbb 2020-06-15)' println!("{}", build::CARGO_TREE); // e.g. the output of '$ cargo tree' + println!("{}", build::CARGO_MANIFEST_DIR); // e.g. /User/baoyachi/shadow-rs/ println!("{}", build::PROJECT_NAME); // your project name, e.g. 'shadow-rs' // Time respects SOURCE_DATE_EPOCH environment variable - see below @@ -140,7 +145,8 @@ fn main() { #### Reproducibility This tool includes the current time in the binary which would normally make it non-reproducible. -However, it respects the [`SOURCE_DATE_EPOCH` variable](https://reproducible-builds.org/docs/source-date-epoch/) - if set to a Unix timestamp it will override the value of build time. +However, it respects the [`SOURCE_DATE_EPOCH` variable](https://reproducible-builds.org/docs/source-date-epoch/) - if +set to a Unix timestamp it will override the value of build time. ## Clap Example @@ -185,6 +191,7 @@ You also can use shadow-rs with [`clap`](https://github.com/baoyachi/shadow-rs/b | CARGO_VERSION | cargo 1.45.0 (744bd1fbb 2020-06-15) | | PKG_VERSION | 0.3.13 | | CARGO_TREE | cargo tree | +| CARGO_MANIFEST_DIR | /User/baoyachi/shadow-rs/ | | PROJECT_NAME | shadow-rs | | BUILD_TIME | 2021-06-24 21:33:59 | | BUILD_TIME_2822 | Thu, 24 Jun 2021 21:33:59 +0800 | @@ -208,6 +215,6 @@ here: [Shadow Users Collection](https://github.com/baoyachi/shadow-rs/issues/19) exocore
exocore

starship
bagua-core

starship
inclavare-containers

- + diff --git a/src/env.rs b/src/env.rs index 76ca060..9272f62 100644 --- a/src/env.rs +++ b/src/env.rs @@ -22,6 +22,7 @@ const CARGO_TREE: ShadowConst = "CARGO_TREE"; const BUILD_TARGET: ShadowConst = "BUILD_TARGET"; const BUILD_TARGET_ARCH: ShadowConst = "BUILD_TARGET_ARCH"; +const CARGO_MANIFEST_DIR: ShadowConst = "CARGO_MANIFEST_DIR"; // const CARGO_METADATA: ShadowConst = "CARGO_METADATA"; const PKG_VERSION: ShadowConst = "PKG_VERSION"; @@ -108,6 +109,9 @@ impl SystemEnv { if let Some(v) = std_env.get("CARGO_PKG_VERSION_PRE") { update_val(PKG_VERSION_PRE, v.to_string()); } + if let Some(v) = std_env.get("CARGO_MANIFEST_DIR") { + update_val(CARGO_MANIFEST_DIR, v.to_string()); + } Ok(()) } @@ -262,6 +266,10 @@ pub fn new_system_env(std_env: &BTreeMap) -> BTreeMap