[erlang-questions] Cover compilation vs. dialyzer

Roger Lipscombe roger@REDACTED
Tue Mar 3 21:30:15 CET 2015


I've got a couple of modules in my project which exist purely to hold
binary objects. That is: given a file foo.bin, I generate a module
foo_bin, with a function bin/0 which has the original file inlined in
it.

Something like this:

embed_file_in_beam(InputPath) ->
    {ok, Bytes} = file:read_file(InputPath),
    ModuleName = get_module_name(filename:basename(InputPath)),
    OutputPath = filename:join("ebin", ModuleName ++ ".beam"),

    % We'll export a function Mod:bin/0.
    Mod = list_to_atom(ModuleName),
    Fun = 'bin',

    % An Erlang module is a list of forms; we need three:
    Forms = [
        module_form(Mod), % -module(foo).
        export_form(Fun), % -export([bin/0]).
        function_form(Fun, Bytes) % bin() -> <<...>>.
    ],

    {ok, Mod, Bin} = compile:forms(Forms, [debug_info]),
    ok = file:write_file(OutputPath, Bin).

function_form(Fun, Binary) ->
    % a function,
    {function, 1, Fun, 0,
        % with one clause,
        [{clause, 1, [], [],
            % which has one expression, a binary,
            [{bin, 1, [
                % made up of the stuff we first thought of.
                {bin_element, 1, {integer, 1, Byte}, default, default}
                || <<Byte:8>> <= Binary
            ]}
        ]}
    ]}.

However, cover compilation and dialyzer are fighting each-other over
whether these files should include debug information or not.

If I _don't_ include the debug_info option, then dialyzer fails
complaining that there's no abstract code.

If I _do_ include the debug_info option, then cover compilation fails,
apparently while trying to find the original source file. Which
doesn't exist.

How can I keep both tools happy?

Or, alternatively, is there a better way to embed binary resources in
an Erlang module? And by "better", I don't mean "stick 'em in the priv
directory".



More information about the erlang-questions mailing list