Skip to content

Commit

Permalink
chore: add buildtime_env::CommonVariables, common to abi and wasm steps
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Oct 15, 2024
1 parent b982e74 commit 1f8e72c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
46 changes: 16 additions & 30 deletions cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ use super::abi;

/// 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> {
let nep330_build_cmd_env = buildtime_env::Nep330BuildCommand::compute(&args)?;

let external_cargo_target_path_env =
buildtime_env::CargoTargetDir::maybe_new(args.override_cargo_target_dir);
let external_nep330_contract_path_env =
buildtime_env::Nep330ContractPath::maybe_new(args.override_nep330_contract_path);
let override_cargo_target_path_env =
buildtime_env::CargoTargetDir::maybe_new(args.override_cargo_target_dir.clone());

env_keys::nep330::print_env();

Expand All @@ -44,7 +40,7 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
})?;

let crate_metadata = pretty_print::handle_step("Collecting cargo project metadata...", || {
let manifest_path: Utf8PathBuf = if let Some(manifest_path) = args.manifest_path {
let manifest_path: Utf8PathBuf = if let Some(manifest_path) = args.manifest_path.clone() {
manifest_path
} else {
MANIFEST_FILE_NAME.into()
Expand All @@ -53,11 +49,11 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
CrateMetadata::collect(
manifest_path,
args.no_locked,
external_cargo_target_path_env.as_ref(),
override_cargo_target_path_env.as_ref(),
)
})?;

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

let mut cargo_args = vec!["--target", COMPILATION_TARGET];
let cargo_feature_args = {
Expand All @@ -82,25 +78,23 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
let mut abi = None;
let mut min_abi_path = None;
let builder_version_info = VersionInfo::get_coerced_builder_version()?;
let builder_abi_versions_env = builder_version_info.compute_env_variables()?;

let common_vars_env = buildtime_env::CommonVariables::new(
&args,
&builder_version_info,
&crate_metadata,
override_cargo_target_path_env,
)?;

if !args.no_abi {
let mut contract_abi = {
let mut abi_env = args
.env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref()))
.collect::<Vec<_>>();
common_vars_env.append_borrowed_to(&mut abi_env);

// 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);
external_nep330_contract_path_env.append_borrowed_to(&mut abi_env);
builder_abi_versions_env.append_borrowed_to(&mut abi_env);
if let Some(ref target_dir) = external_cargo_target_path_env {
target_dir.append_borrowed_to(&mut abi_env);
}
abi::generate::procedure(
&crate_metadata,
args.no_locked,
Expand Down Expand Up @@ -143,8 +137,6 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
cargo_args.extend(&["--features", "near-sdk/__abi-embed"]);
}

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 = {
Expand All @@ -156,14 +148,8 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
);

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);
external_nep330_contract_path_env.append_borrowed_to(&mut build_env);
builder_abi_versions_env.append_borrowed_to(&mut build_env);
if let Some(ref target_dir) = external_cargo_target_path_env {
target_dir.append_borrowed_to(&mut build_env);
}
common_vars_env.append_borrowed_to(&mut build_env);

build_env
};
pretty_print::step("Building contract");
Expand Down
51 changes: 51 additions & 0 deletions cargo-near-build/src/types/near/build/buildtime_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,54 @@ pub use abi_builder_version::BuilderAbiVersions;

pub use external::cargo_target_dir::CargoTargetDir;
pub use external::nep330_path::Nep330ContractPath;

use crate::types::cargo::metadata::CrateMetadata;
use crate::BuildOpts;

use super::output::version_info::VersionInfo;

/// varibles, common for both steps of build, abi-gen and wasm build
pub struct CommonVariables {
pub nep330_version: Nep330Version,
pub nep330_link: Nep330Link,
pub nep330_build_cmd: Nep330BuildCommand,
pub builder_abi_versions: BuilderAbiVersions,
pub override_nep330_contract_path: Nep330ContractPath,
pub override_cargo_target_path: Option<CargoTargetDir>,
}

impl CommonVariables {
pub fn new(
opts: &BuildOpts,
builder_version_info: &VersionInfo,
crate_metadata: &CrateMetadata,
override_cargo_target_path: Option<CargoTargetDir>,
) -> eyre::Result<Self> {
let nep330_version = Nep330Version::new(crate_metadata);
let nep330_link = Nep330Link::new(crate_metadata);

let nep330_build_cmd = Nep330BuildCommand::compute(opts)?;
let builder_abi_versions = builder_version_info.compute_env_variables()?;
let override_nep330_contract_path =
Nep330ContractPath::maybe_new(opts.override_nep330_contract_path.clone());
let result = Self {
nep330_version,
nep330_link,
nep330_build_cmd,
builder_abi_versions,
override_nep330_contract_path,
override_cargo_target_path,
};
Ok(result)
}
pub fn append_borrowed_to<'a>(&'a self, env: &mut Vec<(&str, &'a str)>) {
self.nep330_version.append_borrowed_to(env);
self.nep330_link.append_borrowed_to(env);
self.nep330_build_cmd.append_borrowed_to(env);
self.builder_abi_versions.append_borrowed_to(env);
self.override_nep330_contract_path.append_borrowed_to(env);
if let Some(ref target_dir) = self.override_cargo_target_path {
target_dir.append_borrowed_to(env);
}
}
}
2 changes: 1 addition & 1 deletion cargo-near-build/src/types/near/build/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Opts {
///
/// otherwise it's [ColorPreference::Always] if stderr is a terminal device,
/// and [ColorPreference::Never] in the remaining cases
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum ColorPreference {
Auto,
Always,
Expand Down

0 comments on commit 1f8e72c

Please sign in to comment.