Skip to content

Commit

Permalink
Merge pull request #166 from baoyachi/issue/153
Browse files Browse the repository at this point in the history
Default deny gen CARGO_METADATA
  • Loading branch information
baoyachi authored Jul 22, 2024
2 parents 42dc425 + d6f3ced commit 149639e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
1 change: 0 additions & 1 deletion example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub fn print_build() {
println!("rust_channel:{}", build::RUST_CHANNEL);
println!("cargo_version:{}", build::CARGO_VERSION);
println!("cargo_tree:{}", build::CARGO_TREE);
println!("cargo_metadata:{:?}", build::CARGO_METADATA);

println!("project_name:{}", build::PROJECT_NAME);
println!("build_time:{}", build::BUILD_TIME);
Expand Down
8 changes: 5 additions & 3 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ mod dep_source_replace {

/// Create all `shadow-rs` constants which are determined by the build environment.
/// The data for these constants is provided by the `std_env` argument.
pub fn new_system_env(std_env: &BTreeMap<String, String>) -> BTreeMap<ShadowConst, ConstVal> {
pub(crate) fn new_system_env(
std_env: &BTreeMap<String, String>,
) -> BTreeMap<ShadowConst, ConstVal> {
let mut env = SystemEnv::default();
env.map.insert(
BUILD_OS,
Expand Down Expand Up @@ -328,7 +330,7 @@ The debug configuration with which the project was built.
Note that this is not the Rust channel, but either `debug` or `release`, depending on whether debug assertions were enabled in the build or not. "#;
const BUILD_RUST_CHANNEL: ShadowConst = "BUILD_RUST_CHANNEL";

pub fn build_time(project: &mut Project) {
pub(crate) fn build_time(project: &mut Project) {
// Enable reproducible builds: https://reproducible-builds.org/docs/source-date-epoch/
let time = now_date_time();
project.map.insert(
Expand Down Expand Up @@ -358,7 +360,7 @@ pub fn build_time(project: &mut Project) {
);
}

pub fn new_project(std_env: &BTreeMap<String, String>) -> BTreeMap<ShadowConst, ConstVal> {
pub(crate) fn new_project(std_env: &BTreeMap<String, String>) -> BTreeMap<ShadowConst, ConstVal> {
let mut project = Project::default();
build_time(&mut project);
project.map.insert(
Expand Down
2 changes: 1 addition & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl Git {
}
}

pub fn new_git(
pub(crate) fn new_git(
path: &Path,
ci: CiType,
std_env: &BTreeMap<String, String>,
Expand Down
18 changes: 14 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,20 @@ macro_rules! shadow {
/// }
/// ```
pub fn new() -> SdResult<()> {
Shadow::build(Default::default())?;
Shadow::build(default_deny())?;
Ok(())
}

/// Since [cargo metadata](https://crates.io/crates/cargo_metadata) details about workspace
/// membership and resolved dependencies for the current package, storing this data can result in
/// significantly larger crate sizes. As such, the CARGO_METADATA const is disabled by default.
///
/// Should you choose to retain this information, you have the option to customize a deny_const object
/// and override the `new_deny` method parameters accordingly.
pub fn default_deny() -> BTreeSet<ShadowConst> {
BTreeSet::from([CARGO_METADATA])
}

/// Identical to [`new`], but additionally accepts a build output denylist.
/// This list determines constants to be excluded in the build output.
///
Expand Down Expand Up @@ -272,12 +282,12 @@ pub fn new_hook<F>(f: F) -> SdResult<()>
where
F: FnOnce(&File) -> SdResult<()>,
{
let shadow = Shadow::build(Default::default())?;
let shadow = Shadow::build(default_deny())?;
shadow.hook(f)
}

/// Returns the contents of [`std::env::vars`] as an ordered map.
pub fn get_std_env() -> BTreeMap<String, String> {
pub(crate) fn get_std_env() -> BTreeMap<String, String> {
let mut env_map = BTreeMap::new();
for (k, v) in std_env::vars() {
env_map.insert(k, v);
Expand All @@ -295,7 +305,7 @@ pub struct Shadow {
pub f: File,
/// The values of build constants to be written.
pub map: BTreeMap<ShadowConst, ConstVal>,
/// Build environment variables, obtained through [`get_std_env`].
/// Build environment variables, obtained through [`std::env::vars`].
pub std_env: BTreeMap<String, String>,
/// Constants in the denylist, passed through [`new_deny`] or [`Shadow::build`].
pub deny_const: BTreeSet<ShadowConst>,
Expand Down

0 comments on commit 149639e

Please sign in to comment.