Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

219 righe
7.9 KiB

  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(_Args) ->
  5. %% Fetch and build deps required to build rebar3
  6. BaseDeps = [{providers, []}
  7. ,{getopt, []}
  8. ,{erlware_commons, ["ec_dictionary.erl", "ec_vsn.erl"]}],
  9. Deps = get_deps(),
  10. [fetch_and_compile(Dep, Deps) || Dep <- BaseDeps],
  11. %% Build rebar3 modules with compile:file
  12. bootstrap_rebar3(),
  13. %% Build rebar.app from rebar.app.src
  14. {ok, App} = rebar_app_info:new(rebar, "3.0.0", filename:absname("_build/default/lib/rebar/")),
  15. rebar_otp_app:compile(rebar_state:new(), App),
  16. %% Because we are compiling files that are loaded already we want to silence
  17. %% not_purged errors in rebar_erlc_compiler:opts_changed/1
  18. error_logger:tty(false),
  19. setup_env(),
  20. os:putenv("REBAR_PROFILE", "bootstrap"),
  21. {ok, State} = rebar3:run(["compile"]),
  22. reset_env(),
  23. os:putenv("REBAR_PROFILE", ""),
  24. DepsPaths = rebar_state:code_paths(State, all_deps),
  25. code:add_pathsa(DepsPaths),
  26. rebar3:run(["clean", "-a"]),
  27. rebar3:run(["escriptize"]),
  28. %% Done with compile, can turn back on error logger
  29. error_logger:tty(true),
  30. %% Finally, update executable perms for our script on *nix,
  31. %% or write out script files on win32.
  32. ec_file:copy("_build/default/bin/rebar3", "./rebar3"),
  33. case os:type() of
  34. {unix,_} ->
  35. [] = os:cmd("chmod u+x rebar3"),
  36. ok;
  37. {win32,_} ->
  38. write_windows_scripts(),
  39. ok;
  40. _ ->
  41. ok
  42. end.
  43. fetch_and_compile({Name, ErlFirstFiles}, Deps) ->
  44. {Name, _, Repo} = lists:keyfind(Name, 1, Deps),
  45. ok = fetch(Repo, Name),
  46. compile(Name, ErlFirstFiles).
  47. fetch({git, Url, Source}, App) ->
  48. Dir = filename:join([filename:absname("_build/default/lib/"), App]),
  49. case filelib:is_dir(Dir) of
  50. true ->
  51. true = code:add_path(filename:join(Dir, "ebin")),
  52. ok;
  53. false ->
  54. fetch_source(Dir, Url, Source),
  55. ok
  56. end.
  57. fetch_source(Dir, Url, {ref, Ref}) ->
  58. ok = filelib:ensure_dir(Dir),
  59. os:cmd(io_lib:format("git clone ~s ~s", [Url, Dir])),
  60. {ok, Cwd} = file:get_cwd(),
  61. file:set_cwd(Dir),
  62. os:cmd(io_lib:format("git checkout -q ~s", [Ref])),
  63. file:set_cwd(Cwd);
  64. fetch_source(Dir, Url, {_, Branch}) ->
  65. ok = filelib:ensure_dir(Dir),
  66. os:cmd(io_lib:format("git clone ~s ~s -b ~s --single-branch",
  67. [Url, Dir, Branch])).
  68. compile(App, FirstFiles) ->
  69. Dir = filename:join(filename:absname("_build/default/lib/"), App),
  70. filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])),
  71. code:add_path(filename:join(Dir, "ebin")),
  72. FirstFilesPaths = [filename:join([Dir, "src", Module]) || Module <- FirstFiles],
  73. Sources = FirstFilesPaths ++ filelib:wildcard(filename:join([Dir, "src", "*.erl"])),
  74. [compile_file(X, [{i, filename:join(Dir, "include")}
  75. ,{outdir, filename:join(Dir, "ebin")}
  76. ,return | additional_defines()]) || X <- Sources].
  77. compile_file(File, Opts) ->
  78. case compile:file(File, Opts) of
  79. {ok, _Mod} ->
  80. ok;
  81. {ok, _Mod, []} ->
  82. ok;
  83. {ok, _Mod, Ws} ->
  84. io:format("~s~n", [format_warnings(File, Ws)]),
  85. halt(1);
  86. {error, Es, Ws} ->
  87. io:format("~s ~s~n", [format_errors(File, Es), format_warnings(File, Ws)]),
  88. halt(1)
  89. end.
  90. bootstrap_rebar3() ->
  91. filelib:ensure_dir("_build/default/lib/rebar/ebin/dummy.beam"),
  92. code:add_path("_build/default/lib/rebar/ebin/"),
  93. file:make_symlink(filename:absname("src"), filename:absname("_build/default/lib/rebar/src")),
  94. Sources = ["src/rebar_resource.erl" | filelib:wildcard("src/*.erl")],
  95. [compile_file(X, [{outdir, "_build/default/lib/rebar/ebin/"}
  96. ,return | additional_defines()]) || X <- Sources],
  97. code:add_patha(filename:absname("_build/default/lib/rebar/ebin")).
  98. setup_env() ->
  99. %% We don't need or want relx providers loaded yet
  100. application:load(rebar),
  101. {ok, Providers} = application:get_env(rebar, providers),
  102. Providers1 = Providers -- [rebar_prv_release,
  103. rebar_prv_tar],
  104. application:set_env(rebar, providers, Providers1).
  105. reset_env() ->
  106. %% Reset the env so we get all providers
  107. application:unset_env(rebar, providers),
  108. application:unload(rebar),
  109. application:load(rebar).
  110. write_windows_scripts() ->
  111. CmdScript=
  112. "@echo off\r\n"
  113. "setlocal\r\n"
  114. "set rebarscript=%~f0\r\n"
  115. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  116. ok = file:write_file("rebar3.cmd", CmdScript).
  117. get_deps() ->
  118. case file:consult("rebar.lock") of
  119. {ok, [Deps]} ->
  120. [{binary_to_atom(Name, utf8), "", Source} || {Name, Source, _Level} <- Deps];
  121. _ ->
  122. {ok, Config} = file:consult("rebar.config"),
  123. proplists:get_value(deps, Config)
  124. end.
  125. format_errors(Source, Errors) ->
  126. format_errors(Source, "", Errors).
  127. format_warnings(Source, Warnings) ->
  128. format_warnings(Source, Warnings, []).
  129. format_warnings(Source, Warnings, Opts) ->
  130. Prefix = case lists:member(warnings_as_errors, Opts) of
  131. true -> "";
  132. false -> "Warning: "
  133. end,
  134. format_errors(Source, Prefix, Warnings).
  135. format_errors(_MainSource, Extra, Errors) ->
  136. [begin
  137. [format_error(Source, Extra, Desc) || Desc <- Descs]
  138. end
  139. || {Source, Descs} <- Errors].
  140. format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
  141. ErrorDesc = Mod:format_error(Desc),
  142. io_lib:format("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
  143. format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
  144. ErrorDesc = Mod:format_error(Desc),
  145. io_lib:format("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
  146. format_error(AbsSource, Extra, {Mod, Desc}) ->
  147. ErrorDesc = Mod:format_error(Desc),
  148. io_lib:format("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).
  149. additional_defines() ->
  150. [{d, D} || {Re, D} <- [{"^[0-9]+", namespaced_types}, {"^R1[4|5]", deprecated_crypto}], is_otp_release(Re)].
  151. is_otp_release(ArchRegex) ->
  152. case re:run(otp_release(), ArchRegex, [{capture, none}]) of
  153. match ->
  154. true;
  155. nomatch ->
  156. false
  157. end.
  158. otp_release() ->
  159. otp_release1(erlang:system_info(otp_release)).
  160. %% If OTP <= R16, otp_release is already what we want.
  161. otp_release1([$R,N|_]=Rel) when is_integer(N) ->
  162. Rel;
  163. %% If OTP >= 17.x, erlang:system_info(otp_release) returns just the
  164. %% major version number, we have to read the full version from
  165. %% a file. See http://www.erlang.org/doc/system_principles/versions.html
  166. %% Read vsn string from the 'OTP_VERSION' file and return as list without
  167. %% the "\n".
  168. otp_release1(Rel) ->
  169. File = filename:join([code:root_dir(), "releases", Rel, "OTP_VERSION"]),
  170. {ok, Vsn} = file:read_file(File),
  171. %% It's fine to rely on the binary module here because we can
  172. %% be sure that it's available when the otp_release string does
  173. %% not begin with $R.
  174. Size = byte_size(Vsn),
  175. %% The shortest vsn string consists of at least two digits
  176. %% followed by "\n". Therefore, it's safe to assume Size >= 3.
  177. case binary:part(Vsn, {Size, -3}) of
  178. <<"**\n">> ->
  179. %% The OTP documentation mentions that a system patched
  180. %% using the otp_patch_apply tool available to licensed
  181. %% customers will leave a '**' suffix in the version as a
  182. %% flag saying the system consists of application versions
  183. %% from multiple OTP versions. We ignore this flag and
  184. %% drop the suffix, given for all intents and purposes, we
  185. %% cannot obtain relevant information from it as far as
  186. %% tooling is concerned.
  187. binary:bin_to_list(Vsn, {0, Size - 3});
  188. _ ->
  189. binary:bin_to_list(Vsn, {0, Size - 1})
  190. end.