erlangmakefilelfe

Compiling LFE files with make


Is there a standard way of compiling .lfe source files from a make rule in an OTP project?

According to the docs, I'm supposed to use lfe_comp:file/1, which doesn't help much if I want to compile multiple such files in an OTP application (where I'm supposed to keep the source files in src, but the binaries in ebin).

Ideally, I'd be able to do something like

erlc -Wf -o ebin src/*lfe

But there doesn't seem to be lfe support in erlc. The best solution I can think of off the top of my head is

find src/*lfe -exec erl -s lfe_comp file {} -s init stop \;
mv src/*beam ebin/

but that seems inelegant. Any better ideas?


Solution

  • On suggestion from rvirding, here's a first stab at lfec that does what I want above (and pretty much nothing else). I'd invoke it from a Makefile with ./lfec -o ebin src/*lfe.

    #!/usr/bin/env escript
    %% -*- erlang -*-
    %%! -smp enable -sname lfec -mnesia debug verbose
    main(Arguments) ->
        try
            {Opts, Args} = parse_opts(Arguments),
            case get_opt("-o", Opts) of
                false ->
                   lists:map(fun lfe_comp:file/1, Args);
                Path ->
                   lists:map(fun (Arg) -> lfe_comp:file(Arg, [{outdir, Path}]) end,
                             Args)
            end
        catch
            _:_ -> usage()
        end;
    main(_) -> usage().
    
    get_opt(Target, Opts) -> 
        case lists:keyfind(Target, 1, Opts) of
            false -> false;
            {_} -> true;
            {_, Setting} -> Setting
        end.
    
    parse_opts(Args) -> parse_opts(Args, []).
    parse_opts(["-o", TargetDir | Rest], Opts) ->
        parse_opts(Rest, [{"-o", TargetDir} | Opts]);
    parse_opts(Args, Opts) -> {Opts, Args}.
    
    usage() ->
        io:format("usage:\n"),
        io:format("-o [TargetDir] -- output files to specified directory\n"),
        halt(1).