-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: return JSON-based ABI generation #73
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,165 @@ | ||
use crate::cargo::manifest::CargoManifestPath; | ||
use crate::cargo::metadata::CrateMetadata; | ||
use anyhow::Context; | ||
use quote::{format_ident, quote}; | ||
use std::path::{Path, PathBuf}; | ||
use std::{collections::HashSet, fs}; | ||
use toml::value; | ||
|
||
pub(crate) fn generate_toml( | ||
manifest_path: &CargoManifestPath, | ||
crate_metadata: &CrateMetadata, | ||
) -> anyhow::Result<String> { | ||
let original_cargo_toml = fs::read_to_string(&manifest_path.path)?; | ||
let original_cargo_toml: toml::value::Table = toml::from_str(&original_cargo_toml)?; | ||
|
||
let mut near_sdk = original_cargo_toml | ||
.get("dependencies") | ||
.context("Cargo.toml [dependencies] section not found")? | ||
.get("near-sdk") | ||
.context("`near-sdk` dependency not found")? | ||
.as_table() | ||
.context("`near-sdk` dependency should be a table")? | ||
.clone(); | ||
|
||
let cargo_toml = include_str!("../../templates/_Cargo.toml"); | ||
let mut cargo_toml: toml::value::Table = toml::from_str(cargo_toml)?; | ||
let package = cargo_toml | ||
.get_mut("package") | ||
.context("Cargo.toml template [package] section not found")? | ||
.as_table_mut() | ||
.context("expected Cargo.toml template [package] section to be a table")?; | ||
package.insert( | ||
"name".to_string(), | ||
toml::value::Value::String(format!("{}-near-abi-gen", crate_metadata.root_package.name)), | ||
); | ||
let deps = cargo_toml | ||
.get_mut("dependencies") | ||
.context("Cargo.toml template [dependencies] section not found")? | ||
.as_table_mut() | ||
.context("expected Cargo.toml template [dependencies] section to be a table")?; | ||
|
||
// Make near-sdk dependency not use default features to save on compilation time, but ensure `abi` is enabled | ||
near_sdk.remove("optional"); | ||
near_sdk.insert("default-features".to_string(), value::Value::Boolean(false)); | ||
near_sdk.insert( | ||
"features".to_string(), | ||
value::Value::Array(vec![value::Value::String("abi".to_string())]), | ||
); | ||
|
||
// If near-sdk is a local path dependency, then convert the path to be absolute | ||
if let Some(near_sdk_path) = near_sdk.get_mut("path") { | ||
let path = near_sdk_path | ||
.as_str() | ||
.context("`near-sdk` path should be a string")?; | ||
let path = manifest_path.directory()?.join(PathBuf::from(path)); | ||
*near_sdk_path = value::Value::String(path.canonicalize()?.to_string_lossy().into()); | ||
} | ||
|
||
deps.insert("near-sdk".into(), near_sdk.into()); | ||
|
||
let cargo_toml = toml::to_string(&cargo_toml)?; | ||
|
||
log::debug!("Cargo.toml contents:\n{}", &cargo_toml); | ||
|
||
Ok(cargo_toml) | ||
} | ||
|
||
pub(crate) fn generate_build_rs(dylib_path: &Path) -> anyhow::Result<String> { | ||
let dylib_dir = dylib_path.parent().ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"Unable to infer the directory containing dylib file: {}", | ||
dylib_path.display() | ||
) | ||
})?; | ||
let dylib_name = dylib_path | ||
.file_stem() | ||
.ok_or_else(|| anyhow::anyhow!("Generated dylib is not a file: {}", dylib_path.display()))? | ||
.to_str() | ||
.ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"Unable to infer the directory containing dylib file: {}", | ||
dylib_path.display() | ||
) | ||
})?; | ||
|
||
let dylib_name = if let Some(dylib_name_stripped) = dylib_name.strip_prefix("lib") { | ||
dylib_name_stripped | ||
} else { | ||
anyhow::bail!( | ||
"Expected the generated dylib file to start with 'lib', but got '{}'", | ||
dylib_name | ||
); | ||
}; | ||
|
||
let cargo_link_lib = format!("cargo:rustc-link-lib=dylib={}", &dylib_name); | ||
let cargo_link_search = format!("cargo:rustc-link-search=all={}", dylib_dir.display()); | ||
|
||
let build_rs = quote! { | ||
fn main() { | ||
println!(#cargo_link_lib); | ||
println!(#cargo_link_search); | ||
} | ||
} | ||
.to_string(); | ||
let build_rs_file = syn::parse_file(&build_rs).unwrap(); | ||
let build_rs = prettyplease::unparse(&build_rs_file); | ||
|
||
log::debug!("build.rs contents:\n{}", &build_rs); | ||
|
||
Ok(build_rs) | ||
} | ||
|
||
pub(crate) fn generate_main_rs(dylib_path: &Path) -> anyhow::Result<String> { | ||
let dylib_file_contents = fs::read(&dylib_path)?; | ||
let object = symbolic_debuginfo::Object::parse(&dylib_file_contents)?; | ||
log::debug!( | ||
"A dylib was built at {:?} with format {} for architecture {}", | ||
&dylib_path, | ||
&object.file_format(), | ||
&object.arch() | ||
); | ||
let near_abi_symbols = object | ||
.symbols() | ||
.flat_map(|sym| sym.name) | ||
.filter(|sym_name| sym_name.starts_with("__near_abi_")) | ||
.map(|sym_name| sym_name.to_string()) | ||
.collect::<HashSet<_>>(); | ||
if near_abi_symbols.is_empty() { | ||
anyhow::bail!("No NEAR ABI symbols found in the dylib"); | ||
} | ||
log::debug!("Detected NEAR ABI symbols: {:?}", &near_abi_symbols); | ||
|
||
let near_abi_function_defs = near_abi_symbols.iter().map(|s| { | ||
let name = format_ident!("{}", s); | ||
quote! { | ||
fn #name() -> near_sdk::__private::ChunkedAbiEntry; | ||
} | ||
}); | ||
let near_abi_function_invocations = near_abi_symbols.iter().map(|s| { | ||
let name = format_ident!("{}", s); | ||
quote! { | ||
unsafe { #name() } | ||
} | ||
}); | ||
|
||
let main_rs = quote! { | ||
extern "Rust" { | ||
#(#near_abi_function_defs)* | ||
} | ||
|
||
fn main() -> Result<(), std::io::Error> { | ||
let abi_entries: Vec<near_sdk::__private::ChunkedAbiEntry> = vec![#(#near_abi_function_invocations),*]; | ||
let contents = serde_json::to_string_pretty(&abi_entries)?; | ||
print!("{}", contents); | ||
Ok(()) | ||
} | ||
} | ||
.to_string(); | ||
let main_rs_file = syn::parse_file(&main_rs).unwrap(); | ||
let main_rs = prettyplease::unparse(&main_rs_file); | ||
|
||
log::debug!("main.rs contents:\n{}", &main_rs); | ||
|
||
Ok(main_rs) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can actually move this bit to the SDK, simplifying things. See alt: #74
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, it may look similar but there is an important difference that I have touched upon here: #74 (review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was thinking a Rust
String
might be safe, but since we don't have that guarantee, it's just best to switch to the C ABI.