How to compile .ex code into .S assembly, like erl -S

Setting ERL_COMPILER_OPTIONS drops .S files alongside the files it compiles - for instance, running ERL_COMPILER_OPTIONS="'S'" mix compile on a project locally fails with the message:

$ ERL_COMPILER_OPTIONS="'S'" mix compile
** (CompileError) mix.exs: could not compile module Relay.Mixfile. We expected the compiler to return a .beam binary but got something else. This usually happens because ERL_COMPILER_OPTIONS or @compile was set to change the compilation outcome in a way that is incompatible with Elixir

Which also leaves a mix.exs.S file in the working directory.

You can customize the compiler options on a per-module basis with the @compile attribute (see previous discussion), so a module like this in a file lib/foo.ex:

defmodule Foo do
  @compile :S

  def foo(a, b) do
    a + b
  end
end

Running mix compile when a file has this @compile in it will cause compilation to fail, but will also drop a foo.ex.S file in the current working directory.

2 Likes