-
Notifications
You must be signed in to change notification settings - Fork 1
/
mix.exs
320 lines (254 loc) · 8.02 KB
/
mix.exs
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
defmodule EMLX.MixProject do
use Mix.Project
@app :emlx
@version "0.1.0"
@mlx_version "0.21.0"
def project do
libmlx_config = libmlx_config()
[
app: @app,
version: @version,
elixir: "~> 1.15",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
deps: deps(),
# elixir_make
make_env: %{
"MLX_INCLUDE_DIR" => Path.join(libmlx_config.dir, "include"),
"MLX_LIB_DIR" => Path.join(libmlx_config.dir, "lib"),
"MLX_VARIANT" => libmlx_config.variant,
"EMLX_VERSION" => @version,
"MIX_BUILD_EMBEDDED" => "#{Mix.Project.config()[:build_embedded]}"
},
# Compilers
compilers: [:mlx, :elixir_make] ++ Mix.compilers(),
aliases: aliases()
]
end
def application do
[
extra_applications: [:logger, :inets, :ssl, :public_key, :crypto]
]
end
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
defp deps do
[
{:elixir_make, "~> 0.6"},
{:nx, "~> 0.9.2"}
]
end
defp aliases do
[
"compile.mlx": &download_and_unarchive/1
]
end
defp libmlx_config() do
version = System.get_env("LIBMLX_VERSION", @mlx_version)
features = %{
jit?: to_boolean(System.get_env("LIBMLX_ENABLE_JIT")),
debug?: to_boolean(System.get_env("LIBMLX_ENABLE_DEBUG"))
}
variant = to_variant(features)
%{
version: version,
dir: Path.join(__DIR__, "cache/libmlx-#{version}#{variant}"),
variant: variant
}
end
defp to_boolean(nil), do: false
defp to_boolean(var) when is_boolean(var) do
var
end
defp to_boolean(var) do
String.downcase(to_string(var)) in ["1", "true", "on", "yes", "y"]
end
defp to_variant(features) do
[if(features.debug?, do: "debug", else: nil), if(features.jit?, do: "jit", else: nil)]
|> Enum.filter(&(&1 != nil))
|> Enum.sort()
|> Enum.map(&"-#{&1}")
|> Enum.join("")
end
defp download_and_unarchive(args) do
libmlx_config = libmlx_config()
cache_dir =
if dir = System.get_env("LIBMLX_CACHE") do
Path.expand(dir)
else
:filename.basedir(:user_cache, "libmlx")
end
if "--force" in args do
File.rm_rf(libmlx_config.dir)
File.rm_rf(cache_dir)
end
if File.dir?(libmlx_config.dir) do
{:ok, []}
else
download_and_unarchive(cache_dir, libmlx_config)
end
end
defp download_and_unarchive(cache_dir, libmlx_config) do
File.mkdir_p!(cache_dir)
libmlx_archive =
Path.join(cache_dir, "libmlx-#{libmlx_config.version}#{libmlx_config.variant}.tar.gz")
libmlx_archive = System.get_env("MLX_ARCHIVE_PATH", libmlx_archive)
url =
"https://github.com/cocoa-xu/mlx-build/releases/download/v#{libmlx_config.version}/mlx-arm64-apple-darwin#{libmlx_config.variant}.tar.gz"
sha256_url = "#{url}.sha256"
verify_integrity = "sha256=url:#{sha256_url}"
{url, verify_integrity} =
if customized_url = System.get_env("MLX_ARCHIVE_URL") do
verify_integrity = System.get_env("MLX_ARCHIVE_INTEGRITY")
{customized_url, verify_integrity}
else
{url, verify_integrity}
end
unless File.exists?(libmlx_archive) do
# Download libmlx
if {:unix, :darwin} != :os.type() do
Mix.raise("No MLX support on non Apple Silicon machines")
end
download!(url, libmlx_archive)
:ok = maybe_verify_integrity!(verify_integrity, libmlx_archive)
end
# Unpack libmlx and move to the target cache dir
parent_libmlx_dir = Path.join(Path.dirname(libmlx_config.dir), "libmlx")
File.mkdir_p!(parent_libmlx_dir)
# Extract to the parent directory (it will be inside the libmlx directory)
:ok = :erl_tar.extract(libmlx_archive, [:compressed, {:cwd, parent_libmlx_dir}])
# And then rename
File.rename!(parent_libmlx_dir, libmlx_config.dir)
:ok
end
defp maybe_verify_integrity!(nil, _libmlx_archive), do: :ok
defp maybe_verify_integrity!(verify_integrity, libmlx_archive) do
{checksum_algo, expected_checksum} = get_checksum_info!(verify_integrity)
libmlx_archive_checksum = checksum!(libmlx_archive, checksum_algo)
if expected_checksum != libmlx_archive_checksum do
Mix.raise(
"Checksum (#{checksum_algo}) mismatch for #{libmlx_archive}. Expected: #{expected_checksum}, got: #{libmlx_archive_checksum}"
)
else
:ok
end
end
@known_checksum_algos [
"sha",
"sha224",
"sha256",
"sha384",
"sha512",
"sha3_224",
"sha3_256",
"sha3_384",
"sha3_512",
"blake2b",
"blake2s",
"ripemd160",
"md4",
"md5"
]
defp get_checksum_info!(verify_integrity) do
case String.split(verify_integrity, "=", parts: 2, trim: true) do
[algo, checksum] when algo in @known_checksum_algos ->
{String.to_existing_atom(algo), get_checksum_value!(checksum)}
_ ->
Mix.raise("Invalid checksum: #{verify_integrity}")
end
end
defp get_checksum_value!("url:" <> url) do
checksum_from_url!(url)
end
defp get_checksum_value!(checksum) do
checksum
end
defp checksum_from_url!(url) do
data = download!(url)
checksum = String.split(data, " ", parts: 2, trim: true)
if length(checksum) == 0 do
Mix.raise("Invalid checksum file: #{url}")
end
hd(checksum)
end
def download!(url, save_as \\ nil) do
url_charlist = String.to_charlist(url)
if proxy = System.get_env("HTTP_PROXY") || System.get_env("http_proxy") do
Mix.shell().info("Using HTTP_PROXY: #{proxy}")
%{host: host, port: port} = URI.parse(proxy)
:httpc.set_options([{:proxy, {{String.to_charlist(host), port}, []}}])
end
if proxy = System.get_env("HTTPS_PROXY") || System.get_env("https_proxy") do
Mix.shell().info("Using HTTPS_PROXY: #{proxy}")
%{host: host, port: port} = URI.parse(proxy)
:httpc.set_options([{:https_proxy, {{String.to_charlist(host), port}, []}}])
end
# https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
# TODO: This may no longer be necessary from Erlang/OTP 25.0 or later.
https_options = [
ssl:
[
verify: :verify_peer,
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
] ++ cacerts_options()
]
options = [body_format: :binary]
case :httpc.request(:get, {url_charlist, []}, https_options, options) do
{:ok, {{_, 200, _}, _headers, body}} ->
if save_as do
File.write!(save_as, body)
end
body
other ->
Mix.raise("Failed to download #{url}, reason: #{inspect(other)}")
end
end
defp cacerts_options do
cond do
path = System.get_env("HEX_CACERTS_PATH") ->
[cacertfile: path]
certs = otp_cacerts() ->
[cacerts: certs]
true ->
warn_no_cacerts()
[]
end
end
defp otp_cacerts do
if System.otp_release() >= "25" do
# cacerts_get/0 raises if no certs found
try do
:public_key.cacerts_get()
rescue
_ -> nil
end
end
end
defp warn_no_cacerts do
Mix.shell().error("""
No certificate trust store was found.
A certificate trust store is required in
order to download locales for your configuration.
Since elixir_make could not detect a system
installed certificate trust store one of the
following actions may be taken:
1. Specify the location of a certificate trust store
by configuring it in environment variable:
export HEX_CACERTS_PATH="/path/to/cacerts.pem"
2. Use OTP 25+ on an OS that has built-in certificate
trust store.
""")
end
defp checksum!(file_path, algo) do
case File.read(file_path) do
{:ok, content} ->
Base.encode16(:crypto.hash(algo, content), case: :lower)
{:error, reason} ->
Mix.raise(
"Cannot read the file for checksum comparison: #{file_path}. Reason: #{inspect(reason)}"
)
end
end
end