How can I look at the final code generated by the macros in Parity Substrate?

Viewed 392

Substrate uses a lot of macros to make it easier to write runtime modules:

  • construct_runtime!
  • decl_module!
  • decl_storage!
  • decl_event!
  • etc...

However, it is hard to understand what these macros actually do and what the final code looks like. How can I dig deeper into these macros and expansions?

2 Answers

If you wanted to take a look at the final generated code for a crate, you can run the following:

cargo +nightly rustc --profile=check --package <crate-name> --lib -- -Zunstable-options --pretty=expanded > <output-file>

Note there are two variables here: <crate-name> and <output-file>.

Thus, if you wanted to look at your final runtime from the substrate-node-template, you would run:

cargo +nightly rustc --profile=check --package node-template-runtime --lib -- -Zunstable-options --pretty=expanded > substrate-node-template-runtime.rs

Or if you wanted to look at just the expansion of a single module like the Sudo module, you could do:

cargo +nightly rustc --profile=check --package srml-sudo --lib -- -Zunstable-options --pretty=expanded > sudo-module.rs

These would produce files with all the expanded code that look like this: https://gist.github.com/shawntabrizi/b4a1952dbd3af113e8a3498418e52741

Related