-module(mad_erl). %%-author('Sina Samavati'). -export([ compile/5 ]). -define(COMPILE_OPTS(Inc, Ebin, Opts, Deps), [return_errors, return_warnings, warn_export_all, warn_export_all, warn_unused_import, {i, [Inc]}, {outdir, Ebin}] ++ Opts ++ Deps). %% warnings_as_errors erl_to_beam(Bin, F) -> filename:join(Bin, filename:basename(F, ".erl") ++ ".beam"). compile(File, Inc, Bin, Opt, Deps) -> BeamFile = erl_to_beam(Bin, File), Compiled = mad_compile:is_compiled(BeamFile, File), case Compiled of false -> Opt1 = case {application:get_env(mad, debug, false), application:get_env(mad, warnings_as_errors, false)} of {true, true} -> [ debug_info, warnings_as_errors ]; {true, false} -> [ debug_info ]; {false, true} -> [ warnings_as_errors ]; _ -> [] end ++ Opt, Opts2 = ?COMPILE_OPTS(Inc, Bin, Opt1, Deps), mad:info("Compiling ~s~n", [File -- mad_utils:cwd()]), R1 = compile:file(File, Opts2), %mad:info("compile: ~p~n", [R1]), ret(R1); _ -> false end. ret(error) -> mad:info("compile: error~n", []), true; ret({error, X} = _R) -> %%mad:info("compile: ~p~n", [R]), lines(error, X); ret({error, X, Y} = _R) -> %%mad:info("compile: ~p~n", [R]), lines(error, X), lines(error, Y); ret({ok, _}) -> false; ret({ok, _, []}) -> false; ret({ok, _, X}) -> lines(warning, X), false; ret({ok, _, X, _}) -> lines(warning, X), false. lines(Tag,X) -> S = case file:get_cwd() of {ok, Cwd} -> erlang:length(Cwd); _ -> 0 end, [[ mad:info("Line ~p: ~p ~p in ~p~n", [ L, Tag, R, lists:nthtail(S, F) ]) || {L, _, R} <- E ] || {F, E} <- X ], true.