Просмотр исходного кода

Don't compile a file if it's been already compiled

Sina Samavati 11 лет назад
Родитель
Сommit
06a65bf22e
2 измененных файлов с 23 добавлено и 2 удалено
  1. 14 2
      src/mad_compile.erl
  2. 9 0
      src/mad_utils.erl

+ 14 - 2
src/mad_compile.erl

@@ -85,8 +85,13 @@ compile_fun(SrcDir, IncDir, EbinDir, Opts) ->
             F1 = filename:join(SrcDir, F),
             case is_app_src(F1) of
                 false ->
-                    io:format("Compiling ~s~n", [F1]),
-                    compile:file(F1, ?COMPILE_OPTS(IncDir, EbinDir) ++ Opts);
+                    Compiled = is_compiled(EbinDir, F1),
+                    if Compiled =:= false ->
+                            io:format("Compiling ~s~n", [F1]),
+                            compile:file(F1, ?COMPILE_OPTS(IncDir, EbinDir) ++ Opts);
+                       true ->
+                            ok
+                    end;
                 true ->
                     %% add {modules, [Modules]} to .app file
                     AppFile = filename:join(EbinDir, app_src_to_app(F1)),
@@ -113,3 +118,10 @@ is_app_src(Filename) ->
 
 app_src_to_app(Filename) ->
     filename:basename(Filename, ".app.src") ++ ".app".
+
+erl_to_beam(EbinDir, Filename) ->
+    filename:join(EbinDir, filename:basename(Filename, ".erl") ++ ".beam").
+
+is_compiled(EbinDir, ErlFile) ->
+    BeamFile = erl_to_beam(EbinDir, ErlFile),
+    mad_utils:last_modified(BeamFile) > mad_utils:last_modified(ErlFile).

+ 9 - 0
src/mad_utils.erl

@@ -16,6 +16,7 @@
 -export([lib_dirs/2]).
 -export([https_to_git/1]).
 -export([git_to_https/1]).
+-export([last_modified/1]).
 
 
 %% get current working directory
@@ -107,3 +108,11 @@ https_to_git(X) ->
 
 git_to_https(X) ->
     re:replace(X, "git://", "https://", [{return, list}]).
+
+last_modified(File) ->
+    case filelib:last_modified(File) of
+        0 ->
+            0;
+        Else ->
+            calendar:datetime_to_gregorian_seconds(Else)
+    end.