-
Notifications
You must be signed in to change notification settings - Fork 369
/
virtual_packages.cpp
357 lines (318 loc) · 12.2 KB
/
virtual_packages.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2019, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.
#include <regex>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <reproc++/run.hpp>
#include "mamba/core/context.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/util_os.hpp"
#include "mamba/core/virtual_packages.hpp"
#include "mamba/util/build.hpp"
#include "mamba/util/environment.hpp"
#include "mamba/util/os_linux.hpp"
#include "mamba/util/os_osx.hpp"
#include "mamba/util/os_win.hpp"
#include "mamba/util/string.hpp"
namespace mamba
{
namespace detail
{
std::string glibc_version()
{
auto override_version = util::get_env("CONDA_OVERRIDE_GLIBC");
if (override_version)
{
return override_version.value();
}
if (!util::on_linux)
{
return "";
}
const char* version = "";
#ifdef __linux__
std::vector<char> ver;
const size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, size_t{ 0 });
if (n > 0)
{
ver.assign(n, '\n');
confstr(_CS_GNU_LIBC_VERSION, ver.data(), n);
version = ver.data();
}
#endif
return std::string(util::strip(version, "glibc "));
}
std::string cuda_version()
{
LOG_DEBUG << "Loading CUDA virtual package";
auto override_version = util::get_env("CONDA_OVERRIDE_CUDA");
if (override_version)
{
return override_version.value();
}
std::string out, err;
std::vector<std::string> args = { "nvidia-smi", "--query", "-u", "-x" };
auto [status, ec] = reproc::run(
args,
reproc::options{},
reproc::sink::string(out),
reproc::sink::string(err)
);
if (ec)
{
out = "";
}
if (ec && util::on_win)
{
// Windows fallback
bool may_exist = false;
std::string path = util::get_env("PATH").value_or("");
std::vector<std::string> paths = util::split(path, util::pathsep());
for (auto& p : paths)
{
if (fs::exists(fs::u8path(p) / "nvcuda.dll"))
{
may_exist = true;
break;
}
}
std::string base = "C:\\Windows\\System32\\DriverStore\\FileRepository";
if (may_exist)
{
for (auto& p : fs::directory_iterator(base))
{
if (util::starts_with(p.path().filename().string(), "nv")
&& fs::exists(p.path() / "nvidia-smi.exe"))
{
std::string f = (p.path() / "nvidia-smi.exe").string();
LOG_DEBUG << "Found nvidia-smi in: " << f;
std::vector<std::string> command = { f, "--query", "-u", "-x" };
auto [_ /*cmd_status*/, cmd_ec] = reproc::run(
command,
reproc::options{},
reproc::sink::string(out),
reproc::sink::string(err)
);
if (!cmd_ec)
{
break;
}
}
}
}
}
if (out.empty())
{
LOG_DEBUG << "Could not find CUDA version by calling 'nvidia-smi' (skipped)\n";
return "";
}
std::regex re("<cuda_version>(.*)<\\/cuda_version>");
std::smatch m;
if (std::regex_search(out, m, re))
{
if (m.size() == 2)
{
std::ssub_match cuda_version = m[1];
LOG_DEBUG << "CUDA driver version found: " << cuda_version;
return cuda_version.str();
}
}
LOG_DEBUG << "CUDA not found";
return "";
}
auto make_virtual_package( //
std::string name,
std::string subdir,
std::string version,
std::string build_string
) -> specs::PackageInfo
{
specs::PackageInfo res(std::move(name));
res.version = version.empty() ? "0" : std::move(version);
res.build_string = build_string.empty() ? "0" : std::move(build_string);
res.build_number = 0;
res.channel = "@";
res.platform = std::move(subdir);
res.md5 = "12345678901234567890123456789012";
res.filename = res.name;
return res;
}
std::string get_archspec_x86_64()
{
#if (defined(__GNUC__) || defined(__clang__)) && __x86_64__
/* if (__builtin_cpu_supports ("x86-64-v4")) */
if (__builtin_cpu_supports("avx512f") && __builtin_cpu_supports("avx512bw")
&& __builtin_cpu_supports("avx512cd") && __builtin_cpu_supports("avx512dq")
&& __builtin_cpu_supports("avx512vl"))
{
return "x86_64-v4";
}
/* if (__builtin_cpu_supports ("x86-64-v3")) */
if (__builtin_cpu_supports("avx") && __builtin_cpu_supports("avx2")
&& __builtin_cpu_supports("bmi") && __builtin_cpu_supports("bmi2")
&& __builtin_cpu_supports("fma"))
{
return "x86_64-v3";
}
/* if (__builtin_cpu_supports ("x86-64-v2")) */
if (__builtin_cpu_supports("popcnt") && __builtin_cpu_supports("sse3")
&& __builtin_cpu_supports("ssse3") && __builtin_cpu_supports("sse4.1")
&& __builtin_cpu_supports("sse4.2"))
{
return "x86_64-v2";
}
#endif
return "x86_64";
}
std::string get_archspec(const std::string& arch)
{
auto override_version = util::get_env("CONDA_OVERRIDE_ARCHSPEC");
if (override_version)
{
return override_version.value();
}
if (arch == "64")
{
return get_archspec_x86_64();
}
else if (arch == "32")
{
return "x86";
}
else
{
return arch;
}
}
[[nodiscard]] auto overridable_linux_version() -> tl::expected<std::string, util::OSError>
{
if (auto override_version = util::get_env("CONDA_OVERRIDE_LINUX"))
{
return { std::move(override_version).value() };
}
return util::linux_version();
}
[[nodiscard]] auto overridable_osx_version() -> tl::expected<std::string, util::OSError>
{
if (auto override_version = util::get_env("CONDA_OVERRIDE_OSX"))
{
return { std::move(override_version).value() };
}
return util::osx_version();
}
[[nodiscard]] auto overridable_windows_version() -> tl::expected<std::string, util::OSError>
{
if (auto override_version = util::get_env("CONDA_OVERRIDE_WIN"))
{
return { std::move(override_version).value() };
}
return util::windows_version();
}
std::vector<specs::PackageInfo> dist_packages(const std::string& platform)
{
LOG_DEBUG << "Loading distribution virtual packages";
std::vector<specs::PackageInfo> res;
const auto split_platform = util::split(platform, "-", 1);
if (split_platform.size() != 2)
{
LOG_ERROR << "Platform is ill-formed, expected <os>-<arch> in: '" + platform + "'";
return res;
}
std::string os = split_platform[0];
std::string arch = split_platform[1];
if (os == "win")
{
overridable_windows_version()
.transform(
[&](std::string&& version) {
res.push_back(make_virtual_package("__win", platform, std::move(version)));
}
)
.or_else(
[&](util::OSError err)
{
res.push_back(make_virtual_package("__win", platform, "0"));
LOG_WARNING
<< "Windows version not found, defaulting virtual package version to 0."
" Try setting CONDA_OVERRIDE_WIN environment variable to the"
" desired version.";
LOG_DEBUG << err.message;
}
);
}
if (os == "linux")
{
res.push_back(make_virtual_package("__unix", platform));
overridable_linux_version()
.transform(
[&](std::string&& version) {
res.push_back(
make_virtual_package("__linux", platform, std::move(version))
);
}
)
.or_else(
[&](util::OSError err)
{
res.push_back(make_virtual_package("__linux", platform, "0"));
LOG_WARNING
<< "Linux version not found, defaulting virtual package version to 0."
" Try setting CONDA_OVERRIDE_LINUX environment variable to the"
" desired version.";
LOG_DEBUG << err.message;
}
);
std::string libc_ver = detail::glibc_version();
if (!libc_ver.empty())
{
res.push_back(make_virtual_package("__glibc", platform, libc_ver));
}
else
{
LOG_WARNING << "glibc version not found (virtual package skipped)";
}
}
if (os == "osx")
{
res.push_back(make_virtual_package("__unix", platform));
overridable_osx_version()
.transform(
[&](std::string&& version) {
res.push_back(make_virtual_package("__osx", platform, std::move(version)));
}
)
.or_else(
[&](util::OSError err)
{
res.push_back(make_virtual_package("__osx", platform, "0"));
LOG_WARNING
<< "OSX version not found, defaulting virtual package version to 0."
" Try setting CONDA_OVERRIDE_OSX environment variable to the"
" desired version.";
LOG_DEBUG << err.message;
}
);
}
res.push_back(make_virtual_package("__archspec", platform, "1", get_archspec(arch)));
return res;
}
}
std::vector<specs::PackageInfo> get_virtual_packages(const std::string& platform)
{
LOG_DEBUG << "Loading virtual packages";
auto res = detail::dist_packages(platform);
auto cuda_ver = detail::cuda_version();
if (!cuda_ver.empty())
{
res.push_back(detail::make_virtual_package("__cuda", platform, cuda_ver));
}
return res;
}
}