]> git.saurik.com Git - apt.git/blame - apt-private/private-source.cc
avoid building dependency tree in 'source' command
[apt.git] / apt-private / private-source.cc
CommitLineData
9055d5e6
DK
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>
a249b3e6 24#include <apt-pkg/policy.h>
9055d5e6
DK
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
a249b3e6
DK
32#include <apt-pkg/debindexfile.h>
33
9055d5e6
DK
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>
a249b3e6 42#include <sstream>
9055d5e6
DK
43#include <set>
44#include <string>
45#include <vector>
46
47#include <apti18n.h>
48 /*}}}*/
49
9055d5e6
DK
50// GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
51static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
a249b3e6 52 pkgSourceList const * const SrcList, pkgSrcRecords::Parser const * const Parse)
9055d5e6
DK
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 /*{{{*/
72static pkgSrcRecords::Parser *FindSrc(const char *Name,
73 pkgSrcRecords &SrcRecs,std::string &Src,
a249b3e6 74 CacheFile &Cache)
9055d5e6 75{
a249b3e6
DK
76 if (Cache.BuildCaches(false) == false)
77 return nullptr;
9055d5e6
DK
78 std::string VerTag, UserRequestedVerTag;
79 std::string ArchTag = "";
80 std::string RelTag = _config->Find("APT::Default-Release");
81 std::string TmpSrc = Name;
9055d5e6
DK
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 != "")
a249b3e6 111 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc, ArchTag);
9055d5e6 112 else
a249b3e6 113 Pkg = Cache.GetPkgCache()->FindPkg(TmpSrc);
9055d5e6
DK
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
07aca07a 125 if (MatchSrcOnly == false && Pkg.end() == false)
9055d5e6
DK
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 == "")
cd907113 155 if(Cache.GetPkgCache()->VS->CmpVersion(VerTag, Ver.VerStr()) < 0)
9055d5e6
DK
156 VerTag = Ver.VerStr();
157
158 // We match against a concrete version (or a part of this version)
159 if (VerTag.empty() == false &&
cd907113 160 (fuzzy == true || Cache.GetPkgCache()->VS->CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
9055d5e6
DK
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) ||
07aca07a 179 (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
9055d5e6
DK
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
07aca07a 193 if (Src.empty() == true && ArchTag.empty() == false)
9055d5e6 194 {
07aca07a 195 if (VerTag.empty() == false)
9055d5e6
DK
196 _error->Error(_("Can not find a package '%s' with version '%s'"),
197 Pkg.FullName().c_str(), VerTag.c_str());
07aca07a 198 if (RelTag.empty() == false)
9055d5e6
DK
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
a249b3e6
DK
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);
294a8020 221 if (Ver.end() == false)
9055d5e6
DK
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;
a249b3e6 252 pkgSourceList const * const SrcList = Cache.GetSourceList();
9055d5e6
DK
253
254 /* Iterate over all of the hits, which includes the resulting
255 binary packages in the search */
256 pkgSrcRecords::Parser *Parse;
07aca07a 257 while (true)
9055d5e6
DK
258 {
259 SrcRecs.Restart();
07aca07a 260 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
9055d5e6
DK
261 {
262 const std::string Ver = Parse->Version();
263
264 // See if we need to look for a specific release tag
07aca07a 265 if (RelTag.empty() == false && UserRequestedVerTag.empty() == true)
9055d5e6 266 {
a249b3e6 267 pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(Cache, SrcList, Parse);
9055d5e6
DK
268 if (Rls.end() == false)
269 {
07aca07a
DK
270 if ((Rls->Archive != 0 && RelTag != Rls.Archive()) &&
271 (Rls->Codename != 0 && RelTag != Rls.Codename()))
272 continue;
9055d5e6 273 }
07aca07a 274 }
9055d5e6
DK
275
276 // Ignore all versions which doesn't fit
277 if (VerTag.empty() == false &&
cd907113 278 Cache.GetPkgCache()->VS->CmpVersion(VerTag, Ver) != 0) // exact match
9055d5e6
DK
279 continue;
280
281 // Newer version or an exact match? Save the hit
cd907113 282 if (Last == 0 || Cache.GetPkgCache()->VS->CmpVersion(Version,Ver) < 0) {
9055d5e6
DK
283 Last = Parse;
284 Offset = Parse->Offset();
285 Version = Ver;
286 }
287
288 // was the version check above an exact match?
289 // If so, we don't need to look further
290 if (VerTag.empty() == false && (VerTag == Ver))
291 break;
292 }
293 if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
07aca07a 294 ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
9055d5e6
DK
295 Version.c_str(), RelTag.c_str(), Src.c_str());
296
297 if (Last != 0 || VerTag.empty() == true)
298 break;
299 _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
300 return 0;
301 }
302
303 if (Last == 0 || Last->Jump(Offset) == false)
304 return 0;
305
306 return Last;
307}
308 /*}}}*/
309// DoSource - Fetch a source archive /*{{{*/
310// ---------------------------------------------------------------------
311/* Fetch souce packages */
312struct DscFile
313{
314 std::string Package;
315 std::string Version;
316 std::string Dsc;
317};
318bool DoSource(CommandLine &CmdL)
319{
9055d5e6
DK
320 if (CmdL.FileSize() <= 1)
321 return _error->Error(_("Must specify at least one package to fetch source for"));
322
d603afd9 323 CacheFile Cache;
9055d5e6
DK
324 // Read the source list
325 if (Cache.BuildSourceList() == false)
326 return false;
327 pkgSourceList *List = Cache.GetSourceList();
328
329 // Create the text record parsers
330 pkgSrcRecords SrcRecs(*List);
331 if (_error->PendingError() == true)
332 return false;
333
334 std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
335
336 // insert all downloaded uris into this set to avoid downloading them
337 // twice
338 std::set<std::string> queued;
339
340 // Diff only mode only fetches .diff files
341 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
342 // Tar only mode only fetches .tar files
343 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
344 // Dsc only mode only fetches .dsc files
345 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
346
347 // Load the requestd sources into the fetcher
348 aptAcquireWithTextStatus Fetcher;
349 unsigned J = 0;
350 std::vector<std::string> UntrustedList;
351 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
352 {
353 std::string Src;
354 pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
355 if (Last == 0) {
356 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
357 }
358
359 if (Last->Index().IsTrusted() == false)
360 UntrustedList.push_back(Src);
361
362 std::string srec = Last->AsStr();
363 std::string::size_type pos = srec.find("\nVcs-");
364 while (pos != std::string::npos)
365 {
366 pos += strlen("\nVcs-");
367 std::string vcs = srec.substr(pos,srec.find(":",pos)-pos);
368 if(vcs == "Browser")
369 {
370 pos = srec.find("\nVcs-", pos);
371 continue;
372 }
373 pos += vcs.length()+2;
374 std::string::size_type epos = srec.find("\n", pos);
375 std::string const uri = srec.substr(pos,epos-pos);
376 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
377 "the '%s' version control system at:\n"
378 "%s\n"),
379 Src.c_str(), vcs.c_str(), uri.c_str());
380 std::string vcscmd;
381 if (vcs == "Bzr")
382 vcscmd = "bzr branch " + uri;
383 else if (vcs == "Git")
384 vcscmd = "git clone " + uri;
385
386 if (vcscmd.empty() == false)
387 ioprintf(c1out,_("Please use:\n%s\n"
388 "to retrieve the latest (possibly unreleased) "
389 "updates to the package.\n"),
390 vcscmd.c_str());
391 break;
392 }
393
394 // Back track
395 std::vector<pkgSrcRecords::File2> Lst;
396 if (Last->Files2(Lst) == false) {
397 return false;
398 }
399
400 // Load them into the fetcher
401 for (std::vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
402 I != Lst.end(); ++I)
403 {
404 // Try to guess what sort of file it is we are getting.
405 if (I->Type == "dsc")
406 {
407 Dsc[J].Package = Last->Package();
408 Dsc[J].Version = Last->Version();
409 Dsc[J].Dsc = flNotDir(I->Path);
410 }
411
412 // Handle the only options so that multiple can be used at once
413 if (diffOnly == true || tarOnly == true || dscOnly == true)
414 {
415 if ((diffOnly == true && I->Type == "diff") ||
416 (tarOnly == true && I->Type == "tar") ||
417 (dscOnly == true && I->Type == "dsc"))
418 ; // Fine, we want this file downloaded
419 else
420 continue;
421 }
422
423 // don't download the same uri twice (should this be moved to
424 // the fetcher interface itself?)
425 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
426 continue;
427 queued.insert(Last->Index().ArchiveURI(I->Path));
428
429 // check if we have a file with that md5 sum already localy
430 std::string localFile = flNotDir(I->Path);
431 if (FileExists(localFile) == true)
432 if(I->Hashes.VerifyFile(localFile) == true)
433 {
434 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
435 localFile.c_str());
436 continue;
437 }
438
439 // see if we have a hash (Acquire::ForceHash is the only way to have none)
440 if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
441 {
442 ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
443 localFile.c_str());
444 continue;
445 }
446
447 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
448 I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
449 }
450 }
451
452 // Display statistics
453 unsigned long long FetchBytes = Fetcher.FetchNeeded();
454 unsigned long long FetchPBytes = Fetcher.PartialPresent();
455 unsigned long long DebBytes = Fetcher.TotalNeeded();
456
457 if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
458 return false;
459
460 // Number of bytes
461 if (DebBytes != FetchBytes)
462 //TRANSLATOR: The required space between number and unit is already included
463 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
464 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
465 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
466 else
467 //TRANSLATOR: The required space between number and unit is already included
468 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
469 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
470 SizeToStr(DebBytes).c_str());
471
472 if (_config->FindB("APT::Get::Simulate",false) == true)
473 {
474 for (unsigned I = 0; I != J; I++)
475 ioprintf(std::cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
476 return true;
477 }
478
479 // Just print out the uris an exit if the --print-uris flag was used
480 if (_config->FindB("APT::Get::Print-URIs") == true)
481 {
482 pkgAcquire::UriIterator I = Fetcher.UriBegin();
483 for (; I != Fetcher.UriEnd(); ++I)
484 std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
485 I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
486 return true;
487 }
488
489 // check authentication status of the source as well
490 if (UntrustedList.empty() == false && AuthPrompt(UntrustedList, false) == false)
491 return false;
492
493 // Run it
494 bool Failed = false;
495 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
496 {
497 return _error->Error(_("Failed to fetch some archives."));
498 }
499
500 if (_config->FindB("APT::Get::Download-only",false) == true)
501 {
502 c1out << _("Download complete and in download only mode") << std::endl;
503 return true;
504 }
505
506 // Unpack the sources
507 pid_t Process = ExecFork();
508
509 if (Process == 0)
510 {
511 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
512 for (unsigned I = 0; I != J; ++I)
513 {
cd907113 514 std::string Dir = Dsc[I].Package + '-' + Cache.GetPkgCache()->VS->UpstreamVersion(Dsc[I].Version.c_str());
9055d5e6
DK
515
516 // Diff only mode only fetches .diff files
517 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
518 _config->FindB("APT::Get::Tar-Only",false) == true ||
519 Dsc[I].Dsc.empty() == true)
520 continue;
521
522 // See if the package is already unpacked
523 struct stat Stat;
524 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
525 S_ISDIR(Stat.st_mode) != 0)
526 {
527 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
528 Dir.c_str());
529 }
530 else
531 {
532 // Call dpkg-source
533 std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
534 std::string S;
535 strprintf(S, "%s %s %s",
536 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
537 sourceopts.c_str(), Dsc[I].Dsc.c_str());
538 if (system(S.c_str()) != 0)
539 {
540 fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
541 fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
542 _exit(1);
543 }
544 }
545
546 // Try to compile it with dpkg-buildpackage
547 if (_config->FindB("APT::Get::Compile",false) == true)
548 {
549 std::string buildopts = _config->Find("APT::Get::Host-Architecture");
550 if (buildopts.empty() == false)
551 buildopts = "-a" + buildopts + " ";
552
553 // get all active build profiles
554 std::string const profiles = APT::Configuration::getBuildProfilesString();
555 if (profiles.empty() == false)
556 buildopts.append(" -P").append(profiles).append(" ");
557
558 buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
559
560 // Call dpkg-buildpackage
561 std::string S;
562 strprintf(S, "cd %s && %s %s",
563 Dir.c_str(),
564 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
565 buildopts.c_str());
566
567 if (system(S.c_str()) != 0)
568 {
569 fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
570 _exit(1);
571 }
572 }
573 }
574
575 _exit(0);
576 }
577
578 return ExecWait(Process, "dpkg-source");
579}
580 /*}}}*/
581// DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
582// ---------------------------------------------------------------------
583/* This function will look at the build depends list of the given source
584 package and install the necessary packages to make it true, or fail. */
f359b7e8
DK
585static std::vector<pkgSrcRecords::Parser::BuildDepRec> GetBuildDeps(pkgSrcRecords::Parser * const Last,
586 char const * const Src, bool const StripMultiArch, std::string const &hostArch)
587{
588 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
589 // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
590 if (hostArch.empty() == false)
591 {
592 std::string nativeArch = _config->Find("APT::Architecture");
593 _config->Set("APT::Architecture", hostArch);
594 bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
595 _config->Set("APT::Architecture", nativeArch);
596 if (Success == false)
597 {
598 _error->Error(_("Unable to get build-dependency information for %s"), Src);
599 return {};
600 }
601 }
602 else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
603 {
604 _error->Error(_("Unable to get build-dependency information for %s"), Src);
605 return {};
606 }
607
608 if (BuildDeps.empty() == true)
609 ioprintf(c1out,_("%s has no build depends.\n"), Src);
610
611 return BuildDeps;
612}
a249b3e6
DK
613static void WriteBuildDependencyPackage(std::ostringstream &buildDepsPkgFile,
614 std::string const &PkgName, std::string const &Arch,
615 std::vector<pkgSrcRecords::Parser::BuildDepRec> const &Dependencies)
616{
617 buildDepsPkgFile << "Package: " << PkgName << "\n"
618 << "Architecture: " << Arch << "\n"
619 << "Version: 1\n";
620
621 std::string depends, conflicts;
622 for (auto const &dep: Dependencies)
623 {
624 std::string * type;
625 if (dep.Type == pkgSrcRecords::Parser::BuildConflict || dep.Type == pkgSrcRecords::Parser::BuildConflictIndep)
626 type = &conflicts;
627 else
628 type = &depends;
629
630 type->append(" ").append(dep.Package);
631 if (dep.Version.empty() == false)
632 type->append(" (").append(pkgCache::CompTypeDeb(dep.Op)).append(" ").append(dep.Version).append(")");
633 if ((dep.Op & pkgCache::Dep::Or) == pkgCache::Dep::Or)
634 {
635 type->append("\n |");
636 }
637 else
638 type->append(",\n");
639 }
640 if (depends.empty() == false)
641 buildDepsPkgFile << "Depends:\n" << depends;
642 if (conflicts.empty() == false)
643 buildDepsPkgFile << "Conflicts:\n" << conflicts;
644 buildDepsPkgFile << "\n";
645}
9055d5e6
DK
646bool DoBuildDep(CommandLine &CmdL)
647{
648 CacheFile Cache;
f359b7e8
DK
649 std::vector<char const *> VolatileCmdL;
650 Cache.GetSourceList()->AddVolatileFiles(CmdL, &VolatileCmdL);
9055d5e6
DK
651
652 _config->Set("APT::Install-Recommends", false);
653
f359b7e8 654 if (CmdL.FileSize() <= 1 && VolatileCmdL.empty())
9055d5e6
DK
655 return _error->Error(_("Must specify at least one package to check builddeps for"));
656
9055d5e6
DK
657 bool StripMultiArch;
658 std::string hostArch = _config->Find("APT::Get::Host-Architecture");
659 if (hostArch.empty() == false)
660 {
661 std::vector<std::string> archs = APT::Configuration::getArchitectures();
662 if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
663 return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
664 StripMultiArch = false;
665 }
666 else
667 StripMultiArch = true;
668
a249b3e6
DK
669 std::ostringstream buildDepsPkgFile;
670 std::vector<std::pair<std::string,std::string>> pseudoPkgs;
7d19ee92
DK
671 // deal with the build essentials first
672 {
673 std::vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
7d19ee92
DK
674 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
675 if (Opts)
676 Opts = Opts->Child;
677 for (; Opts; Opts = Opts->Next)
678 {
679 if (Opts->Value.empty() == true)
680 continue;
681
682 pkgSrcRecords::Parser::BuildDepRec rec;
683 rec.Package = Opts->Value;
684 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
685 rec.Op = 0;
686 BuildDeps.push_back(rec);
687 }
a249b3e6
DK
688 std::string const pseudo = "builddeps:essentials";
689 std::string const nativeArch = _config->Find("APT::Architecture");
690 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, nativeArch, BuildDeps);
691 pseudoPkgs.emplace_back(pseudo, nativeArch);
7d19ee92
DK
692 }
693
fa47f406
DK
694 // Read the source list
695 if (Cache.BuildSourceList() == false)
696 return false;
697 pkgSourceList *List = Cache.GetSourceList();
a249b3e6 698 std::string const pseudoArch = hostArch.empty() ? _config->Find("APT::Architecture") : hostArch;
fa47f406 699
f359b7e8 700 // FIXME: Avoid volatile sources == cmdline assumption
9055d5e6 701 {
f359b7e8
DK
702 auto const VolatileSources = List->GetVolatileFiles();
703 if (VolatileSources.size() == VolatileCmdL.size())
9055d5e6 704 {
f359b7e8
DK
705 for (size_t i = 0; i < VolatileSources.size(); ++i)
706 {
707 char const * const Src = VolatileCmdL[i];
708 if (DirectoryExists(Src))
709 ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), Src);
710 else
711 ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), Src);
712 std::unique_ptr<pkgSrcRecords::Parser> Last(VolatileSources[i]->CreateSrcParser());
713 if (Last == nullptr)
714 return _error->Error(_("Unable to find a source package for %s"), Src);
715
a249b3e6
DK
716 std::string const pseudo = std::string("builddeps:") + Src;
717 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
718 GetBuildDeps(Last.get(), Src, StripMultiArch, hostArch));
719 pseudoPkgs.emplace_back(pseudo, pseudoArch);
f359b7e8 720 }
9055d5e6 721 }
f359b7e8 722 else
a249b3e6
DK
723 return _error->Error("Implementation error: Volatile sources (%lu) and"
724 "commandline elements (%lu) do not match!", VolatileSources.size(),
725 VolatileCmdL.size());
f359b7e8 726 }
9055d5e6 727
fa47f406 728 if (CmdL.FileList[1] != 0)
f359b7e8 729 {
fa47f406
DK
730 // Create the text record parsers
731 pkgSrcRecords SrcRecs(*List);
732 if (_error->PendingError() == true)
aa368243 733 return false;
fa47f406
DK
734 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
735 {
736 std::string Src;
737 pkgSrcRecords::Parser * const Last = FindSrc(*I,SrcRecs,Src,Cache);
738 if (Last == nullptr)
739 return _error->Error(_("Unable to find a source package for %s"), *I);
740
a249b3e6
DK
741 std::string const pseudo = std::string("builddeps:") + Src;
742 WriteBuildDependencyPackage(buildDepsPkgFile, pseudo, pseudoArch,
743 GetBuildDeps(Last, Src.c_str(), StripMultiArch, hostArch));
744 pseudoPkgs.emplace_back(pseudo, pseudoArch);
fa47f406 745 }
9055d5e6
DK
746 }
747
a249b3e6
DK
748 Cache.AddIndexFile(new debStringPackageIndex(buildDepsPkgFile.str()));
749
750 bool WantLock = _config->FindB("APT::Get::Print-URIs", false) == false;
751 if (Cache.Open(WantLock) == false)
752 return false;
753 pkgProblemResolver Fix(Cache.GetDepCache());
754
755 APT::PackageVector removeAgain;
756 {
757 pkgDepCache::ActionGroup group(Cache);
758 TryToInstall InstallAction(Cache, &Fix, false);
759 for (auto const &pkg: pseudoPkgs)
760 {
761 pkgCache::PkgIterator const Pkg = Cache->FindPkg(pkg.first, pkg.second);
762 if (Pkg.end())
763 continue;
764 Cache->SetCandidateVersion(Pkg.VersionList());
765 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
766 removeAgain.push_back(Pkg);
767 }
768 InstallAction.doAutoInstall();
769
770 OpTextProgress Progress(*_config);
771 bool const resolver_fail = Fix.Resolve(true, &Progress);
772 if (resolver_fail == false && Cache->BrokenCount() == 0)
773 return false;
774 if (CheckNothingBroken(Cache) == false)
775 return false;
776 }
777 if (DoAutomaticRemove(Cache) == false)
778 return false;
779 {
780 pkgDepCache::ActionGroup group(Cache);
781 for (auto const &pkg: removeAgain)
782 Cache->MarkDelete(pkg, false, 0, true);
783 }
784
785 pseudoPkgs.clear();
5f1b8fad 786 if (_error->PendingError() || InstallPackages(Cache, false, true) == false)
9055d5e6
DK
787 return _error->Error(_("Failed to process build dependencies"));
788 return true;
789}
790 /*}}}*/