mad_erl.erl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -module(mad_erl).
  2. %%-author('Sina Samavati').
  3. -export([
  4. compile/5
  5. ]).
  6. -define(COMPILE_OPTS(Inc, Ebin, Opts, Deps),
  7. [return_errors, return_warnings, warn_export_all, warn_export_all, warn_unused_import,
  8. {i, [Inc]}, {outdir, Ebin}] ++ Opts ++ Deps). %% warnings_as_errors
  9. erl_to_beam(Bin, F) ->
  10. filename:join(Bin, filename:basename(F, ".erl") ++ ".beam").
  11. compile(File, Inc, Bin, Opt, Deps) ->
  12. BeamFile = erl_to_beam(Bin, File),
  13. Compiled = mad_compile:is_compiled(BeamFile, File),
  14. case Compiled of
  15. false ->
  16. Opt1 = case {application:get_env(mad, debug, false), application:get_env(mad, warnings_as_errors, false)} of
  17. {true, true} -> [ debug_info, warnings_as_errors ];
  18. {true, false} -> [ debug_info ];
  19. {false, true} -> [ warnings_as_errors ];
  20. _ -> []
  21. end ++ Opt,
  22. Opts2 = ?COMPILE_OPTS(Inc, Bin, Opt1, Deps),
  23. mad:info("Compiling ~s~n", [File -- mad_utils:cwd()]),
  24. R1 = compile:file(File, Opts2),
  25. %mad:info("compile: ~p~n", [R1]),
  26. ret(R1);
  27. _ -> false
  28. end.
  29. ret(error) -> mad:info("compile: error~n", []), true;
  30. ret({error, X} = _R) ->
  31. %%mad:info("compile: ~p~n", [R]),
  32. lines(error, X);
  33. ret({error, X, Y} = _R) ->
  34. %%mad:info("compile: ~p~n", [R]),
  35. lines(error, X),
  36. lines(error, Y);
  37. ret({ok, _}) -> false;
  38. ret({ok, _, []}) -> false;
  39. ret({ok, _, X}) -> lines(warning, X), false;
  40. ret({ok, _, X, _}) -> lines(warning, X), false.
  41. lines(Tag,X) ->
  42. S = case file:get_cwd() of
  43. {ok, Cwd} -> erlang:length(Cwd);
  44. _ -> 0
  45. end,
  46. [[ mad:info("Line ~p: ~p ~p in ~p~n", [ L, Tag, R, lists:nthtail(S, F) ]) || {L, _, R} <- E ] || {F, E} <- X ],
  47. true.