Skip to content
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: macro recompile trigger #1061

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add docs
  • Loading branch information
segfault-magnet committed Jul 25, 2023
commit 19a025b4c091824eabcab93b03f35194637fe236
35 changes: 19 additions & 16 deletions packages/fuels-code-gen/src/program_bindings/abigen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::{collections::HashSet, path::PathBuf};

pub use abigen_target::{AbigenTarget, ProgramType};
use fuel_abi_types::abi::full_program::FullTypeDeclaration;
Expand Down Expand Up @@ -102,21 +102,8 @@ impl Abigen {
) -> Result<GeneratedCode> {
let mod_name = ident(&format!("{}_mod", &target.name.to_snake_case()));

let recompile_trigger = {
let code = target
.source
.path
.as_ref()
.map(|path| {
let stringified_path = path.display().to_string();
quote! {
const _: &[u8] = include_bytes!(#stringified_path);
}
})
.unwrap_or_default();
GeneratedCode::new(code, Default::default(), no_std)
};

let recompile_trigger =
Self::generate_macro_recompile_trigger(target.source.path.as_ref(), no_std);
let types = generate_types(&target.source.abi.types, shared_types, no_std)?;
let bindings = generate_bindings(target, no_std)?;
Ok(recompile_trigger
Expand All @@ -125,6 +112,22 @@ impl Abigen {
.wrap_in_mod(mod_name))
}

/// Any changes to the file pointed to by `path` will cause the reevaluation of the current
/// procedural macro. This is a hack until https://github.com/rust-lang/rust/issues/99515
/// lands.
fn generate_macro_recompile_trigger(path: Option<&PathBuf>, no_std: bool) -> GeneratedCode {
let code = path
.as_ref()
.map(|path| {
let stringified_path = path.display().to_string();
quote! {
const _: &[u8] = include_bytes!(#stringified_path);
}
})
.unwrap_or_default();
GeneratedCode::new(code, Default::default(), no_std)
}

fn parse_targets(targets: Vec<AbigenTarget>) -> Result<Vec<ParsedAbigenTarget>> {
targets
.into_iter()
Expand Down