-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrebar.config.script
More file actions
268 lines (252 loc) · 9.85 KB
/
Copy pathrebar.config.script
File metadata and controls
268 lines (252 loc) · 9.85 KB
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
FDBReleasesBaseUrl = "https://api.github.com/repos/apple/foundationdb/releases".
IncludeDir = case os:getenv("ERLFDB_INCLUDE_DIR") of
false -> "/usr/local/include";
Dir -> Dir
end.
% Detect rebar3 or mix
RebarApi = {error, nofile} =/= code:ensure_loaded(rebar_api).
% Logs to stdout/stderr from either rebar3 or mix
Log = fun(Level, Fmt, Args) ->
if
RebarApi ->
rebar_api:Level("[erlfdb] " ++ Fmt, Args);
true ->
Message = iolist_to_binary(io_lib:format(Fmt, Args)),
Level2 =
if
Level =:= abort -> error;
true -> Level
end,
('Elixir.Mix':shell()):Level2(io_lib:format("===> [erlfdb] ~s", [Message])),
if
Level =:= abort ->
'Elixir.Mix':raise(Message);
true ->
ok
end
end
end.
% Look for fdbcli on PATH, but also check /usr/local/bin explicitly since that is the common location.
% Override with ERLFDB_FDBCLI env var to point at a specific version's fdbcli.
% Check architecture against regex
IsArch = fun(ArchRe) ->
match =:= re:run(erlang:system_info(system_architecture), ArchRe, [{capture, none}])
end.
CompileAPIVersion =
case os:getenv("ERLFDB_COMPILE_API_VERSION") of
false ->
% Look for fdbcli on PATH, but also check /usr/local/bin explicitly since that is the common location.
% Override with ERLFDB_FDBCLI env var to point at a specific version's fdbcli.
FdbCli =
case os:getenv("ERLFDB_FDBCLI") of
false ->
case os:find_executable("fdbcli") of
false ->
case os:find_executable("fdbcli", "/usr/local/bin") of
false -> "fdbcli";
Exec -> Exec
end;
Exec ->
Exec
end;
Path ->
Path
end,
% Hacky means to extract API version from fdbcli protocol version output
% See https://github.com/apple/foundationdb/blob/master/flow/ProtocolVersion.h
VsnInfo = os:cmd(FdbCli ++ " --version"),
case re:run(VsnInfo, "protocol ([a-f0-9]*)", [{capture, [1], list}]) of
{match, [ProtocolStr]} ->
ProtocolVsn = list_to_integer(ProtocolStr, 16),
APIVersionBytes = (ProtocolVsn band 16#0000000FFF00000) bsr 20,
list_to_integer(integer_to_list(APIVersionBytes, 16));
nomatch ->
undefined
end;
ApiVsnStr ->
Log(info, "Using ERLFDB_COMPILE_API_VERSION=~s", [ApiVsnStr]),
list_to_integer(ApiVsnStr)
end.
% Make an HTTP GET request, expect JSON body
GetJson = fun(Url) ->
case code:ensure_loaded(json) of
{module, _} ->
Log(info, " GET ~s", [Url]),
case httpc:request(get, {Url, [{"User-Agent", "rebar3 erlfdb"}]}, [], []) of
{ok, {{"HTTP/1.1", 200, "OK"}, _, Body}} ->
{ok, json:decode(iolist_to_binary(Body))};
_ ->
{error, "HTTP GET Failed"}
end;
{error, _} ->
{error, "Minimum OTP 27 required for release auto-detection"}
end
end.
% Gets the latest release version from the official FoundationDB GitHub Releases
GetLatestRelease = fun() ->
case GetJson(FDBReleasesBaseUrl ++ "/latest") of
{ok, #{<<"tag_name">> := LatestTag}} ->
case
re:run(LatestTag, "(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)", [
{capture, [major, minor, patch], list}
])
of
{match, Matches} ->
{ok, [list_to_integer(X) || X <- Matches]};
_ ->
{error, "Failed to parse FoundationDB version"}
end;
Error ->
Error
end
end.
% From the list of assets in the FDB Release, get those that match the regex
GetAssetByNameRe = fun(NameRe, Assets) ->
case
lists:filter(
fun(#{<<"name">> := Name}) ->
match =:= re:run(Name, NameRe, [{capture, none}])
end,
Assets
)
of
[Asset | _] -> Asset;
_ -> undefined
end
end.
% Filter the list of assets in the FDB Release by the known supported architectures
GetSupportedAssets = fun(Assets) ->
[
{"(aarch64-apple-darwin)", #{
server => GetAssetByNameRe("^FoundationDB.*arm64\\.pkg$", Assets),
clients => undefined
}},
{"(x86_64-.*-linux)", #{
server => GetAssetByNameRe("^foundationdb-server.*amd64\\.deb$", Assets),
clients => GetAssetByNameRe("^foundationdb-clients.*amd64\\.deb$", Assets)
}}
]
end.
% Gets the asset map for the latest non-AVX FDB Release on the current architecture
GetLatestNonAVXAssets = fun() ->
% AVX releases have odd patch numbers, so we make sure we have the even one
% If you want the AVX release, please install it manually.
RVsn =
case GetLatestRelease() of
{ok, [Major, Minor, Patch]} when Patch rem 2 == 1 ->
{ok, [Major, Minor, Patch - 1]};
Res ->
Res
end,
case RVsn of
{ok, Vsn = [_, _, _]} ->
Url = lists:flatten([FDBReleasesBaseUrl ++ "/tags/", io_lib:format("~p.~p.~p", Vsn)]),
case GetJson(Url) of
{ok, #{<<"assets">> := Assets}} ->
SupportedAssets = GetSupportedAssets(Assets),
case
lists:filter(fun({ArchRe, Assets}) -> IsArch(ArchRe) end, SupportedAssets)
of
[{_, AssetsMap} | _] ->
{ok, AssetsMap};
[] ->
{error, "Architecture may not have an official FoundationDB release."}
end;
Error ->
Error
end;
Error ->
Error
end
end.
% Logs the list of assets to stdout
LogAssetsMap = fun
LL(#{server := ServerAsset, clients := undefined}) ->
Log(
info,
"For both the foundationdb-server and foundationdb-clients packages, download and install~n~n ~ts~n",
[maps:get(<<"browser_download_url">>, ServerAsset)]
);
LL(#{server := ServerAsset, clients := ClientsAsset}) ->
Log(
info,
"For the foundationdb-server package, download and install~n~n ~ts~n",
[
maps:get(<<"browser_download_url">>, ServerAsset)
]
),
Log(
info,
"For the foundationdb-clients package, download and install~n~n ~ts~n",
[
maps:get(<<"browser_download_url">>, ClientsAsset)
]
)
end.
% If CompileAPIVersion undetected, print helpful information about where to download the latest FDB Release
case CompileAPIVersion of
undefined ->
Log(info, "Checking for latest FoundationDB release...", []),
{ok, _} = application:ensure_all_started(inets),
Message =
case application:ensure_all_started(ssl) of
{ok, _} ->
case GetLatestNonAVXAssets() of
{ok, AssetsMap} ->
LogAssetsMap(AssetsMap),
undefined;
{error, _Reason} ->
"The foundationdb-clients package is required to compile erlfdb. Please visit~n~n https://github.com/apple/foundationdb/releases~n"
end;
{error, _Reason} ->
"The foundationdb-clients package is required to compile erlfdb. Please visit~n~n https://github.com/apple/foundationdb/releases~n"
end,
[Log(info, Message, []) || Message =/= undefined],
AssertApiVersion = "0" =/= os:getenv("ERLFDB_ASSERT_API_VERSION"),
if
AssertApiVersion ->
Log(abort, "Error: FDB API version could not be determined. Set ERLFDB_COMPILE_API_VERSION or install fdbcli.", []);
true ->
Log(error, "FDB API version could not be determined. Continuing anyway...", [])
end;
_ ->
ok
end.
ErlOpts = proplists:get_value(erl_opts, CONFIG, []).
CONFIG1 = proplists:delete(erl_opts, CONFIG).
DynamicLibrary = case IsArch("darwin") of
true ->
"/usr/local/lib/libfdb_c.dylib";
false ->
"/usr/lib/libfdb_c.so"
end.
DynamicLibraryDir = filename:dirname(DynamicLibrary).
[
{erl_opts, ErlOpts ++ [
{d, erlfdb_compile_time_api_version, CompileAPIVersion},
{d, erlfdb_compile_time_external_client_library, DynamicLibrary}
]},
{port_env, [
{
"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|darwin|gnu)",
"CFLAGS",
"$CFLAGS -I"++IncludeDir++" -Ic_src/ -g -Wall -Werror " ++
(if
is_integer(CompileAPIVersion) ->
"-DFDB_API_VERSION=" ++ integer_to_list(CompileAPIVersion);
true ->
"-DFDB_USE_LATEST_API_VERSION=1"
end)
},
{
"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|gnu)",
"LDFLAGS",
"$LDFLAGS -Wl,-rpath,\\$ORIGIN -Wl,-rpath,"++DynamicLibraryDir++" -L"++DynamicLibraryDir++" -lfdb_c"
},
{
"(darwin)",
"LDFLAGS",
"$LDFLAGS -rpath @loader_path -rpath "++DynamicLibraryDir++" -L"++DynamicLibraryDir++" -lfdb_c"
}
]}
] ++ CONFIG1.