forked from mamba-org/mamba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.cpp
631 lines (572 loc) · 19.7 KB
/
solver.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// 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 <sstream>
#include <stdexcept>
#include <iostream>
#include <list>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <solv/pool.h>
#include <solv/solver.h>
#include "mamba/core/channel.hpp"
#include "mamba/core/context.hpp"
#include "mamba/core/match_spec.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/package_info.hpp"
#include "mamba/core/pool.hpp"
#include "mamba/core/repo.hpp"
#include "mamba/core/satisfiability_error.hpp"
#include "mamba/core/solver.hpp"
#include "mamba/core/util_string.hpp"
#include "solv-cpp/queue.hpp"
namespace mamba
{
// TODO this should belong in libsolv.
const char* solver_ruleinfo_name(SolverRuleinfo rule)
{
switch (rule)
{
case (SOLVER_RULE_UNKNOWN):
{
return "SOLVER_RULE_UNKNOWN";
}
case (SOLVER_RULE_PKG):
{
return "SOLVER_RULE_PKG";
}
case (SOLVER_RULE_PKG_NOT_INSTALLABLE):
{
return "SOLVER_RULE_PKG_NOT_INSTALLABLE";
}
case (SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP):
{
return "SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP";
}
case (SOLVER_RULE_PKG_REQUIRES):
{
return "SOLVER_RULE_PKG_REQUIRES";
}
case (SOLVER_RULE_PKG_SELF_CONFLICT):
{
return "SOLVER_RULE_PKG_SELF_CONFLICT";
}
case (SOLVER_RULE_PKG_CONFLICTS):
{
return "SOLVER_RULE_PKG_CONFLICTS";
}
case (SOLVER_RULE_PKG_SAME_NAME):
{
return "SOLVER_RULE_PKG_SAME_NAME";
}
case (SOLVER_RULE_PKG_OBSOLETES):
{
return "SOLVER_RULE_PKG_OBSOLETES";
}
case (SOLVER_RULE_PKG_IMPLICIT_OBSOLETES):
{
return "SOLVER_RULE_PKG_IMPLICIT_OBSOLETES";
}
case (SOLVER_RULE_PKG_INSTALLED_OBSOLETES):
{
return "SOLVER_RULE_PKG_INSTALLED_OBSOLETES";
}
case (SOLVER_RULE_PKG_RECOMMENDS):
{
return "SOLVER_RULE_PKG_RECOMMENDS";
}
case (SOLVER_RULE_PKG_CONSTRAINS):
{
return "SOLVER_RULE_PKG_CONSTRAINS";
}
case (SOLVER_RULE_UPDATE):
{
return "SOLVER_RULE_UPDATE";
}
case (SOLVER_RULE_FEATURE):
{
return "SOLVER_RULE_FEATURE";
}
case (SOLVER_RULE_JOB):
{
return "SOLVER_RULE_JOB";
}
case (SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP):
{
return "SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP";
}
case (SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM):
{
return "SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM";
}
case (SOLVER_RULE_JOB_UNKNOWN_PACKAGE):
{
return "SOLVER_RULE_JOB_UNKNOWN_PACKAGE";
}
case (SOLVER_RULE_JOB_UNSUPPORTED):
{
return "SOLVER_RULE_JOB_UNSUPPORTED";
}
case (SOLVER_RULE_DISTUPGRADE):
{
return "SOLVER_RULE_DISTUPGRADE";
}
case (SOLVER_RULE_INFARCH):
{
return "SOLVER_RULE_INFARCH";
}
case (SOLVER_RULE_CHOICE):
{
return "SOLVER_RULE_CHOICE";
}
case (SOLVER_RULE_LEARNT):
{
return "SOLVER_RULE_LEARNT";
}
case (SOLVER_RULE_BEST):
{
return "SOLVER_RULE_BEST";
}
case (SOLVER_RULE_YUMOBS):
{
return "SOLVER_RULE_YUMOBS";
}
case (SOLVER_RULE_RECOMMENDS):
{
return "SOLVER_RULE_RECOMMENDS";
}
case (SOLVER_RULE_BLACK):
{
return "SOLVER_RULE_BLACK";
}
case (SOLVER_RULE_STRICT_REPO_PRIORITY):
{
return "SOLVER_RULE_STRICT_REPO_PRIORITY";
}
default:
{
throw std::runtime_error("Invalid SolverRuleinfo: " + std::to_string(rule));
}
}
}
namespace
{
void delete_libsolve_solver(::Solver* solver)
{
LOG_INFO << "Freeing solver.";
if (solver != nullptr)
{
solver_free(solver);
}
}
MSolverProblem make_solver_problem(
const MSolver& solver,
const MPool& pool,
SolverRuleinfo type,
Id source_id,
Id target_id,
Id dep_id
)
{
const ::Solver* const solver_ptr = solver;
return {
/* .type= */ type,
/* .source_id= */ source_id,
/* .target_id= */ target_id,
/* .dep_id= */ dep_id,
/* .source= */ pool.id2pkginfo(source_id),
/* .target= */ pool.id2pkginfo(target_id),
/* .dep= */ pool.dep2str(dep_id),
/* .description= */
solver_problemruleinfo2str(
const_cast<::Solver*>(solver_ptr), // Not const because might alloctmp space
type,
source_id,
target_id,
dep_id
),
};
}
}
MSolver::MSolver(MPool pool, const std::vector<std::pair<int, int>> flags)
: m_flags(std::move(flags))
, m_is_solved(false)
, m_pool(std::move(pool))
, m_solver(nullptr, &delete_libsolve_solver)
, m_jobs(std::make_unique<solv::ObjQueue>())
{
pool_createwhatprovides(m_pool);
}
MSolver::~MSolver() = default;
void MSolver::add_global_job(int job_flag)
{
m_jobs->push_back(job_flag, 0);
}
void MSolver::add_reinstall_job(MatchSpec& ms, int job_flag)
{
Pool* const pool = m_pool;
if (pool->installed == nullptr)
{
throw std::runtime_error("Did not find any packages marked as installed.");
}
// 1. check if spec is already installed
Id needle = pool_str2id(m_pool, ms.name.c_str(), 0);
static Id real_repo_key = pool_str2id(pool, "solvable:real_repo_url", 1);
if (needle && (pool->installed != nullptr))
{
Id pkg_id;
Solvable* s;
FOR_REPO_SOLVABLES(pool->installed, pkg_id, s)
{
if (s->name == needle)
{
// the data about the channel is only in the prefix_data unfortunately
std::string selected_channel;
if (solvable_lookup_str(s, real_repo_key))
{
// this is the _full_ url to the file (incl. abc.tar.bz2)
selected_channel = solvable_lookup_str(s, real_repo_key);
}
else
{
throw std::runtime_error(
"Could not find channel associated with reinstall package"
);
}
selected_channel = make_channel(selected_channel).name();
MatchSpec modified_spec(ms);
if (!ms.channel.empty() || !ms.version.empty() || !ms.build_string.empty())
{
Console::stream() << ms.conda_build_form()
<< ": overriding channel, version and build from "
"installed packages due to --force-reinstall.";
ms.channel = "";
ms.version = "";
ms.build_string = "";
}
modified_spec.channel = selected_channel;
modified_spec.version = raw_str_or_empty(pool_id2str(pool, s->evr));
modified_spec.build_string = raw_str_or_empty(
solvable_lookup_str(s, SOLVABLE_BUILDFLAVOR)
);
LOG_INFO << "Reinstall " << modified_spec.conda_build_form() << " from channel "
<< selected_channel;
m_jobs->push_back(
job_flag | SOLVER_SOLVABLE_PROVIDES,
m_pool.matchspec2id(modified_spec)
);
return;
}
}
}
m_jobs->push_back(job_flag | SOLVER_SOLVABLE_PROVIDES, m_pool.matchspec2id(ms));
}
void MSolver::add_jobs(const std::vector<std::string>& jobs, int job_flag)
{
for (const auto& job : jobs)
{
MatchSpec ms(job);
int job_type = job_flag & SOLVER_JOBMASK;
if (job_type & SOLVER_INSTALL)
{
m_install_specs.emplace_back(job);
}
else if (job_type == SOLVER_ERASE)
{
m_remove_specs.emplace_back(job);
}
else if (job_type == SOLVER_LOCK)
{
m_neuter_specs.emplace_back(job); // not used for the moment
}
::Id const job_id = m_pool.matchspec2id(ms);
// This is checking if SOLVER_ERASE and SOLVER_INSTALL are set
// which are the flags for SOLVER_UPDATE
if ((job_flag & SOLVER_UPDATE) == SOLVER_UPDATE)
{
// ignoring update specs here for now
if (!ms.is_simple())
{
std::string old_channel = ms.channel;
ms.channel.clear();
::Id const job_id = m_pool.matchspec2id(ms);
ms.channel = old_channel;
m_jobs->push_back(SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES, job_id);
}
m_jobs->push_back(job_flag | SOLVER_SOLVABLE_PROVIDES, job_id);
}
else if ((job_flag & SOLVER_INSTALL) && force_reinstall)
{
add_reinstall_job(ms, job_flag);
}
else
{
LOG_INFO << "Adding job: " << ms.str();
m_jobs->push_back(job_flag | SOLVER_SOLVABLE_PROVIDES, job_id);
}
}
}
void MSolver::add_constraint(const std::string& job)
{
m_jobs->push_back(SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES, m_pool.matchspec2id({ job }));
}
void MSolver::add_pin(const std::string& pin)
{
// if we pin a package, we need to remove all packages that don't match the
// pin from being available for installation! This is done by adding
// SOLVER_LOCK to the packages, so that they are prevented from being
// installed A lock basically says: keep the state of the package. I.e.
// uninstalled packages stay uninstalled, installed packages stay installed.
// A lock is a hard requirement, we could also use SOLVER_FAVOR for soft
// requirements
// First we need to check if the pin is OK given the currently installed
// packages
Pool* pool = m_pool;
MatchSpec ms(pin);
// TODO
// if (m_prefix_data)
// {
// for (auto& [name, record] : m_prefix_data->records())
// {
// LOG_ERROR << "NAME " << name;
// if (name == ms.name)
// {
// LOG_ERROR << "Found pinned package in installed packages, need
// to check pin now."; LOG_ERROR << record.version << " vs " <<
// ms.version;
// }
// }
// }
Id match = m_pool.matchspec2id(ms);
std::set<Id> matching_solvables;
for (Id* wp = pool_whatprovides_ptr(pool, match); *wp; wp++)
{
matching_solvables.insert(*wp);
}
std::set<Id> all_solvables;
Id name_id = pool_str2id(pool, ms.name.c_str(), 1);
for (Id* wp = pool_whatprovides_ptr(pool, name_id); *wp; wp++)
{
all_solvables.insert(*wp);
}
if (all_solvables.size() != 0 && matching_solvables.size() == 0)
{
throw std::runtime_error(fmt::format("No package can be installed for pin: {}", pin));
}
m_pinned_specs.push_back(ms);
solv::ObjQueue selected_pkgs;
for (auto& id : all_solvables)
{
if (matching_solvables.find(id) == matching_solvables.end())
{
// the solvable is _NOT_ matched by our pinning expression! So we have to
// lock it to make it un-installable
selected_pkgs.push_back(id);
}
}
Id d = pool_queuetowhatprovides(pool, selected_pkgs.raw());
m_jobs->push_back(SOLVER_LOCK | SOLVER_SOLVABLE_ONE_OF, d);
}
void MSolver::add_pins(const std::vector<std::string>& pins)
{
for (auto pin : pins)
{
add_pin(pin);
}
}
void MSolver::set_postsolve_flags(const std::vector<std::pair<int, int>>& flags)
{
for (const auto& option : flags)
{
switch (option.first)
{
case MAMBA_NO_DEPS:
no_deps = option.second;
break;
case MAMBA_ONLY_DEPS:
only_deps = option.second;
break;
case MAMBA_FORCE_REINSTALL:
force_reinstall = option.second;
break;
}
}
}
void MSolver::set_flags(const std::vector<std::pair<int, int>>& flags)
{
for (const auto& option : flags)
{
solver_set_flag(m_solver.get(), option.first, option.second);
}
}
bool MSolver::is_solved() const
{
return m_is_solved;
}
const MPool& MSolver::pool() const&
{
return m_pool;
}
MPool& MSolver::pool() &
{
return m_pool;
}
MPool&& MSolver::pool() &&
{
return std::move(m_pool);
}
const std::vector<MatchSpec>& MSolver::install_specs() const
{
return m_install_specs;
}
const std::vector<MatchSpec>& MSolver::remove_specs() const
{
return m_remove_specs;
}
const std::vector<MatchSpec>& MSolver::neuter_specs() const
{
return m_neuter_specs;
}
const std::vector<MatchSpec>& MSolver::pinned_specs() const
{
return m_pinned_specs;
}
bool MSolver::try_solve()
{
m_solver.reset(solver_create(m_pool));
set_flags(m_flags);
std::cout << "\n>>> M_FLAGS: ";
for (auto v : m_flags)
std::cout << v.first << " " << v.second << " ";
std::cout << std::endl;
std::cout << "\n>>> M_JOBS: ";
const auto l = std::list<::Id>(m_jobs->begin(), m_jobs->end());
for (auto v : l)
std::cout << v << " ";
solver_solve(m_solver.get(), m_jobs->raw());
m_is_solved = true;
LOG_INFO << "Problem count: " << solver_problem_count(m_solver.get());
const bool success = solver_problem_count(m_solver.get()) == 0;
Console::instance().json_write({ { "success", success } });
return success;
}
void MSolver::must_solve()
{
const bool success = try_solve();
if (!success)
{
explain_problems(LOG_ERROR);
throw mamba_error(
"Could not solve for environment specs",
mamba_error_code::satisfiablitity_error
);
}
}
std::vector<MSolverProblem> MSolver::all_problems_structured() const
{
std::vector<MSolverProblem> res;
solv::ObjQueue problem_rules;
const auto count = static_cast<Id>(solver_problem_count(m_solver.get()));
for (Id i = 1; i <= count; ++i)
{
solver_findallproblemrules(m_solver.get(), i, problem_rules.raw());
for (const Id r : problem_rules)
{
if (r != 0)
{
Id source, target, dep;
const SolverRuleinfo type = solver_ruleinfo(m_solver.get(), r, &source, &target, &dep);
res.push_back(make_solver_problem(
/* solver= */ *this,
/* pool= */ m_pool,
/* type= */ type,
/* source_id= */ source,
/* target_id= */ target,
/* dep_id= */ dep
));
}
}
}
return res;
}
std::string MSolver::all_problems_to_str() const
{
std::stringstream problems;
solv::ObjQueue problem_rules;
auto count = static_cast<Id>(solver_problem_count(m_solver.get()));
for (Id i = 1; i <= count; ++i)
{
solver_findallproblemrules(m_solver.get(), i, problem_rules.raw());
for (const Id r : problem_rules)
{
Id source, target, dep;
if (!r)
{
problems << "- [SKIP] no problem rule?\n";
}
else
{
const SolverRuleinfo type = solver_ruleinfo(m_solver.get(), r, &source, &target, &dep);
problems << " - "
<< solver_problemruleinfo2str(m_solver.get(), type, source, target, dep)
<< "\n";
}
}
}
return problems.str();
}
std::ostream& MSolver::explain_problems(std::ostream& out) const
{
const auto& ctx = Context::instance();
out << "Could not solve for environment specs\n";
const auto pbs = ProblemsGraph::from_solver(*this, pool());
const auto pbs_simplified = simplify_conflicts(pbs);
const auto cp_pbs = CompressedProblemsGraph::from_problems_graph(pbs_simplified);
print_problem_tree_msg(
out,
cp_pbs,
{ /* .unavailable= */ ctx.palette.failure, /* .available= */ ctx.palette.success }
);
return out;
}
std::string MSolver::explain_problems() const
{
std::stringstream ss;
explain_problems(ss);
return ss.str();
}
std::string MSolver::problems_to_str() const
{
solv::ObjQueue problem_queue;
auto count = static_cast<int>(solver_problem_count(m_solver.get()));
std::stringstream problems;
for (int i = 1; i <= count; i++)
{
problem_queue.push_back(i);
problems << " - " << solver_problem2str(m_solver.get(), i) << "\n";
}
return "Encountered problems while solving:\n" + problems.str();
}
std::vector<std::string> MSolver::all_problems() const
{
std::vector<std::string> problems;
solv::ObjQueue problem_queue;
int count = static_cast<int>(solver_problem_count(m_solver.get()));
for (int i = 1; i <= count; i++)
{
problem_queue.push_back(i);
problems.emplace_back(solver_problem2str(m_solver.get(), i));
}
return problems;
}
MSolver::operator const Solver*() const
{
return m_solver.get();
}
MSolver::operator Solver*()
{
return m_solver.get();
}
} // namespace mamba