You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 line
6.4 KiB

  1. %% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. -module(rebar_base_compiler).
  28. -include("rebar.hrl").
  29. -export([run/4, run/7, run/8]).
  30. %% ===================================================================
  31. %% Public API
  32. %% ===================================================================
  33. run(Config, FirstFiles, RestFiles, CompileFn) ->
  34. %% Compile the first files in sequence
  35. compile_each(FirstFiles, Config, CompileFn),
  36. %% Spin up workers for the rest of the files
  37. case RestFiles of
  38. [] ->
  39. ok;
  40. _ ->
  41. Self = self(),
  42. F = fun() -> compile_worker(Self, Config, CompileFn) end,
  43. Pids = [spawn_monitor(F) || _I <- lists:seq(1,3)],
  44. compile_queue(Pids, RestFiles)
  45. end.
  46. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt, Compile3Fn) ->
  47. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  48. Compile3Fn, [check_last_mod]).
  49. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  50. Compile3Fn, Opts) ->
  51. %% Convert simple extension to proper regex
  52. SourceExtRe = ".*\\" ++ SourceExt ++ [$$],
  53. %% Find all possible source files
  54. FoundFiles = rebar_utils:find_files(SourceDir, SourceExtRe),
  55. %% Remove first files from found files
  56. RestFiles = [Source || Source <- FoundFiles,
  57. lists:member(Source, FirstFiles) == false],
  58. %% Check opts for flag indicating that compile should check lastmod
  59. CheckLastMod = proplists:get_bool(check_last_mod, Opts),
  60. run(Config, FirstFiles, RestFiles,
  61. fun(S, C) ->
  62. Target = target_file(S, SourceDir, SourceExt, TargetDir, TargetExt),
  63. simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod)
  64. end).
  65. %% ===================================================================
  66. %% Internal functions
  67. %% ===================================================================
  68. simple_compile_wrapper(Source, Target, Compile3Fn, Config, false) ->
  69. Compile3Fn(Source, Target, Config);
  70. simple_compile_wrapper(Source, Target, Compile3Fn, Config, true) ->
  71. case filelib:last_modified(Target) < filelib:last_modified(Source) of
  72. true ->
  73. Compile3Fn(Source, Target, Config);
  74. false ->
  75. skipped
  76. end.
  77. target_file(SourceFile, SourceDir, SourceExt, TargetDir, TargetExt) ->
  78. %% Remove all leading components of the source dir from the file -- we want
  79. %% to maintain the deeper structure (if any) of the source file path
  80. BaseFile = remove_common_path(SourceFile, SourceDir),
  81. filename:join([TargetDir, filename:dirname(BaseFile),
  82. filename:basename(BaseFile, SourceExt) ++ TargetExt]).
  83. remove_common_path(Fname, Path) ->
  84. remove_common_path1(filename:split(Fname), filename:split(Path)).
  85. remove_common_path1([Part | RestFilename], [Part | RestPath]) ->
  86. remove_common_path1(RestFilename, RestPath);
  87. remove_common_path1(FilenameParts, _) ->
  88. filename:join(FilenameParts).
  89. compile(Source, Config, CompileFn) ->
  90. case CompileFn(Source, Config) of
  91. ok ->
  92. ok;
  93. skipped ->
  94. skipped
  95. end.
  96. compile_each([], _Config, _CompileFn) ->
  97. ok;
  98. compile_each([Source | Rest], Config, CompileFn) ->
  99. case compile(Source, Config, CompileFn) of
  100. ok ->
  101. ?CONSOLE("Compiled ~s\n", [Source]);
  102. skipped ->
  103. ?INFO("Skipped ~s\n", [Source])
  104. end,
  105. compile_each(Rest, Config, CompileFn).
  106. compile_queue([], []) ->
  107. ok;
  108. compile_queue(Pids, Targets) ->
  109. receive
  110. {next, Worker} ->
  111. case Targets of
  112. [] ->
  113. Worker ! empty,
  114. compile_queue(Pids, Targets);
  115. [Source | Rest] ->
  116. Worker ! {compile, Source},
  117. compile_queue(Pids, Rest)
  118. end;
  119. {fail, Error} ->
  120. ?DEBUG("Worker compilation failed: ~p\n", [Error]),
  121. ?FAIL;
  122. {compiled, Source} ->
  123. ?CONSOLE("Compiled ~s\n", [Source]),
  124. compile_queue(Pids, Targets);
  125. {skipped, Source} ->
  126. ?INFO("Skipped ~s\n", [Source]),
  127. compile_queue(Pids, Targets);
  128. {'DOWN', Mref, _, Pid, normal} ->
  129. ?DEBUG("Worker exited cleanly\n", []),
  130. Pids2 = lists:delete({Pid, Mref}, Pids),
  131. compile_queue(Pids2, Targets);
  132. {'DOWN', _Mref, _, _Pid, Info} ->
  133. ?DEBUG("Worker failed: ~p\n", [Info]),
  134. ?FAIL
  135. end.
  136. compile_worker(QueuePid, Config, CompileFn) ->
  137. QueuePid ! {next, self()},
  138. receive
  139. {compile, Source} ->
  140. case catch(compile(Source, Config, CompileFn)) of
  141. ok ->
  142. QueuePid ! {compiled, Source},
  143. compile_worker(QueuePid, Config, CompileFn);
  144. skipped ->
  145. QueuePid ! {skipped, Source},
  146. compile_worker(QueuePid, Config, CompileFn);
  147. Error ->
  148. QueuePid ! {fail, Error},
  149. ok
  150. end;
  151. empty ->
  152. ok
  153. end.