Skip to content

Commit

Permalink
IDL 2.100.0 update (#74)
Browse files Browse the repository at this point in the history
* supports high maintenance mode in margin calc
  • Loading branch information
jordy25519 authored Nov 18, 2024
1 parent be4d4e3 commit af5187f
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
rustup component add clippy rustfmt
- name: install libdrift_ffi_sys
run: |
curl -L https://github.com/user-attachments/files/17160233/libdrift_ffi_sys.so.zip > ffi.zip
curl -L https://github.com/user-attachments/files/17806677/libdrift_ffi_sys.so.zip > ffi.zip
unzip ffi.zip
ldd libdrift_ffi_sys.so
sudo cp libdrift_ffi_sys.so /usr/lib
Expand Down
31 changes: 16 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ fn main() {
fail_build();
}

// install the dylib to system path
let libffi_out_path =
drift_ffi_sys_crate.join(Path::new(&format!("target/{profile}/{LIB}.{lib_ext}")));

// Build ffi crate and link
let mut ffi_build = std::process::Command::new("rustup");
ffi_build
Expand Down Expand Up @@ -89,9 +93,6 @@ fn main() {
String::from_utf8_lossy(output.stderr.as_slice())
);
}
// install the dylib to system path
let libffi_out_path =
drift_ffi_sys_crate.join(Path::new(&format!("target/{profile}/{LIB}.{lib_ext}")));

if let Ok(out_dir) = std::env::var("OUT_DIR") {
let _output = std::process::Command::new("cp")
Expand All @@ -103,19 +104,19 @@ fn main() {
.expect("install ok");
println!("{LIB}: searching for lib at: {out_dir}");
println!("cargo:rustc-link-search=native={out_dir}");
} else {
let _output = std::process::Command::new("ln")
.args([
"-sf",
libffi_out_path.to_str().expect("ffi build path"),
"/usr/local/lib/",
])
.output()
.expect("install ok");

println!("{LIB}: searching for lib at: /usr/local/lib");
println!("cargo:rustc-link-search=native=/usr/local/lib");
}

let _output = std::process::Command::new("ln")
.args([
"-sf",
libffi_out_path.to_str().expect("ffi build path"),
"/usr/local/lib/",
])
.output()
.expect("install ok");

println!("{LIB}: searching for lib at: /usr/local/lib");
println!("cargo:rustc-link-search=native=/usr/local/lib");
}

if let Ok(lib_path) = std::env::var("CARGO_DRIFT_FFI_PATH") {
Expand Down
2 changes: 1 addition & 1 deletion crates/drift-ffi-sys
Submodule drift-ffi-sys updated 5 files
+57 −57 Cargo.lock
+2 −2 Cargo.toml
+16 −0 README.md
+11 −3 src/exports.rs
+1 −1 src/types.rs
74 changes: 52 additions & 22 deletions crates/drift-idl-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,47 +264,77 @@ fn generate_idl_types(idl: &Idl) -> String {
for account in &idl.accounts {
let struct_name = Ident::new(&account.name, proc_macro2::Span::call_site());

let struct_fields = account.account_type.fields.iter().map(|field| {
let field_name =
Ident::new(&to_snake_case(&field.name), proc_macro2::Span::call_site());
let mut has_vec_field = false;
let struct_fields: Vec<TokenStream> = account
.account_type
.fields
.iter()
.map(|field| {
let field_name =
Ident::new(&to_snake_case(&field.name), proc_macro2::Span::call_site());
if let ArgType::Vec { .. } = field.field_type {
has_vec_field = true;
}
let mut serde_decorator = TokenStream::new();
let mut field_type: Type =
syn::parse_str(&field.field_type.to_rust_type()).unwrap();
// workaround for padding types preventing outertype from deriving 'Default'
if field_name == "padding" {
if let ArgType::Array { array: (_t, len) } = &field.field_type {
field_type = syn::parse_str(&format!("Padding<{len}>")).unwrap();
serde_decorator = quote! {
#[serde(skip)]
};
}
}

let mut serde_decorator = TokenStream::new();
let mut field_type: Type = syn::parse_str(&field.field_type.to_rust_type()).unwrap();
// workaround for padding types preventing outertype from deriving 'Default'
if field_name == "padding" {
if let ArgType::Array { array: (_t, len) } = &field.field_type {
field_type = syn::parse_str(&format!("Padding<{len}>")).unwrap();
serde_decorator = quote! {
#[serde(skip)]
};
quote! {
#serde_decorator
pub #field_name: #field_type,
}
})
.collect();

let derive_tokens = if !has_vec_field {
quote! {
#[derive(AnchorSerialize, AnchorDeserialize, InitSpace, Serialize, Deserialize, Copy, Clone, Default, Debug, PartialEq)]
}
} else {
// can't derive `Copy` on accounts with `Vec` field
// `InitSpace` requires a 'max_len' but no point enforcing here if unset on program side
quote! {
#[derive(AnchorSerialize, AnchorDeserialize, Serialize, Deserialize, Clone, Default, Debug, PartialEq)]
}
};

let zc_tokens = if !has_vec_field {
// without copy can't derive the ZeroCopy trait
quote! {
#serde_decorator
pub #field_name: #field_type,
#[automatically_derived]
unsafe impl anchor_lang::__private::bytemuck::Pod for #struct_name {}
#[automatically_derived]
unsafe impl anchor_lang::__private::bytemuck::Zeroable for #struct_name {}
#[automatically_derived]
impl anchor_lang::ZeroCopy for #struct_name {}
}
});
} else {
Default::default()
};

let discriminator: TokenStream = format!("{:?}", sighash("account", &account.name))
.parse()
.unwrap();
let struct_def = quote! {
#[repr(C)]
#[derive(AnchorSerialize, AnchorDeserialize, InitSpace, Serialize, Deserialize, Copy, Clone, Default, Debug, PartialEq)]
#derive_tokens
pub struct #struct_name {
#(#struct_fields)*
}
#[automatically_derived]
impl anchor_lang::Discriminator for #struct_name {
const DISCRIMINATOR: [u8; 8] = #discriminator;
}
#[automatically_derived]
unsafe impl anchor_lang::__private::bytemuck::Pod for #struct_name {}
#[automatically_derived]
unsafe impl anchor_lang::__private::bytemuck::Zeroable for #struct_name {}
#[automatically_derived]
impl anchor_lang::ZeroCopy for #struct_name {}
#zc_tokens
#[automatically_derived]
impl anchor_lang::AccountSerialize for #struct_name {
fn try_serialize<W: std::io::Write>(&self, writer: &mut W) -> anchor_lang::Result<()> {
Expand Down
Loading

0 comments on commit af5187f

Please sign in to comment.