在windows开发环境下写bat脚本实现对erlang项目的并行编译:
1.项目结构:
config————>项目配置文件
ebin ————-> 编译文件路径
include———–> 引入文件
logs ————-> 日志文件
mmake———-> 并行编译所需的脚本
script ———–> 项目启动脚本
src ————–> 项目逻辑文件
MEmakefile —–>编译所需文件
3.编译脚本:mmake_all.bat(在script下)
并行编译所需文件:mmake.erl,mmake.beam(在mmake下),mmake.beam是mmake.erl编译后的文件
4.脚本代码:
mmake_all.bat
cd ../ebin
del *.beam
cd ../mmake
mkdir include
erlc -I include/ mmake.erl
cd ..
erl +P 1024000 -smp auto -pa mmake/ -s mmake all
pause
mmake.erl
%% 多进程编译
%% 解析Emakefile,根据获取{mods, options}列表,
%% 按照次序编译每项(解决编译顺序的问题)
%% 其中mods也可以包含多个模块,当大于1个时,
%% 可以启动多个process进行编译,从而提高编译速度.
-module(mmake).
-export([all/0,files/2, files/3]).
-compile(export_all).
-include_lib("kernel/include/file.hrl").
-define(MakeOpts,[noexec,load,netload,noload]).
all() ->
all(30).
all(Worker) when is_integer(Worker) ->
all(Worker, []).
all(Worker, Options) when is_integer(Worker) ->
{MakeOpts, CompileOpts} = sort_options(Options,[],[]),
case read_emakefile('MEmakefile', CompileOpts) of
Files when is_list(Files) ->
do_make_files(Worker, Files, MakeOpts),
io:format("@@@@@@@@@@@@@@ finish compile @@@@@@@@@@@@@@@@@~n");
error ->
error
end,
erlang:halt().
files(Worker, Fs) ->
files(Worker, Fs, []).
files(Worker, Fs0, Options) ->
Fs = [filename:rootname(F,".erl") || F <- Fs0],
{MakeOpts,CompileOpts} = sort_options(Options,[],[]),
case get_opts_from_emakefile(Fs,'MEmakefile',CompileOpts) of
Files when is_list(Files) ->
do_make_files(Worker, Files,MakeOpts);
error -> error
end.
do_make_files(Worker, Fs, Opts) ->
%io:format("worker:~p~nfs:~p~nopts:~p~n", [Worker, Fs, Opts]),
process(Fs, Worker, lists:member(noexec, Opts), load_opt(Opts)).
sort_options([H|T],Make,Comp) ->
case lists:member(H,?MakeOpts) of
true ->
sort_options(T,[H|Make],Comp);
false ->
sort_options(T,Make,[H|Comp])
end;
sort_options([],Make,Comp) ->
{Make,lists:reverse(Comp)}.
%%% Reads the given Emakefile and returns a list of tuples: {Mods,Opts}
%%% Mods is a list of module names (strings)
%%% Opts is a list