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

fix: remove from env CARGO_ENCODED_RUSTFLAGS for easier nested builds, simplify RUSTFLAGS computation rule #289

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing this allows doing

container_build_command = ["cargo", "near", "build", "non-reproducible-wasm", "--locked", "--env", "RUSTFLAGS=--verbose -C link-arg=-s"]

{
return Err(eyre::eyre!(
"{}: `{}`\n{}",
Expand Down
Loading