Skip to content

Commit

Permalink
idl: Remove anchor-syn dependency (#3030)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jun 16, 2024
1 parent 1c2aabe commit d4318cc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Add `idl type` command ([#3017](https://github.com/coral-xyz/anchor/pull/3017)).
- lang: Add `anchor_lang::pubkey` macro for declaring `Pubkey` const values ([#3021](https://github.com/coral-xyz/anchor/pull/3021)).
- cli: Sync program ids on the initial build ([#3023](https://github.com/coral-xyz/anchor/pull/3023)).
- idl: Remove `anchor-syn` dependency ([#3030](https://github.com/coral-xyz/anchor/pull/3030)).

### Fixes

Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

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

3 changes: 1 addition & 2 deletions idl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
build = ["anchor-syn", "regex"]
build = ["regex"]
convert = ["heck", "sha2"]

[dependencies]
Expand All @@ -21,7 +21,6 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

# `build` feature only
anchor-syn = { path = "../lang/syn", version = "0.30.0", optional = true }
regex = { version = "1", optional = true }

# `convert` feature only
Expand Down
17 changes: 6 additions & 11 deletions idl/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
process::{Command, Stdio},
};

use anchor_syn::parser::context::CrateContext;
use anyhow::{anyhow, Result};
use regex::Regex;
use serde::Deserialize;
Expand Down Expand Up @@ -51,15 +50,7 @@ pub fn build_idl(
skip_lint: bool,
no_docs: bool,
) -> Result<Idl> {
// Check safety comments
let program_path = program_path.as_ref();
let lib_path = program_path.join("src").join("lib.rs");
let ctx = CrateContext::parse(lib_path)?;
if !skip_lint {
ctx.safety_checks()?;
}

let idl = build(program_path, resolution, no_docs)?;
let idl = build(program_path.as_ref(), resolution, skip_lint, no_docs)?;
let idl = convert_module_paths(idl);
let idl = sort(idl);
verify(&idl)?;
Expand All @@ -68,7 +59,7 @@ pub fn build_idl(
}

/// Build IDL.
fn build(program_path: &Path, resolution: bool, no_docs: bool) -> Result<Idl> {
fn build(program_path: &Path, resolution: bool, skip_lint: bool, no_docs: bool) -> Result<Idl> {
// `nightly` toolchain is currently required for building the IDL.
let toolchain = std::env::var("RUSTUP_TOOLCHAIN")
.map(|toolchain| format!("+{}", toolchain))
Expand All @@ -95,6 +86,10 @@ fn build(program_path: &Path, resolution: bool, no_docs: bool) -> Result<Idl> {
"ANCHOR_IDL_BUILD_RESOLUTION",
if resolution { "TRUE" } else { "FALSE" },
)
.env(
"ANCHOR_IDL_BUILD_SKIP_LINT",
if skip_lint { "TRUE" } else { "FALSE" },
)
.env("ANCHOR_IDL_BUILD_PROGRAM_PATH", program_path)
.env("RUSTFLAGS", "--cfg procmacro2_semver_exempt")
.current_dir(program_path)
Expand Down
29 changes: 27 additions & 2 deletions lang/syn/src/idl/program.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::Result;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
Expand All @@ -7,10 +9,15 @@ use super::{
common::{gen_print_section, get_idl_module_path, get_no_docs},
defined::gen_idl_type,
};
use crate::{parser::docs, Program};
use crate::{
parser::{context::CrateContext, docs},
Program,
};

/// Generate the IDL build print function for the program module.
pub fn gen_idl_print_fn_program(program: &Program) -> TokenStream {
check_safety_comments().unwrap_or_else(|e| panic!("Safety checks failed: {e}"));

let idl = get_idl_module_path();
let no_docs = get_no_docs();

Expand Down Expand Up @@ -139,3 +146,21 @@ pub fn gen_idl_print_fn_program(program: &Program) -> TokenStream {
}
}
}

/// Check safety comments.
fn check_safety_comments() -> Result<()> {
let skip_lint = option_env!("ANCHOR_IDL_BUILD_SKIP_LINT")
.map(|val| val == "TRUE")
.unwrap_or_default();
if skip_lint {
return Ok(());
}

std::env::var("ANCHOR_IDL_BUILD_PROGRAM_PATH")
.map(PathBuf::from)
.map(|path| path.join("src").join("lib.rs"))
.map_err(|_| anyhow!("Failed to get program path"))
.map(CrateContext::parse)?
.map_err(|e| anyhow!("Failed to parse crate: {e}"))?
.safety_checks()
}

0 comments on commit d4318cc

Please sign in to comment.