mad_compile.erl 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = mad_utils:get_value(deps, Conf1, []),
  22. deps(Cwd, Conf, ConfigFile, Deps),
  23. SrcDir = filename:join([mad_utils:src(DepPath)]),
  24. % io:format("DepPath ==> ~p~n\r",[DepPath]),
  25. Files = files(SrcDir,".yrl") ++
  26. files(SrcDir,".xrl") ++
  27. files(SrcDir,".erl") ++ % comment this to build with erlc/1
  28. files(SrcDir,".app.src"),
  29. case Files of
  30. [] -> ok;
  31. Files ->
  32. IncDir = mad_utils:include(DepPath),
  33. EbinDir = mad_utils:ebin(DepPath),
  34. LibDirs = mad_utils:get_value(lib_dirs, Conf, []),
  35. Includes = lists:flatten([
  36. [{i,filename:join([DepPath,L,D,include])} || D<-mad_utils:raw_deps(Deps) ] % for -include
  37. ++ [{i,filename:join([DepPath,L])}] || L <- LibDirs ]), % for -include_lib
  38. % io:format("DepPath ~p~n Includes: ~p~nLibDirs: ~p~n",[DepPath,Includes,LibDirs]),
  39. %% create EbinDir and add it to code path
  40. file:make_dir(EbinDir),
  41. code:replace_path(Name,EbinDir),
  42. %erlc(DepPath), % comment this to build with files/2
  43. Opts = mad_utils:get_value(erl_opts, Conf1, []),
  44. lists:foreach(compile_fun(IncDir, EbinDir, Opts,Includes), Files),
  45. mad_dtl:compile(DepPath,Conf1),
  46. mad_port:compile(DepPath,Conf1),
  47. put(Name, compiled),
  48. ok
  49. end.
  50. compile_fun(Inc,Bin,Opt,Deps) -> fun(File) -> (module(filetype(File))):compile(File,Inc,Bin,Opt,Deps) end.
  51. module("erl") -> mad_erl;
  52. module("erl.src") -> mad_utils;
  53. module("yrl") -> mad_yecc;
  54. module("xrl") -> mad_leex;
  55. module("app.src") -> mad_app;
  56. module(_) -> mad_none.
  57. filetype(Path) -> string:join(tl(string:tokens(filename:basename(Path), ".")), ".").
  58. files(Dir,Ext) -> filelib:fold_files(Dir, Ext, true, fun(F, Acc) -> [F|Acc] end, []).
  59. is_compiled(BeamFile, File) -> mad_utils:last_modified(BeamFile) >= mad_utils:last_modified(File).
  60. 'compile-apps'(Cwd, ConfigFile, Conf) ->
  61. Dirs = mad_utils:sub_dirs(Cwd, ConfigFile, Conf),
  62. case Dirs of
  63. [] -> mad_compile:dep(Cwd, Conf, ConfigFile, Cwd);
  64. Apps -> mad_compile:deps(Cwd, Conf, ConfigFile, Apps) end.
  65. 'compile-deps'(Cwd, ConfigFile, Conf) ->
  66. mad_compile:deps(Cwd, Conf, ConfigFile, mad_utils:get_value(deps, Conf, [])).
  67. list(X) when is_atom(X) -> atom_to_list(X);
  68. list(X) -> X.
  69. erlc(DepPath) ->
  70. ErlFiles = filelib:wildcard(DepPath++"/src/**/*.erl"),
  71. io:format("Files: ~s~n\r",[[filename:basename(Erl)++" " ||Erl<-ErlFiles]]),
  72. {_,Status,X} = sh:run("erlc",["-o"++DepPath++"/ebin/","-I"++DepPath++"/include"]++
  73. ErlFiles,binary,filename:absname("."),[{"ERL_LIBS","apps:deps"}]),
  74. case Status == 0 of
  75. true -> skip;
  76. false -> io:format("Error: ~s~n\r",[binary_to_list(X)]) end.