-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
310 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
[package] | ||
name = "cargo-near" | ||
version = "0.1.0" | ||
authors = ["Near Inc <[email protected]>"] | ||
edition = "2021" | ||
rust-version = "1.56.0" | ||
license = "MIT OR Apache-2.0" | ||
readme = "README.md" | ||
repository = "https://github.com/near/cargo-near" | ||
description = """ | ||
Cargo extension for building Rust smart contracts on NEAR. | ||
Cargo extension for building Rust smart contracts on NEAR | ||
""" | ||
|
||
[dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::abi::AbiResult; | ||
use crate::cargo::{manifest::CargoManifestPath, metadata::CrateMetadata}; | ||
use crate::{abi, util, BuildCommand}; | ||
use colored::Colorize; | ||
use std::io::BufRead; | ||
|
||
const COMPILATION_TARGET: &str = "wasm32-unknown-unknown"; | ||
|
||
pub(crate) fn run(args: BuildCommand) -> anyhow::Result<()> { | ||
if !util::invoke_rustup(&["target", "list", "--installed"])? | ||
.lines() | ||
.any(|target| target.as_ref().map_or(false, |t| t == COMPILATION_TARGET)) | ||
{ | ||
anyhow::bail!("rust target `{}` is not installed", COMPILATION_TARGET); | ||
} | ||
|
||
let crate_metadata = CrateMetadata::collect(CargoManifestPath::try_from( | ||
args.manifest_path.unwrap_or_else(|| "Cargo.toml".into()), | ||
)?)?; | ||
|
||
let out_dir = util::force_canonicalize_dir( | ||
&args | ||
.out_dir | ||
.unwrap_or_else(|| crate_metadata.target_directory.clone()), | ||
)?; | ||
|
||
let mut build_env = vec![("RUSTFLAGS", "-C link-arg=-s")]; | ||
let mut cargo_args = vec!["--target", COMPILATION_TARGET]; | ||
if args.release { | ||
cargo_args.push("--release"); | ||
} | ||
|
||
let mut abi_path = None; | ||
if !args.no_abi { | ||
let AbiResult { path } = abi::write_to_file(&crate_metadata, args.doc)?; | ||
abi_path.replace(util::copy(&path, &out_dir)?); | ||
} | ||
|
||
if let (true, Some(abi_path)) = (args.embed_abi, &abi_path) { | ||
cargo_args.extend(&["--features", "near-sdk/__abi-embed"]); | ||
build_env.push(("CARGO_NEAR_ABI_PATH", abi_path.to_str().unwrap())); | ||
} | ||
|
||
let mut wasm_artifact = util::compile_project( | ||
&crate_metadata.manifest_path, | ||
&cargo_args, | ||
build_env, | ||
"wasm", | ||
)?; | ||
|
||
wasm_artifact.path = util::copy(&wasm_artifact.path, &out_dir)?; | ||
|
||
// todo! if we embedded, check that the binary exports the __contract_abi symbol | ||
println!("{}", "Contract Successfully Built!".green().bold()); | ||
println!( | ||
" - Binary: {}", | ||
wasm_artifact | ||
.path | ||
.display() | ||
.to_string() | ||
.bright_yellow() | ||
.bold() | ||
); | ||
if let Some(abi_path) = abi_path { | ||
println!( | ||
" - ABI: {}", | ||
abi_path.display().to_string().yellow().bold() | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.