使用Erlang实现游戏服务器-02
1、编译脚本的编写
在上一篇文章的最后,我们编译了我们的erl文件,但是工作中我们不可能每次每次写完一个模块就编译一个,因此我们需要编写脚本
1.1、Emakefile
具体介绍请在网上搜索
在script目录创建Emakefile文件
内容如下
{"../src/*", [debug_info, {i, "../include"}, {outdir, "../ebin"}]}.
1.2、编写game_compile
%% API
-export([compile_all/0]).
%%%================================== export func ==================================
%%%---------------------------------------------------------------------------------
%% des:
%%%---------------------------------------------------------------------------------
compile_all() ->
Terms = read_makefile(),
transform(Terms).
%%%=================================== local func ==================================
%%%---------------------------------------------------------------------------------
%% Description:读取Emakefile文件
%%%---------------------------------------------------------------------------------
read_makefile() ->
case file:consult("Emakefile") of
{ok, Terms} ->
Terms;
{error, Reason} ->
io:format("Error reading Emakefile: ~p~n", [Reason]),
[]
end.
%%%---------------------------------------------------------------------------------
%% Description:整理Emakefile读取出的数据,拿取所有的模块名
%%%---------------------------------------------------------------------------------
transform([{Dir, OptList} | T]) ->
spawn(fun() ->
NewModDirList = expend(Dir),
[begin
case c:c(ModDir, OptList) of
{ok, ModName} ->
io:format("compile file: ~p success, mod name: ~p~n", [ModDir, ModName]);
{error, Reason} ->
io:format("compile file: ~p error: ~p~n", [ModDir, Reason])
end
end || ModDir <- NewModDirList]
end),
transform(T);
transform([Other | T]) ->
io:format("格式错误:~w~n", [Other]),
transform(T);
transform([]) ->
ok.
%%%---------------------------------------------------------------------------------
%% Description:拿取所有模块名
%%%---------------------------------------------------------------------------------
expend(Dir) ->
case lists:member($*, Dir) of
true ->
Dirs = filelib:wildcard(Dir),
find_file(Dirs);
false ->
[]
end.
%% ----------------------------------------------------
%% Description: 找到路径下的所有erl文件
%% ----------------------------------------------------
find_file(Dirs) ->
find_file_(Dirs, []).
find_file_([F | T], FindFiles) ->
case filelib:is_dir(F) of %%是否是文件夹
true ->
{ok, Files} = file:list_dir(F),
%% 加上路径
AddFiles = [F ++ "/" ++ File || File <- Files],
find_file_(AddFiles ++ T, FindFiles);
false ->
%% 过滤出erl文件
case filename:extension(F) of
".erl" ->
case lists:member(F, FindFiles) of
true ->
find_file_(T, FindFiles);
false ->
find_file_(T, [F | FindFiles])
end;
_ ->
find_file_(T, FindFiles)
end
end;
find_file_([], FindFiles) ->
FindFiles.
把erl文件编译为beam文件

1.3 编写脚本
compile_all.bat
@ECHO OFF
chcp 65001
erl -noshell -pa "../deps/ebin" -eval "game_compile:compile_all(),erlang:halt(0)"
echo 编译完成
pause

执行脚本

编译成功
410

被折叠的 条评论
为什么被折叠?



