Debian Codegraph @

bootstrap

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
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
5 main(Args) ->
6 case lists:member("--help", Args) of
7 true ->
8 usage(),
9 halt(0);
10 false ->
11 ok
12 end,
13
14 %% Get a string repr of build time
15 Built = build_time(),
16
17 %% Get a string repr of first matching VCS changeset
18 VcsInfo = vcs_info([{hg, ".hg", "hg identify -i", "hg status"},
19 {git, ".git", "git describe --always --tags",
20 "git status -s"}]),
21
22 %% Check for force=1 flag to force a rebuild
23 case lists:member("force=1", Args) of
24 true ->
25 rm("ebin/*.beam");
26 false ->
27 case filelib:is_file("ebin/rebar.beam") of
28 true -> rm("ebin/rebar.beam");
29 false -> io:fwrite("No beam files found.~n")
30 end
31 end,
32
33 %% Add check for debug flag
34 DebugFlag = case lists:member("debug", Args) of
35 true -> debug_info;
36 false -> undefined
37 end,
38
39 %% Extract the system info of the version of OTP we use to compile rebar
40 OtpInfo = string:strip(erlang:system_info(otp_release), both, $\n),
41
42 %% Types dict:dict() and digraph:digraph() have been introduced in
43 %% Erlang 17.
44 %% At the same time, their counterparts dict() and digraph() are to be
45 %% deprecated in Erlang 18. namespaced_types option is used to select
46 %% proper type name depending on the OTP version used.
47 NamespacedTypes = case is_otp(OtpInfo, "^[0-9]+") of
48 true -> {d, namespaced_types};
49 false -> undefined
50 end,
51
52 %% Compile all src/*.erl to ebin
53 %% To not accidentally try to compile files like Mac OS X resource forks,
54 %% we only look for rebar source files that start with a letter.
55 case make:files(filelib:wildcard("src/[a-zA-Z]*.erl"),
56 [{outdir, "ebin"}, {i, "include"},
57 nowarn_deprecated_catch,
58 DebugFlag,
59 NamespacedTypes,
60 {d, 'BUILD_TIME', Built},
61 {d, 'VCS_INFO', VcsInfo},
62 {d, 'OTP_INFO', OtpInfo}]) of
63 up_to_date ->
64 ok;
65 error ->
66 io:format("Failed to compile rebar files!\n"),
67 halt(1)
68 end,
69
70 %% Make sure file:consult can parse the .app file
71 case file:consult("ebin/rebar.app") of
72 {ok, _} ->
73 ok;
74 {error, Reason} ->
75 io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
76 halt(1)
77 end,
78
79 %% Add ebin/ to our path
80 true = code:add_path("ebin"),
81
82 %% Run rebar compile to do proper .app validation etc.
83 %% and rebar escriptize to create the rebar script
84 RebarArgs = Args -- ["debug"], %% Avoid trying to run 'debug' command
85 rebar:main(["compile", "escriptize"] ++ RebarArgs),
86
87 %% Finally, update executable perms for our script on *nix,
88 %% or write out script files on win32.
89 case os:type() of
90 {unix,_} ->
91 [] = os:cmd("chmod u+x rebar"),
92 ok;
93 {win32,_} ->
94 write_windows_scripts(),
95 ok;
96 _ ->
97 ok
98 end,
99
100 %% Add a helpful message
101 io:format("Congratulations! You now have a self-contained script called"
102 " \"rebar\" in\n"
103 "your current working directory. "
104 "Place this script anywhere in your path\n"
105 "and you can use rebar to build OTP-compliant apps.\n").
106
107 usage() ->
108 io:format("Usage: bootstrap [OPTION]...~n"),
109 io:format(" force=1 unconditional build~n"),
110 io:format(" debug add debug information~n").
111
112 is_otp(OtpInfo, Regex) ->
113 case re:run(OtpInfo, Regex, [{capture, none}]) of
114 match -> true;
115 nomatch -> false
116 end.
117
118 rm(Path) ->
119 NativePath = filename:nativename(Path),
120 Cmd = case os:type() of
121 {unix,_} -> "rm -f ";
122 {win32,_} -> "del /q "
123 end,
124 [] = os:cmd(Cmd ++ NativePath),
125 ok.
126
127 build_time() ->
128 {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(rebar_now()),
129 lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
130 [Y, M, D, H, Min, S])).
131 rebar_now() ->
132 case erlang:function_exported(erlang, timestamp, 0) of
133 true ->
134 erlang:timestamp();
135 false ->
136 %% erlang:now/0 was deprecated in 18.0, and as the escript has to
137 %% pass erl_lint:module/1 (even without -mode(compile)), we would
138 %% see a deprecation warning for erlang:now/0. One solution is to
139 %% use -compile({nowarn_deprecated_function, [{erlang, now, 0}]}),
140 %% but that would raise a warning in versions older than 18.0.
141 %% Calling erlang:now/0 via apply/3 avoids that.
142 apply(erlang, now, [])
143 end.
144
145 vcs_info([]) ->
146 "No VCS info available.";
147 vcs_info([{Id, Dir, VsnCmd, StatusCmd} | Rest]) ->
148 case filelib:is_dir(Dir) of
149 true ->
150 Vsn = string:strip(os:cmd(VsnCmd), both, $\n),
151 Status = case string:strip(os:cmd(StatusCmd), both, $\n) of
152 [] ->
153 "";
154 _ ->
155 "-dirty"
156 end,
157 lists:concat([Id, " ", Vsn, Status]);
158 false ->
159 vcs_info(Rest)
160 end.
161
162 write_windows_scripts() ->
163 CmdScript=
164 "@echo off\r\n"
165 "setlocal\r\n"
166 "set rebarscript=%~f0\r\n"
167 "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
168 ok = file:write_file("rebar.cmd", CmdScript).