mad_compile.erl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. -module(mad_compile).
  2. -copyright('Sina Samavati').
  3. -compile(export_all).
  4. %% compile dependencies
  5. deps(_, _, _, []) -> ok;
  6. deps(Cwd, Conf, ConfigFile, [H|T]) ->
  7. {Name, _} = mad_deps:name_and_repo(H),
  8. case get(Name) == compiled andalso get(mode) /= active of
  9. true -> ok;
  10. _ -> dep(Cwd, Conf, ConfigFile, Name) end,
  11. deps(Cwd, Conf, ConfigFile, T).
  12. %% compile a dependency
  13. dep(Cwd, _Conf, ConfigFile, Name) ->
  14. %% check dependencies of the dependency
  15. DepsDir = filename:join([mad_utils:get_value(deps_dir, _Conf, ["deps"])]),
  16. DepPath = filename:join([Cwd, DepsDir, Name]),
  17. io:format("==> ~p~n\r",[Name]),
  18. DepConfigFile = filename:join(DepPath, ConfigFile),
  19. Conf = mad_utils:consult(DepConfigFile),
  20. Conf1 = mad_script:script(DepConfigFile, Conf, Name),
  21. deps(Cwd, Conf, ConfigFile, mad_utils:get_value(deps, Conf1, [])),
  22. SrcDir = filename:join([mad_utils:src(DepPath)]),
  23. % io:format("DepPath ==> ~p~n\r",[DepPath]),
  24. Files = files(SrcDir,".yrl") ++
  25. files(SrcDir,".xrl") ++
  26. files(SrcDir,".erl") ++ % comment this to build with erlc/1
  27. files(SrcDir,".app.src"),
  28. case Files of
  29. [] -> ok;
  30. Files ->
  31. IncDir = mad_utils:include(DepPath),
  32. EbinDir = mad_utils:ebin(DepPath),
  33. %% create EbinDir and add it to code path
  34. file:make_dir(EbinDir),
  35. code:replace_path(Name,EbinDir),
  36. %erlc(DepPath), % comment this to build with files/2
  37. Opts = mad_utils:get_value(erl_opts, Conf1, []),
  38. lists:foreach(compile_fun(IncDir, EbinDir, Opts), Files),
  39. mad_dtl:compile(DepPath,Conf1),
  40. mad_port:compile(DepPath,Conf1),
  41. put(Name, compiled),
  42. ok
  43. end.
  44. compile_fun(Inc,Bin,Opt) -> fun(File) -> (module(filetype(File))):compile(File,Inc,Bin,Opt) end.
  45. module("erl") -> mad_erl;
  46. module("erl.src") -> mad_utils;
  47. module("yrl") -> mad_yecc;
  48. module("xrl") -> mad_leex;
  49. module("app.src") -> mad_app;
  50. module(_) -> mad_none.
  51. filetype(Path) -> string:join(tl(string:tokens(filename:basename(Path), ".")), ".").
  52. files(Dir,Ext) -> filelib:fold_files(Dir, Ext, true, fun(F, Acc) -> [F|Acc] end, []).
  53. is_compiled(BeamFile, File) -> mad_utils:last_modified(BeamFile) >= mad_utils:last_modified(File).
  54. 'compile-apps'(Cwd, ConfigFile, Conf) ->
  55. Dirs = mad_utils:sub_dirs(Cwd, ConfigFile, Conf),
  56. case Dirs of
  57. [] -> mad_compile:dep(Cwd, Conf, ConfigFile, Cwd);
  58. Apps -> mad_compile:deps(Cwd, Conf, ConfigFile, Apps) end.
  59. 'compile-deps'(Cwd, ConfigFile, Conf) ->
  60. mad_compile:deps(Cwd, Conf, ConfigFile, mad_utils:get_value(deps, Conf, [])).
  61. list(X) when is_atom(X) -> atom_to_list(X);
  62. list(X) -> X.
  63. erlc(DepPath) ->
  64. ErlFiles = filelib:wildcard(DepPath++"/src/**/*.erl"),
  65. io:format("Files: ~s~n\r",[[filename:basename(Erl)++" " ||Erl<-ErlFiles]]),
  66. {_,Status,X} = sh:run("erlc",["-o"++DepPath++"/ebin/","-I"++DepPath++"/include"]++
  67. ErlFiles,binary,filename:absname("."),[{"ERL_LIBS","apps:deps"}]),
  68. case Status == 0 of
  69. true -> skip;
  70. false -> io:format("Error: ~s~n\r",[binary_to_list(X)]) end.