Skip to content

Commit

Permalink
fix: remove from env CARGO_ENCODED_RUSTFLAGS for easier nested builds…
Browse files Browse the repository at this point in the history
…, simplify RUSTFLAGS computation rule (#289)

![image](https://github.com/user-attachments/assets/544ecaba-bf95-4080-bc8d-47db93394ab0)

resolves  #287 

this stuff gets set for build-scripts
https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/custom_build.rs#L378,
while `RUSTFLAGS` is unset

---

tested to help resolve problem in #287 on
dj8yfo/neardevhub-treasury-dashboard@56bcbcf

---------

Co-authored-by: dj8yf0μl <[email protected]>
  • Loading branch information
dj8yfo and dj8yf0μl authored Jan 22, 2025
1 parent 2851dc6 commit 4c97799
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,44 @@ This forwards to [reproducible-wasm](#reproducible-wasm) variant of `build` comm
2. has been pushed to remote repository, identified by
[`package.repository`](https://github.com/near/cargo-near/blob/main/cargo-near/src/commands/new/new-project-template/Cargo.template.toml#L9).

## Special `cargo` environment variables

Both of the following are mentioned on https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags

### `RUSTFLAGS`

running e.g.

```bash
RUSTFLAGS="your_custom_value" cargo near build non-reproducible-wasm
```
won't result in `"your_custom_value"` affecting the build.

`RUSTFLAGS="-Awarnings"` is always used for abi build stage, and `RUSTFLAGS="-C link-arg=-s"` for wasm build stage.

Logic for concatenating default values of this variable with values from env was removed in `cargo-near-0.13.3`/`cargo-near-build-0.4.3`, as it was seen as
an unnecessary complication.

There's still a way to override this parameter for wasm build stage, e.g.:

```lang
cargo near build non-reproducible-wasm --env 'RUSTFLAGS=--verbose'
RUST_LOG=info cargo near build non-reproducible-wasm --env 'RUSTFLAGS=--verbose -C link-arg=-s'
```

### `CARGO_ENCODED_RUSTFLAGS`

This variable is always unset during build, so

```bash
CARGO_ENCODED_RUSTFLAGS="your_custom_value" cargo near build non-reproducible-wasm
```
won't result in `"your_custom_value"` affecting the build.

This is so to avoid weird issues like [#287](https://github.com/near/cargo-near/issues/287) when composing multiple builds via build scripts.




## Contribution

Expand Down
39 changes: 14 additions & 25 deletions cargo-near-build/src/cargo_native/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,29 @@ use super::ArtifactType;
pub fn run<T>(
manifest_path: &ManifestPath,
args: &[&str],
mut env: Vec<(&str, &str)>,
env: Vec<(&str, &str)>,
hide_warnings: bool,
color: ColorPreference,
) -> eyre::Result<CompilationArtifact<T>>
where
T: ArtifactType,
{
let mut final_env = BTreeMap::new();

if hide_warnings {
env.push(("RUSTFLAGS", "-Awarnings"));
}

for (key, value) in env {
match key {
"RUSTFLAGS" => {
let rustflags: &mut String = final_env
.entry(key)
.or_insert_with(|| std::env::var(key).unwrap_or_default());
// helps avoids situation on complete match `RUSTFLAGS="-C link-arg=-s -C link-arg=-s"`
if !rustflags.contains(value) {
if !rustflags.is_empty() {
rustflags.push(' ');
}
rustflags.push_str(value);
}
}
_ => {
final_env.insert(key, value.to_string());
}
let final_env = {
let mut env: BTreeMap<_, _> = env.into_iter().collect();
if hide_warnings {
env.insert(crate::env_keys::RUSTFLAGS, "-Awarnings");
}
}
env
};

let removed_env = [crate::env_keys::CARGO_ENCODED_RUSTFLAGS];

let artifacts = invoke_cargo(
"build",
[&["--message-format=json-render-diagnostics"], args].concat(),
manifest_path.directory().ok(),
final_env.iter(),
&removed_env,
color,
)?;

Expand Down Expand Up @@ -105,6 +90,7 @@ fn invoke_cargo<A, P, E, S, EK, EV>(
args: A,
working_dir: Option<P>,
env: E,
removed_env: &[&str],
color: ColorPreference,
) -> eyre::Result<Vec<Artifact>>
where
Expand All @@ -118,6 +104,9 @@ where
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let mut cmd = Command::new(cargo);

for key in removed_env {
cmd.env_remove(key);
}
cmd.envs(env);

if let Some(path) = working_dir {
Expand Down
19 changes: 19 additions & 0 deletions cargo-near-build/src/env_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ pub const CARGO_TARGET_DIR: &str = "CARGO_TARGET_DIR";
/// this variable is set to `"true"` during ABI generation operation
pub const BUILD_RS_ABI_STEP_HINT: &str = "CARGO_NEAR_ABI_GENERATION";

/// <https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags>
///
/// this behaviour that
/// 1. default value for RUSTFLAGS for wasm build is "-C link-arg=-s"
/// 2. it can be overriden with values from --env arguments
/// 3. default RUSTFLAGS for abi gen are "-Awarnings"
/// 4. RUSTFLAGS aren't concatenated (implicitly) with values from environment
///
/// is documented in RUSTFLAGS section of README.md
pub const RUSTFLAGS: &str = "RUSTFLAGS";

/// <https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags>
///
/// this behaviour that
/// 1. CARGO_ENCODED_RUSTFLAGS gets unset by default
///
/// is documented in CARGO_ENCODED_RUSTFLAGS section of README.md
pub const CARGO_ENCODED_RUSTFLAGS: &str = "CARGO_ENCODED_RUSTFLAGS";

pub(crate) const CARGO_NEAR_ABI_PATH: &str = "CARGO_NEAR_ABI_PATH";

pub(crate) const CARGO_NEAR_VERSION: &str = "CARGO_NEAR_VERSION";
Expand Down
2 changes: 1 addition & 1 deletion cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
let abi_path_env = buildtime_env::AbiPath::new(args.no_embed_abi, &min_abi_path);

let build_env = {
let mut build_env = vec![("RUSTFLAGS", "-C link-arg=-s")];
let mut build_env = vec![(env_keys::RUSTFLAGS, "-C link-arg=-s")];
build_env.extend(
args.env
.iter()
Expand Down
2 changes: 1 addition & 1 deletion cargo-near-build/src/types/near/docker_build/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl ReproducibleBuild {
for command_token in build_command {
if command_token
.chars()
.any(|c| !c.is_ascii() || c.is_ascii_control() || c.is_ascii_whitespace())
.any(|c| !c.is_ascii() || c.is_ascii_control())
{
return Err(eyre::eyre!(
"{}: `{}`\n{}",
Expand Down

0 comments on commit 4c97799

Please sign in to comment.