2

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?

Inaimathi
  • 13,853
  • 9
  • 49
  • 93

2 Answers2

4

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).
Inaimathi
  • 13,853
  • 9
  • 49
  • 93
  • @rvirding - feel free to; that's what it's here for. – Inaimathi Jul 03 '12 at 14:51
  • I HAVE borrowed it and added a simple lfec to the development branch. As Joe once "Robert is incapable of making small changes to anything", which is true so I rewrote it. Now I just need to work out which options are useful in an LFE context. – rvirding Aug 13 '12 at 11:13
1

Not really. LFE is not supported by OTP so erlc does not know about .lfe files. And as far as I know there is no way to "open up" erlc and dynamically add information how to process files. An alternative would be to write an lfec script for this. I will think about it.

Just as a matter of interest what are using LFE for?

rvirding
  • 20,848
  • 2
  • 37
  • 56
  • I've posted a first stab at it. I'm mostly using LFE for fun at the moment. Certainly nothing I couldn't do with vanilla Erlang. It just looks interesting, and I'm partial to Lisp, so I'm playing with writing some simple components in it to get comfortable. – Inaimathi Jul 02 '12 at 05:09
  • 1
    @Inaimathi OK, there is a Google group http://groups.google.com/group/lisp-flavoured-erlang with some traffic, not much. The develop branch on github has some new stuff which hasn't made it to the master branch yet. – rvirding Jul 02 '12 at 09:21