How to make a library perform a specific behavior when compiling?

For example, ex_cldr download some files while compiling.

I’m making Doumi.Port that helps to use Python, Ruby in Elixir application.

Libraries that use Doumi.Port needs to install Python dependencies while compiling.
I tried to find documents for it, but failed.

How to make a library perform a specific behavior when compiling?

Thanks!

  1. Perform the task outside of a module. Then the code will be be executed at compile time.

  2. Or maybe better: create a custom mix task.

  3. Best: use tip 1 to check for deps during compile time and raise when Python deps are not found. This delegates the task of installing them to the user who can choose the best method.

Example:

# file: lib/foo.ex
defmodule Foo do
def deps_present? do
…..
end

# this is called at compile time
unless Foo.deps_present?(), do: raise “Install python deps”

Do notice that by default Foo won’t be recompiled (and check not reperformed) when the file does not change.

1 Like

Checking external dependencies at compile time doesn’t really solve the problem. The dependencies could be gone at any later time. You’ll want to check them whenever you access them.

Compare this to ex_cldr. Once the modules are compiled the source data is no longer needed, so it’s fine to just do that stuff at compile time. After compilation the modules are self sufficient.

5 Likes

Thanks!
Then, it is similar to rustler.
I have to look at it.

Why do you want to install python dependencies during compilation? I think that this is generally a bad approach, I would just leave package management to the specialized software and check in installations in compile time or during initialization in runtime

1 Like

Another extension point to accomplish this with would be to define a custom Mix.Task.Compiler, which has an entry point for defining how to handle stale sources, and can then be listed in a project’s mix configuration to hook into mix compile.

1 Like