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.

379 lines
13 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. application:start(crypto),
  6. application:start(asn1),
  7. application:start(public_key),
  8. application:start(ssl),
  9. inets:start(),
  10. %% Fetch and build deps required to build rebar3
  11. BaseDeps = [{providers, []}
  12. ,{getopt, []}
  13. ,{erlware_commons, ["ec_dictionary.erl", "ec_vsn.erl"]}],
  14. Deps = get_deps(),
  15. [fetch_and_compile(Dep, Deps) || Dep <- BaseDeps],
  16. %% Build rebar3 modules with compile:file
  17. bootstrap_rebar3(),
  18. %% Build rebar.app from rebar.app.src
  19. {ok, App} = rebar_app_info:new(rebar, "3.0.0", filename:absname("_build/default/lib/rebar/")),
  20. rebar_otp_app:compile(rebar_state:new(), App),
  21. %% Because we are compiling files that are loaded already we want to silence
  22. %% not_purged errors in rebar_erlc_compiler:opts_changed/1
  23. error_logger:tty(false),
  24. setup_env(),
  25. os:putenv("REBAR_PROFILE", "bootstrap"),
  26. rebar3:run(["update"]),
  27. {ok, State} = rebar3:run(["compile"]),
  28. reset_env(),
  29. os:putenv("REBAR_PROFILE", ""),
  30. DepsPaths = rebar_state:code_paths(State, all_deps),
  31. code:add_pathsa(DepsPaths),
  32. rebar3:run(["clean", "-a"]),
  33. rebar3:run(["escriptize"]),
  34. %% Done with compile, can turn back on error logger
  35. error_logger:tty(true),
  36. %% Finally, update executable perms for our script on *nix,
  37. %% or write out script files on win32.
  38. ec_file:copy("_build/default/bin/rebar3", "./rebar3"),
  39. case os:type() of
  40. {unix,_} ->
  41. [] = os:cmd("chmod u+x rebar3"),
  42. ok;
  43. {win32,_} ->
  44. write_windows_scripts(),
  45. ok;
  46. _ ->
  47. ok
  48. end.
  49. fetch_and_compile({Name, ErlFirstFiles}, Deps) ->
  50. {Name, _, Repo} = lists:keyfind(Name, 1, Deps),
  51. ok = fetch(Repo, Name),
  52. compile(Name, ErlFirstFiles).
  53. fetch({pkg, Name, Vsn}, App) ->
  54. Dir = filename:join([filename:absname("_build/default/lib/"), App]),
  55. CDN = "https://s3.amazonaws.com/s3.hex.pm/tarballs",
  56. Package = binary_to_list(<<Name/binary, "-", Vsn/binary, ".tar">>),
  57. Url = string:join([CDN, Package], "/"),
  58. case request(Url) of
  59. {ok, Binary} ->
  60. {ok, Contents} = extract(Binary),
  61. ok = erl_tar:extract({binary, Contents}, [{cwd, Dir}, compressed]);
  62. _ ->
  63. io:format("Error: Unable to fetch package ~p ~p~n", [Name, Vsn])
  64. end.
  65. extract(Binary) ->
  66. {ok, Files} = erl_tar:extract({binary, Binary}, [memory]),
  67. {"contents.tar.gz", Contents} = lists:keyfind("contents.tar.gz", 1, Files),
  68. {ok, Contents}.
  69. request(Url) ->
  70. setHttpcOptions(),
  71. case httpc:request(get, {Url, []},
  72. [{relaxed, true}],
  73. [{body_format, binary}]) of
  74. {ok, {{_Version, 200, _Reason}, _Headers, Body}} ->
  75. {ok, Body};
  76. Error ->
  77. Error
  78. end.
  79. setHttpcOptions() ->
  80. %% Get http_proxy and https_proxy environment variables
  81. case os:getenv("http_proxy") of
  82. false ->
  83. Http = "";
  84. Http ->
  85. Http
  86. end,
  87. case os:getenv("https_proxy") of
  88. false ->
  89. Https = "";
  90. Https ->
  91. Https
  92. end,
  93. %% Parse the variables to extract host and port
  94. Opts = [],
  95. case http_uri:parse(Http) of
  96. {ok,{_, [], Host, Port, _, []}} ->
  97. Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}];
  98. {error, _} ->
  99. Opts1 = Opts
  100. end,
  101. case http_uri:parse(Https) of
  102. {ok,{_, [], Host2, Port2, _, []}} ->
  103. Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}];
  104. {error, _} ->
  105. Opts2 = Opts1
  106. end,
  107. case Opts2 of
  108. [] ->
  109. ok;
  110. _ ->
  111. httpc:set_options(Opts2),
  112. ok
  113. end.
  114. compile(App, FirstFiles) ->
  115. Dir = filename:join(filename:absname("_build/default/lib/"), App),
  116. filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])),
  117. code:add_path(filename:join(Dir, "ebin")),
  118. FirstFilesPaths = [filename:join([Dir, "src", Module]) || Module <- FirstFiles],
  119. Sources = FirstFilesPaths ++ filelib:wildcard(filename:join([Dir, "src", "*.erl"])),
  120. [compile_file(X, [{i, filename:join(Dir, "include")}
  121. ,debug_info
  122. ,{outdir, filename:join(Dir, "ebin")}
  123. ,return | additional_defines()]) || X <- Sources].
  124. compile_file(File, Opts) ->
  125. case compile:file(File, Opts) of
  126. {ok, _Mod} ->
  127. ok;
  128. {ok, _Mod, []} ->
  129. ok;
  130. {ok, _Mod, Ws} ->
  131. io:format("~s~n", [format_warnings(File, Ws)]),
  132. halt(1);
  133. {error, Es, Ws} ->
  134. io:format("~s ~s~n", [format_errors(File, Es), format_warnings(File, Ws)]),
  135. halt(1)
  136. end.
  137. bootstrap_rebar3() ->
  138. filelib:ensure_dir("_build/default/lib/rebar/ebin/dummy.beam"),
  139. code:add_path("_build/default/lib/rebar/ebin/"),
  140. ok = symlink_or_copy(filename:absname("src"),
  141. filename:absname("_build/default/lib/rebar/src")),
  142. Sources = ["src/rebar_resource.erl" | filelib:wildcard("src/*.erl")],
  143. [compile_file(X, [{outdir, "_build/default/lib/rebar/ebin/"}
  144. ,return | additional_defines()]) || X <- Sources],
  145. code:add_patha(filename:absname("_build/default/lib/rebar/ebin")).
  146. %%rebar.hrl
  147. -define(FMT(Str, Args), lists:flatten(io_lib:format(Str, Args))).
  148. %%/rebar.hrl
  149. %%rebar_file_utils
  150. symlink_or_copy(Source, Target) ->
  151. Link = case os:type() of
  152. {win32, _} ->
  153. Source;
  154. _ ->
  155. make_relative_path(Source, Target)
  156. end,
  157. case file:make_symlink(Link, Target) of
  158. ok ->
  159. ok;
  160. {error, eexist} ->
  161. ok;
  162. {error, _} ->
  163. cp_r([Source], Target)
  164. end.
  165. make_relative_path(Source, Target) ->
  166. do_make_relative_path(filename:split(Source), filename:split(Target)).
  167. do_make_relative_path([H|T1], [H|T2]) ->
  168. do_make_relative_path(T1, T2);
  169. do_make_relative_path(Source, Target) ->
  170. Base = lists:duplicate(max(length(Target) - 1, 0), ".."),
  171. filename:join(Base ++ Source).
  172. cp_r([], _Dest) ->
  173. ok;
  174. cp_r(Sources, Dest) ->
  175. case os:type() of
  176. {unix, _} ->
  177. EscSources = [escape_path(Src) || Src <- Sources],
  178. SourceStr = string:join(EscSources, " "),
  179. os:cmd(?FMT("cp -R ~s \"~s\"", [SourceStr, Dest])),
  180. ok;
  181. {win32, _} ->
  182. lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
  183. ok
  184. end.
  185. xcopy_win32(Source,Dest)->
  186. R = os:cmd(?FMT("xcopy \"~s\" \"~s\" /q /y /e 2> nul",
  187. [filename:nativename(Source), filename:nativename(Dest)])),
  188. case length(R) > 0 of
  189. %% when xcopy fails, stdout is empty and and error message is printed
  190. %% to stderr (which is redirected to nul)
  191. true -> ok;
  192. false ->
  193. {error, lists:flatten(
  194. io_lib:format("Failed to xcopy from ~s to ~s~n",
  195. [Source, Dest]))}
  196. end.
  197. cp_r_win32({true, SourceDir}, {true, DestDir}) ->
  198. %% from directory to directory
  199. SourceBase = filename:basename(SourceDir),
  200. ok = case file:make_dir(filename:join(DestDir, SourceBase)) of
  201. {error, eexist} -> ok;
  202. Other -> Other
  203. end,
  204. ok = xcopy_win32(SourceDir, filename:join(DestDir, SourceBase));
  205. cp_r_win32({false, Source} = S,{true, DestDir}) ->
  206. %% from file to directory
  207. cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
  208. cp_r_win32({false, Source},{false, Dest}) ->
  209. %% from file to file
  210. {ok,_} = file:copy(Source, Dest),
  211. ok;
  212. cp_r_win32({true, SourceDir}, {false, DestDir}) ->
  213. case filelib:is_regular(DestDir) of
  214. true ->
  215. %% From directory to file? This shouldn't happen
  216. {error, lists:flatten(
  217. io_lib:format("Cannot copy dir (~p) to file (~p)\n",
  218. [SourceDir, DestDir]))};
  219. false ->
  220. %% Specifying a target directory that doesn't currently exist.
  221. %% So let's attempt to create this directory
  222. case filelib:ensure_dir(filename:join(DestDir, "dummy")) of
  223. ok ->
  224. ok = xcopy_win32(SourceDir, DestDir);
  225. {error, Reason} ->
  226. {error, lists:flatten(
  227. io_lib:format("Unable to create dir ~p: ~p\n",
  228. [DestDir, Reason]))}
  229. end
  230. end;
  231. cp_r_win32(Source,Dest) ->
  232. Dst = {filelib:is_dir(Dest), Dest},
  233. lists:foreach(fun(Src) ->
  234. ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
  235. end, filelib:wildcard(Source)),
  236. ok.
  237. escape_path(Str) ->
  238. re:replace(Str, "([ ()?])", "\\\\&", [global, {return, list}]).
  239. %%/rebar_file_utils
  240. setup_env() ->
  241. %% We don't need or want relx providers loaded yet
  242. application:load(rebar),
  243. {ok, Providers} = application:get_env(rebar, providers),
  244. Providers1 = Providers -- [rebar_prv_release,
  245. rebar_prv_relup,
  246. rebar_prv_tar],
  247. application:set_env(rebar, providers, Providers1).
  248. reset_env() ->
  249. %% Reset the env so we get all providers
  250. application:unset_env(rebar, providers),
  251. application:unload(rebar),
  252. application:load(rebar).
  253. write_windows_scripts() ->
  254. CmdScript=
  255. "@echo off\r\n"
  256. "setlocal\r\n"
  257. "set rebarscript=%~f0\r\n"
  258. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  259. ok = file:write_file("rebar3.cmd", CmdScript).
  260. get_deps() ->
  261. case file:consult("rebar.lock") of
  262. {ok, [Deps]} ->
  263. [{binary_to_atom(Name, utf8), "", Source} || {Name, Source, _Level} <- Deps];
  264. _ ->
  265. {ok, Config} = file:consult("rebar.config"),
  266. proplists:get_value(deps, Config)
  267. end.
  268. format_errors(Source, Errors) ->
  269. format_errors(Source, "", Errors).
  270. format_warnings(Source, Warnings) ->
  271. format_warnings(Source, Warnings, []).
  272. format_warnings(Source, Warnings, Opts) ->
  273. Prefix = case lists:member(warnings_as_errors, Opts) of
  274. true -> "";
  275. false -> "Warning: "
  276. end,
  277. format_errors(Source, Prefix, Warnings).
  278. format_errors(_MainSource, Extra, Errors) ->
  279. [begin
  280. [format_error(Source, Extra, Desc) || Desc <- Descs]
  281. end
  282. || {Source, Descs} <- Errors].
  283. format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
  284. ErrorDesc = Mod:format_error(Desc),
  285. io_lib:format("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
  286. format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
  287. ErrorDesc = Mod:format_error(Desc),
  288. io_lib:format("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
  289. format_error(AbsSource, Extra, {Mod, Desc}) ->
  290. ErrorDesc = Mod:format_error(Desc),
  291. io_lib:format("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).
  292. additional_defines() ->
  293. [{d, D} || {Re, D} <- [{"^[0-9]+", namespaced_types}, {"^R1[4|5]", deprecated_crypto}], is_otp_release(Re)].
  294. is_otp_release(ArchRegex) ->
  295. case re:run(otp_release(), ArchRegex, [{capture, none}]) of
  296. match ->
  297. true;
  298. nomatch ->
  299. false
  300. end.
  301. otp_release() ->
  302. otp_release1(erlang:system_info(otp_release)).
  303. %% If OTP <= R16, otp_release is already what we want.
  304. otp_release1([$R,N|_]=Rel) when is_integer(N) ->
  305. Rel;
  306. %% If OTP >= 17.x, erlang:system_info(otp_release) returns just the
  307. %% major version number, we have to read the full version from
  308. %% a file. See http://www.erlang.org/doc/system_principles/versions.html
  309. %% Read vsn string from the 'OTP_VERSION' file and return as list without
  310. %% the "\n".
  311. otp_release1(Rel) ->
  312. File = filename:join([code:root_dir(), "releases", Rel, "OTP_VERSION"]),
  313. case file:read_file(File) of
  314. {error, _} ->
  315. Rel;
  316. {ok, Vsn} ->
  317. %% It's fine to rely on the binary module here because we can
  318. %% be sure that it's available when the otp_release string does
  319. %% not begin with $R.
  320. Size = byte_size(Vsn),
  321. %% The shortest vsn string consists of at least two digits
  322. %% followed by "\n". Therefore, it's safe to assume Size >= 3.
  323. case binary:part(Vsn, {Size, -3}) of
  324. <<"**\n">> ->
  325. %% The OTP documentation mentions that a system patched
  326. %% using the otp_patch_apply tool available to licensed
  327. %% customers will leave a '**' suffix in the version as a
  328. %% flag saying the system consists of application versions
  329. %% from multiple OTP versions. We ignore this flag and
  330. %% drop the suffix, given for all intents and purposes, we
  331. %% cannot obtain relevant information from it as far as
  332. %% tooling is concerned.
  333. binary:bin_to_list(Vsn, {0, Size - 3});
  334. _ ->
  335. binary:bin_to_list(Vsn, {0, Size - 1})
  336. end
  337. end.