summaryrefslogtreecommitdiff
path: root/bootstrap
blob: 62e7fa502ec52f19c73491ef5eff1f4e25ff7a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env escript
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ft=erlang ts=4 sw=4 et


main(_Args) ->
    application:start(crypto),
    application:start(asn1),
    application:start(public_key),
    application:start(ssl),
    inets:start(),

    %% Fetch and build deps required to build rebar3
    BaseDeps = [{providers, []}
               ,{getopt, []}
               ,{erlware_commons, ["ec_dictionary.erl", "ec_vsn.erl"]}],
    Deps = get_deps(),
    [fetch_and_compile(Dep, Deps) || Dep <- BaseDeps],

    %% Build rebar3 modules with compile:file
    bootstrap_rebar3(),

    %% Build rebar.app from rebar.app.src
    {ok, App} = rebar_app_info:new(rebar, "3.0.0", filename:absname("_build/default/lib/rebar/")),
    rebar_otp_app:compile(rebar_state:new(), App),

    %% Because we are compiling files that are loaded already we want to silence
    %% not_purged errors in rebar_erlc_compiler:opts_changed/1
    error_logger:tty(false),

    setup_env(),
    os:putenv("REBAR_PROFILE", "bootstrap"),
    rebar3:run(["update"]),
    {ok, State} = rebar3:run(["compile"]),
    reset_env(),
    os:putenv("REBAR_PROFILE", ""),

    DepsPaths = rebar_state:code_paths(State, all_deps),
    code:add_pathsa(DepsPaths),

    rebar3:run(["clean", "-a"]),
    rebar3:run(["escriptize"]),

    %% Done with compile, can turn back on error logger
    error_logger:tty(true),

    %% Finally, update executable perms for our script on *nix,
    %%  or write out script files on win32.
    ec_file:copy("_build/default/bin/rebar3", "./rebar3"),
    case os:type() of
        {unix,_} ->
            [] = os:cmd("chmod u+x rebar3"),
            ok;
        {win32,_} ->
            write_windows_scripts(),
            ok;
        _ ->
            ok
    end.

fetch_and_compile({Name, ErlFirstFiles}, Deps) ->
    {Name, _, Repo} = lists:keyfind(Name, 1, Deps),
    ok = fetch(Repo, Name),
    compile(Name, ErlFirstFiles).

fetch({pkg, Name, Vsn}, App) ->
    Dir = filename:join([filename:absname("_build/default/lib/"), App]),
    CDN = "https://s3.amazonaws.com/s3.hex.pm/tarballs",
    Package = binary_to_list(<<Name/binary, "-", Vsn/binary, ".tar">>),
    Url = string:join([CDN, Package], "/"),
    case request(Url) of
        {ok, Binary} ->
            {ok, Contents} = extract(Binary),
            ok = erl_tar:extract({binary, Contents}, [{cwd, Dir}, compressed]);
        _ ->
            io:format("Error: Unable to fetch package ~p ~p~n", [Name, Vsn])
    end.

extract(Binary) ->
    {ok, Files} = erl_tar:extract({binary, Binary}, [memory]),
    {"contents.tar.gz", Contents} = lists:keyfind("contents.tar.gz", 1, Files),
    {ok, Contents}.

request(Url) ->
    case httpc:request(get, {Url, []},
                       [{relaxed, true}],
                       [{body_format, binary}]) of
        {ok, {{_Version, 200, _Reason}, _Headers, Body}} ->
            {ok, Body};
        Error ->
            Error
    end.

compile(App, FirstFiles) ->
    Dir = filename:join(filename:absname("_build/default/lib/"), App),
    filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])),
    code:add_path(filename:join(Dir, "ebin")),
    FirstFilesPaths = [filename:join([Dir, "src", Module]) || Module <- FirstFiles],
    Sources = FirstFilesPaths ++ filelib:wildcard(filename:join([Dir, "src", "*.erl"])),
    [compile_file(X, [{i, filename:join(Dir, "include")}
                     ,debug_info
                     ,{outdir,  filename:join(Dir, "ebin")}
                     ,return | additional_defines()]) || X <- Sources].

compile_file(File, Opts) ->
    case compile:file(File, Opts) of
        {ok, _Mod} ->
            ok;
        {ok, _Mod, []} ->
            ok;
        {ok, _Mod, Ws} ->
            io:format("~s~n", [format_warnings(File, Ws)]),
            halt(1);
        {error, Es, Ws} ->
            io:format("~s ~s~n", [format_errors(File, Es), format_warnings(File, Ws)]),
            halt(1)
    end.

bootstrap_rebar3() ->
    filelib:ensure_dir("_build/default/lib/rebar/ebin/dummy.beam"),
    code:add_path("_build/default/lib/rebar/ebin/"),
    ok = symlink_or_copy(filename:absname("src"),
                         filename:absname("_build/default/lib/rebar/src")),
    Sources = ["src/rebar_resource.erl" | filelib:wildcard("src/*.erl")],
    [compile_file(X, [{outdir, "_build/default/lib/rebar/ebin/"}
                     ,return | additional_defines()]) || X <- Sources],
    code:add_patha(filename:absname("_build/default/lib/rebar/ebin")).

%%rebar.hrl
-define(FMT(Str, Args), lists:flatten(io_lib:format(Str, Args))).
%%/rebar.hrl
%%rebar_file_utils
symlink_or_copy(Source, Target) ->
    Link = case os:type() of
               {win32, _} ->
                   Source;
               _ ->
                   make_relative_path(Source, Target)
           end,
    case file:make_symlink(Link, Target) of
        ok ->
            ok;
        {error, eexist} ->
            ok;
        {error, _} ->
            cp_r([Source], Target)
    end.

make_relative_path(Source, Target) ->
    do_make_relative_path(filename:split(Source), filename:split(Target)).

do_make_relative_path([H|T1], [H|T2]) ->
    do_make_relative_path(T1, T2);
do_make_relative_path(Source, Target) ->
    Base = lists:duplicate(max(length(Target) - 1, 0), ".."),
    filename:join(Base ++ Source).

cp_r([], _Dest) ->
    ok;
cp_r(Sources, Dest) ->
    case os:type() of
        {unix, _} ->
            EscSources = [escape_path(Src) || Src <- Sources],
            SourceStr = string:join(EscSources, " "),
            os:cmd(?FMT("cp -R ~s \"~s\"", [SourceStr, Dest])),
            ok;
        {win32, _} ->
            lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
            ok
    end.

xcopy_win32(Source,Dest)->
    R = os:cmd(?FMT("xcopy \"~s\" \"~s\" /q /y /e 2> nul",
                     [filename:nativename(Source), filename:nativename(Dest)])),
    case length(R) > 0 of
        %% when xcopy fails, stdout is empty and and error message is printed
        %% to stderr (which is redirected to nul)
        true -> ok;
        false ->
            {error, lists:flatten(
                      io_lib:format("Failed to xcopy from ~s to ~s~n",
                                    [Source, Dest]))}
    end.

cp_r_win32({true, SourceDir}, {true, DestDir}) ->
    %% from directory to directory
    SourceBase = filename:basename(SourceDir),
    ok = case file:make_dir(filename:join(DestDir, SourceBase)) of
             {error, eexist} -> ok;
             Other -> Other
         end,
    ok = xcopy_win32(SourceDir, filename:join(DestDir, SourceBase));
cp_r_win32({false, Source} = S,{true, DestDir}) ->
    %% from file to directory
    cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
cp_r_win32({false, Source},{false, Dest}) ->
    %% from file to file
    {ok,_} = file:copy(Source, Dest),
    ok;
cp_r_win32({true, SourceDir}, {false, DestDir}) ->
    case filelib:is_regular(DestDir) of
        true ->
            %% From directory to file? This shouldn't happen
            {error, lists:flatten(
                      io_lib:format("Cannot copy dir (~p) to file (~p)\n",
                                    [SourceDir, DestDir]))};
        false ->
            %% Specifying a target directory that doesn't currently exist.
            %% So let's attempt to create this directory
            case filelib:ensure_dir(filename:join(DestDir, "dummy")) of
                ok ->
                    ok = xcopy_win32(SourceDir, DestDir);
                {error, Reason} ->
                    {error, lists:flatten(
                              io_lib:format("Unable to create dir ~p: ~p\n",
                                            [DestDir, Reason]))}
            end
    end;
cp_r_win32(Source,Dest) ->
    Dst = {filelib:is_dir(Dest), Dest},
    lists:foreach(fun(Src) ->
                          ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
                  end, filelib:wildcard(Source)),
    ok.

escape_path(Str) ->
    re:replace(Str, "([ ()?])", "\\\\&", [global, {return, list}]).
%%/rebar_file_utils

setup_env() ->
    %% We don't need or want relx providers loaded yet
    application:load(rebar),
    {ok, Providers} = application:get_env(rebar, providers),
    Providers1 = Providers -- [rebar_prv_release,
                               rebar_prv_tar],
    application:set_env(rebar, providers, Providers1).

reset_env() ->
    %% Reset the env so we get all providers
    application:unset_env(rebar, providers),
    application:unload(rebar),
    application:load(rebar).

write_windows_scripts() ->
    CmdScript=
        "@echo off\r\n"
        "setlocal\r\n"
        "set rebarscript=%~f0\r\n"
        "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
    ok = file:write_file("rebar3.cmd", CmdScript).

get_deps() ->
    case file:consult("rebar.lock") of
        {ok, [Deps]} ->
            [{binary_to_atom(Name, utf8), "", Source} || {Name, Source, _Level} <- Deps];
        _ ->
            {ok, Config} = file:consult("rebar.config"),
            proplists:get_value(deps, Config)
    end.

format_errors(Source, Errors) ->
    format_errors(Source, "", Errors).

format_warnings(Source, Warnings) ->
    format_warnings(Source, Warnings, []).

format_warnings(Source, Warnings, Opts) ->
    Prefix = case lists:member(warnings_as_errors, Opts) of
                 true -> "";
                 false -> "Warning: "
             end,
    format_errors(Source, Prefix, Warnings).

format_errors(_MainSource, Extra, Errors) ->
    [begin
         [format_error(Source, Extra, Desc) || Desc <- Descs]
     end
     || {Source, Descs} <- Errors].

format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
    ErrorDesc = Mod:format_error(Desc),
    io_lib:format("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
    ErrorDesc = Mod:format_error(Desc),
    io_lib:format("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
format_error(AbsSource, Extra, {Mod, Desc}) ->
    ErrorDesc = Mod:format_error(Desc),
    io_lib:format("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).

additional_defines() ->
    [{d, D} || {Re, D} <- [{"^[0-9]+", namespaced_types}, {"^R1[4|5]", deprecated_crypto}], is_otp_release(Re)].

is_otp_release(ArchRegex) ->
    case re:run(otp_release(), ArchRegex, [{capture, none}]) of
        match ->
            true;
        nomatch ->
            false
    end.

otp_release() ->
    otp_release1(erlang:system_info(otp_release)).

%% If OTP <= R16, otp_release is already what we want.
otp_release1([$R,N|_]=Rel) when is_integer(N) ->
    Rel;
%% If OTP >= 17.x, erlang:system_info(otp_release) returns just the
%% major version number, we have to read the full version from
%% a file. See http://www.erlang.org/doc/system_principles/versions.html
%% Read vsn string from the 'OTP_VERSION' file and return as list without
%% the "\n".
otp_release1(Rel) ->
    File = filename:join([code:root_dir(), "releases", Rel, "OTP_VERSION"]),
    case file:read_file(File) of
        {error, _} ->
            Rel;
        {ok, Vsn} ->
            %% It's fine to rely on the binary module here because we can
            %% be sure that it's available when the otp_release string does
            %% not begin with $R.
            Size = byte_size(Vsn),
            %% The shortest vsn string consists of at least two digits
            %% followed by "\n". Therefore, it's safe to assume Size >= 3.
            case binary:part(Vsn, {Size, -3}) of
                <<"**\n">> ->
                    %% The OTP documentation mentions that a system patched
                    %% using the otp_patch_apply tool available to licensed
                    %% customers will leave a '**' suffix in the version as a
                    %% flag saying the system consists of application versions
                    %% from multiple OTP versions. We ignore this flag and
                    %% drop the suffix, given for all intents and purposes, we
                    %% cannot obtain relevant information from it as far as
                    %% tooling is concerned.
                    binary:bin_to_list(Vsn, {0, Size - 3});
                _ ->
                    binary:bin_to_list(Vsn, {0, Size - 1})
            end
    end.