]> git.saurik.com Git - apt.git/blob - apt-private/private-source.cc
a6b446587fafd6b207aa4e7a02cc5765bd46cbdd
[apt.git] / apt-private / private-source.cc
1 // Include Files /*{{{*/
2 #include <config.h>
3
4 #include <apt-pkg/acquire-item.h>
5 #include <apt-pkg/acquire.h>
6 #include <apt-pkg/algorithms.h>
7 #include <apt-pkg/aptconfiguration.h>
8 #include <apt-pkg/cachefile.h>
9 #include <apt-pkg/cacheiterators.h>
10 #include <apt-pkg/cacheset.h>
11 #include <apt-pkg/cmndline.h>
12 #include <apt-pkg/configuration.h>
13 #include <apt-pkg/depcache.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/hashes.h>
17 #include <apt-pkg/indexfile.h>
18 #include <apt-pkg/metaindex.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/sourcelist.h>
21 #include <apt-pkg/srcrecords.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/version.h>
24 #include <apt-pkg/policy.h>
25
26 #include <apt-private/private-cachefile.h>
27 #include <apt-private/private-cacheset.h>
28 #include <apt-private/private-download.h>
29 #include <apt-private/private-install.h>
30 #include <apt-private/private-source.h>
31
32 #include <apt-pkg/debindexfile.h>
33
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include <iostream>
42 #include <sstream>
43 #include <set>
44 #include <string>
45 #include <vector>
46
47 #include <apti18n.h>
48 /*}}}*/
49
50 // GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
51 static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
52 pkgSourceList const * const SrcList, pkgSrcRecords::Parser const * const Parse)
53 {
54 // try to find release
55 const pkgIndexFile& CurrentIndexFile = Parse->Index();
56
57 for (pkgSourceList::const_iterator S = SrcList->begin();
58 S != SrcList->end(); ++S)
59 {
60 std::vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
61 for (std::vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
62 IF != Indexes->end(); ++IF)
63 {
64 if (&CurrentIndexFile == (*IF))
65 return (*S)->FindInCache(CacheFile, false);
66 }
67 }
68 return pkgCache::RlsFileIterator(CacheFile);
69 }
70 /*}}}*/
71 // FindSrc - Find a source record /*{{{*/
72 static pkgSrcRecords::Parser *FindSrc(const char *Name,
73 pkgSrcRecords &SrcRecs,std::string &Src,
74 CacheFile &Cache)
75 {
76 if (Cache.BuildCaches(false) == false)
77 return nullptr;
78 std::string VerTag, UserRequestedVerTag;
79 std::string ArchTag = "";
80 std::string RelTag = _config->Find("APT::Default-Release");
81 std::string TmpSrc = Name;
82
83 // extract release
84 size_t found = TmpSrc.find_last_of("/");
85 if (found != std::string::npos)
86 {
87 RelTag = TmpSrc.substr(found+1);
88 TmpSrc = TmpSrc.substr(0,found);
89 }
90 // extract the version
91 found = TmpSrc.find_last_of("=");
92 if (found != std::string::npos)
93 {
94 VerTag = UserRequestedVerTag = TmpSrc.substr(found+1);
95 TmpSrc = TmpSrc.substr(0,found);
96 }
97 // extract arch
98 found = TmpSrc.find_last_of(":");
99 if (found != std::string::npos)
100 {
101 ArchTag = TmpSrc.substr(found+1);
102 TmpSrc = TmpSrc.substr(0,found);
103 }
104
105 /* Lookup the version of the package we would install if we were to
106 install a version and determine the source package name, then look
107 in the archive for a source package of the same name. */
108 bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
109 pkgCache::PkgIterator Pkg;
110 if (ArchTag != "")
111 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc, ArchTag);
112 else
113 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc);
114
115 // if we can't find a package but the user qualified with a arch,
116 // error out here
117 if (Pkg.end() && ArchTag != "")
118 {
119 Src = Name;
120 _error->Error(_("Can not find a package for architecture '%s'"),
121 ArchTag.c_str());
122 return 0;
123 }
124
125 if (MatchSrcOnly == false && Pkg.end() == false)
126 {
127 if(VerTag != "" || RelTag != "" || ArchTag != "")
128 {
129 bool fuzzy = false;
130 // we have a default release, try to locate the pkg. we do it like
131 // this because GetCandidateVer() will not "downgrade", that means
132 // "apt-get source -t stable apt" won't work on a unstable system
133 for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
134 {
135 // try first only exact matches, later fuzzy matches
136 if (Ver.end() == true)
137 {
138 if (fuzzy == true)
139 break;
140 fuzzy = true;
141 Ver = Pkg.VersionList();
142 // exit right away from the Pkg.VersionList() loop if we
143 // don't have any versions
144 if (Ver.end() == true)
145 break;
146 }
147
148 // ignore arches that are not for us
149 if (ArchTag != "" && Ver.Arch() != ArchTag)
150 continue;
151
152 // pick highest version for the arch unless the user wants
153 // something else
154 if (ArchTag != "" && VerTag == "" && RelTag == "")
155 if(Cache->VS().CmpVersion(VerTag, Ver.VerStr()) < 0)
156 VerTag = Ver.VerStr();
157
158 // We match against a concrete version (or a part of this version)
159 if (VerTag.empty() == false &&
160 (fuzzy == true || Cache->VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
161 (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
162 continue;
163
164 for (pkgCache::VerFileIterator VF = Ver.FileList();
165 VF.end() == false; ++VF)
166 {
167 /* If this is the status file, and the current version is not the
168 version in the status file (ie it is not installed, or somesuch)
169 then it is not a candidate for installation, ever. This weeds
170 out bogus entries that may be due to config-file states, or
171 other. */
172 if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
173 pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
174 continue;
175
176 // or we match against a release
177 if(VerTag.empty() == false ||
178 (VF.File().Archive() != 0 && VF.File().Archive() == RelTag) ||
179 (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
180 {
181 // the Version we have is possibly fuzzy or includes binUploads,
182 // so we use the Version of the SourcePkg (empty if same as package)
183 Src = Ver.SourcePkgName();
184 VerTag = Ver.SourceVerStr();
185 break;
186 }
187 }
188 if (Src.empty() == false)
189 break;
190 }
191 }
192
193 if (Src == "" && ArchTag != "")
194 {
195 if (VerTag != "")
196 _error->Error(_("Can not find a package '%s' with version '%s'"),
197 Pkg.FullName().c_str(), VerTag.c_str());
198 if (RelTag != "")
199 _error->Error(_("Can not find a package '%s' with release '%s'"),
200 Pkg.FullName().c_str(), RelTag.c_str());
201 Src = Name;
202 return 0;
203 }
204
205
206 if (Src.empty() == true)
207 {
208 // if we don't have found a fitting package yet so we will
209 // choose a good candidate and proceed with that.
210 // Maybe we will find a source later on with the right VerTag
211 // or RelTag
212 if (Cache.BuildPolicy() == false)
213 return nullptr;
214 pkgPolicy * Policy = dynamic_cast<pkgPolicy*>(Cache.GetPolicy());
215 if (Policy == nullptr)
216 {
217 _error->Fatal("Implementation error: dynamic up-casting policy engine failed in FindSrc!");
218 return nullptr;
219 }
220 pkgCache::VerIterator const Ver = Policy->GetCandidateVer(Pkg);
221 if (Ver.end() == false)
222 {
223 if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
224 Src = Ver.SourcePkgName();
225 if (VerTag.empty() == true && strcmp(Ver.SourceVerStr(),Ver.VerStr()) != 0)
226 VerTag = Ver.SourceVerStr();
227 }
228 }
229 }
230
231 if (Src.empty() == true)
232 {
233 Src = TmpSrc;
234 }
235 else
236 {
237 /* if we have a source pkg name, make sure to only search
238 for srcpkg names, otherwise apt gets confused if there
239 is a binary package "pkg1" and a source package "pkg1"
240 with the same name but that comes from different packages */
241 MatchSrcOnly = true;
242 if (Src != TmpSrc)
243 {
244 ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
245 }
246 }
247
248 // The best hit
249 pkgSrcRecords::Parser *Last = 0;
250 unsigned long Offset = 0;
251 std::string Version;
252 pkgSourceList const * const SrcList = Cache.GetSourceList();
253
254 /* Iterate over all of the hits, which includes the resulting
255 binary packages in the search */
256 pkgSrcRecords::Parser *Parse;
257 while (true)
258 {
259 SrcRecs.Restart();
260 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
261 {
262 const std::string Ver = Parse->Version();
263 bool CorrectRelTag = false;
264
265 // See if we need to look for a specific release tag
266 if (RelTag != "" && UserRequestedVerTag == "")
267 {
268 pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(Cache, SrcList, Parse);
269 if (Rls.end() == false)
270 {
271 if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
272 (Rls->Codename != 0 && RelTag == Rls.Codename()))
273 CorrectRelTag = true;
274 }
275 } else
276 CorrectRelTag = true;
277
278 // Ignore all versions which doesn't fit
279 if (VerTag.empty() == false &&
280 Cache->VS().CmpVersion(VerTag, Ver) != 0) // exact match
281 continue;
282
283 // Newer version or an exact match? Save the hit
284 if (CorrectRelTag && (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0)) {
285 Last = Parse;
286 Offset = Parse->Offset();
287 Version = Ver;
288 }
289
290 // was the version check above an exact match?
291 // If so, we don't need to look further
292 if (VerTag.empty() == false && (VerTag == Ver))
293 break;
294 }
295 if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
296 ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
297 Version.c_str(), RelTag.c_str(), Src.c_str());
298
299 if (Last != 0 || VerTag.empty() == true)
300 break;
301 _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
302 return 0;
303 }
304
305 if (Last == 0 || Last->Jump(Offset) == false)
306 return 0;
307
308 return Last;
309 }
310 /*}}}*/
311 // DoSource - Fetch a source archive /*{{{*/
312 // ---------------------------------------------------------------------
313 /* Fetch souce packages */
314 struct DscFile
315 {
316 std::string Package;
317 std::string Version;
318 std::string Dsc;
319 };
320 bool DoSource(CommandLine &CmdL)
321 {
322 CacheFile Cache;
323 if (Cache.Open(false) == false)
324 return false;
325
326 if (CmdL.FileSize() <= 1)
327 return _error->Error(_("Must specify at least one package to fetch source for"));
328
329 // Read the source list
330 if (Cache.BuildSourceList() == false)
331 return false;
332 pkgSourceList *List = Cache.GetSourceList();
333
334 // Create the text record parsers
335 pkgSrcRecords SrcRecs(*List);
336 if (_error->PendingError() == true)
337 return false;
338
339 std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
340
341 // insert all downloaded uris into this set to avoid downloading them
342 // twice
343 std::set<std::string> queued;
344
345 // Diff only mode only fetches .diff files
346 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
347 // Tar only mode only fetches .tar files
348 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
349 // Dsc only mode only fetches .dsc files
350 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
351
352 // Load the requestd sources into the fetcher
353 aptAcquireWithTextStatus Fetcher;
354 unsigned J = 0;
355 std::vector<std::string> UntrustedList;
356 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
357 {
358 std::string Src;
359 pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
360 if (Last == 0) {
361 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
362 }
363
364 if (Last->Index().IsTrusted() == false)
365 UntrustedList.push_back(Src);
366
367 std::string srec = Last->AsStr();
368 std::string::size_type pos = srec.find("\nVcs-");
369 while (pos != std::string::npos)
370 {
371 pos += strlen("\nVcs-");
372 std::string vcs = srec.substr(pos,srec.find(":",pos)-pos);
373 if(vcs == "Browser")
374 {
375 pos = srec.find("\nVcs-", pos);
376 continue;
377 }
378 pos += vcs.length()+2;
379 std::string::size_type epos = srec.find("\n", pos);
380 std::string const uri = srec.substr(pos,epos-pos);
381 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
382 "the '%s' version control system at:\n"
383 "%s\n"),
384 Src.c_str(), vcs.c_str(), uri.c_str());
385 std::string vcscmd;
386 if (vcs == "Bzr")
387 vcscmd = "bzr branch " + uri;
388 else if (vcs == "Git")
389 vcscmd = "git clone " + uri;
390
391 if (vcscmd.empty() == false)
392 ioprintf(c1out,_("Please use:\n%s\n"
393 "to retrieve the latest (possibly unreleased) "
394 "updates to the package.\n"),
395 vcscmd.c_str());
396 break;
397 }
398
399 // Back track
400 std::vector<pkgSrcRecords::File2> Lst;
401 if (Last->Files2(Lst) == false) {
402 return false;
403 }
404
405 // Load them into the fetcher
406 for (std::vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
407 I != Lst.end(); ++I)
408 {
409 // Try to guess what sort of file it is we are getting.
410 if (I->Type == "dsc")
411 {
412 Dsc[J].Package = Last->Package();
413 Dsc[J].Version = Last->Version();
414 Dsc[J].Dsc = flNotDir(I->Path);
415 }
416
417 // Handle the only options so that multiple can be used at once
418 if (diffOnly == true || tarOnly == true || dscOnly == true)
419 {
420 if ((diffOnly == true && I->Type == "diff") ||
421 (tarOnly == true && I->Type == "tar") ||
422 (dscOnly == true && I->Type == "dsc"))
423 ; // Fine, we want this file downloaded
424 else
425 continue;
426 }
427
428 // don't download the same uri twice (should this be moved to
429 // the fetcher interface itself?)
430 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
431 continue;
432 queued.insert(Last->Index().ArchiveURI(I->Path));
433
434 // check if we have a file with that md5 sum already localy
435 std::string localFile = flNotDir(I->Path);
436 if (FileExists(localFile) == true)
437 if(I->Hashes.VerifyFile(localFile) == true)
438 {
439 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
440 localFile.c_str());
441 continue;
442 }
443
444 // see if we have a hash (Acquire::ForceHash is the only way to have none)
445 if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
446 {
447 ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
448 localFile.c_str());
449 continue;
450 }
451
452 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
453 I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
454 }
455 }
456
457 // Display statistics
458 unsigned long long FetchBytes = Fetcher.FetchNeeded();
459 unsigned long long FetchPBytes = Fetcher.PartialPresent();
460 unsigned long long DebBytes = Fetcher.TotalNeeded();
461
462 if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
463 return false;
464
465 // Number of bytes
466 if (DebBytes != FetchBytes)
467 //TRANSLATOR: The required space between number and unit is already included
468 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
469 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
470 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
471 else
472 //TRANSLATOR: The required space between number and unit is already included
473 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
474 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
475 SizeToStr(DebBytes).c_str());
476
477 if (_config->FindB("APT::Get::Simulate",false) == true)
478 {
479 for (unsigned I = 0; I != J; I++)
480 ioprintf(std::cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
481 return true;
482 }
483
484 // Just print out the uris an exit if the --print-uris flag was used
485 if (_config->FindB("APT::Get::Print-URIs") == true)
486 {
487 pkgAcquire::UriIterator I = Fetcher.UriBegin();
488 for (; I != Fetcher.UriEnd(); ++I)
489 std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
490 I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
491 return true;
492 }
493
494 // check authentication status of the source as well
495 if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
496 return false;
497
498 // Run it
499 bool Failed = false;
500 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
501 {
502 return _error->Error(_("Failed to fetch some archives."));
503 }
504
505 if (_config->FindB("APT::Get::Download-only",false) == true)
506 {
507 c1out << _("Download complete and in download only mode") << std::endl;
508 return true;
509 }
510
511 // Unpack the sources
512 pid_t Process = ExecFork();
513
514 if (Process == 0)
515 {
516 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
517 for (unsigned I = 0; I != J; ++I)
518 {
519 std::string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
520
521 // Diff only mode only fetches .diff files
522 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
523 _config->FindB("APT::Get::Tar-Only",false) == true ||
524 Dsc[I].Dsc.empty() == true)
525 continue;
526
527 // See if the package is already unpacked
528 struct stat Stat;
529 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
530 S_ISDIR(Stat.st_mode) != 0)
531 {
532 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
533 Dir.c_str());
534 }
535 else
536 {
537 // Call dpkg-source
538 std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
539 std::string S;
540 strprintf(S, "%s %s %s",
541 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
542 sourceopts.c_str(), Dsc[I].Dsc.c_str());
543 if (system(S.c_str()) != 0)
544 {
545 fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
546 fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
547 _exit(1);
548 }
549 }
550
551 // Try to compile it with dpkg-buildpackage
552 if (_config->FindB("APT::Get::Compile",false) == true)
553 {
554 std::string buildopts = _config->Find("APT::Get::Host-Architecture");
555 if (buildopts.empty() == false)
556 buildopts = "-a" + buildopts + " ";
557
558 // get all active build profiles
559 std::string const profiles = APT::Configuration::getBuildProfilesString();
560 if (profiles.empty() == false)
561 buildopts.append(" -P").append(profiles).append(" ");
562
563 buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
564
565 // Call dpkg-buildpackage
566 std::string S;
567 strprintf(S, "cd %s && %s %s",
568 Dir.c_str(),
569 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
570 buildopts.c_str());
571
572 if (system(S.c_str()) != 0)
573 {
574 fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
575 _exit(1);
576 }
577 }
578 }
579
580 _exit(0);
581 }
582
583 return ExecWait(Process, "dpkg-source");
584 }
585 /*}}}*/
586 // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
587 // ---------------------------------------------------------------------
588 /* This function will look at the build depends list of the given source
589 package and install the necessary packages to make it true, or fail. */
590 static std::vector<pkgSrcRecords::Parser::BuildDepRec> GetBuildDeps(pkgSrcRecords::Parser * const Last,
591 char const * const Src, bool const StripMultiArch, std::string const &hostArch)
592 {
593 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
594 // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
595 if (hostArch.empty() == false)
596 {
597 std::string nativeArch = _config->Find("APT::Architecture");
598 _config->Set("APT::Architecture", hostArch);
599 bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
600 _config->Set("APT::Architecture", nativeArch);
601 if (Success == false)
602 {
603 _error->Error(_("Unable to get build-dependency information for %s"), Src);
604 return {};
605 }
606 }
607 else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
608 {
609 _error->Error(_("Unable to get build-dependency information for %s"), Src);
610 return {};
611 }
612
613 if (BuildDeps.empty() == true)
614 ioprintf(c1out,_("%s has no build depends.\n"), Src);
615
616 return BuildDeps;
617 }
618 static void WriteBuildDependencyPackage(std::ostringstream &buildDepsPkgFile,
619 std::string const &PkgName, std::string const &Arch,
620 std::vector<pkgSrcRecords::Parser::BuildDepRec> const &Dependencies)
621 {
622 buildDepsPkgFile << "Package: " << PkgName << "\n"
623 << "Architecture: " << Arch << "\n"
624 << "Version: 1\n";
625
626 std::string depends, conflicts;
627 for (auto const &dep: Dependencies)
628 {
629 std::string * type;
630 if (dep.Type == pkgSrcRecords::Parser::BuildConflict || dep.Type == pkgSrcRecords::Parser::BuildConflictIndep)
631 type = &conflicts;
632 else
633 type = &depends;
634
635 type->append(" ").append(dep.Package);
636 if (dep.Version.empty() == false)
637 type->append(" (").append(pkgCache::CompTypeDeb(dep.Op)).append(" ").append(dep.Version).append(")");
638 if ((dep.Op & pkgCache::Dep::Or) == pkgCache::Dep::Or)
639 {
640 type->append("\n |");
641 }
642 else
643 type->append(",\n");
644 }
645 if (depends.empty() == false)
646 buildDepsPkgFile << "Depends:\n" << depends;
647 if (conflicts.empty() == false)
648 buildDepsPkgFile << "Conflicts:\n" << conflicts;
649 buildDepsPkgFile << "\n";
650 }
651 bool DoBuildDep(CommandLine &CmdL)
652 {
653 CacheFile Cache;
654 std::vector<char const *> VolatileCmdL;
655 Cache.GetSourceList()->AddVolatileFiles(CmdL, &VolatileCmdL);
656
657 _config->Set("APT::Install-Recommends", false);
658
659 if (CmdL.FileSize() <= 1 && VolatileCmdL.empty())
660 return _error->Error(_("Must specify at least one package to check builddeps for"));
661
662 bool StripMultiArch;
663 std::string hostArch = _config->Find("APT::Get::Host-Architecture");
664 if (hostArch.empty() == false)
665 {
666 std::vector<std::string> archs = APT::Configuration::getArchitectures();
667 if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
668 return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
669 StripMultiArch = false;
670 }
671 else
672 StripMultiArch = true;
673
674 std::ostringstream buildDepsPkgFile;
675 std::vector<std::pair<std::string,std::string>> pseudoPkgs;
676 // deal with the build essentials first
677 {
678 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
679 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
680 if (Opts)
681 Opts = Opts->Child;
682 for (; Opts; Opts = Opts->Next)
683 {
684 if (Opts->Value.empty() == true)
685 continue;
686
687 pkgSrcRecords::Parser::BuildDepRec rec;
688 rec.Package = Opts->Value;
689 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
690 rec.Op = 0;
691 BuildDeps.push_back(rec);
692 }
693 std::string const pseudo = "builddeps:essentials";
694 std::string const nativeArch = _config->Find("APT::Architecture");
695 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, nativeArch, BuildDeps);
696 pseudoPkgs.emplace_back(pseudo, nativeArch);
697 }
698
699 // Read the source list
700 if (Cache.BuildSourceList() == false)
701 return false;
702 pkgSourceList *List = Cache.GetSourceList();
703 std::string const pseudoArch = hostArch.empty() ? _config->Find("APT::Architecture") : hostArch;
704
705 // FIXME: Avoid volatile sources == cmdline assumption
706 {
707 auto const VolatileSources = List->GetVolatileFiles();
708 if (VolatileSources.size() == VolatileCmdL.size())
709 {
710 for (size_t i = 0; i < VolatileSources.size(); ++i)
711 {
712 char const * const Src = VolatileCmdL[i];
713 if (DirectoryExists(Src))
714 ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), Src);
715 else
716 ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), Src);
717 std::unique_ptr<pkgSrcRecords::Parser> Last(VolatileSources[i]->CreateSrcParser());
718 if (Last == nullptr)
719 return _error->Error(_("Unable to find a source package for %s"), Src);
720
721 std::string const pseudo = std::string("builddeps:") + Src;
722 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
723 GetBuildDeps(Last.get(), Src, StripMultiArch, hostArch));
724 pseudoPkgs.emplace_back(pseudo, pseudoArch);
725 }
726 }
727 else
728 return _error->Error("Implementation error: Volatile sources (%lu) and"
729 "commandline elements (%lu) do not match!", VolatileSources.size(),
730 VolatileCmdL.size());
731 }
732
733 if (CmdL.FileList[1] != 0)
734 {
735 // Create the text record parsers
736 pkgSrcRecords SrcRecs(*List);
737 if (_error->PendingError() == true)
738 return false;
739 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
740 {
741 std::string Src;
742 pkgSrcRecords::Parser * const Last = FindSrc(*I,SrcRecs,Src,Cache);
743 if (Last == nullptr)
744 return _error->Error(_("Unable to find a source package for %s"), *I);
745
746 std::string const pseudo = std::string("builddeps:") + Src;
747 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
748 GetBuildDeps(Last, Src.c_str(), StripMultiArch, hostArch));
749 pseudoPkgs.emplace_back(pseudo, pseudoArch);
750 }
751 }
752
753 Cache.AddIndexFile(new debStringPackageIndex(buildDepsPkgFile.str()));
754
755 bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
756 if (Cache.Open(WantLock) == false)
757 return false;
758 pkgProblemResolver Fix(Cache.GetDepCache());
759
760 APT::PackageVector removeAgain;
761 {
762 pkgDepCache::ActionGroup group(Cache);
763 TryToInstall InstallAction(Cache, &Fix, false);
764 for (auto const &pkg: pseudoPkgs)
765 {
766 pkgCache::PkgIterator const Pkg = Cache->FindPkg(pkg.first, pkg.second);
767 if (Pkg.end())
768 continue;
769 Cache->SetCandidateVersion(Pkg.VersionList());
770 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
771 removeAgain.push_back(Pkg);
772 }
773 InstallAction.doAutoInstall();
774
775 OpTextProgress Progress(*_config);
776 bool const resolver_fail = Fix.Resolve(true, &Progress);
777 if (resolver_fail == false && Cache->BrokenCount() == 0)
778 return false;
779 if (CheckNothingBroken(Cache) == false)
780 return false;
781 }
782 if (DoAutomaticRemove(Cache) == false)
783 return false;
784 {
785 pkgDepCache::ActionGroup group(Cache);
786 for (auto const &pkg: removeAgain)
787 Cache->MarkDelete(pkg, false, 0, true);
788 }
789
790 pseudoPkgs.clear();
791 if (_error->PendingError() || InstallPackages(Cache, false, true) == false)
792 return _error->Error(_("Failed to process build dependencies"));
793 return true;
794 }
795 /*}}}*/