Skip to content

Commit

Permalink
support CARGO_MANIFEST_DIR const generate, fix #119
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Nov 24, 2022
1 parent 93f3100 commit 712230b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 |
Expand All @@ -208,6 +215,6 @@ here: [Shadow Users Collection](https://github.com/baoyachi/shadow-rs/issues/19)
<td align="center"><a href="https://github.com/appaquet/exocore">exocore<br /><sub><b>exocore</b></sub></a><br /></td>
<td align="center"><a href="https://github.com/BaguaSys/bagua-core"><img src="https://avatars.githubusercontent.com/u/84775468?s=200&v=4" width="100px;" alt="starship"/><br /><sub><b>bagua-core</b></sub></a><br /></td>
<td align="center"><a href="https://github.com/alibaba/inclavare-containers"><img src="https://avatars.githubusercontent.com/u/1961952?s=200&v=4" width="100px;" alt="starship"/><br /><sub><b>inclavare-containers</b></sub></a><br /></td>

</tr>
</table>
8 changes: 8 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -262,6 +266,10 @@ pub fn new_system_env(std_env: &BTreeMap<String, String>) -> BTreeMap<ShadowCons
PKG_VERSION_PRE,
ConstVal::new("display build current project preview version"),
);
env.map.insert(
CARGO_MANIFEST_DIR,
ConstVal::new("display build current build cargo manifest dir"),
);

if let Err(e) = env.init(std_env) {
println!("{}", e);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
//! println!("{}",build::CARGO_VERSION);//cargo 1.45.0 (744bd1fbb 2020-06-15)
//! println!("{}",build::PKG_VERSION);//0.3.13
//! println!("{}",build::CARGO_TREE); //like command:cargo tree
//! println!("{}",build::CARGO_MANIFEST_DIR); // /User/baoyachi/shadow-rs/ |
//!
//! println!("{}",build::PROJECT_NAME);//shadow-rs
//! println!("{}",build::BUILD_TIME);//2020-08-16 14:50:25
Expand Down

0 comments on commit 712230b

Please sign in to comment.