Skip to content

Commit

Permalink
gate idl building behind idl-gen feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kklas committed Mar 9, 2023
1 parent 3ccb67c commit 5da2b51
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 76 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ anyhow = "1.0.32"
syn = { version = "1.0.60", features = ["full", "extra-traits"] }
anchor-lang = { path = "../lang", version = "0.27.0" }
anchor-client = { path = "../client", version = "0.27.0" }
anchor-syn = { path = "../lang/syn", features = ["idl", "init-if-needed"], version = "0.27.0" }
anchor-syn = { path = "../lang/syn", features = ["idl-parse", "init-if-needed"], version = "0.27.0" }
serde_json = "1.0"
shellexpand = "2.1.0"
toml = "0.5.8"
Expand Down
2 changes: 2 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,8 @@ fn idl_build() -> Result<()> {
.args([
"test",
"__anchor_private_print_idl",
"--features",
"idl-gen",
"--",
"--show-output",
"--quiet",
Expand Down
12 changes: 11 additions & 1 deletion lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ allow-missing-optionals = ["anchor-derive-accounts/allow-missing-optionals"]
init-if-needed = ["anchor-derive-accounts/init-if-needed"]
derive = []
default = []
idl-gen = [
"anchor-syn/idl-gen",
"anchor-derive-accounts/idl-gen",
"anchor-derive-serde/idl-gen",
"anchor-attribute-account/idl-gen",
"anchor-attribute-constant/idl-gen",
"anchor-attribute-event/idl-gen",
"anchor-attribute-error/anchor-debug",
"anchor-attribute-program/idl-gen",
]
anchor-debug = [
"anchor-attribute-access-control/anchor-debug",
"anchor-attribute-account/anchor-debug",
Expand All @@ -36,7 +46,7 @@ anchor-derive-space = { path = "./derive/space", version = "0.27.0" }
anchor-derive-serde = { path = "./derive/serde", version = "0.27.0" }
# anchor-syn can and should only be included only for idl-gen. It won't compile
# for bpf due to proc-macro2 crate.
anchor-syn = { path = "./syn", version = "0.27.0" }
anchor-syn = { path = "./syn", version = "0.27.0", optional = true }
arrayref = "0.3.6"
base64 = "0.13.0"
borsh = "0.9"
Expand Down
1 change: 1 addition & 0 deletions lang/attribute/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"
proc-macro = true

[features]
idl-gen = ["anchor-syn/idl-gen"]
anchor-debug = ["anchor-syn/anchor-debug"]

[dependencies]
Expand Down
22 changes: 16 additions & 6 deletions lang/attribute/account/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate proc_macro;

#[cfg(feature = "idl-gen")]
use anchor_syn::idl::gen::*;
use quote::quote;
use syn::parse_macro_input;
Expand Down Expand Up @@ -393,17 +394,26 @@ pub fn zero_copy(
quote! {#[derive(::bytemuck::Zeroable)]}
};

let no_docs = false;
let idl_gen_impl = gen_idl_gen_impl_for_struct(&account_strct, no_docs);

proc_macro::TokenStream::from(quote! {
let ret = quote! {
#[derive(anchor_lang::__private::ZeroCopyAccessor, Copy, Clone)]
#repr
#pod
#zeroable
#account_strct
#idl_gen_impl
})
};

#[cfg(feature = "idl-gen")]
{
let no_docs = false;
let idl_gen_impl = gen_idl_gen_impl_for_struct(&account_strct, no_docs);
return proc_macro::TokenStream::from(quote! {
#ret
#idl_gen_impl
});
}

#[allow(unreachable_code)]
proc_macro::TokenStream::from(ret)
}

/// Defines the program's ID. This should be used at the root of all Anchor
Expand Down
1 change: 1 addition & 0 deletions lang/attribute/constant/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"
proc-macro = true

[features]
idl-gen = ["anchor-syn/idl-gen"]
anchor-debug = ["anchor-syn/anchor-debug"]

[dependencies]
Expand Down
32 changes: 19 additions & 13 deletions lang/attribute/constant/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate proc_macro;

use anchor_syn::idl::gen::gen_idl_print_function_for_constant;
use quote::quote;
#[cfg(feature = "idl-gen")]
use {anchor_syn::idl::gen::gen_idl_print_function_for_constant, quote::quote, syn};

/// A marker attribute used to mark const values that should be included in the
/// generated IDL but functionally does nothing.
Expand All @@ -10,18 +10,24 @@ pub fn constant(
_attr: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let ts = match syn::parse(input).unwrap() {
syn::Item::Const(item) => {
let idl_print = gen_idl_print_function_for_constant(&item);
quote! {
#item
#idl_print
#[cfg(feature = "idl-gen")]
{
let ts = match syn::parse(input).unwrap() {
syn::Item::Const(item) => {
let idl_print = gen_idl_print_function_for_constant(&item);
quote! {
#item
#idl_print
}
}
}
item => quote! {#item},
item => quote! {#item},
};

return proc_macro::TokenStream::from(quote! {
#ts
});
};

proc_macro::TokenStream::from(quote! {
#ts
})
#[allow(unreachable_code)]
input
}
1 change: 1 addition & 0 deletions lang/attribute/error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"
proc-macro = true

[features]
idl-gen = ["anchor-syn/idl-gen"]
anchor-debug = ["anchor-syn/anchor-debug"]

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions lang/attribute/event/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"
proc-macro = true

[features]
idl-gen = ["anchor-syn/idl-gen"]
anchor-debug = ["anchor-syn/anchor-debug"]

[dependencies]
Expand Down
18 changes: 13 additions & 5 deletions lang/attribute/event/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ pub fn event(
format!("{discriminator:?}").parse().unwrap()
};

let idl_gen = anchor_syn::idl::gen::gen_idl_print_function_for_event(&event_strct);

proc_macro::TokenStream::from(quote! {
let ret = quote! {
#[derive(anchor_lang::__private::EventIndex, AnchorSerialize, AnchorDeserialize)]
#event_strct

Expand All @@ -44,9 +42,19 @@ pub fn event(
impl anchor_lang::Discriminator for #event_name {
const DISCRIMINATOR: [u8; 8] = #discriminator;
}
};

#idl_gen
})
#[cfg(feature = "idl-gen")]
{
let idl_gen = anchor_syn::idl::gen::gen_idl_print_function_for_event(&event_strct);
return proc_macro::TokenStream::from(quote! {
#ret
#idl_gen
});
}

#[allow(unreachable_code)]
proc_macro::TokenStream::from(ret)
}

/// Logs an event that can be subscribed to by clients.
Expand Down
1 change: 1 addition & 0 deletions lang/attribute/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"
proc-macro = true

[features]
idl-gen = ["anchor-syn/idl-gen"]
anchor-debug = ["anchor-syn/anchor-debug"]

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions lang/derive/accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ proc-macro = true
allow-missing-optionals = ["anchor-syn/allow-missing-optionals"]
init-if-needed = ["anchor-syn/init-if-needed"]
default = []
idl-gen = ["anchor-syn/idl-gen"]
anchor-debug = ["anchor-syn/anchor-debug"]

[dependencies]
Expand Down
3 changes: 3 additions & 0 deletions lang/derive/serde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ edition = "2021"
proc-macro = true

[features]
idl-gen = [
"anchor-syn/idl-gen",
]

[dependencies]
proc-macro2 = "1.0"
Expand Down
44 changes: 27 additions & 17 deletions lang/derive/serde/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
extern crate proc_macro;

use anchor_syn::idl::gen::*;
use borsh_derive_internal::*;
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::{Ident, Item};

#[cfg(feature = "idl-gen")]
use {anchor_syn::idl::gen::*, quote::quote};

fn gen_borsh_serialize(input: TokenStream) -> TokenStream2 {
let cratename = Ident::new("borsh", Span::call_site());

Expand All @@ -27,25 +28,34 @@ fn gen_borsh_serialize(input: TokenStream) -> TokenStream2 {

#[proc_macro_derive(AnchorSerialize, attributes(borsh_skip))]
pub fn anchor_serialize(input: TokenStream) -> TokenStream {
let borsh = gen_borsh_serialize(input.clone());
#[cfg(not(feature = "idl-gen"))]
let ret = gen_borsh_serialize(input);
#[cfg(feature = "idl-gen")]
let ret = gen_borsh_serialize(input.clone());

let no_docs = false; // TODO
#[cfg(feature = "idl-gen")]
{
let no_docs = false; // TODO

let idl_gen_impl = match syn::parse(input).unwrap() {
Item::Struct(item) => gen_idl_gen_impl_for_struct(&item, no_docs),
Item::Enum(item) => gen_idl_gen_impl_for_enum(item, no_docs),
Item::Union(item) => {
// unions are not included in the IDL - TODO print a warning
idl_gen_impl_skeleton(quote! {None}, quote! {}, &item.ident, &item.generics)
}
// Derive macros can only be defined on structs, enums, and unions.
_ => unreachable!(),
let idl_gen_impl = match syn::parse(input).unwrap() {
Item::Struct(item) => gen_idl_gen_impl_for_struct(&item, no_docs),
Item::Enum(item) => gen_idl_gen_impl_for_enum(item, no_docs),
Item::Union(item) => {
// unions are not included in the IDL - TODO print a warning
idl_gen_impl_skeleton(quote! {None}, quote! {}, &item.ident, &item.generics)
}
// Derive macros can only be defined on structs, enums, and unions.
_ => unreachable!(),
};

return TokenStream::from(quote! {
#ret
#idl_gen_impl
});
};

TokenStream::from(quote! {
#borsh
#idl_gen_impl
})
#[allow(unreachable_code)]
TokenStream::from(ret)
}

fn gen_borsh_deserialize(input: TokenStream) -> TokenStream2 {
Expand Down
2 changes: 1 addition & 1 deletion lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub use borsh::de::BorshDeserialize as AnchorDeserialize;
pub use borsh::ser::BorshSerialize as AnchorSerialize;
pub use solana_program;

// TODO - add this behind feature gate
#[cfg(feature = "idl-gen")]
pub use anchor_syn;

pub type Result<T> = std::result::Result<T, error::Error>;
Expand Down
4 changes: 3 additions & 1 deletion lang/syn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ edition = "2021"
[features]
allow-missing-optionals = []
init-if-needed = []
idl = []
idl-gen = []
idl-parse = []
idl-types = []
hash = []
default = []
anchor-debug = []
Expand Down
19 changes: 14 additions & 5 deletions lang/syn/src/codegen/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
let __client_accounts_mod = __client_accounts::generate(accs);
let __cpi_client_accounts_mod = __cpi_client_accounts::generate(accs);

let no_docs = false;
let idl_gen_impl = crate::idl::gen::gen_idl_gen_impl_for_accounts_strct(accs, no_docs);

quote! {
let ret = quote! {
#impl_try_accounts
#impl_to_account_infos
#impl_to_account_metas
#impl_exit

#__client_accounts_mod
#__cpi_client_accounts_mod
};

#idl_gen_impl
#[cfg(feature = "idl-gen")]
{
#![allow(warnings)]
let no_docs = false;
let idl_gen_impl = crate::idl::gen::gen_idl_gen_impl_for_accounts_strct(&accs, no_docs);
return quote! {
#ret
#idl_gen_impl
};
}

#[allow(unreachable_code)]
ret
}

fn generics(accs: &AccountsStruct) -> ParsedGenerics {
Expand Down
25 changes: 18 additions & 7 deletions lang/syn/src/codegen/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{idl::gen::gen_idl_print_function_for_error, Error};
use crate::Error;
use quote::quote;

#[cfg(feature = "idl-gen")]
use crate::idl::gen::gen_idl_print_function_for_error;

pub fn generate(error: Error) -> proc_macro2::TokenStream {
let error_enum = &error.raw_enum;
let enum_name = &error.ident;
Expand Down Expand Up @@ -47,17 +50,15 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream {
})
.collect();

let idl_gen = gen_idl_print_function_for_error(&error);

let offset = match error.args {
let offset = match &error.args {
None => quote! { anchor_lang::error::ERROR_CODE_OFFSET},
Some(args) => {
let offset = &args.offset;
quote! { #offset }
}
};

quote! {
let ret = quote! {
#[derive(std::fmt::Debug, Clone, Copy)]
#[repr(u32)]
#error_enum
Expand Down Expand Up @@ -98,7 +99,17 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream {
}
}
}
};

#[cfg(feature = "idl-gen")]
{
let idl_gen = gen_idl_print_function_for_error(&error);
return quote! {
#ret
#idl_gen
};
};

#idl_gen
}
#[allow(unreachable_code)]
ret
}
Loading

0 comments on commit 5da2b51

Please sign in to comment.