Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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