mad_compile.erl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. case filelib:wildcard(DepPath++"/ebin/"++list(Name)++".app") /= [] of
  19. true -> skip; _ ->
  20. DepConfigFile = filename:join(DepPath, ConfigFile),
  21. Conf = mad_utils:consult(DepConfigFile),
  22. Conf1 = mad_script:script(DepConfigFile, Conf, Name),
  23. deps(Cwd, Conf, ConfigFile, mad_utils:get_value(deps, Conf1, [])),
  24. SrcDir = filename:join([mad_utils:src(DepPath)]),
  25. Files = files(SrcDir,".yrl") ++
  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 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("app.src") -> mad_app.
  49. filetype(Path) -> string:join(tl(string:tokens(filename:basename(Path), ".")), ".").
  50. files(Dir,Ext) -> filelib:fold_files(Dir, Ext, true, fun(F, Acc) -> [F|Acc] end, []).
  51. is_compiled(BeamFile, File) -> mad_utils:last_modified(BeamFile) >= mad_utils:last_modified(File).
  52. 'compile-apps'(Cwd, ConfigFile, Conf) ->
  53. Dirs = mad_utils:sub_dirs(Cwd, ConfigFile, Conf),
  54. case Dirs of
  55. [] -> mad_compile:dep(Cwd, Conf, ConfigFile, Cwd);
  56. Apps -> mad_compile:deps(Cwd, Conf, ConfigFile, Apps) end.
  57. 'compile-deps'(Cwd, ConfigFile, Conf) ->
  58. mad_compile:deps(Cwd, Conf, ConfigFile, mad_utils:get_value(deps, Conf, [])).
  59. list(X) when is_atom(X) -> atom_to_list(X);
  60. list(X) -> X.
  61. erlc(DepPath) ->
  62. ErlFiles = filelib:wildcard(DepPath++"/src/**/*.erl"),
  63. io:format("Files: ~s~n\r",[[filename:basename(Erl)++" " ||Erl<-ErlFiles]]),
  64. {Res,Status,X} = sh:run("erlc",["-o"++DepPath++"/ebin/","-I"++DepPath++"/include"]++
  65. ErlFiles,binary,filename:absname("."),[{"ERL_LIBS","apps:deps"}]),
  66. case Status == 0 of
  67. true -> skip;
  68. false -> io:format("Error: ~s~n\r",[binary_to_list(X)]) end.