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.

313 lines
11 KiB

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 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(_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. ok = symlink_or_copy(filename:absname("src"),
  94. filename:absname("_build/default/lib/rebar/src")),
  95. Sources = ["src/rebar_resource.erl" | filelib:wildcard("src/*.erl")],
  96. [compile_file(X, [{outdir, "_build/default/lib/rebar/ebin/"}
  97. ,return | additional_defines()]) || X <- Sources],
  98. code:add_patha(filename:absname("_build/default/lib/rebar/ebin")).
  99. %%rebar.hrl
  100. -define(FMT(Str, Args), lists:flatten(io_lib:format(Str, Args))).
  101. %%/rebar.hrl
  102. %%rebar_file_utils
  103. symlink_or_copy(Source, Target) ->
  104. Link = case os:type() of
  105. {win32, _} ->
  106. Source;
  107. _ ->
  108. rebar_dir:make_relative_path(Source, Target)
  109. end,
  110. case file:make_symlink(Link, Target) of
  111. ok ->
  112. ok;
  113. {error, eexist} ->
  114. ok;
  115. {error, _} ->
  116. cp_r([Source], Target)
  117. end.
  118. cp_r([], _Dest) ->
  119. ok;
  120. cp_r(Sources, Dest) ->
  121. case os:type() of
  122. {unix, _} ->
  123. EscSources = [escape_path(Src) || Src <- Sources],
  124. SourceStr = string:join(EscSources, " "),
  125. os:cmd(?FMT("cp -R ~s \"~s\"", [SourceStr, Dest])),
  126. ok;
  127. {win32, _} ->
  128. lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
  129. ok
  130. end.
  131. xcopy_win32(Source,Dest)->
  132. R = os:cmd(?FMT("xcopy \"~s\" \"~s\" /q /y /e 2> nul",
  133. [filename:nativename(Source), filename:nativename(Dest)])),
  134. case length(R) > 0 of
  135. %% when xcopy fails, stdout is empty and and error message is printed
  136. %% to stderr (which is redirected to nul)
  137. true -> ok;
  138. false ->
  139. {error, lists:flatten(
  140. io_lib:format("Failed to xcopy from ~s to ~s~n",
  141. [Source, Dest]))}
  142. end.
  143. cp_r_win32({true, SourceDir}, {true, DestDir}) ->
  144. %% from directory to directory
  145. SourceBase = filename:basename(SourceDir),
  146. ok = case file:make_dir(filename:join(DestDir, SourceBase)) of
  147. {error, eexist} -> ok;
  148. Other -> Other
  149. end,
  150. ok = xcopy_win32(SourceDir, filename:join(DestDir, SourceBase));
  151. cp_r_win32({false, Source} = S,{true, DestDir}) ->
  152. %% from file to directory
  153. cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
  154. cp_r_win32({false, Source},{false, Dest}) ->
  155. %% from file to file
  156. {ok,_} = file:copy(Source, Dest),
  157. ok;
  158. cp_r_win32({true, SourceDir}, {false, DestDir}) ->
  159. case filelib:is_regular(DestDir) of
  160. true ->
  161. %% From directory to file? This shouldn't happen
  162. {error, lists:flatten(
  163. io_lib:format("Cannot copy dir (~p) to file (~p)\n",
  164. [SourceDir, DestDir]))};
  165. false ->
  166. %% Specifying a target directory that doesn't currently exist.
  167. %% So let's attempt to create this directory
  168. case filelib:ensure_dir(filename:join(DestDir, "dummy")) of
  169. ok ->
  170. ok = xcopy_win32(SourceDir, DestDir);
  171. {error, Reason} ->
  172. {error, lists:flatten(
  173. io_lib:format("Unable to create dir ~p: ~p\n",
  174. [DestDir, Reason]))}
  175. end
  176. end;
  177. cp_r_win32(Source,Dest) ->
  178. Dst = {filelib:is_dir(Dest), Dest},
  179. lists:foreach(fun(Src) ->
  180. ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
  181. end, filelib:wildcard(Source)),
  182. ok.
  183. escape_path(Str) ->
  184. re:replace(Str, "([ ()?])", "\\\\&", [global, {return, list}]).
  185. %%/rebar_file_utils
  186. setup_env() ->
  187. %% We don't need or want relx providers loaded yet
  188. application:load(rebar),
  189. {ok, Providers} = application:get_env(rebar, providers),
  190. Providers1 = Providers -- [rebar_prv_release,
  191. rebar_prv_tar],
  192. application:set_env(rebar, providers, Providers1).
  193. reset_env() ->
  194. %% Reset the env so we get all providers
  195. application:unset_env(rebar, providers),
  196. application:unload(rebar),
  197. application:load(rebar).
  198. write_windows_scripts() ->
  199. CmdScript=
  200. "@echo off\r\n"
  201. "setlocal\r\n"
  202. "set rebarscript=%~f0\r\n"
  203. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  204. ok = file:write_file("rebar3.cmd", CmdScript).
  205. get_deps() ->
  206. case file:consult("rebar.lock") of
  207. {ok, [Deps]} ->
  208. [{binary_to_atom(Name, utf8), "", Source} || {Name, Source, _Level} <- Deps];
  209. _ ->
  210. {ok, Config} = file:consult("rebar.config"),
  211. proplists:get_value(deps, Config)
  212. end.
  213. format_errors(Source, Errors) ->
  214. format_errors(Source, "", Errors).
  215. format_warnings(Source, Warnings) ->
  216. format_warnings(Source, Warnings, []).
  217. format_warnings(Source, Warnings, Opts) ->
  218. Prefix = case lists:member(warnings_as_errors, Opts) of
  219. true -> "";
  220. false -> "Warning: "
  221. end,
  222. format_errors(Source, Prefix, Warnings).
  223. format_errors(_MainSource, Extra, Errors) ->
  224. [begin
  225. [format_error(Source, Extra, Desc) || Desc <- Descs]
  226. end
  227. || {Source, Descs} <- Errors].
  228. format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
  229. ErrorDesc = Mod:format_error(Desc),
  230. io_lib:format("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
  231. format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
  232. ErrorDesc = Mod:format_error(Desc),
  233. io_lib:format("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
  234. format_error(AbsSource, Extra, {Mod, Desc}) ->
  235. ErrorDesc = Mod:format_error(Desc),
  236. io_lib:format("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).
  237. additional_defines() ->
  238. [{d, D} || {Re, D} <- [{"^[0-9]+", namespaced_types}, {"^R1[4|5]", deprecated_crypto}], is_otp_release(Re)].
  239. is_otp_release(ArchRegex) ->
  240. case re:run(otp_release(), ArchRegex, [{capture, none}]) of
  241. match ->
  242. true;
  243. nomatch ->
  244. false
  245. end.
  246. otp_release() ->
  247. otp_release1(erlang:system_info(otp_release)).
  248. %% If OTP <= R16, otp_release is already what we want.
  249. otp_release1([$R,N|_]=Rel) when is_integer(N) ->
  250. Rel;
  251. %% If OTP >= 17.x, erlang:system_info(otp_release) returns just the
  252. %% major version number, we have to read the full version from
  253. %% a file. See http://www.erlang.org/doc/system_principles/versions.html
  254. %% Read vsn string from the 'OTP_VERSION' file and return as list without
  255. %% the "\n".
  256. otp_release1(Rel) ->
  257. File = filename:join([code:root_dir(), "releases", Rel, "OTP_VERSION"]),
  258. {ok, Vsn} = file:read_file(File),
  259. %% It's fine to rely on the binary module here because we can
  260. %% be sure that it's available when the otp_release string does
  261. %% not begin with $R.
  262. Size = byte_size(Vsn),
  263. %% The shortest vsn string consists of at least two digits
  264. %% followed by "\n". Therefore, it's safe to assume Size >= 3.
  265. case binary:part(Vsn, {Size, -3}) of
  266. <<"**\n">> ->
  267. %% The OTP documentation mentions that a system patched
  268. %% using the otp_patch_apply tool available to licensed
  269. %% customers will leave a '**' suffix in the version as a
  270. %% flag saying the system consists of application versions
  271. %% from multiple OTP versions. We ignore this flag and
  272. %% drop the suffix, given for all intents and purposes, we
  273. %% cannot obtain relevant information from it as far as
  274. %% tooling is concerned.
  275. binary:bin_to_list(Vsn, {0, Size - 3});
  276. _ ->
  277. binary:bin_to_list(Vsn, {0, Size - 1})
  278. end.