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.

699 line
26 KiB

6 年之前
7 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
  1. #!/usr/bin/env escript
  2. %% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
  3. %% ex: ft=erlang ts=4 sw=4 et
  4. main(_) ->
  5. application:start(crypto),
  6. application:start(asn1),
  7. application:start(public_key),
  8. application:start(ssl),
  9. inets:start(),
  10. inets:start(httpc, [{profile, rebar}]),
  11. set_httpc_options(),
  12. %% Clear directories for builds since bootstrapping may require
  13. %% a changed structure from an older one
  14. rm_rf("_build/bootstrap"),
  15. %% We fetch a few deps from hex for boostraping,
  16. %% so we must compile r3_safe_erl_term.xrl which
  17. %% is part of hex_core.
  18. compile_xrl_file("src/r3_safe_erl_term.xrl"),
  19. %% Fetch and build deps required to build rebar3
  20. BaseDeps = [{providers, []}
  21. ,{getopt, []}
  22. ,{cf, []}
  23. ,{erlware_commons, ["ec_dictionary.erl", "ec_vsn.erl"]}
  24. ,{parse_trans, ["parse_trans.erl", "parse_trans_pp.erl",
  25. "parse_trans_codegen.erl"]}
  26. ,{certifi, []}],
  27. Deps = get_deps(),
  28. [fetch_and_compile(Dep, Deps) || Dep <- BaseDeps],
  29. %% Build rebar3 modules with compile:file
  30. bootstrap_rebar3(),
  31. %% Build rebar.app from rebar.app.src
  32. {ok, App} = rebar_app_info:new(rebar, "3.9.1", filename:absname("_build/default/lib/rebar/")),
  33. rebar_otp_app:compile(rebar_state:new(), App),
  34. %% Because we are compiling files that are loaded already we want to silence
  35. %% not_purged errors in rebar_erlc_compiler:opts_changed/1
  36. error_logger:tty(false),
  37. setup_env(),
  38. os:putenv("REBAR_PROFILE", "bootstrap"),
  39. {ok, State} = rebar3:run(["compile"]),
  40. reset_env(),
  41. os:putenv("REBAR_PROFILE", ""),
  42. DepsPaths = rebar_state:code_paths(State, all_deps),
  43. code:add_pathsa(DepsPaths),
  44. rebar3:run(["clean", "-a"]),
  45. rebar3:run(["as", "prod", "escriptize"]),
  46. %% Done with compile, can turn back on error logger
  47. error_logger:tty(true).
  48. fetch_and_compile({Name, ErlFirstFiles}, Deps) ->
  49. case lists:keyfind(Name, 1, Deps) of
  50. {Name, Vsn} ->
  51. ok = fetch({pkg, atom_to_binary(Name, utf8), list_to_binary(Vsn)}, Name);
  52. {Name, _, Source} ->
  53. ok = fetch(Source, Name)
  54. end,
  55. %% Hack: erlware_commons depends on a .script file to check if it is being built with
  56. %% rebar2 or rebar3. But since rebar3 isn't built yet it can't get the vsn with get_key.
  57. %% So we simply make sure that file is deleted before compiling
  58. file:delete("_build/default/lib/erlware_commons/rebar.config.script"),
  59. compile(Name, ErlFirstFiles).
  60. fetch({pkg, Name, Vsn}, App) ->
  61. Dir = filename:join([filename:absname("_build/default/lib/"), App]),
  62. case filelib:is_dir(Dir) of
  63. false ->
  64. CDN = "https://repo.hex.pm/tarballs",
  65. Package = binary_to_list(<<Name/binary, "-", Vsn/binary, ".tar">>),
  66. Url = join([CDN, Package], "/"),
  67. case request(Url) of
  68. {ok, Binary} ->
  69. {ok, Contents} = extract(Binary),
  70. ok = erl_tar:extract({binary, Contents}, [{cwd, Dir}, compressed]);
  71. {error, {Reason, _}} ->
  72. ReasonText = re:replace(atom_to_list(Reason), "_", " ", [global,{return,list}]),
  73. io:format("Error: Unable to fetch package ~s ~s: ~s~n", [Name, Vsn, ReasonText])
  74. end;
  75. true ->
  76. io:format("Dependency ~s already exists~n", [Name])
  77. end.
  78. extract(Binary) ->
  79. {ok, Files} = erl_tar:extract({binary, Binary}, [memory]),
  80. {"contents.tar.gz", Contents} = lists:keyfind("contents.tar.gz", 1, Files),
  81. {ok, Contents}.
  82. request(Url) ->
  83. HttpOptions = [{relaxed, true} | get_proxy_auth()],
  84. case httpc:request(get, {Url, []},
  85. HttpOptions,
  86. [{body_format, binary}],
  87. rebar) of
  88. {ok, {{_Version, 200, _Reason}, _Headers, Body}} ->
  89. {ok, Body};
  90. Error ->
  91. Error
  92. end.
  93. get_rebar_config() ->
  94. {ok, [[Home]]} = init:get_argument(home),
  95. ConfDir = filename:join(Home, ".config/rebar3"),
  96. case file:consult(filename:join(ConfDir, "rebar.config")) of
  97. {ok, Config} ->
  98. Config;
  99. _ ->
  100. []
  101. end.
  102. get_http_vars(Scheme) ->
  103. OS = case os:getenv(atom_to_list(Scheme)) of
  104. Str when is_list(Str) -> Str;
  105. _ -> []
  106. end,
  107. proplists:get_value(Scheme, get_rebar_config(), OS).
  108. set_httpc_options() ->
  109. set_httpc_options(https_proxy, get_http_vars(https_proxy)),
  110. set_httpc_options(proxy, get_http_vars(http_proxy)).
  111. set_httpc_options(_, []) ->
  112. ok;
  113. set_httpc_options(Scheme, Proxy) ->
  114. {ok, {_, UserInfo, Host, Port, _, _}} = http_uri:parse(Proxy),
  115. httpc:set_options([{Scheme, {{Host, Port}, []}}], rebar),
  116. set_proxy_auth(UserInfo).
  117. compile(App, FirstFiles) ->
  118. Dir = filename:join(filename:absname("_build/default/lib/"), App),
  119. filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])),
  120. code:add_path(filename:join(Dir, "ebin")),
  121. FirstFilesPaths = [filename:join([Dir, "src", Module]) || Module <- FirstFiles],
  122. LeexFiles = filelib:wildcard(filename:join([Dir, "src", "*.xrl"])),
  123. [compile_xrl_file(X) || X <- LeexFiles],
  124. YeccFiles = filelib:wildcard(filename:join([Dir, "src", "*.yrl"])),
  125. [compile_yrl_file(X) || X <- YeccFiles],
  126. Sources = FirstFilesPaths ++ filelib:wildcard(filename:join([Dir, "src", "*.erl"])),
  127. [compile_erl_file(X, [{i, filename:join(Dir, "include")}
  128. ,debug_info
  129. ,{outdir, filename:join(Dir, "ebin")}
  130. ,return | additional_defines()]) || X <- Sources].
  131. compile_xrl_file(File) ->
  132. {ok, _} = leex:file(File).
  133. compile_yrl_file(File) ->
  134. {ok, _} = yecc:file(File).
  135. compile_erl_file(File, Opts) ->
  136. case compile:file(File, Opts) of
  137. {ok, _Mod} ->
  138. ok;
  139. {ok, _Mod, []} ->
  140. ok;
  141. {ok, _Mod, Ws} ->
  142. io:format("~s~n", [format_warnings(File, Ws)]),
  143. halt(1);
  144. {error, Es, Ws} ->
  145. io:format("~s ~s~n", [format_errors(File, Es), format_warnings(File, Ws)]),
  146. halt(1)
  147. end.
  148. bootstrap_rebar3() ->
  149. filelib:ensure_dir("_build/default/lib/rebar/ebin/dummy.beam"),
  150. code:add_path("_build/default/lib/rebar/ebin/"),
  151. Res = symlink_or_copy(filename:absname("src"),
  152. filename:absname("_build/default/lib/rebar/src")),
  153. true = Res == ok orelse Res == exists,
  154. Sources = ["src/rebar_resource_v2.erl", "src/rebar_resource.erl" | filelib:wildcard("src/*.erl") ],
  155. [compile_erl_file(X, [{outdir, "_build/default/lib/rebar/ebin/"}
  156. ,return | additional_defines()]) || X <- Sources],
  157. code:add_patha(filename:absname("_build/default/lib/rebar/ebin")).
  158. %%rebar.hrl
  159. -define(FMT(Str, Args), lists:flatten(io_lib:format(Str, Args))).
  160. %%/rebar.hrl
  161. %%rebar_file_utils
  162. -include_lib("kernel/include/file.hrl").
  163. symlink_or_copy(Source, Target) ->
  164. Link = case os:type() of
  165. {win32, _} ->
  166. Source;
  167. _ ->
  168. make_relative_path(Source, Target)
  169. end,
  170. case file:make_symlink(Link, Target) of
  171. ok ->
  172. ok;
  173. {error, eexist} ->
  174. exists;
  175. {error, _} ->
  176. case os:type() of
  177. {win32, _} ->
  178. S = unicode:characters_to_list(Source),
  179. T = unicode:characters_to_list(Target),
  180. case filelib:is_dir(S) of
  181. true ->
  182. win32_symlink_or_copy(S, T);
  183. false ->
  184. cp_r([S], T)
  185. end;
  186. _ ->
  187. case filelib:is_dir(Target) of
  188. true ->
  189. ok;
  190. false ->
  191. cp_r([Source], Target)
  192. end
  193. end
  194. end.
  195. -spec rm_rf(string()) -> 'ok'.
  196. rm_rf(Target) ->
  197. case os:type() of
  198. {unix, _} ->
  199. EscTarget = escape_chars(Target),
  200. {ok, []} = sh(?FMT("rm -rf ~ts", [EscTarget]),
  201. [{use_stdout, false}, abort_on_error]),
  202. ok;
  203. {win32, _} ->
  204. Filelist = filelib:wildcard(Target),
  205. Dirs = [F || F <- Filelist, filelib:is_dir(F)],
  206. Files = Filelist -- Dirs,
  207. ok = delete_each(Files),
  208. ok = delete_each_dir_win32(Dirs),
  209. ok
  210. end.
  211. -spec cp_r(list(string()), file:filename()) -> 'ok'.
  212. cp_r([], _Dest) ->
  213. ok;
  214. cp_r(Sources, Dest) ->
  215. case os:type() of
  216. {unix, _} ->
  217. EscSources = [escape_chars(Src) || Src <- Sources],
  218. SourceStr = join(EscSources, " "),
  219. {ok, []} = sh(?FMT("cp -Rp ~ts \"~ts\"",
  220. [SourceStr, escape_double_quotes(Dest)]),
  221. [{use_stdout, false}, abort_on_error]),
  222. ok;
  223. {win32, _} ->
  224. lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
  225. ok
  226. end.
  227. %% @private Compatibility function for windows
  228. win32_symlink_or_copy(Source, Target) ->
  229. Res = sh(?FMT("cmd /c mklink /j \"~ts\" \"~ts\"",
  230. [escape_double_quotes(filename:nativename(Target)),
  231. escape_double_quotes(filename:nativename(Source))]),
  232. [{use_stdout, false}, return_on_error]),
  233. case win32_mklink_ok(Res, Target) of
  234. true -> ok;
  235. false -> cp_r_win32(Source, drop_last_dir_from_path(Target))
  236. end.
  237. cp_r_win32({true, SourceDir}, {true, DestDir}) ->
  238. %% from directory to directory
  239. ok = case file:make_dir(DestDir) of
  240. {error, eexist} -> ok;
  241. Other -> Other
  242. end,
  243. ok = xcopy_win32(SourceDir, DestDir);
  244. cp_r_win32({false, Source} = S,{true, DestDir}) ->
  245. %% from file to directory
  246. cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
  247. cp_r_win32({false, Source},{false, Dest}) ->
  248. %% from file to file
  249. {ok,_} = file:copy(Source, Dest),
  250. ok;
  251. cp_r_win32({true, SourceDir}, {false, DestDir}) ->
  252. case filelib:is_regular(DestDir) of
  253. true ->
  254. %% From directory to file? This shouldn't happen
  255. {error, lists:flatten(
  256. io_lib:format("Cannot copy dir (~p) to file (~p)\n",
  257. [SourceDir, DestDir]))};
  258. false ->
  259. %% Specifying a target directory that doesn't currently exist.
  260. %% So let's attempt to create this directory
  261. case filelib:ensure_dir(filename:join(DestDir, "dummy")) of
  262. ok ->
  263. ok = xcopy_win32(SourceDir, DestDir);
  264. {error, Reason} ->
  265. {error, lists:flatten(
  266. io_lib:format("Unable to create dir ~p: ~p\n",
  267. [DestDir, Reason]))}
  268. end
  269. end;
  270. cp_r_win32(Source,Dest) ->
  271. Dst = {filelib:is_dir(Dest), Dest},
  272. lists:foreach(fun(Src) ->
  273. ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
  274. end, filelib:wildcard(Source)),
  275. ok.
  276. %% drops the last 'node' of the filename, presumably the last dir such as 'src'
  277. %% this is because cp_r_win32/2 automatically adds the dir name, to appease
  278. %% robocopy and be more uniform with POSIX
  279. drop_last_dir_from_path([]) ->
  280. [];
  281. drop_last_dir_from_path(Path) ->
  282. case lists:droplast(filename:split(Path)) of
  283. [] -> [];
  284. Dirs -> filename:join(Dirs)
  285. end.
  286. %% @private specifically pattern match against the output
  287. %% of the windows 'mklink' shell call; different values from
  288. %% what win32_ok/1 handles
  289. win32_mklink_ok({ok, _}, _) ->
  290. true;
  291. win32_mklink_ok({error,{1,"Local NTFS volumes are required to complete the operation.\n"}}, _) ->
  292. false;
  293. win32_mklink_ok({error,{1,"Cannot create a file when that file already exists.\n"}}, Target) ->
  294. % File or dir is already in place; find if it is already a symlink (true) or
  295. % if it is a directory (copy-required; false)
  296. is_symlink(Target);
  297. win32_mklink_ok(_, _) ->
  298. false.
  299. xcopy_win32(Source,Dest)->
  300. %% "xcopy \"~ts\" \"~ts\" /q /y /e 2> nul", Changed to robocopy to
  301. %% handle long names. May have issues with older windows.
  302. Cmd = case filelib:is_dir(Source) of
  303. true ->
  304. %% For robocopy, copying /a/b/c/ to /d/e/f/ recursively does not
  305. %% create /d/e/f/c/*, but rather copies all files to /d/e/f/*.
  306. %% The usage we make here expects the former, not the later, so we
  307. %% must manually add the last fragment of a directory to the `Dest`
  308. %% in order to properly replicate POSIX platforms
  309. NewDest = filename:join([Dest, filename:basename(Source)]),
  310. ?FMT("robocopy \"~ts\" \"~ts\" /e 1> nul",
  311. [escape_double_quotes(filename:nativename(Source)),
  312. escape_double_quotes(filename:nativename(NewDest))]);
  313. false ->
  314. ?FMT("robocopy \"~ts\" \"~ts\" \"~ts\" /e 1> nul",
  315. [escape_double_quotes(filename:nativename(filename:dirname(Source))),
  316. escape_double_quotes(filename:nativename(Dest)),
  317. escape_double_quotes(filename:basename(Source))])
  318. end,
  319. Res = sh(Cmd, [{use_stdout, false}, return_on_error]),
  320. case win32_ok(Res) of
  321. true -> ok;
  322. false ->
  323. {error, lists:flatten(
  324. io_lib:format("Failed to copy ~ts to ~ts~n",
  325. [Source, Dest]))}
  326. end.
  327. is_symlink(Filename) ->
  328. {ok, Info} = file:read_link_info(Filename),
  329. Info#file_info.type == symlink.
  330. win32_ok({ok, _}) -> true;
  331. win32_ok({error, {Rc, _}}) when Rc<9; Rc=:=16 -> true;
  332. win32_ok(_) -> false.
  333. %% @private windows rm_rf helpers
  334. delete_each([]) ->
  335. ok;
  336. delete_each([File | Rest]) ->
  337. case file:delete(File) of
  338. ok ->
  339. delete_each(Rest);
  340. {error, enoent} ->
  341. delete_each(Rest);
  342. {error, Reason} ->
  343. io:format("Failed to delete file ~ts: ~p\n", [File, Reason]),
  344. error
  345. end.
  346. delete_each_dir_win32([]) -> ok;
  347. delete_each_dir_win32([Dir | Rest]) ->
  348. {ok, []} = sh(?FMT("rd /q /s \"~ts\"",
  349. [escape_double_quotes(filename:nativename(Dir))]),
  350. [{use_stdout, false}, return_on_error]),
  351. delete_each_dir_win32(Rest).
  352. %%/rebar_file_utils
  353. %%rebar_utils
  354. %% escape\ as\ a\ shell\?
  355. escape_chars(Str) when is_atom(Str) ->
  356. escape_chars(atom_to_list(Str));
  357. escape_chars(Str) ->
  358. re:replace(Str, "([ ()?`!$&;\"\'])", "\\\\&",
  359. [global, {return, list}, unicode]).
  360. %% "escape inside these"
  361. escape_double_quotes(Str) ->
  362. re:replace(Str, "([\"\\\\`!$&*;])", "\\\\&",
  363. [global, {return, list}, unicode]).
  364. sh(Command0, Options0) ->
  365. DefaultOptions = [{use_stdout, false}],
  366. Options = [expand_sh_flag(V)
  367. || V <- proplists:compact(Options0 ++ DefaultOptions)],
  368. ErrorHandler = proplists:get_value(error_handler, Options),
  369. OutputHandler = proplists:get_value(output_handler, Options),
  370. Command = lists:flatten(patch_on_windows(Command0, proplists:get_value(env, Options, []))),
  371. PortSettings = proplists:get_all_values(port_settings, Options) ++
  372. [exit_status, {line, 16384}, use_stdio, stderr_to_stdout, hide, eof],
  373. Port = open_port({spawn, Command}, PortSettings),
  374. try
  375. case sh_loop(Port, OutputHandler, []) of
  376. {ok, _Output} = Ok ->
  377. Ok;
  378. {error, {_Rc, _Output}=Err} ->
  379. ErrorHandler(Command, Err)
  380. end
  381. after
  382. port_close(Port)
  383. end.
  384. sh_loop(Port, Fun, Acc) ->
  385. receive
  386. {Port, {data, {eol, Line}}} ->
  387. sh_loop(Port, Fun, Fun(Line ++ "\n", Acc));
  388. {Port, {data, {noeol, Line}}} ->
  389. sh_loop(Port, Fun, Fun(Line, Acc));
  390. {Port, eof} ->
  391. Data = lists:flatten(lists:reverse(Acc)),
  392. receive
  393. {Port, {exit_status, 0}} ->
  394. {ok, Data};
  395. {Port, {exit_status, Rc}} ->
  396. {error, {Rc, Data}}
  397. end
  398. end.
  399. expand_sh_flag(return_on_error) ->
  400. {error_handler,
  401. fun(_Command, Err) ->
  402. {error, Err}
  403. end};
  404. expand_sh_flag(abort_on_error) ->
  405. {error_handler,
  406. %% moved log_and_abort/2 here because some versions somehow had trouble
  407. %% interpreting it and thought `fun log_and_abort/2' was in `erl_eval'
  408. fun(Command, {Rc, Output}) ->
  409. io:format("sh(~ts)~n"
  410. "failed with return code ~w and the following output:~n"
  411. "~ts", [Command, Rc, Output]),
  412. throw(bootstrap_abort)
  413. end};
  414. expand_sh_flag({use_stdout, false}) ->
  415. {output_handler,
  416. fun(Line, Acc) ->
  417. [Line | Acc]
  418. end};
  419. expand_sh_flag({cd, _CdArg} = Cd) ->
  420. {port_settings, Cd};
  421. expand_sh_flag({env, _EnvArg} = Env) ->
  422. {port_settings, Env}.
  423. %% We do the shell variable substitution ourselves on Windows and hope that the
  424. %% command doesn't use any other shell magic.
  425. patch_on_windows(Cmd, Env) ->
  426. case os:type() of
  427. {win32,nt} ->
  428. Cmd1 = "cmd /q /c "
  429. ++ lists:foldl(fun({Key, Value}, Acc) ->
  430. expand_env_variable(Acc, Key, Value)
  431. end, Cmd, Env),
  432. %% Remove left-over vars
  433. re:replace(Cmd1, "\\\$\\w+|\\\${\\w+}", "",
  434. [global, {return, list}, unicode]);
  435. _ ->
  436. Cmd
  437. end.
  438. %% @doc Given env. variable `FOO' we want to expand all references to
  439. %% it in `InStr'. References can have two forms: `$FOO' and `${FOO}'
  440. %% The end of form `$FOO' is delimited with whitespace or EOL
  441. -spec expand_env_variable(string(), string(), term()) -> string().
  442. expand_env_variable(InStr, VarName, RawVarValue) ->
  443. case chr(InStr, $$) of
  444. 0 ->
  445. %% No variables to expand
  446. InStr;
  447. _ ->
  448. ReOpts = [global, unicode, {return, list}],
  449. VarValue = re:replace(RawVarValue, "\\\\", "\\\\\\\\", ReOpts),
  450. %% Use a regex to match/replace:
  451. %% Given variable "FOO": match $FOO\s | $FOOeol | ${FOO}
  452. RegEx = io_lib:format("\\\$(~ts(\\W|$)|{~ts})", [VarName, VarName]),
  453. re:replace(InStr, RegEx, [VarValue, "\\2"], ReOpts)
  454. end.
  455. %%/rebar_utils
  456. %%rebar_dir
  457. make_relative_path(Source, Target) ->
  458. AbsSource = make_normalized_path(Source),
  459. AbsTarget = make_normalized_path(Target),
  460. do_make_relative_path(filename:split(AbsSource), filename:split(AbsTarget)).
  461. %% @private based on fragments of paths, replace the number of common
  462. %% segments by `../' bits, and add the rest of the source alone after it
  463. -spec do_make_relative_path([string()], [string()]) -> file:filename().
  464. do_make_relative_path([H|T1], [H|T2]) ->
  465. do_make_relative_path(T1, T2);
  466. do_make_relative_path(Source, Target) ->
  467. Base = lists:duplicate(max(length(Target) - 1, 0), ".."),
  468. filename:join(Base ++ Source).
  469. make_normalized_path(Path) ->
  470. AbsPath = make_absolute_path(Path),
  471. Components = filename:split(AbsPath),
  472. make_normalized_path(Components, []).
  473. make_absolute_path(Path) ->
  474. case filename:pathtype(Path) of
  475. absolute ->
  476. Path;
  477. relative ->
  478. {ok, Dir} = file:get_cwd(),
  479. filename:join([Dir, Path]);
  480. volumerelative ->
  481. Volume = hd(filename:split(Path)),
  482. {ok, Dir} = file:get_cwd(Volume),
  483. filename:join([Dir, Path])
  484. end.
  485. -spec make_normalized_path([string()], [string()]) -> file:filename().
  486. make_normalized_path([], NormalizedPath) ->
  487. filename:join(lists:reverse(NormalizedPath));
  488. make_normalized_path([H|T], NormalizedPath) ->
  489. case H of
  490. "." when NormalizedPath == [], T == [] -> make_normalized_path(T, ["."]);
  491. "." -> make_normalized_path(T, NormalizedPath);
  492. ".." when NormalizedPath == [] -> make_normalized_path(T, [".."]);
  493. ".." when hd(NormalizedPath) =/= ".." -> make_normalized_path(T, tl(NormalizedPath));
  494. _ -> make_normalized_path(T, [H|NormalizedPath])
  495. end.
  496. %%/rebar_dir
  497. setup_env() ->
  498. %% We don't need or want relx providers loaded yet
  499. application:load(rebar),
  500. {ok, Providers} = application:get_env(rebar, providers),
  501. Providers1 = Providers -- [rebar_prv_release,
  502. rebar_prv_relup,
  503. rebar_prv_tar],
  504. application:set_env(rebar, providers, Providers1).
  505. reset_env() ->
  506. %% Reset the env so we get all providers
  507. application:unset_env(rebar, providers),
  508. application:unload(rebar),
  509. application:load(rebar).
  510. get_deps() ->
  511. case file:consult("rebar.lock") of
  512. {ok, [[]]} ->
  513. %% Something went wrong in a previous build, lock file shouldn't be empty
  514. io:format("Empty list in lock file, deleting rebar.lock~n"),
  515. ok = file:delete("rebar.lock"),
  516. {ok, Config} = file:consult("rebar.config"),
  517. proplists:get_value(deps, Config);
  518. {ok, [Deps]} ->
  519. [{binary_to_atom(Name, utf8), "", Source} || {Name, Source, _Level} <- Deps];
  520. _ ->
  521. {ok, Config} = file:consult("rebar.config"),
  522. proplists:get_value(deps, Config)
  523. end.
  524. format_errors(Source, Errors) ->
  525. format_errors(Source, "", Errors).
  526. format_warnings(Source, Warnings) ->
  527. format_warnings(Source, Warnings, []).
  528. format_warnings(Source, Warnings, Opts) ->
  529. Prefix = case lists:member(warnings_as_errors, Opts) of
  530. true -> "";
  531. false -> "Warning: "
  532. end,
  533. format_errors(Source, Prefix, Warnings).
  534. format_errors(_MainSource, Extra, Errors) ->
  535. [begin
  536. [format_error(Source, Extra, Desc) || Desc <- Descs]
  537. end
  538. || {Source, Descs} <- Errors].
  539. format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
  540. ErrorDesc = Mod:format_error(Desc),
  541. io_lib:format("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
  542. format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
  543. ErrorDesc = Mod:format_error(Desc),
  544. io_lib:format("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
  545. format_error(AbsSource, Extra, {Mod, Desc}) ->
  546. ErrorDesc = Mod:format_error(Desc),
  547. io_lib:format("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).
  548. additional_defines() ->
  549. [{d, D} || {Re, D} <- [{"^[0-9]+", namespaced_types},
  550. {"^R1[4|5]", deprecated_crypto},
  551. {"^2", unicode_str},
  552. {"^(R|1|20)", fun_stacktrace},
  553. {"^((1[8|9])|2)", rand_module}],
  554. is_otp_release(Re)].
  555. is_otp_release(ArchRegex) ->
  556. case re:run(otp_release(), ArchRegex, [{capture, none}]) of
  557. match ->
  558. true;
  559. nomatch ->
  560. false
  561. end.
  562. otp_release() ->
  563. otp_release1(erlang:system_info(otp_release)).
  564. %% If OTP <= R16, otp_release is already what we want.
  565. otp_release1([$R,N|_]=Rel) when is_integer(N) ->
  566. Rel;
  567. %% If OTP >= 17.x, erlang:system_info(otp_release) returns just the
  568. %% major version number, we have to read the full version from
  569. %% a file. See http://www.erlang.org/doc/system_principles/versions.html
  570. %% Read vsn string from the 'OTP_VERSION' file and return as list without
  571. %% the "\n".
  572. otp_release1(Rel) ->
  573. File = filename:join([code:root_dir(), "releases", Rel, "OTP_VERSION"]),
  574. case file:read_file(File) of
  575. {error, _} ->
  576. Rel;
  577. {ok, Vsn} ->
  578. %% It's fine to rely on the binary module here because we can
  579. %% be sure that it's available when the otp_release string does
  580. %% not begin with $R.
  581. Size = byte_size(Vsn),
  582. %% The shortest vsn string consists of at least two digits
  583. %% followed by "\n". Therefore, it's safe to assume Size >= 3.
  584. case binary:part(Vsn, {Size, -3}) of
  585. <<"**\n">> ->
  586. %% The OTP documentation mentions that a system patched
  587. %% using the otp_patch_apply tool available to licensed
  588. %% customers will leave a '**' suffix in the version as a
  589. %% flag saying the system consists of application versions
  590. %% from multiple OTP versions. We ignore this flag and
  591. %% drop the suffix, given for all intents and purposes, we
  592. %% cannot obtain relevant information from it as far as
  593. %% tooling is concerned.
  594. binary:bin_to_list(Vsn, {0, Size - 3});
  595. _ ->
  596. binary:bin_to_list(Vsn, {0, Size - 1})
  597. end
  598. end.
  599. set_proxy_auth([]) ->
  600. ok;
  601. set_proxy_auth(UserInfo) ->
  602. [Username, Password] = re:split(UserInfo, ":",
  603. [{return, list}, {parts,2}, unicode]),
  604. %% password may contain url encoded characters, need to decode them first
  605. put(proxy_auth, [{proxy_auth, {Username, http_uri:decode(Password)}}]).
  606. get_proxy_auth() ->
  607. case get(proxy_auth) of
  608. undefined -> [];
  609. ProxyAuth -> ProxyAuth
  610. end.
  611. %% string:join/2 copy; string:join/2 is getting obsoleted
  612. %% and replaced by lists:join/2, but lists:join/2 is too new
  613. %% for version support (only appeared in 19.0) so it cannot be
  614. %% used. Instead we just adopt join/2 locally and hope it works
  615. %% for most unicode use cases anyway.
  616. join([], Sep) when is_list(Sep) ->
  617. [];
  618. join([H|T], Sep) ->
  619. H ++ lists:append([Sep ++ X || X <- T]).
  620. %% Same for chr; no non-deprecated equivalent in OTP20+
  621. chr(S, C) when is_integer(C) -> chr(S, C, 1).
  622. chr([C|_Cs], C, I) -> I;
  623. chr([_|Cs], C, I) -> chr(Cs, C, I+1);
  624. chr([], _C, _I) -> 0.