| 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}} = |
| 129 |
try list_to_integer(os:getenv("SOURCE_DATE_EPOCH")) of |
| 130 |
Epoch -> |
| 131 |
calendar:system_time_to_universal_time(Epoch, second) |
| 132 |
catch _:_ -> |
| 133 |
calendar:now_to_universal_time(rebar_now()) |
| 134 |
end, |
| 135 |
lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w", |
| 136 |
[Y, M, D, H, Min, S])). |
| 137 |
rebar_now() -> |
| 138 |
case erlang:function_exported(erlang, timestamp, 0) of |
| 139 |
true -> |
| 140 |
erlang:timestamp(); |
| 141 |
false -> |
| 142 |
%% erlang:now/0 was deprecated in 18.0, and as the escript has to |
| 143 |
%% pass erl_lint:module/1 (even without -mode(compile)), we would |
| 144 |
%% see a deprecation warning for erlang:now/0. One solution is to |
| 145 |
%% use -compile({nowarn_deprecated_function, [{erlang, now, 0}]}), |
| 146 |
%% but that would raise a warning in versions older than 18.0. |
| 147 |
%% Calling erlang:now/0 via apply/3 avoids that. |
| 148 |
apply(erlang, now, []) |
| 149 |
end. |
| 150 |
|
| 151 |
vcs_info([]) -> |
| 152 |
"No VCS info available."; |
| 153 |
vcs_info([{Id, Dir, VsnCmd, StatusCmd} | Rest]) -> |
| 154 |
case filelib:is_dir(Dir) of |
| 155 |
true -> |
| 156 |
Vsn = string:strip(os:cmd(VsnCmd), both, $\n), |
| 157 |
Status = case string:strip(os:cmd(StatusCmd), both, $\n) of |
| 158 |
[] -> |
| 159 |
""; |
| 160 |
_ -> |
| 161 |
"-dirty" |
| 162 |
end, |
| 163 |
lists:concat([Id, " ", Vsn, Status]); |
| 164 |
false -> |
| 165 |
vcs_info(Rest) |
| 166 |
end. |
| 167 |
|
| 168 |
write_windows_scripts() -> |
| 169 |
CmdScript= |
| 170 |
"@echo off\r\n" |
| 171 |
"setlocal\r\n" |
| 172 |
"set rebarscript=%~f0\r\n" |
| 173 |
"escript.exe \"%rebarscript:.cmd=%\" %*\r\n", |
| 174 |
ok = file:write_file("rebar.cmd", CmdScript). |