Debian Codegraph @

bootstrap

raw ◀ ▶
Source reconstructed at patch state 0 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 DebugFlag,
58 NamespacedTypes,
59 {d, 'BUILD_TIME', Built},
60 {d, 'VCS_INFO', VcsInfo},
61 {d, 'OTP_INFO', OtpInfo}]) of
62 up_to_date ->
63 ok;
64 error ->
65 io:format("Failed to compile rebar files!\n"),
66 halt(1)
67 end,
68
69 %% Make sure file:consult can parse the .app file
70 case file:consult("ebin/rebar.app") of
71 {ok, _} ->
72 ok;
73 {error, Reason} ->
74 io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
75 halt(1)
76 end,
77
78 %% Add ebin/ to our path
79 true = code:add_path("ebin"),
80
81 %% Run rebar compile to do proper .app validation etc.
82 %% and rebar escriptize to create the rebar script
83 RebarArgs = Args -- ["debug"], %% Avoid trying to run 'debug' command
84 rebar:main(["compile", "escriptize"] ++ RebarArgs),
85
86 %% Finally, update executable perms for our script on *nix,
87 %% or write out script files on win32.
88 case os:type() of
89 {unix,_} ->
90 [] = os:cmd("chmod u+x rebar"),
91 ok;
92 {win32,_} ->
93 write_windows_scripts(),
94 ok;
95 _ ->
96 ok
97 end,
98
99 %% Add a helpful message
100 io:format("Congratulations! You now have a self-contained script called"
101 " \"rebar\" in\n"
102 "your current working directory. "
103 "Place this script anywhere in your path\n"
104 "and you can use rebar to build OTP-compliant apps.\n").
105
106 usage() ->
107 io:format("Usage: bootstrap [OPTION]...~n"),
108 io:format(" force=1 unconditional build~n"),
109 io:format(" debug add debug information~n").
110
111 is_otp(OtpInfo, Regex) ->
112 case re:run(OtpInfo, Regex, [{capture, none}]) of
113 match -> true;
114 nomatch -> false
115 end.
116
117 rm(Path) ->
118 NativePath = filename:nativename(Path),
119 Cmd = case os:type() of
120 {unix,_} -> "rm -f ";
121 {win32,_} -> "del /q "
122 end,
123 [] = os:cmd(Cmd ++ NativePath),
124 ok.
125
126 build_time() ->
127 {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(rebar_now()),
128 lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
129 [Y, M, D, H, Min, S])).
130 rebar_now() ->
131 case erlang:function_exported(erlang, timestamp, 0) of
132 true ->
133 erlang:timestamp();
134 false ->
135 %% erlang:now/0 was deprecated in 18.0, and as the escript has to
136 %% pass erl_lint:module/1 (even without -mode(compile)), we would
137 %% see a deprecation warning for erlang:now/0. One solution is to
138 %% use -compile({nowarn_deprecated_function, [{erlang, now, 0}]}),
139 %% but that would raise a warning in versions older than 18.0.
140 %% Calling erlang:now/0 via apply/3 avoids that.
141 apply(erlang, now, [])
142 end.
143
144 vcs_info([]) ->
145 "No VCS info available.";
146 vcs_info([{Id, Dir, VsnCmd, StatusCmd} | Rest]) ->
147 case filelib:is_dir(Dir) of
148 true ->
149 Vsn = string:strip(os:cmd(VsnCmd), both, $\n),
150 Status = case string:strip(os:cmd(StatusCmd), both, $\n) of
151 [] ->
152 "";
153 _ ->
154 "-dirty"
155 end,
156 lists:concat([Id, " ", Vsn, Status]);
157 false ->
158 vcs_info(Rest)
159 end.
160
161 write_windows_scripts() ->
162 CmdScript=
163 "@echo off\r\n"
164 "setlocal\r\n"
165 "set rebarscript=%~f0\r\n"
166 "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
167 ok = file:write_file("rebar.cmd", CmdScript).