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.

718 line
26 KiB

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