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.

715 regels
26 KiB

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