Mix compiler that regenerates Elixir source from .fbs schemas before
the Elixir compiler runs. Wire it into your project by adding :flatbuf
to the :compilers list in mix.exs and configuring schema paths in
application config:
def project do
[
# ...
compilers: [:flatbuf | Mix.compilers()]
]
end
# config/config.exs
config :my_app, :flatbuf,
schemas: ["priv/fbs/monster.fbs"],
out: "lib/my_app/schema",
namespace: "MyApp.Schema",
wire_module: "MyApp.Schema.Wire",
include: ["priv/fbs"]How it works
On each mix compile, the configured schemas are parsed, resolved,
and code-generated. An output file is only (re)written when it is
missing or its on-disk content differs from the freshly generated
source — unchanged artifacts keep their mtime, so downstream
compilers don't see spurious changes. A manifest under
_build/<env>/lib/<app>/.mix/compile.flatbuf records schema digests,
option digests, and the list of files last emitted. Outputs whose
schema was removed from :schemas are deleted on the next compile,
so a stale .ex from an old schema doesn't linger.
Configuration keys (config :my_app, :flatbuf)
:schemas(required) — list of paths to.fbsfiles. Each is processed independently; cross-schema references go throughincludestatements (or the--includesearch path).:out— output directory; defaults to"lib".:namespace— root namespace override; seemix flatbuf.gen.:wire_module— name of the emitted wire helper module; defaults toFlatbuf.Generated.Wire.:include— list of include search paths.:niceties— list of atoms enabling generated-table niceties::behaviour(implementsFlatbuf.Table),:jason(derivesJason.Encoder). Unknown atoms abort the compile.
Nicety dependency caveats
Niceties add references the consuming project must satisfy:
:jason— the generated@derive Jason.Encoderonly compiles if the project depends on:jason.:behaviour— the generated@behaviour Flatbuf.Tableneeds:flatbufavailable when the generated code compiles. With aonly: [:dev, :test]dependency on:flatbuf, prod compiles warn that theFlatbuf.Tablebehaviour is undefined (a hard failure under--warnings-as-errors). Note that using this compiler at all requires:flatbufin the compile path, so pair:behaviourwith a regular dependency.
Summary
Functions
Internal compile entry point with the manifest path threaded
explicitly. Used by run/1 and by tests that want to drive the
compiler outside Mix's project stack.