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

chore!: remove unsafe std::env::set_var #228

Merged
merged 32 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1276512
chore: remove std::env::set_var of nep330::BUILD_COMMAND
Sep 24, 2024
a53ed80
chore: uncomment `nep330_build_cmd.append_borrowed_to` for `abi::gene…
Sep 24, 2024
0e8498c
chore: reorg other build time environment
Sep 24, 2024
f065182
chore: move `VersionMismatch` exports to explicitly passed env tuples
Sep 25, 2024
17c9f5f
chore: avoid appending value to rustflags on complete equality (imper…
Sep 25, 2024
62d8ed9
chore: remove unneeded unsafe env::set_var of CARGO_TARGET_DIR in in…
Sep 26, 2024
806fab5
chore: remote `set_var` from `build_extended` logic
Sep 27, 2024
01c7d94
ci: fix clippy
Sep 27, 2024
9f2f270
doc: add comment about `lib::extended::build`
Sep 27, 2024
c021279
doc: correct example usage of `lib::extended` module
Sep 27, 2024
c8e8910
chore: replace `mute_env` generic `BuildOpts` field with additional O…
Oct 1, 2024
17d8a56
ci: add missing lib doc integrity check
Oct 2, 2024
959a05a
chore: improve naming of new envs
Oct 2, 2024
e998396
chore: replace &str -> String everywhere in exported Opts
Oct 2, 2024
e135aaf
doc: add comment inside of extended module example
Oct 2, 2024
db9ae5c
feat: add derive of `bon::Builder` for build and build_extended Opts
Oct 3, 2024
2785018
doc: replace example doc snippets with builder-based code
Oct 3, 2024
8151c15
Merge branch 'main'
Oct 10, 2024
bc1713a
fmt: lib Cargo.toml
Oct 11, 2024
1cfb2a9
feat: remove workdir from extended::Opts
Oct 11, 2024
a60fbc5
doc: change usage api snippets
Oct 11, 2024
a89cf6f
chore: correct env! -> std::env::var
Oct 11, 2024
e8083a8
doc: remove reference to delete workdir field
Oct 11, 2024
64f8b24
lock: avoid yanked futures-util version
Oct 14, 2024
04e64a8
chore: add override_ prefix to nep330_contract_path
Oct 14, 2024
2123673
chore: splice fields of BuildOpts onto DockerBuildOpts
Oct 14, 2024
66f8fef
chore: forbid `--manifest-path` in manifest command
Oct 14, 2024
df1a3ee
chore: put fields of BuildImplicitEnvOpts onto BuildOpts
Oct 14, 2024
2302bdc
chore: readability with ManifestPath::try_from expr
Oct 15, 2024
b982e74
ci: add doc checks, move one doc check from msrv
Oct 15, 2024
42e48bf
chore: add buildtime_env::CommonVariables, common to abi and wasm steps
Oct 15, 2024
973c4d8
Merge branch 'main'
Oct 17, 2024
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
34 changes: 3 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions cargo-near-build/src/cargo_native/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use eyre::{ContextCompat, WrapErr};
use std::io::BufRead;

use crate::types::near::build::input::ColorPreference;
use crate::types::{
cargo::manifest_path::ManifestPath,
near::build::{output::version_mismatch::VersionMismatch, output::CompilationArtifact},
};
use crate::types::{cargo::manifest_path::ManifestPath, near::build::output::CompilationArtifact};

use super::ArtifactType;

Expand All @@ -36,10 +33,13 @@ where
let rustflags: &mut String = final_env
.entry(key)
.or_insert_with(|| std::env::var(key).unwrap_or_default());
if !rustflags.is_empty() {
rustflags.push(' ');
// 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);
}
rustflags.push_str(value);
}
_ => {
final_env.insert(key, value.to_string());
Expand Down Expand Up @@ -84,7 +84,7 @@ where
path,
fresh: !compile_artifact.fresh,
from_docker: false,
builder_version_mismatch: VersionMismatch::None,
builder_version_info: None,
artifact_type: PhantomData,
}),
_ => eyre::bail!(
Expand Down
9 changes: 3 additions & 6 deletions cargo-near-build/src/env_keys.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// this variable is set to `"true"` during ABI generation operation
pub const BUILD_RS_ABI_STEP_HINT: &str = "CARGO_NEAR_ABI_GENERATION";

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

pub(crate) const CARGO_NEAR_VERSION: &str = "CARGO_NEAR_VERSION";
pub(crate) const CARGO_NEAR_ABI_SCHEMA_VERSION: &str = "CARGO_NEAR_ABI_SCHEMA_VERSION";

Expand Down Expand Up @@ -32,12 +34,7 @@ pub mod nep330 {

pub(crate) fn print_env() {
tracing::info!("Variables, relevant for reproducible builds:");
for key in [
BUILD_ENVIRONMENT,
BUILD_COMMAND,
CONTRACT_PATH,
SOURCE_CODE_SNAPSHOT,
] {
for key in [BUILD_ENVIRONMENT, CONTRACT_PATH, SOURCE_CODE_SNAPSHOT] {
let value = std::env::var(key)
.map(|val| format!("'{}'", val))
.unwrap_or("unset".to_string());
Expand Down
17 changes: 11 additions & 6 deletions cargo-near-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub use build_exports::*;
/// Potential import may look like this:
/// ```ignore
/// [build-dependencies.cargo-near-build]
/// version = "0.1.0"
/// version = "x.y.z"
/// features = ["build_script"]
/// ```
///
Expand All @@ -77,11 +77,16 @@ pub use build_exports::*;
/// use cargo_near_build::extended::BuildScriptOpts;
/// let opts = cargo_near_build::extended::BuildOptsExtended {
/// workdir: "../another-contract",
/// env: vec![
/// // unix path of target contract from root of repo
/// (cargo_near_build::env_keys::nep330::CONTRACT_PATH, "another-contract")
/// ],
/// build_opts: cargo_near_build::BuildOpts::default(),
/// build_opts: cargo_near_build::BuildOpts {
/// mute_env: vec![
/// // unix path of target contract from root of repo
/// (
/// cargo_near_build::env_keys::nep330::CONTRACT_PATH.into(),
/// "another-contract".into(),
/// ),
/// ],
/// ..Default::default()
/// },
/// build_script_opts: BuildScriptOpts {
/// result_env_key: Some("BUILD_RS_SUB_BUILD_ARTIFACT_1"),
/// rerun_if_changed_list: vec!["../another-contract", "../Cargo.toml", "../Cargo.lock"],
Expand Down
4 changes: 2 additions & 2 deletions cargo-near-build/src/near/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::types::near::abi as abi_types;
pub mod generate;

#[cfg(feature = "abi_build")]
pub fn build(args: abi_types::Opts) -> eyre::Result<()> {
pub fn build(args: abi_types::Opts) -> eyre::Result<camino::Utf8PathBuf> {
// imports #[cfg(feature = "abi_build")]
use crate::{
pretty_print,
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn build(args: abi_types::Opts) -> eyre::Result<()> {
pretty_print::success("ABI Successfully Generated!");
eprintln!(" - ABI: {}", abi_path.to_string().yellow().bold());

Ok(())
Ok(abi_path)
}

pub fn write_to_file(
Expand Down
32 changes: 0 additions & 32 deletions cargo-near-build/src/near/build/export.rs

This file was deleted.

77 changes: 50 additions & 27 deletions cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cargo_native::Wasm;
use crate::types::near::abi as abi_types;
use crate::types::near::build::buildtime_env;
use camino::Utf8PathBuf;
use colored::Colorize;
use near_abi::BuildInfo;
Expand All @@ -15,18 +16,15 @@ use crate::{
manifest_path::{ManifestPath, MANIFEST_FILE_NAME},
metadata::CrateMetadata,
},
near::build::{input::Opts, output::version_mismatch::VersionMismatch},
near::build::{input::Opts, output::version_info::VersionInfo},
},
};

use super::abi;

pub mod export;

/// builds a contract whose crate root is current workdir, or identified by [`Cargo.toml`/BuildOpts::manifest_path](crate::BuildOpts::manifest_path) location
pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
VersionMismatch::export_builder_and_near_abi_versions();
export::nep_330_build_command(&args)?;
let nep330_build_cmd_env = buildtime_env::Nep330BuildCommand::compute(&args)?;
env_keys::nep330::print_env();

let color = args.color.unwrap_or(ColorPreference::Auto);
Expand All @@ -50,12 +48,6 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {

let out_dir = crate_metadata.resolve_output_dir(args.out_dir.map(Into::into))?;

let mut build_env = vec![("RUSTFLAGS", "-C link-arg=-s")];
build_env.extend(
args.env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref())),
);
let mut cargo_args = vec!["--target", COMPILATION_TARGET];
let cargo_feature_args = {
let mut feat_args = vec![];
Expand All @@ -78,30 +70,47 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {

let mut abi = None;
let mut min_abi_path = None;
let (builder_version, builder_version_mismatch) =
VersionMismatch::get_coerced_builder_version()?;
let builder_version_info = VersionInfo::get_coerced_builder_version()?;
let builder_abi_versions_env = builder_version_info.compute_env_variables()?;
if !args.no_abi {
let mut contract_abi = {
let env = args
let mut abi_env = args
.env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref()))
.collect::<Vec<_>>();

abi_env.extend(
args.mute_env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref())),
);

// required, otherwise `message: Build Details Extension field not provided or malformed: \
// "`NEP330_BUILD_INFO_BUILD_COMMAND` is required, when \
// `NEP330_BUILD_INFO_BUILD_ENVIRONMENT` is set, but it's either not set or empty!"`
// when generating abi in docker build
nep330_build_cmd_env.append_borrowed_to(&mut abi_env);
builder_abi_versions_env.append_borrowed_to(&mut abi_env);
abi::generate::procedure(
&crate_metadata,
args.no_locked,
!args.no_doc,
true,
&cargo_feature_args,
&env,
&abi_env,
color.clone(),
)?
};

let embedding_binary = args.cli_description.cli_name_abi;
contract_abi.metadata.build = Some(BuildInfo {
compiler: format!("rustc {}", rustc_version::version()?),
builder: format!("{} {}", embedding_binary, builder_version),
builder: format!(
"{} {}",
embedding_binary,
builder_version_info.result_builder_version()?
),
image: None,
});
if !args.no_embed_abi {
Expand All @@ -121,20 +130,34 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {

cargo_args.extend(cargo_feature_args);

if let (false, Some(abi_path)) = (args.no_embed_abi, &min_abi_path) {
if let (false, Some(..)) = (args.no_embed_abi, &min_abi_path) {
cargo_args.extend(&["--features", "near-sdk/__abi-embed"]);
build_env.push(("CARGO_NEAR_ABI_PATH", abi_path.as_str()));
}

let version = crate_metadata.root_package.version.to_string();
build_env.push((env_keys::nep330::VERSION, &version));
// this will be set in docker builds (externally to current process), having more info about git commit
if std::env::var(env_keys::nep330::LINK).is_err() {
if let Some(ref repository) = crate_metadata.root_package.repository {
build_env.push((env_keys::nep330::LINK, repository));
}
}
let nep330_version_env = buildtime_env::Nep330Version::new(&crate_metadata);
let nep330_link_env = buildtime_env::Nep330Link::new(&crate_metadata);
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")];
build_env.extend(
args.env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref())),
);

build_env.extend(
args.mute_env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref())),
);
abi_path_env.append_borrowed_to(&mut build_env);
nep330_version_env.append_borrowed_to(&mut build_env);
nep330_link_env.append_borrowed_to(&mut build_env);
nep330_build_cmd_env.append_borrowed_to(&mut build_env);
builder_abi_versions_env.append_borrowed_to(&mut build_env);
build_env
};
pretty_print::step("Building contract");
let mut wasm_artifact = cargo_native::compile::run::<Wasm>(
&crate_metadata.manifest_path,
Expand All @@ -145,7 +168,7 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
)?;

wasm_artifact.path = crate::fs::copy(&wasm_artifact.path, &out_dir)?;
wasm_artifact.builder_version_mismatch = builder_version_mismatch;
wasm_artifact.builder_version_info = Some(builder_version_info);

// todo! if we embedded, check that the binary exports the __contract_abi symbol

Expand Down
13 changes: 7 additions & 6 deletions cargo-near-build/src/near/build_extended/build_script.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use crate::types::near::build::output::version_mismatch::VersionMismatch;
use crate::types::near::build::output::version_info::VersionInfo;
use crate::types::near::build::output::CompilationArtifact;
use crate::types::near::build_extended::build_script::Opts;
use rustc_version::Version;
Expand Down Expand Up @@ -59,15 +59,15 @@ impl<'a> Opts<'a> {
path: stub_path,
fresh: true,
from_docker: false,
builder_version_mismatch: VersionMismatch::None,
builder_version_info: None,
artifact_type: PhantomData,
}
};
Ok(artifact)
}

pub(crate) fn post_build(
&self,
self,
skipped: bool,
artifact: &CompilationArtifact,
workdir: &str,
Expand All @@ -78,13 +78,14 @@ impl<'a> Opts<'a> {
} else {
":"
};
if let ref version_mismatch @ VersionMismatch::Some { .. } =
artifact.builder_version_mismatch
if let ref version_info @ Some(VersionInfo::EnvMismatch { .. }) =
artifact.builder_version_info
{
let version_info = version_info.as_ref().unwrap();
print_warn!(
version,
"INFO: `cargo-near` version was coerced during build: {}.",
version_mismatch
version_info
);
print_warn!(version, "`cargo-near` crate version (used in `build.rs`) did not match `cargo-near` build environment.");
print_warn!(version, "You may consider to optionally make 2 following versions match exactly, if they're too far away:");
Expand Down
Loading
Loading