%%% BEGIN erlaunch_erlc.erl %%%
%%%
%%% erlaunch - execute selected Erlang functions from system command line
%%% Copyright (c)2003-2004 Cat's Eye Technologies. All rights reserved.
%%%
%%% Redistribution and use in source and binary forms, with or without
%%% modification, are permitted provided that the following conditions
%%% are met:
%%%
%%% Redistributions of source code must retain the above copyright
%%% notice, this list of conditions and the following disclaimer.
%%%
%%% Redistributions in binary form must reproduce the above copyright
%%% notice, this list of conditions and the following disclaimer in
%%% the documentation and/or other materials provided with the
%%% distribution.
%%%
%%% Neither the name of Cat's Eye Technologies nor the names of its
%%% contributors may be used to endorse or promote products derived
%%% from this software without specific prior written permission.
%%%
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
%%% CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
%%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
%%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
%%% DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
%%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
%%% OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
%%% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
%%% OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
%%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
%%% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
%%% OUT OF THE USE OF THIS SOFTWARE, EVEN IF AsDVISED OF THE
%%% POSSIBILITY OF SUCH DAMAGE.
%% @doc Implementation of the erlc compiler command-line
%% interface through erlaunch.
%%
%%
This is not, as of yet, 100% faithful to erlc's
%% interface. Only .erl and .yrl files are
%% supported. The ERLC_EMULATOR environment variable is
%% ignored as essentially meaningless in this context. Support for
%% other things may be shaky.
%%
%% @end
-module(erlaunch_erlc).
-vsn('2004.0309').
-author('cpressey@catseye.mine.nu').
-copyright('Copyright (c)2003-2004 Cat`s Eye Technologies. All rights reserved.').
-export([erlc/1]).
%% @spec erlc([string()]) -> ok
%% @doc Compile Erlang sources.
erlc(Args) ->
OrigCodePath = code:get_path(),
{Options, Files} = parse_opts(tl(Args), [report_errors]),
% io:fwrite("options: ~p~nfiles: ~p~n", [Options, Files]),
lists:foreach(fun(File) ->
case filename:extension(File) of
".erl" ->
compile:file(File, Options);
".yrl" ->
Verbose = lists:member(verbose, Options),
RootName = filename:rootname(File),
OutFile = case lists:keysearch(outdir, 1, Options) of
{value, {outdir, Dest}} ->
case filelib:is_dir(Dest) of
true ->
filename:join([Dest, filename:basename(RootName)]);
false ->
filename:rootname(Dest)
end;
_ ->
filename:basename(RootName)
end,
% io:fwrite("yecc:yecc(~p,~p,~p)~n", [RootName, OutFile, Verbose]),
yecc:yecc(RootName, OutFile, Verbose);
Ext ->
io:fwrite("erlaunch_erlc: Unsupported file type ~p~n", [Ext])
end
end, Files),
code:set_path(OrigCodePath),
ok.
parse_opts(["-I", Directory | Tail], OptAcc) ->
OptAcc0 = [{i, Directory} | OptAcc],
parse_opts(Tail, OptAcc0);
parse_opts(["-o", Directory | Tail], OptAcc) ->
OptAcc0 = [{outdir, Directory} | OptAcc],
parse_opts(Tail, OptAcc0);
parse_opts(["-D" ++ Text | Tail], OptAcc) ->
Tuple = case string:chr(Text, $=) of
0 -> {d, list_to_atom(Text)};
N -> {d, list_to_atom(string:substr(Text, 1, N - 1)),
list_to_atom(string:substr(Text, N + 1))}
end,
OptAcc0 = [Tuple | OptAcc],
parse_opts(Tail, OptAcc0);
parse_opts(["-W" | Tail], OptAcc) ->
OptAcc0 = [report_warnings | OptAcc],
parse_opts(Tail, OptAcc0);
parse_opts(["-v" | Tail], OptAcc) ->
OptAcc0 = [verbose | OptAcc],
parse_opts(Tail, OptAcc0);
parse_opts(["-b", Type | Tail], OptAcc) ->
OptAcc0 = [{output_format, Type} | OptAcc],
parse_opts(Tail, OptAcc0);
parse_opts(["-pa", Directory | Tail], OptAcc) ->
code:add_path(Directory),
parse_opts(Tail, OptAcc);
parse_opts(["-pz", Directory | Tail], OptAcc) ->
code:add_pathz(Directory),
parse_opts(Tail, OptAcc);
parse_opts(["+" ++ String | Tail], OptAcc) ->
{ok, T, _} = erl_scan:string(String ++ "."),
{ok, C} = erl_parse:parse_exprs(T),
{value, Term, _} =
erl_eval:exprs(C, erl_eval:bindings(erl_eval:new_bindings())),
OptAcc0 = [Term | OptAcc],
parse_opts(Tail, OptAcc0);
parse_opts(Else, OptAcc) -> {OptAcc, Else}.
%%% END of erlaunch_erlc.erl %%%