Browse Source

ft: 代码优化

master
SisMaker 3 years ago
parent
commit
f602921b5b
1 changed files with 27 additions and 25 deletions
  1. +27
    -25
      src/eMake.erl

+ 27
- 25
src/eMake.erl View File

@ -13,24 +13,25 @@
-define(MakeOpts, [noexec, load, netload, noload]). -define(MakeOpts, [noexec, load, netload, noload]).
-define(EMakefile, "./Emakefile_dev"). -define(EMakefile, "./Emakefile_dev").
-define(OnceCnt, 16).
main(Args) -> main(Args) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
case Args of case Args of
[] -> [] ->
all(max(1, erlang:system_info(schedulers) - 1), ?EMakefile, []);
make(max(1, erlang:system_info(schedulers) - 1), ?EMakefile, []);
[EMakeFileOrWorkCnt] -> [EMakeFileOrWorkCnt] ->
try list_to_integer(EMakeFileOrWorkCnt) of try list_to_integer(EMakeFileOrWorkCnt) of
Cnt -> Cnt ->
all(max(1, Cnt), ?EMakefile, [])
make(max(1, Cnt), ?EMakefile, [])
catch _:_ -> catch _:_ ->
all(max(1, erlang:system_info(schedulers) - 1), EMakeFileOrWorkCnt, [])
make(max(1, erlang:system_info(schedulers) - 1), EMakeFileOrWorkCnt, [])
end; end;
[EMakeFile, WorkCntStr] -> [EMakeFile, WorkCntStr] ->
all(max(1, list_to_integer(WorkCntStr)), EMakeFile, []);
make(max(1, list_to_integer(WorkCntStr)), EMakeFile, []);
[EMakeFile, WorkCntStr, OptsStr] -> [EMakeFile, WorkCntStr, OptsStr] ->
{ok, Opts} = strToTerm(OptsStr), {ok, Opts} = strToTerm(OptsStr),
all(max(1, list_to_integer(WorkCntStr)), EMakeFile, Opts)
make(max(1, list_to_integer(WorkCntStr)), EMakeFile, Opts)
end. end.
eMakeFile() -> eMakeFile() ->
@ -55,7 +56,7 @@ saveEMake(NowTime) ->
ok ok
end. end.
all(WorkerCnt, EMakeFile, Opts) ->
make(WorkerCnt, EMakeFile, Opts) ->
io:format("compile start use EMakefile: ~ts~n", [EMakeFile]), io:format("compile start use EMakefile: ~ts~n", [EMakeFile]),
StartTime = erlang:system_time(second), StartTime = erlang:system_time(second),
{MakeOpts, CompileOpts} = splitOpts(Opts, [], []), {MakeOpts, CompileOpts} = splitOpts(Opts, [], []),
@ -138,7 +139,7 @@ transform([{Mod, ModOpts} | EMake], Opts, Files) ->
transform(EMake, Opts, Files); transform(EMake, Opts, Files);
Mods -> Mods ->
NewOpts = Opts ++ ModOpts, NewOpts = Opts ++ ModOpts,
ensure_dir(NewOpts),
ensureOutDir(NewOpts),
transform(EMake, Opts, [{Mods, NewOpts} | Files]) transform(EMake, Opts, [{Mods, NewOpts} | Files])
end; end;
transform([Mod | EMake], Opts, Files) -> transform([Mod | EMake], Opts, Files) ->
@ -146,11 +147,11 @@ transform([Mod | EMake], Opts, Files) ->
[] -> [] ->
transform(EMake, Opts, Files); transform(EMake, Opts, Files);
Mods -> Mods ->
ensure_dir(Opts),
ensureOutDir(Opts),
transform(EMake, Opts, [{Mods, Opts} | Files]) transform(EMake, Opts, [{Mods, Opts} | Files])
end. end.
ensure_dir(Opts) ->
ensureOutDir(Opts) ->
case lists:keysearch(outdir, 1, Opts) of case lists:keysearch(outdir, 1, Opts) of
{value, {outdir, OutDir}} -> {value, {outdir, OutDir}} ->
BeamDir = filename:join(OutDir, "xxx.beam"), BeamDir = filename:join(OutDir, "xxx.beam"),
@ -159,7 +160,6 @@ ensure_dir(Opts) ->
ignore ignore
end. end.
expand(Mod) when is_atom(Mod) -> expand(Mod) when is_atom(Mod) ->
expand(atom_to_list(Mod)); expand(atom_to_list(Mod));
expand(Mods) when is_list(Mods), not is_integer(hd(Mods)) -> expand(Mods) when is_list(Mods), not is_integer(hd(Mods)) ->
@ -191,7 +191,6 @@ foldErl([OneFile | Left], Acc) ->
foldErl(Left, Acc) foldErl(Left, Acc)
end. end.
-define(OnceCnt, 8).
forMake([], _Worker, _NoExec, _Load, IsAll, AllWorkPids) -> forMake([], _Worker, _NoExec, _Load, IsAll, AllWorkPids) ->
case AllWorkPids of case AllWorkPids of
[] -> [] ->
@ -266,16 +265,19 @@ errorStop(Err, AllWorkPids) ->
end, end,
{error, compile_error}. {error, compile_error}.
compileWorker([], _Opts, Parent, NoExec, Load, IsAll) ->
compileWorker(Files, Opts, Parent, NoExec, Load, IsAll) ->
compileWork(Files, Opts, Parent, NoExec, Load, IsAll).
compileWork([], _Opts, Parent, NoExec, Load, IsAll) ->
erlang:send(Parent, {mOverCompile, self()}), erlang:send(Parent, {mOverCompile, self()}),
receive receive
{mNewFile, Files, Opts} -> {mNewFile, Files, Opts} ->
compileWorker(Files, Opts, Parent, NoExec, Load, IsAll);
compileWork(Files, Opts, Parent, NoExec, Load, IsAll);
_Other -> _Other ->
io:format("compileWorker [] receive unexpect msg:~p ~n", [_Other]) io:format("compileWorker [] receive unexpect msg:~p ~n", [_Other])
end; end;
compileWorker([OneFile | Files], Opts, Parent, NoExec, Load, IsAll) ->
case compile(coerce_2_list(OneFile), NoExec, Load, IsAll, Opts) of
compileWork([OneFile | Files], Opts, Parent, NoExec, Load, IsAll) ->
case tryCompile(coerce_2_list(OneFile), NoExec, Load, IsAll, Opts) of
error -> error ->
Parent ! {mCompileError, OneFile}, Parent ! {mCompileError, OneFile},
exit(error); exit(error);
@ -283,10 +285,10 @@ compileWorker([OneFile | Files], Opts, Parent, NoExec, Load, IsAll) ->
Parent ! {mCompileError, {OneFile, Errors, Warnings}}, Parent ! {mCompileError, {OneFile, Errors, Warnings}},
exit(error); exit(error);
_ -> _ ->
compileWorker(Files, Opts, Parent, NoExec, Load, IsAll)
compileWork(Files, Opts, Parent, NoExec, Load, IsAll)
end. end.
compile(File, NoExec, Load, IsAll, Opts) ->
tryCompile(File, NoExec, Load, IsAll, Opts) ->
case IsAll of case IsAll of
false -> false ->
ObjName = lists:append(filename:basename(File), code:objfile_extension()), ObjName = lists:append(filename:basename(File), code:objfile_extension()),
@ -301,10 +303,10 @@ compile(File, NoExec, Load, IsAll, Opts) ->
true -> true ->
reCompile(File, NoExec, Load, Opts, ObjFile); reCompile(File, NoExec, Load, Opts, ObjFile);
false -> false ->
recompile(File, NoExec, Load, Opts)
doCompile(File, NoExec, Load, Opts)
end; end;
_ -> _ ->
recompile(File, NoExec, Load, Opts)
doCompile(File, NoExec, Load, Opts)
end. end.
reCompile(File, NoExec, Load, Opts, ObjFile) -> reCompile(File, NoExec, Load, Opts, ObjFile) ->
@ -312,7 +314,7 @@ reCompile(File, NoExec, Load, Opts, ObjFile) ->
{ok, #file_info{mtime = ObjTime}} = file:read_file_info(ObjFile), {ok, #file_info{mtime = ObjTime}} = file:read_file_info(ObjFile),
case SrcTime > ObjTime of case SrcTime > ObjTime of
true -> true ->
recompile(File, NoExec, Load, Opts);
doCompile(File, NoExec, Load, Opts);
_ -> _ ->
ckIncludeRecompile(ObjTime, File, NoExec, Load, Opts) ckIncludeRecompile(ObjTime, File, NoExec, Load, Opts)
end. end.
@ -323,7 +325,7 @@ ckIncludeRecompile(ObjMTime, File, NoExec, Load, Opts) ->
IncludePath = include_opt(Opts), IncludePath = include_opt(Opts),
case check_includes(lists:append(File, ".erl"), IncludePath, ObjMTime) of case check_includes(lists:append(File, ".erl"), IncludePath, ObjMTime) of
true -> true ->
recompile(File, NoExec, Load, Opts);
doCompile(File, NoExec, Load, Opts);
false -> false ->
false false
end. end.
@ -339,15 +341,15 @@ include_opt([]) ->
%% Actually recompile and load the file, depending on the flags. %% Actually recompile and load the file, depending on the flags.
%% Where load can be netload | load | noload %% Where load can be netload | load | noload
recompile(File, true, _Load, _Opts) ->
doCompile(File, true, _Load, _Opts) ->
io:format("Out of date: ~ts\n", [File]); io:format("Out of date: ~ts\n", [File]);
recompile(File, false, noload, Opts) ->
doCompile(File, false, noload, Opts) ->
% io:format("Recompile: ~ts\n", [File]), % io:format("Recompile: ~ts\n", [File]),
compile:file(File, [report_errors, report_warnings, error_summary | Opts]); compile:file(File, [report_errors, report_warnings, error_summary | Opts]);
recompile(File, false, load, Opts) ->
doCompile(File, false, load, Opts) ->
% io:format("Recompile: ~ts\n", [File]), % io:format("Recompile: ~ts\n", [File]),
c:c(File, Opts); c:c(File, Opts);
recompile(File, false, netload, Opts) ->
doCompile(File, false, netload, Opts) ->
% io:format("Recompile: ~ts\n", [File]), % io:format("Recompile: ~ts\n", [File]),
c:nc(File, Opts). c:nc(File, Opts).

Loading…
Cancel
Save