Why is __after_compile__/2 called before __before_compile__/1?

defmodule Bar do
  @after_compile __MODULE__

  defmacro __before_compile__(_env) do
    IO.inspect("#{__MODULE__}.__before_compile__")
  end

  def __after_compile__(_env, _bytecode) do
    IO.inspect("#{__MODULE__}.__after_compile__")
  end
end

defmodule Foo do
  require Bar

  @before_compile Bar
  @after_compile __MODULE__

  def __after_compile__(_env, _bytecode) do
    IO.inspect("#{__MODULE__}.__after_compile__")
  end
end

Will print:

“Elixir.Bar.after_compile
“Elixir.Bar.before_compile
“Elixir.Foo.after_compile
[Bar, Foo]

Why is after_compile called before before_compile for the Bar module?

Because you are compiling Foo after Bar.

2 Likes
  1. Bar is compiled, you see its output of Bar.__after_compile__
  2. Foo is going to get compiled, it calls Bar.__before_compile__, you see the output Bar.__before_compile__, because __MODULE__ is Bar in that context. You wan’t to check env.mod to see the callers module.
  3. Foo gets actuially compiled, its after compile gets called and you see its Foo.__after_compile__.
5 Likes

Thank you! Can I ask something more as you seem to have good knowledge of macros?

defmodule Baz do
  defmacro __using__(_opts), do: Module.register_attribute(__MODULE__, :attr, [])
end

defmodule MyApp do
  use Baz
end

== Compilation error in file test.exs ==
** (ArgumentError) could not call Module.register_attribute/3 because the module Baz is already compiled

Why does it say Baz is already compiled, when I’m executing __using__? As to my understanding, __using__ is run on compile time, so the file should not be compiled yet?

Baz is already compiled when used in MyApp. MyApp is not yet compiled. In macros it’s important to be aware if you‘re working in the context of the macro (__MODULE__ is the module with the macro definition) or if you‘re working on the AST of the macros result, which is effectively becoming part of the module calling the macro (__MODULE__ refers to the module calling the macro).

2 Likes

So if I understand right, if a module A calls __using__ of a module B, then __using__ macro of B will be executed after B is compiled.

Then if I try to register an attribute on another module:

defmodule Bar do
end

defmodule Baz do
  defmacro __using__(_), do: Module.register_attribute(Bar, :attr, [])
end

defmodule MyApp do
  use Baz
end

But again:

** (ArgumentError) could not call Module.register_attribute/3 because the module Bar is already compiled

So Bar is already compiled, but how can I tell that it should not compile before registering the attributes in the __using__ macro of Baz?

I tried require MyApp into Bar, which I thought means "wait for MyApp to be compiled before the compilation of Bar", then I am sure that Bar will not be compiled yet, but obviously got it all wrong again.

In your example there’s no compile time relationship between MyApp(/Baz) and Bar, so they’re compiled in parallel. I’m not sure though why exactly it doesn’t work with require MyApp added.

But I guess more important would be to know why you want to do that. Why should calling use Baz register a module attribute on a third module?

These were indeed all examples with Foo/Bar/Baz placeholders because I did not want to bloat the problem with application-specific details. But here is what I try to do:

I want my library to receive options at compile time, e.g. the user can pass option otp_app: :my_app to TheLibrary somehow. Often this is done with use but I don’t need to inject anything in a user’s module.

Then in some module of the library, I need to use those options in a __before_compile__/1 macro (because based on those options, I want to generate some functions into that module in the library).

  1. I could not find a way for __before_compile__ to get access to the option; keep having the blah-blah-blah-already-compiled.

  2. I do not know what is the best way for the user to pass the option to the library.

As the option is already compile time only you can just as easily use the app env for configuration and the value will be available everywhere without intruducing compile time dependencies just to pass along configuration values.

Again, you need to look into the callers environment. Macros get passed __CALLER__ as a magic variable, so you probably want to Module.register_attribute(__CALLER__.mod, :attr, []) or in this case even more appropriate, as you do not make it accumulating or persistent:

defmacro __using__(_opts) do
  quote do
    @attr []
  end
end

Or instead of the empty list use any other default value you prefer.

Beeing aware of the current context you are in is important for macros.

Perhaps give Metaprogramming Elixir by @chrismccord a read? I only skimmed about the first quarter of it, but it helped me to understand macros enough for my usecase.

1 Like
defmodule A do
  defmacro __using__(opts) do
    otp_app = opts[:otp_app]      # this gets set to :foo
    quote do                           # note 'use' is called by B, so the next two lines live in B.
      @otp_app unquote(otp_app)  # becomes @otp_app :foo
      @before_compile A 
    end
  end

  defmacro __before_compile__(_) do
    __CALLER__.module
    |> IO.inspect(label: "module")
    |> Module.get_attribute(:otp_app)
    |> IO.inspect(label: "otp app")
    quote do end
  end
end

defmodule B do
  use A, otp_app: :foo
end

compile-time output:

module: B
otp app: :foo

If it’s clearer, this is effectively equivalent:

defmodule A do
  defmacro __using__(opts) do
    otp_app = opts[:otp_app]
    quote do
      @otp_app unquote(otp_app)
    end
  end

  defmacro __before_compile__(_) do
    __CALLER__.module
    |> IO.inspect(label: "module")
    |> Module.get_attribute(:otp_app)
    |> IO.inspect(label: "otp app")
    quote do end
  end
end

defmodule B do
  use A, otp_app: :foo
  @before_compile A
end
1 Like