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.

516 line
18 KiB

15 年之前
15 年之前
15 年之前
14 年之前
13 年之前
13 年之前
  1. %% -*- 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, 2010 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_utils).
  28. -export([get_cwd/0,
  29. is_arch/1,
  30. get_arch/0,
  31. wordsize/0,
  32. sh/2,
  33. find_files/2, find_files/3,
  34. now_str/0,
  35. ensure_dir/1,
  36. beam_to_mod/2, beams/1,
  37. erl_to_mod/1,
  38. abort/0, abort/2,
  39. escript_foldl/3,
  40. find_executable/1,
  41. prop_check/3,
  42. expand_code_path/0,
  43. expand_env_variable/3,
  44. vcs_vsn/3,
  45. deprecated/3, deprecated/4,
  46. get_deprecated_global/4, get_deprecated_global/5,
  47. get_experimental_global/3, get_experimental_local/3,
  48. get_deprecated_list/4, get_deprecated_list/5,
  49. get_deprecated_local/4, get_deprecated_local/5,
  50. delayed_halt/1,
  51. erl_opts/1,
  52. src_dirs/1,
  53. ebin_dir/0,
  54. base_dir/1,
  55. processing_base_dir/1, processing_base_dir/2]).
  56. -include("rebar.hrl").
  57. %% ====================================================================
  58. %% Public API
  59. %% ====================================================================
  60. get_cwd() ->
  61. {ok, Dir} = file:get_cwd(),
  62. Dir.
  63. is_arch(ArchRegex) ->
  64. case re:run(get_arch(), ArchRegex, [{capture, none}]) of
  65. match ->
  66. true;
  67. nomatch ->
  68. false
  69. end.
  70. get_arch() ->
  71. Words = wordsize(),
  72. erlang:system_info(otp_release) ++ "-"
  73. ++ erlang:system_info(system_architecture) ++ "-" ++ Words.
  74. wordsize() ->
  75. try erlang:system_info({wordsize, external}) of
  76. Val ->
  77. integer_to_list(8 * Val)
  78. catch
  79. error:badarg ->
  80. integer_to_list(8 * erlang:system_info(wordsize))
  81. end.
  82. %%
  83. %% Options = [Option] -- defaults to [use_stdout, abort_on_error]
  84. %% Option = ErrorOption | OutputOption | {cd, string()} | {env, Env}
  85. %% ErrorOption = return_on_error | abort_on_error | {abort_on_error, string()}
  86. %% OutputOption = use_stdout | {use_stdout, bool()}
  87. %% Env = [{string(), Val}]
  88. %% Val = string() | false
  89. %%
  90. sh(Command0, Options0) ->
  91. ?INFO("sh info:\n\tcwd: ~p\n\tcmd: ~s\n", [get_cwd(), Command0]),
  92. ?DEBUG("\topts: ~p\n", [Options0]),
  93. DefaultOptions = [use_stdout, abort_on_error],
  94. Options = [expand_sh_flag(V)
  95. || V <- proplists:compact(Options0 ++ DefaultOptions)],
  96. ErrorHandler = proplists:get_value(error_handler, Options),
  97. OutputHandler = proplists:get_value(output_handler, Options),
  98. Command = patch_on_windows(Command0, proplists:get_value(env, Options, [])),
  99. PortSettings = proplists:get_all_values(port_settings, Options) ++
  100. [exit_status, {line, 16384}, use_stdio, stderr_to_stdout, hide],
  101. ?DEBUG("Port Cmd: ~p\nPort Opts: ~p\n", [Command, PortSettings]),
  102. Port = open_port({spawn, Command}, PortSettings),
  103. case sh_loop(Port, OutputHandler, []) of
  104. {ok, _Output} = Ok ->
  105. Ok;
  106. {error, {_Rc, _Output}=Err} ->
  107. ErrorHandler(Command, Err)
  108. end.
  109. find_files(Dir, Regex) ->
  110. find_files(Dir, Regex, true).
  111. find_files(Dir, Regex, Recursive) ->
  112. filelib:fold_files(Dir, Regex, Recursive,
  113. fun(F, Acc) -> [F | Acc] end, []).
  114. now_str() ->
  115. {{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
  116. lists:flatten(io_lib:format("~4b/~2..0b/~2..0b ~2..0b:~2..0b:~2..0b",
  117. [Year, Month, Day, Hour, Minute, Second])).
  118. %% TODO: filelib:ensure_dir/1 corrected in R13B04. Remove when we drop
  119. %% support for OTP releases older than R13B04.
  120. ensure_dir(Path) ->
  121. case filelib:ensure_dir(Path) of
  122. ok ->
  123. ok;
  124. {error,eexist} ->
  125. ok;
  126. Error ->
  127. Error
  128. end.
  129. -spec abort() -> no_return().
  130. abort() ->
  131. throw(rebar_abort).
  132. -spec abort(string(), [term()]) -> no_return().
  133. abort(String, Args) ->
  134. ?ERROR(String, Args),
  135. abort().
  136. %% TODO: Rename emulate_escript_foldl to escript_foldl and remove
  137. %% this function when the time is right. escript:foldl/3 was an
  138. %% undocumented exported fun and has been removed in R14.
  139. escript_foldl(Fun, Acc, File) ->
  140. {module, zip} = code:ensure_loaded(zip),
  141. case erlang:function_exported(zip, foldl, 3) of
  142. true ->
  143. emulate_escript_foldl(Fun, Acc, File);
  144. false ->
  145. escript:foldl(Fun, Acc, File)
  146. end.
  147. find_executable(Name) ->
  148. case os:find_executable(Name) of
  149. false -> false;
  150. Path ->
  151. "\"" ++ filename:nativename(Path) ++ "\""
  152. end.
  153. %% Helper function for checking values and aborting when needed
  154. prop_check(true, _, _) -> true;
  155. prop_check(false, Msg, Args) -> ?ABORT(Msg, Args).
  156. %% Convert all the entries in the code path to absolute paths.
  157. expand_code_path() ->
  158. CodePath = lists:foldl(fun(Path, Acc) ->
  159. [filename:absname(Path) | Acc]
  160. end, [], code:get_path()),
  161. code:set_path(lists:reverse(CodePath)).
  162. %%
  163. %% Given env. variable FOO we want to expand all references to
  164. %% it in InStr. References can have two forms: $FOO and ${FOO}
  165. %% The end of form $FOO is delimited with whitespace or eol
  166. %%
  167. expand_env_variable(InStr, VarName, RawVarValue) ->
  168. case string:chr(InStr, $$) of
  169. 0 ->
  170. %% No variables to expand
  171. InStr;
  172. _ ->
  173. ReOpts = [global, unicode, {return, list}],
  174. VarValue = re:replace(RawVarValue, "\\\\", "\\\\\\\\", ReOpts),
  175. %% Use a regex to match/replace:
  176. %% Given variable "FOO": match $FOO\s | $FOOeol | ${FOO}
  177. RegEx = io_lib:format("\\\$(~s(\\s|$)|{~s})", [VarName, VarName]),
  178. re:replace(InStr, RegEx, [VarValue, "\\2"], ReOpts)
  179. end.
  180. vcs_vsn(Config, Vsn, Dir) ->
  181. Key = {Vsn, Dir},
  182. Cache = rebar_config:get_xconf(Config, vsn_cache),
  183. case dict:find(Key, Cache) of
  184. error ->
  185. VsnString = vcs_vsn_1(Vsn, Dir),
  186. Cache1 = dict:store(Key, VsnString, Cache),
  187. Config1 = rebar_config:set_xconf(Config, vsn_cache, Cache1),
  188. {Config1, VsnString};
  189. {ok, VsnString} ->
  190. {Config, VsnString}
  191. end.
  192. get_deprecated_global(Config, OldOpt, NewOpt, When) ->
  193. get_deprecated_global(Config, OldOpt, NewOpt, undefined, When).
  194. get_deprecated_global(Config, OldOpt, NewOpt, Default, When) ->
  195. get_deprecated_3(fun rebar_config:get_global/3,
  196. Config, OldOpt, NewOpt, Default, When).
  197. get_experimental_global(Config, Opt, Default) ->
  198. get_experimental_3(fun rebar_config:get_global/3, Config, Opt, Default).
  199. get_experimental_local(Config, Opt, Default) ->
  200. get_experimental_3(fun rebar_config:get_local/3, Config, Opt, Default).
  201. get_deprecated_list(Config, OldOpt, NewOpt, When) ->
  202. get_deprecated_list(Config, OldOpt, NewOpt, undefined, When).
  203. get_deprecated_list(Config, OldOpt, NewOpt, Default, When) ->
  204. get_deprecated_3(fun rebar_config:get_list/3,
  205. Config, OldOpt, NewOpt, Default, When).
  206. get_deprecated_local(Config, OldOpt, NewOpt, When) ->
  207. get_deprecated_local(Config, OldOpt, NewOpt, undefined, When).
  208. get_deprecated_local(Config, OldOpt, NewOpt, Default, When) ->
  209. get_deprecated_3(fun rebar_config:get_local/3,
  210. Config, OldOpt, NewOpt, Default, When).
  211. deprecated(Old, New, Opts, When) when is_list(Opts) ->
  212. case lists:member(Old, Opts) of
  213. true ->
  214. deprecated(Old, New, When);
  215. false ->
  216. ok
  217. end;
  218. deprecated(Old, New, Config, When) ->
  219. case rebar_config:get(Config, Old, undefined) of
  220. undefined ->
  221. ok;
  222. _ ->
  223. deprecated(Old, New, When)
  224. end.
  225. deprecated(Old, New, When) ->
  226. io:format(
  227. <<"WARNING: deprecated ~p option used~n"
  228. "Option '~p' has been deprecated~n"
  229. "in favor of '~p'.~n"
  230. "'~p' will be removed ~s.~n~n">>,
  231. [Old, Old, New, Old, When]).
  232. -spec delayed_halt(integer()) -> no_return().
  233. delayed_halt(Code) ->
  234. %% Work around buffer flushing issue in erlang:halt if OTP older
  235. %% than R15B01.
  236. %% TODO: remove workaround once we require R15B01 or newer
  237. %% R15B01 introduced erlang:halt/2
  238. case erlang:is_builtin(erlang, halt, 2) of
  239. true ->
  240. halt(Code);
  241. false ->
  242. case os:type() of
  243. {win32, nt} ->
  244. timer:sleep(100),
  245. halt(Code);
  246. _ ->
  247. halt(Code),
  248. %% workaround to delay exit until all output is written
  249. receive after infinity -> ok end
  250. end
  251. end.
  252. %% @doc Return list of erl_opts
  253. -spec erl_opts(rebar_config:config()) -> list().
  254. erl_opts(Config) ->
  255. RawErlOpts = filter_defines(rebar_config:get(Config, erl_opts, []), []),
  256. Defines = [{d, list_to_atom(D)} ||
  257. D <- rebar_config:get_xconf(Config, defines, [])],
  258. Opts = Defines ++ RawErlOpts,
  259. case proplists:is_defined(no_debug_info, Opts) of
  260. true ->
  261. [O || O <- Opts, O =/= no_debug_info];
  262. false ->
  263. [debug_info|Opts]
  264. end.
  265. -spec src_dirs([string()]) -> [file:filename(), ...].
  266. src_dirs([]) ->
  267. ["src"];
  268. src_dirs(SrcDirs) ->
  269. SrcDirs.
  270. ebin_dir() ->
  271. filename:join(get_cwd(), "ebin").
  272. base_dir(Config) ->
  273. rebar_config:get_xconf(Config, base_dir).
  274. processing_base_dir(Config) ->
  275. Cwd = rebar_utils:get_cwd(),
  276. processing_base_dir(Config, Cwd).
  277. processing_base_dir(Config, Dir) ->
  278. Dir_abs = filename:absname(Dir),
  279. Dir_abs =:= filename:absname(base_dir(Config)).
  280. %% ====================================================================
  281. %% Internal functions
  282. %% ====================================================================
  283. get_deprecated_3(Get, Config, OldOpt, NewOpt, Default, When) ->
  284. case Get(Config, NewOpt, Default) of
  285. Default ->
  286. case Get(Config, OldOpt, Default) of
  287. Default ->
  288. Default;
  289. Old ->
  290. deprecated(OldOpt, NewOpt, When),
  291. Old
  292. end;
  293. New ->
  294. New
  295. end.
  296. get_experimental_3(Get, Config, Opt, Default) ->
  297. Val = Get(Config, Opt, Default),
  298. case Val of
  299. Default ->
  300. Default;
  301. Val ->
  302. ?CONSOLE("NOTICE: Using experimental option '~p'~n", [Opt]),
  303. Val
  304. end.
  305. %% We do the shell variable substitution ourselves on Windows and hope that the
  306. %% command doesn't use any other shell magic.
  307. patch_on_windows(Cmd, Env) ->
  308. case os:type() of
  309. {win32,nt} ->
  310. Cmd1 = "cmd /q /c "
  311. ++ lists:foldl(fun({Key, Value}, Acc) ->
  312. expand_env_variable(Acc, Key, Value)
  313. end, Cmd, Env),
  314. %% Remove left-over vars
  315. re:replace(Cmd1, "\\\$\\w+|\\\${\\w+}", "",
  316. [global, {return, list}]);
  317. _ ->
  318. Cmd
  319. end.
  320. expand_sh_flag(return_on_error) ->
  321. {error_handler,
  322. fun(_Command, Err) ->
  323. {error, Err}
  324. end};
  325. expand_sh_flag({abort_on_error, Message}) ->
  326. {error_handler,
  327. log_msg_and_abort(Message)};
  328. expand_sh_flag(abort_on_error) ->
  329. {error_handler,
  330. fun log_and_abort/2};
  331. expand_sh_flag(use_stdout) ->
  332. {output_handler,
  333. fun(Line, Acc) ->
  334. ?CONSOLE("~s", [Line]),
  335. [Line | Acc]
  336. end};
  337. expand_sh_flag({use_stdout, false}) ->
  338. {output_handler,
  339. fun(Line, Acc) ->
  340. [Line | Acc]
  341. end};
  342. expand_sh_flag({cd, _CdArg} = Cd) ->
  343. {port_settings, Cd};
  344. expand_sh_flag({env, _EnvArg} = Env) ->
  345. {port_settings, Env}.
  346. -type err_handler() :: fun((string(), {integer(), string()}) -> no_return()).
  347. -spec log_msg_and_abort(string()) -> err_handler().
  348. log_msg_and_abort(Message) ->
  349. fun(_Command, {_Rc, _Output}) ->
  350. ?ABORT(Message, [])
  351. end.
  352. -spec log_and_abort(string(), {integer(), string()}) -> no_return().
  353. log_and_abort(Command, {Rc, Output}) ->
  354. ?ABORT("sh(~s)~n"
  355. "failed with return code ~w and the following output:~n"
  356. "~s~n", [Command, Rc, Output]).
  357. sh_loop(Port, Fun, Acc) ->
  358. receive
  359. {Port, {data, {eol, Line}}} ->
  360. sh_loop(Port, Fun, Fun(Line ++ "\n", Acc));
  361. {Port, {data, {noeol, Line}}} ->
  362. sh_loop(Port, Fun, Fun(Line, Acc));
  363. {Port, {exit_status, 0}} ->
  364. {ok, lists:flatten(lists:reverse(Acc))};
  365. {Port, {exit_status, Rc}} ->
  366. {error, {Rc, lists:flatten(lists:reverse(Acc))}}
  367. end.
  368. beam_to_mod(Dir, Filename) ->
  369. [Dir | Rest] = filename:split(Filename),
  370. list_to_atom(filename:basename(string:join(Rest, "."), ".beam")).
  371. erl_to_mod(Filename) ->
  372. list_to_atom(filename:rootname(filename:basename(Filename))).
  373. beams(Dir) ->
  374. filelib:fold_files(Dir, ".*\.beam\$", true,
  375. fun(F, Acc) -> [F | Acc] end, []).
  376. emulate_escript_foldl(Fun, Acc, File) ->
  377. case escript:extract(File, [compile_source]) of
  378. {ok, [_Shebang, _Comment, _EmuArgs, Body]} ->
  379. case Body of
  380. {source, BeamCode} ->
  381. GetInfo = fun() -> file:read_file_info(File) end,
  382. GetBin = fun() -> BeamCode end,
  383. {ok, Fun(".", GetInfo, GetBin, Acc)};
  384. {beam, BeamCode} ->
  385. GetInfo = fun() -> file:read_file_info(File) end,
  386. GetBin = fun() -> BeamCode end,
  387. {ok, Fun(".", GetInfo, GetBin, Acc)};
  388. {archive, ArchiveBin} ->
  389. zip:foldl(Fun, Acc, {File, ArchiveBin})
  390. end;
  391. {error, _} = Error ->
  392. Error
  393. end.
  394. vcs_vsn_1(Vcs, Dir) ->
  395. case vcs_vsn_cmd(Vcs) of
  396. {plain, VsnString} ->
  397. VsnString;
  398. {cmd, CmdString} ->
  399. vcs_vsn_invoke(CmdString, Dir);
  400. unknown ->
  401. ?ABORT("vcs_vsn: Unknown vsn format: ~p\n", [Vcs]);
  402. Cmd ->
  403. %% If there is a valid VCS directory in the application directory,
  404. %% use that version info
  405. Extension = lists:concat([".", Vcs]),
  406. case filelib:is_dir(filename:join(Dir, Extension)) of
  407. true ->
  408. ?DEBUG("vcs_vsn: Primary vcs used for ~s\n", [Dir]),
  409. vcs_vsn_invoke(Cmd, Dir);
  410. false ->
  411. %% No VCS directory found for the app. Depending on source
  412. %% tree structure, there may be one higher up, but that can
  413. %% yield unexpected results when used with deps. So, we
  414. %% fallback to searching for a priv/vsn.Vcs file.
  415. VsnFile = filename:join([Dir, "priv", "vsn" ++ Extension]),
  416. case file:read_file(VsnFile) of
  417. {ok, VsnBin} ->
  418. ?DEBUG("vcs_vsn: Read ~s from priv/vsn.~p\n",
  419. [VsnBin, Vcs]),
  420. string:strip(binary_to_list(VsnBin), right, $\n);
  421. {error, enoent} ->
  422. ?DEBUG("vcs_vsn: Fallback to vcs for ~s\n", [Dir]),
  423. vcs_vsn_invoke(Cmd, Dir)
  424. end
  425. end
  426. end.
  427. vcs_vsn_cmd(git) -> "git describe --always --tags";
  428. vcs_vsn_cmd(hg) -> "hg identify -i";
  429. vcs_vsn_cmd(bzr) -> "bzr revno";
  430. vcs_vsn_cmd(svn) -> "svnversion";
  431. vcs_vsn_cmd(fossil) -> "fossil info";
  432. vcs_vsn_cmd({cmd, _Cmd}=Custom) -> Custom;
  433. vcs_vsn_cmd(Version) when is_list(Version) -> {plain, Version};
  434. vcs_vsn_cmd(_) -> unknown.
  435. vcs_vsn_invoke(Cmd, Dir) ->
  436. {ok, VsnString} = rebar_utils:sh(Cmd, [{cd, Dir}, {use_stdout, false}]),
  437. string:strip(VsnString, right, $\n).
  438. %%
  439. %% Filter a list of erl_opts platform_define options such that only
  440. %% those which match the provided architecture regex are returned.
  441. %%
  442. filter_defines([], Acc) ->
  443. lists:reverse(Acc);
  444. filter_defines([{platform_define, ArchRegex, Key} | Rest], Acc) ->
  445. case rebar_utils:is_arch(ArchRegex) of
  446. true ->
  447. filter_defines(Rest, [{d, Key} | Acc]);
  448. false ->
  449. filter_defines(Rest, Acc)
  450. end;
  451. filter_defines([{platform_define, ArchRegex, Key, Value} | Rest], Acc) ->
  452. case rebar_utils:is_arch(ArchRegex) of
  453. true ->
  454. filter_defines(Rest, [{d, Key, Value} | Acc]);
  455. false ->
  456. filter_defines(Rest, Acc)
  457. end;
  458. filter_defines([Opt | Rest], Acc) ->
  459. filter_defines(Rest, [Opt | Acc]).